diff --git a/src/ioxide/Reactor/Loop/Reactor.Drainers.cs b/src/ioxide/Reactor/Loop/Reactor.Drainers.cs index 1c75a425..a693b0b2 100644 --- a/src/ioxide/Reactor/Loop/Reactor.Drainers.cs +++ b/src/ioxide/Reactor/Loop/Reactor.Drainers.cs @@ -82,6 +82,11 @@ public void EnqueueReturnQ(ushort bid) SpinWait sw = default; while (!_returnQ.TryEnqueue(bid)) { + // Nothing drains the queue past the stop, and there is no ring left to take the buffer. + if (_postsClosed) + { + return; + } sw.SpinOnce(); } // Without the wake, a queued return waits for an unrelated CQE; if the ring diff --git a/src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs b/src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs index 2e15c390..02f404d8 100644 --- a/src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs +++ b/src/ioxide/Reactor/Loop/Reactor.Loop.Incremental.cs @@ -137,6 +137,11 @@ public void EnqueueReturnQIncremental(int fd, ushort gen, ushort bid) SpinWait sw = default; while (!_returnQInc!.TryEnqueue(packed)) { + // Nothing drains the queue past the stop, and there is no ring left to take the slice. + if (_postsClosed) + { + return; + } sw.SpinOnce(); } Wake(); diff --git a/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs b/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs index 008675b3..5962d1f2 100644 --- a/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs +++ b/tests/Ioxide.Tests.E2E/Core/StopParkedTests.cs @@ -325,6 +325,85 @@ public static void Register(Runner runner) } } }, skip: incremental && !TestServer.KernelAtLeast(6, 12)); + + runner.Test($"stop: handlers holding more buffers than the return queue takes give them all back from another thread after the stop ({mode})", () => + { + // Past the queue's 16384 entries shared, 65536 incremental. One-byte buffers make each byte a + // recv, so each byte sent is a buffer, or a slice, of its own, and 32768 is the most a ring takes. + int connections = incremental ? 3 : 1; + int perConnection = incremental ? 22_000 : 17_000; + var tcp = new TcpOptions { RecvQueueEntries = 32_768 }; + ServerConfig config = incremental + ? new ServerConfig { Incremental = new IncrementalOptions { MaxConnections = 8, RecvSlots = 32_768, RecvBufferSize = 1 }, Tcp = tcp } + : new ServerConfig { RecvSlots = 32_768, RecvBufferSize = 1, Tcp = tcp }; + + using var holding = new CountdownEvent(connections); + using var gaveBack = new CountdownEvent(connections); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + int returned = 0; + + (int port, Reactor reactor, Thread thread) = TestServer.StartConfigured(async (_, conn) => + { + var held = new List(); + try + { + while (held.Count < perConnection) + { + RecvSnapshot snapshot = await conn.ReadAsync(); + while (conn.TryGetItem(snapshot, out SpscRecvRing.Item item)) + { + if (item.HasBuffer) + { + held.Add(item); + } + } + if (snapshot.IsClosed) + { + return; // the harness's listen probe + } + } + + holding.Signal(); + await release.Task; // resumes on the thread pool, after the stop + foreach (SpscRecvRing.Item item in held) + { + conn.ReturnBuffer(in item); + Interlocked.Increment(ref returned); + } + gaveBack.Signal(); + } + finally + { + conn.DecRef(); + } + }, config); + + var clients = new List(); + try + { + for (int c = 0; c < connections; c++) + { + Socket client = Connect(port); + clients.Add(client); + client.Send(new byte[perConnection]); + } + Assert.True(holding.Wait(10_000), $"{holding.CurrentCount} of {connections} handlers never came to hold their buffers"); + + StopAndJoin(reactor, thread); + release.TrySetResult(); + + Assert.True(gaveBack.Wait(ReturnsWithinMs), + $"{Volatile.Read(ref returned)} of the {connections * perConnection} buffers went back after the stop, and the next return still spins on a queue nothing drains"); + } + finally + { + release.TrySetResult(); + foreach (Socket client in clients) + { + client.Dispose(); + } + } + }, skip: incremental && !TestServer.KernelAtLeast(6, 12)); } runner.Test("stop: a flush from another thread after the reactor stopped returns", () => diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index bba8de8b..2138ef0d 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -56,6 +56,7 @@ private static int Main() SpillSegmentTests.Register(runner); RecvQueueExtendTests.Register(runner); StoppedPostTests.Register(runner); + StoppedReturnTests.Register(runner); HttpClientGrowthTests.Register(runner); return runner.Summary(); diff --git a/tests/Ioxide.Tests.Unit/StoppedReturnTests.cs b/tests/Ioxide.Tests.Unit/StoppedReturnTests.cs new file mode 100644 index 00000000..cfee834b --- /dev/null +++ b/tests/Ioxide.Tests.Unit/StoppedReturnTests.cs @@ -0,0 +1,72 @@ +using System.Runtime.CompilerServices; +using ioxide; + +namespace Ioxide.Tests; + +/// +/// A buffer return from another thread that finds the reactor's return queue full. While the reactor runs, the loop +/// drains the queue and the return waits for room; once it has stopped nothing drains it, and the return must not wait. +/// +/// +/// The real returns, on a reactor that never runs, with this test setting the flag the end of Teardown sets. +/// +internal static class StoppedReturnTests +{ + [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_postsClosed")] + private static extern ref bool PostsClosedOf(Reactor reactor); + + [UnsafeAccessor(UnsafeAccessorKind.Method, Name = "InitIncremental")] + private static extern void InitIncremental(Reactor reactor); + + public static void Register(Runner runner) + { + foreach (bool incremental in new[] { false, true }) + { + string mode = incremental ? "incremental" : "shared ring"; + + runner.Test($"reactor: a buffer return that finds its queue full waits while the reactor runs, and leaves when it stops ({mode})", () => + { + Reactor reactor = NeverRunning(incremental); + int capacity = incremental ? 1 << 16 : 1 << 14; + for (int i = 0; i < capacity; i++) + { + Return(reactor, incremental); + } + + var waiting = new Thread(() => Return(reactor, incremental)) { IsBackground = true, Name = "full-queue-return" }; + waiting.Start(); + Assert.True(!waiting.Join(200), "a return that found the queue full while the reactor runs was dropped instead of waiting for room"); + + Volatile.Write(ref PostsClosedOf(reactor), true); + Assert.True(waiting.Join(3_000), "a return waiting on the full queue still spins after the stop, where nothing will drain it"); + }); + } + } + + // Every caller is another thread, and the wake finds no eventfd to write. + private static Reactor NeverRunning(bool incremental) + { + var reactor = new Reactor(0, new ServerConfig + { + ReactorCount = 1, Tcp = null, Udp = null, Incremental = incremental ? new IncrementalOptions() : null, + }); + if (incremental) + { + InitIncremental(reactor); + } + + return reactor; + } + + private static void Return(Reactor reactor, bool incremental) + { + if (incremental) + { + reactor.EnqueueReturnQIncremental(3, 0, 0); + } + else + { + reactor.EnqueueReturnQ(0); + } + } +}