From 2b7bd62b5e45d3d6d8d6c5219b5dbd5bfba801d9 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 18:34:21 +0000 Subject: [PATCH] reactor: a buffer return that finds its queue full after the stop is dropped, rather than spinning forever on a queue nothing drains (#607) Off the reactor, EnqueueReturnQ and EnqueueReturnQIncremental spin until their queue takes the buffer, and only the loop drains those queues. After the stop every return from another thread kept its entry, so once the 16384 entries of the shared ring's queue, or the 65536 of the incremental one, were taken, the next return spun forever and held its thread: a handler that held buffers across the stop and gave them back from the thread pool never finished. A return that finds its queue full now gives up once the reactor's posts are closed, at the end of Teardown, when nothing will drain the queue and no ring is left to take the buffer. The test sits inside the spin, so a return that finds room runs the same code as before. StoppedReturnTests (Unit): a return that finds the queue full waits while the reactor runs, and leaves when the stop lands, in both modes. StopParkedTests (E2E): handlers holding more buffers than the queue takes, 17000 on the shared ring and 3 x 22000 slices incremental, give them all back from the thread pool after the stop. --- src/ioxide/Reactor/Loop/Reactor.Drainers.cs | 5 ++ .../Reactor/Loop/Reactor.Loop.Incremental.cs | 5 ++ .../Ioxide.Tests.E2E/Core/StopParkedTests.cs | 79 +++++++++++++++++++ tests/Ioxide.Tests.Unit/Program.cs | 1 + tests/Ioxide.Tests.Unit/StoppedReturnTests.cs | 72 +++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 tests/Ioxide.Tests.Unit/StoppedReturnTests.cs 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); + } + } +}