Skip to content
Closed
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
14 changes: 12 additions & 2 deletions src/clients/ioxide.httpclient/Http1/HttpClientPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static HttpClientPool Start(Reactor reactor, HttpClientOptions options)
}
reactor.AddService(pool);
reactor.AddTicker(pool.Sweep);
reactor.AddStopper(pool.Dispose);
return pool;
}

Expand Down Expand Up @@ -181,7 +182,15 @@ private async ValueTask<HttpClientConnection> AcquireAsync()
_waiters.Enqueue((waiter, deadlineMs));

HttpClientConnection? handed = await waiter.Task;
await ReactorSwitch.Yield(_host); // out of the completer's frame, still on the reactor
try
{
await ReactorSwitch.Yield(_host); // out of the completer's frame, still on the reactor
}
catch (ObjectDisposedException) when (handed is not null)
{
handed.Dispose(); // stopped before this request took it: nothing else holds it, and closing needs no ring
throw;
}
if (handed is not null)
{
return handed; // Release hands over nothing broken
Expand Down Expand Up @@ -333,7 +342,8 @@ private string AcquireTimeoutMessage()
/// Close the idle connections and stop replenishing. Connections currently checked out by an
/// in-flight request are not touched - the request owns them, and they are dropped by
/// <see cref="Release"/> when it finishes, because a disposed pool takes nothing back. Called
/// from another thread, it is queued to the reactor and returns.
/// from another thread, it is queued to the reactor and returns. A pool from <see cref="Start"/>
/// is disposed by its reactor's stop too, which ends the requests still waiting on it.
/// </summary>
public void Dispose()
{
Expand Down
4 changes: 3 additions & 1 deletion src/clients/ioxide.httpclient/Http1/RingHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static RingHttpClient Start(Reactor reactor, RingHttpClientOptions? optio
var client = new RingHttpClient(reactor, options ?? new RingHttpClientOptions());
reactor.AddService(client);
reactor.AddTicker(client.Sweep);
reactor.AddStopper(client.Dispose);
return client;
}

Expand Down Expand Up @@ -161,7 +162,8 @@ private void Sweep()
}

/// <summary>Close every origin's idle connections; requests in flight finish first, as with the pool.
/// Called from another thread, it is queued to the reactor and returns.</summary>
/// Called from another thread, it is queued to the reactor and returns. The reactor's stop disposes
/// the client too.</summary>
public void Dispose()
{
// The origins and their pools are the reactor's.
Expand Down
10 changes: 10 additions & 0 deletions src/ioxide/Reactor/Reactor.RingHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public T GetService<T>() where T : class

internal void OwnTlsService(TlsService service) => (_tlsServices ??= []).Add(service);

// What the clients started on this reactor run when it stops: Teardown runs them first.
private List<Action>? _stoppers;

/// <summary>
/// Register a callback run once on the reactor thread when it stops, before its connections close. Call from
/// <c>OnStart</c>. A client closes what it holds there and wakes the requests waiting on it, which a Dispose
/// called after the stop can no longer do: it would be posted to a reactor that runs nothing more.
/// </summary>
public void AddStopper(Action stopper) => (_stoppers ??= []).Add(stopper);

/// <summary>
/// Runs on the reactor thread before the loop starts - open ring-native clients here.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions src/ioxide/Reactor/Reactor.Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ private void Teardown(bool startFailed)
// A faulted loop never set it: UdpSendTo sends directly from here, as the ring may never be submitted again.
_stopRequested = true;

// First: a client stopped here takes no new request from a handler the closes below resume, and what it wakes
// posts its switch in time for the last drain, which ends it on the thread pool.
RunStoppers();

// Every connection closed to its handler first, then the queue sent while each socket it names is still its
// own: left queued, a later submit would send it to whatever took the number (#531).
MarkAcceptedTcpConnectionsClosed();
Expand Down Expand Up @@ -218,6 +222,22 @@ private void Teardown(bool startFailed)
}
}

// One that throws must not leave the rest of the teardown undone: the ring, the sockets and the memory.
private void RunStoppers()
{
for (int i = 0; i < (_stoppers?.Count ?? 0); i++)
{
try
{
_stoppers![i]();
}
catch (Exception e)
{
Console.Error.WriteLine($"[r{_id}] stopper faulted: {e.Message}");
}
}
}

// True once the kernel has taken everything queued, a few tries allowed: a cancel can't find what never reached it.
private bool SubmitQueued()
{
Expand Down
40 changes: 40 additions & 0 deletions tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,46 @@ public static void Register(Runner runner)
Assert.True(switched!.Exception?.InnerException is ObjectDisposedException,
$"it ended {switched.Status}: {switched.Exception?.InnerException?.GetType().Name}");
});

runner.Test("stop: the stoppers run on the reactor thread before its connections close, and one that throws leaves the teardown to finish", () =>
{
var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
bool closed = false;
var ran = new List<string>();

(int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) =>
{
if (!await IsThisTestsConnection(conn))
{
conn.DecRef();
return;
}

try
{
parked.TrySetResult();
await conn.ReadAsync();
closed = true;
}
finally
{
conn.DecRef();
}
}, new ServerConfig { Tcp = new TcpOptions() }, r =>
{
r.AddStopper(() => ran.Add($"on the reactor {Reactor.Current == r}, its connection closed {closed}"));
r.AddStopper(() => throw new InvalidOperationException("a stopper that throws"));
r.AddStopper(() => ran.Add("the next one"));
});

using Socket client = Connect(port);
client.Send("hi"u8);
Assert.True(parked.Task.Wait(5_000), "the handler never parked");

StopAndJoin(reactor, thread);
Assert.Equal("on the reactor True, its connection closed False; the next one", string.Join("; ", ran));
Assert.True(closed, "the stop never closed the connection to its handler");
});
}

private static readonly byte[] Queued = "QUEUED-FOR-THE-CLOSED-SOCKET"u8.ToArray();
Expand Down
Loading
Loading