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