From 2d76a044c18df3a71494d4f8ee2e650a5da502f4 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 19:02:27 +0000 Subject: [PATCH] reactor: a ring op submitted from a completion the stop gives fails as one handed over after the stop does, rather than never completing (#609) At the stop, CompleteParkedOps freed each parked client op's slot and then completed it with -ECANCELED, inline on the reactor thread. A waiter that submitted a ring op again from that completion took a slot at once: the one just freed, from the top of the free stack, which the loop had already passed. Its SQE stayed in an SQ that nothing submitted again, nothing completed it, and the ring was then disposed, so whatever awaited it waited forever. ClientTransport got there for a name with more than one address: a connect the stop cancelled was followed inline by one to the next address, which never ended, and neither did the pool's open or the requests queued on it. A handler that retries a ring op inline got the same. CompleteParkedOps now empties the free stack before it completes anything and gives no slot back to it, so such a submit reaches AllocOpSlot's grow branch, which throws ObjectDisposedException once the stop has emptied the stack, as HandOff does for an op handed over from another thread after the stop. The test sits where only a submit to a full table goes, so no submit pays for it. The throw also ends an inline retry loop that ignores an error result. A failed final submit is handled as before: CompleteParkedOps does not run and parked ops stay parked. Tests: in E2E's StopParkedTests, a handler that waits on its RingTimer again once the stop ended its first wait gets ObjectDisposedException. In Http's HttpClientConnectTests, a ClientTransport connect to a first address the stop cancels ends with ObjectDisposedException, instead of going on to a connect to the next address that never ends. On main both are still waiting 3 s after the stop, and without either half of the change the E2E test still fails. --- src/ioxide/Reactor/Reactor.RingHost.cs | 10 ++++- .../Ioxide.Tests.E2E/Core/StopParkedTests.cs | 42 +++++++++++++++++++ .../HttpClientConnectTests.cs | 36 ++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/ioxide/Reactor/Reactor.RingHost.cs b/src/ioxide/Reactor/Reactor.RingHost.cs index a537f70c..43c39113 100644 --- a/src/ioxide/Reactor/Reactor.RingHost.cs +++ b/src/ioxide/Reactor/Reactor.RingHost.cs @@ -191,16 +191,19 @@ private void DrainStoppedOps() } } - // At stop, each once the kernel is done with it. A waiter that submits again reuses its slot, past this loop. + // At stop, each once the kernel is done with it. The free slots go first: an op a waiter submits again from its + // completion fails as a handoff after the stop does, where it would otherwise wait in an SQ nothing submits again. private void CompleteParkedOps() { + _opFree = []; + _opFreeTop = 0; + int slots = _opTargets.Length; for (int slot = 0; slot < slots; slot++) { if (_opTargets[slot] is { } target && CancelAndWait(Tag(KindClient, 0, slot))) { _opTargets[slot] = null; - _opFree[_opFreeTop++] = slot; target.Complete(-ECANCELED); } } @@ -271,6 +274,9 @@ private int AllocOpSlot(IRingCompletion completion) { if (_opFreeTop == 0) { + // The stop empties it (CompleteParkedOps); else only a full table gets here, so no submit pays for the test. + ObjectDisposedException.ThrowIf(_opFree.Length == 0, this); + int oldLength = _opTargets.Length; Array.Resize(ref _opTargets, oldLength * 2); Array.Resize(ref _opFree, oldLength * 2); diff --git a/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs b/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs index 008675b3..0c37f7df 100644 --- a/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs +++ b/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs @@ -85,6 +85,48 @@ public static void Register(Runner runner) Assert.True(finished.Task.Wait(ReturnsWithinMs), "the handler is still parked in its wait, its finally not run"); }); + runner.Test("stop: a RingTimer wait a handler starts again from the one the stop ended fails rather than waiting forever", () => + { + var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var again = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (r, conn) => + { + if (!await IsThisTestsConnection(conn)) + { + conn.DecRef(); + return; + } + + var timer = new RingTimer(r); + try + { + parked.TrySetResult(); + await timer.DelayAsync(600_000); + + // Inline in the completion the stop gives, as a retry loop would. + int result = await timer.DelayAsync(600_000); + again.TrySetResult($"the second wait returned {result}"); + } + catch (Exception e) + { + again.TrySetResult(e.GetType().Name); + } + finally + { + conn.DecRef(); + } + }, new ServerConfig { Tcp = new TcpOptions() }); + + using Socket client = Connect(port); + client.Send("hi"u8); + Assert.True(parked.Task.Wait(5_000), "the handler never parked"); + + StopAndJoin(reactor, thread); + Assert.True(again.Task.Wait(ReturnsWithinMs), "the handler is still parked in the wait it started again"); + Assert.Equal(nameof(ObjectDisposedException), again.Task.Result); + }); + runner.Test("stop: a handler parked in a flush its client holds up gets to run its finally", () => { var parked = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); diff --git a/tests/Ioxide.Tests.Http/HttpClientConnectTests.cs b/tests/Ioxide.Tests.Http/HttpClientConnectTests.cs index 923ca21b..ff886e2e 100644 --- a/tests/Ioxide.Tests.Http/HttpClientConnectTests.cs +++ b/tests/Ioxide.Tests.Http/HttpClientConnectTests.cs @@ -89,6 +89,42 @@ public static void Register(Runner runner) } }); + runner.Test("httpclient h1: a connect its reactor's stop cancels ends with the stop, rather than going on to the next address and waiting forever", () => + { + using var silent = new TcpListener(IPAddress.Parse("127.0.0.3"), 0); + silent.Start(0); + var silentAt = (IPEndPoint)silent.LocalEndpoint; + Socket[] queued = FillAcceptQueue(silentAt); + + try + { + // 127.0.0.2 refuses at once, should the kernel ever be handed the connect to it. + var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + (_, Reactor reactor, Thread thread) = TestServer.StartConfigured( + (_, connection) => { connection.DecRef(); return Task.CompletedTask; }, + new ServerConfig { Tcp = new TcpOptions() }, + r => started.SetResult(ClientTransport.ConnectAsync(r, [silentAt.Address, IPAddress.Parse("127.0.0.2")], + (ushort)silentAt.Port, tls: null, serverName: null, connectTimeoutMs: 0).AsTask())); + + Task connect = started.Task.Result; + Thread.Sleep(200); // the first connect reaches the kernel, which holds it + Assert.True(!connect.IsCompleted, $"the connect ended before the stop: {connect.Status}"); + + reactor.Stop(); + Assert.True(thread.Join(TimeSpan.FromSeconds(10)), "the reactor did not stop"); + Assert.True(((IAsyncResult)connect).AsyncWaitHandle.WaitOne(3_000), "the connect is still waiting 3 s after its reactor stopped"); + Assert.True(connect.Exception?.InnerException is ObjectDisposedException, + $"expected ObjectDisposedException, got {connect.Status}: {connect.Exception?.InnerException?.GetType().Name}"); + } + finally + { + foreach (Socket socket in queued) + { + socket.Dispose(); + } + } + }); + runner.Test("httpclient h1: an idle connection past MaxLifetimeMs is replaced by the sweep", () => { int clientPort = 0;