From 98a9484f97e01fd911e390f9c4e60f8d6ba3e3c2 Mon Sep 17 00:00:00 2001 From: Kebechet <37485681+Kebechet@users.noreply.github.com> Date: Fri, 4 Dec 2026 13:38:39 +0100 Subject: [PATCH] Stop reporting a listener that was stopped on purpose as a fault OnListeningEnded runs as a continuation of the accept loops and tells a stop from a dead listener by stopRequested. ExecuteSynchronously is only a hint: when the continuation is queued instead, it can run after the stop that ended those loops has finished and the next start has cleared the flag. The stopped generation was then logged at error as "The server stopped listening while it believed it was running", with the stand-in "The accept loop ended while the server was still running." An app that stops and starts the server on resume hits this. Seen in production on iOS (2.2.1): "Shutting down", then "Listening on ...", then that error 5 ms after the new listener was up. The accept loop ends without throwing only when its token was cancelled or stopRequested was set, and only a stop does either, so that stand-in always means a stop. OnListeningEnded now also checks that the loops are still the server's own, the identity rule RecoverFromListenerFaultAsync already applies to the same window. --- .../Hosting/HttpServer.cs | 7 ++++- .../RecordingLogger.cs | 10 ++++++ .../ResilienceTests.cs | 31 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) 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() {