diff --git a/src/Shiny.Net.HttpServer/Hosting/HttpServer.cs b/src/Shiny.Net.HttpServer/Hosting/HttpServer.cs index 10c10bf..56cc07e 100644 --- a/src/Shiny.Net.HttpServer/Hosting/HttpServer.cs +++ b/src/Shiny.Net.HttpServer/Hosting/HttpServer.cs @@ -1091,7 +1091,12 @@ void OnListeningEnded(Task loops) // StopCoreAsync unbinds the listeners *before* it cancels — cancelling first would abort the // very requests it is trying to drain — and in that window a normal shutdown is // indistinguishable from a listener that died. - if (this.stopRequested || this.disposed) + // + // The flag alone is not enough either. This continuation can be queued rather than inlined, + // and then it can run after that stop has finished and the next start has cleared + // stopRequested. Loops that are no longer the server's own were stopped on purpose: the + // same identity rule RecoverFromListenerFaultAsync applies. + if (this.stopRequested || this.disposed || !ReferenceEquals(Volatile.Read(ref this.acceptLoops), loops)) return; // Reading .Exception is also what marks the fault observed, so this must happen whatever we diff --git a/tests/Shiny.Net.HttpServer.Tests/RecordingLogger.cs b/tests/Shiny.Net.HttpServer.Tests/RecordingLogger.cs index 9f33a73..642ccab 100644 --- a/tests/Shiny.Net.HttpServer.Tests/RecordingLogger.cs +++ b/tests/Shiny.Net.HttpServer.Tests/RecordingLogger.cs @@ -41,3 +41,13 @@ public IReadOnlyList At(LogLevel level) return [.. this.entries.Where(x => x.Level == level)]; } } + +/// Hands every category the one , for a type that takes a factory. +sealed class RecordingLoggerFactory(RecordingLogger logger) : ILoggerFactory +{ + public ILogger CreateLogger(string categoryName) => logger; + + public void AddProvider(ILoggerProvider provider) { } + + public void Dispose() { } +} diff --git a/tests/Shiny.Net.HttpServer.Tests/ResilienceTests.cs b/tests/Shiny.Net.HttpServer.Tests/ResilienceTests.cs index 1950b46..d304df4 100644 --- a/tests/Shiny.Net.HttpServer.Tests/ResilienceTests.cs +++ b/tests/Shiny.Net.HttpServer.Tests/ResilienceTests.cs @@ -1,5 +1,7 @@ using System.Net; using System.Net.Sockets; +using System.Reflection; +using Microsoft.Extensions.Logging; using Shiny.Net.HttpServer.Transports; namespace Shiny.Net.HttpServer.Tests; @@ -111,6 +113,35 @@ await WaitFor( Assert.True(server.IsRunning); } + [Fact] + public async Task A_stopped_listener_reported_after_the_next_start_is_not_a_fault() + { + // OnListeningEnded runs as a continuation of the accept loops, and ExecuteSynchronously is + // only a hint: when the runtime queues it instead, it can run after the stop that ended + // those loops has finished and the next start has cleared stopRequested. A stop and start + // on resume does exactly that, and the stopped listener was then reported at error as one + // that died underneath a running server. The runtime decides when a queued continuation + // runs, so this hands OnListeningEnded the stopped loops itself, after the next start. + var logger = new RecordingLogger(); + var options = new HttpServerOptions { Port = 0, Address = IPAddress.Loopback }; + await using var server = new HttpServer(options, loggerFactory: new RecordingLoggerFactory(logger)); + server.MapGet("/ping", ctx => ctx.Response.WriteAsync("pong")); + + var acceptLoops = typeof(HttpServer).GetField("acceptLoops", BindingFlags.Instance | BindingFlags.NonPublic)!; + var onListeningEnded = typeof(HttpServer).GetMethod("OnListeningEnded", BindingFlags.Instance | BindingFlags.NonPublic)!; + + await server.StartAsync(Token); + var stoppedLoops = (Task)acceptLoops.GetValue(server)!; + await server.StopAsync(Token); + await server.StartAsync(Token); + + onListeningEnded.Invoke(server, [stoppedLoops]); + + Assert.Empty(logger.At(LogLevel.Error)); + Assert.True(server.IsRunning); + Assert.Equal("pong", await GetAsync(server)); + } + [Fact] public async Task A_transient_accept_failure_recovers_without_stopping_the_server() {