From c97ab0dc6c22909cd0973401b1aed0975ae6e7ed Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Sat, 14 Nov 2026 18:52:16 +0000 Subject: [PATCH] reactor: once it has stopped, neither the thread that ran it nor a new thread given that one's managed id is taken for the reactor's (#608) BindReactorThread records the reactor thread's managed id and sets t_current, and nothing took either back at the stop. Every test of whether a caller is on the reactor compares that id: OnReactorThread, ScheduleOnReactor, SubmitClientOp, SubmitTimeout, the Enqueue fast paths and UdpSendTo. So after a stop two kinds of thread took the reactor's own path against a reactor whose ring the teardown had unmapped: the thread that ran it, if it went on running after Run returned, and any new thread the runtime gave the same id once that thread's Thread object was collected. From either, a ring op read the SQ head from the unmapped ring and the process aborted with AccessViolationException, and a ReactorSwitch.Yield was queued where nothing drains it and never resumed. The thread that ran it also still had the stopped reactor as Reactor.Current. The end of Teardown, still on the reactor's thread, now sets the recorded id to -1 and clears t_current. A thread's id is its own while it runs, and Teardown runs on the reactor's thread before Run returns, so from then on no thread is taken for the reactor's. -1 is no thread's id, and it is not 0, which the submit and UDP checks read as not started, so every caller hands off and gets the stopped reactor's answer. The checks compile as before. A ring the kernel refuses still returns from Run before Teardown, and that path is unchanged. Comparing t_current with the reactor instead costs each check: a thread static outside CoreLib is reached through the thread's array of static bases, a bounds test, two loads and a null test, where the managed id is read at a fixed offset. OnReactorThread compiled to 81 bytes against 47, and ScheduleOnReactor, EnqueueReturnQ and EnqueueFlush each 21 to 28 bytes longer. AffinityTests (E2E): once Run has returned, the thread that ran a reactor is not taken for its reactor's, and neither is a new thread the runtime gives the stopped reactor thread's id; from both, a yield and a ring op throw ObjectDisposedException. --- src/ioxide/Reactor/Reactor.Post.cs | 10 +- src/ioxide/Reactor/Reactor.Runner.cs | 7 +- tests/Ioxide.Tests.E2E/Core/AffinityTests.cs | 129 +++++++++++++++++++ 3 files changed, 143 insertions(+), 3 deletions(-) diff --git a/src/ioxide/Reactor/Reactor.Post.cs b/src/ioxide/Reactor/Reactor.Post.cs index df625de5..3fe5ab61 100644 --- a/src/ioxide/Reactor/Reactor.Post.cs +++ b/src/ioxide/Reactor/Reactor.Post.cs @@ -27,13 +27,19 @@ public PostItem(Action callback, object? state) /// True once the reactor has stopped for good: nothing posted to it runs on it any more. internal bool PostsClosed => _postsClosed; - /// True when the caller is executing on this reactor's loop thread. + /// + /// True when the caller is executing on this reactor's loop thread. False on every thread once the reactor has + /// stopped, the one that ran it included. + /// public bool OnReactorThread => Environment.CurrentManagedThreadId == _reactorThreadId; [ThreadStatic] private static Reactor? t_current; - /// The reactor whose loop runs on the calling thread, or null on any other thread. + /// + /// The reactor whose loop runs on the calling thread, or null on any other thread. Null too on the thread that ran a + /// reactor, once that reactor has stopped. + /// public static Reactor? Current => t_current; /// diff --git a/src/ioxide/Reactor/Reactor.Runner.cs b/src/ioxide/Reactor/Reactor.Runner.cs index 400ef75c..7923503b 100644 --- a/src/ioxide/Reactor/Reactor.Runner.cs +++ b/src/ioxide/Reactor/Reactor.Runner.cs @@ -90,7 +90,7 @@ public void Run() } // Record the owning thread: off-reactor callers detect themselves and go through the handoff - // queues. Thread-lifetime; nothing to uninstall. + // queues. Teardown takes both back, as the runtime gives a dead thread's id to a new one. private void BindReactorThread() { _reactorThreadId = Environment.CurrentManagedThreadId; @@ -216,6 +216,11 @@ private void Teardown(bool startFailed) { service.Free(); } + + // No thread is the reactor's from here: not this one, which may run on, nor a new one given its id once this one's + // Thread object is collected. -1 is no thread's id and not 0, so a caller still hands off, and finds it stopped. + _reactorThreadId = -1; + t_current = null; } // True once the kernel has taken everything queued, a few tries allowed: a cancel can't find what never reached it. diff --git a/tests/Ioxide.Tests.E2E/Core/AffinityTests.cs b/tests/Ioxide.Tests.E2E/Core/AffinityTests.cs index 2ef012e0..f5833473 100644 --- a/tests/Ioxide.Tests.E2E/Core/AffinityTests.cs +++ b/tests/Ioxide.Tests.E2E/Core/AffinityTests.cs @@ -293,6 +293,54 @@ public static void Register(Runner runner) static async Task YieldTo(Reactor r) => await ReactorSwitch.Yield(r); }); + runner.Test("affinity: once Run has returned, the thread that ran the reactor is not its reactor's, so a yield or a ring op from it fails as from any other", () => + { + Reactor reactor = Bare(); + using var started = new ManualResetEventSlim(); + reactor.OnStart = _ => started.Set(); + + StoppedView? seen = null; + var thread = new Thread(() => + { + reactor.Run(); + seen = StoppedView.From(reactor); + }) { IsBackground = true, Name = "affinity-ran-the-reactor" }; + thread.Start(); + + Assert.True(started.Wait(TimeSpan.FromSeconds(10)), "the reactor never started"); + reactor.Stop(); + Assert.True(thread.Join(TimeSpan.FromSeconds(10)), "the reactor did not stop"); + seen!.AssertStopped("the thread that ran it"); + }); + + runner.Test("affinity: a new thread the runtime gives a stopped reactor thread's managed id is not the reactor's", () => + { + // A dead thread's id goes back to the runtime once its Thread object is collected, among those of every + // other thread that died since, in no set order. + (Reactor reactor, int deadId) = RunAndStop(); + Collect(); + + StoppedView? seen = null; + var spawned = new List(); // each held, so its id is not handed out again + while (seen is null && spawned.Count < 4096) + { + var thread = new Thread(() => + { + if (Environment.CurrentManagedThreadId == deadId) + { + seen = StoppedView.From(reactor); + } + }); + thread.Start(); + thread.Join(); + spawned.Add(thread); + } + GC.KeepAlive(spawned); + + Assert.True(seen is not null, $"control: none of {spawned.Count} new threads was given id {deadId}, so nothing was tested"); + seen!.AssertStopped($"a new thread given its id {deadId}"); + }); + runner.Test("affinity: handoffs from another thread share one eventfd write until the loop's next pass", () => { // Only posts shared a wake: a buffer return, a flush, a recycle or a client op handed over @@ -380,6 +428,87 @@ private sealed class NoCompletion : IRingCompletion public void Complete(int result) { } } + // A reactor with no transport: it runs, and stops, without a port. + private static Reactor Bare() => new(0, new ServerConfig { ReactorCount = 1, Tcp = null, Udp = null, RecvBufferSize = 4096, RecvSlots = 64 }); + + private static void Collect() + { + for (int i = 0; i < 3; i++) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + } + } + + // Not inlined, so nothing in the test's frame keeps the reactor's Thread object, which holds on to its id. + [MethodImpl(MethodImplOptions.NoInlining)] + private static (Reactor Reactor, int ThreadId) RunAndStop() + { + Reactor reactor = Bare(); + int threadId = 0; + var started = new ManualResetEventSlim(); + reactor.OnStart = _ => + { + threadId = Environment.CurrentManagedThreadId; + started.Set(); + }; + + var thread = new Thread(reactor.Run) { IsBackground = true, Name = "affinity-stopped-reactor" }; + thread.Start(); + Assert.True(started.Wait(TimeSpan.FromSeconds(10)), "the reactor never started"); + reactor.Stop(); + Assert.True(thread.Join(TimeSpan.FromSeconds(10)), "the reactor did not stop"); + return (reactor, threadId); + } + + // A stopped reactor as one thread sees it. The yield and the ring op are made only from a thread neither check takes + // for the reactor's: on the reactor's own path the op reaches the ring the teardown unmapped. + private sealed class StoppedView + { + private bool _onReactorThread; + private bool _current; + private Task? _yielded; + private Exception? _submitted; + + public static StoppedView From(Reactor reactor) + { + var view = new StoppedView + { + _onReactorThread = reactor.OnReactorThread, + _current = ReferenceEquals(Reactor.Current, reactor), + }; + + if (!view._onReactorThread && !view._current) + { + view._yielded = YieldTo(reactor); + try + { + reactor.SubmitTimeout(1_000_000, new NoCompletion()); + } + catch (Exception e) + { + view._submitted = e; + } + } + + return view; + + static async Task YieldTo(Reactor r) => await ReactorSwitch.Yield(r); + } + + public void AssertStopped(string who) + { + Assert.True(!_onReactorThread, $"{who} passes OnReactorThread for the stopped reactor"); + Assert.True(!_current, $"Reactor.Current on {who} is still the stopped reactor"); + Assert.True(((IAsyncResult)_yielded!).AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5)), + $"a yield to the stopped reactor from {who} never resumed"); + Assert.True(_yielded.Exception?.InnerException is ObjectDisposedException, + $"a yield to the stopped reactor from {who} ended {_yielded.Status}: {_yielded.Exception?.InnerException?.GetType().Name}"); + Assert.True(_submitted is ObjectDisposedException, + $"a ring op on the stopped reactor from {who} ended in {_submitted?.GetType().Name ?? "no exception"}"); + } + } + // Each counter on its own line, so the race's own bookkeeping does not share one with the flag. [StructLayout(LayoutKind.Explicit, Size = 320)] private sealed class PostRace