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;