Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Shiny.Net.HttpServer/Hosting/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/Shiny.Net.HttpServer.Tests/RecordingLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ public IReadOnlyList<LogEntry> At(LogLevel level)
return [.. this.entries.Where(x => x.Level == level)];
}
}

/// <summary>Hands every category the one <see cref="RecordingLogger{T}"/>, for a type that takes a factory.</summary>
sealed class RecordingLoggerFactory<T>(RecordingLogger<T> logger) : ILoggerFactory
{
public ILogger CreateLogger(string categoryName) => logger;

public void AddProvider(ILoggerProvider provider) { }

public void Dispose() { }
}
31 changes: 31 additions & 0 deletions tests/Shiny.Net.HttpServer.Tests/ResilienceTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<HttpServer>();
var options = new HttpServerOptions { Port = 0, Address = IPAddress.Loopback };
await using var server = new HttpServer(options, loggerFactory: new RecordingLoggerFactory<HttpServer>(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()
{
Expand Down