From 962f7110e392075359421bbf3f38450d44d79717 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 5 Sep 2026 03:23:50 +0200 Subject: [PATCH] Close the intra-tick quarantine race: recheck after permit acquisition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to the pass-9 follow-ups: deviation 2 flagged that run_cover_tick's per-cell quarantine check only ran once, before a cell contended for its permit, so a sibling already parked behind the permit pool when another cell's fault raised the flag would still run once the permit freed up. Add a second check right after permits.acquire(): with the pool sized to one, the second cell cannot be granted the permit until the first has fully returned, and for a faulting cell that means the flag is already set by then, so the post-acquire check is deterministic rather than a race. The top-of-function guard against a later tick's cells stays as-is. Renamed and extended quarantine_raised_mid_tick_stops_every_later_tick_from_reaching_the_port to quarantine_raised_mid_tick_stops_the_sibling_cell_and_every_later_tick: same cross-tick assertion as before, plus permit pool 1 and an exact (not >=1) count of one call in the first tick, proving the sibling cell never reaches the port once the first cell's fault quarantines the organ. Ran the new test 8x in isolation with no flakes. world:: 281 passed, full crate 320 passed (unchanged counts — this extends an existing test rather than adding one). cargo fmt -p ghostlight-dungeon -- --check is clean. Co-Authored-By: Claude Fable 5.1 --- crates/ghostlight-dungeon/src/runtime.rs | 47 ++++++++++++++++-------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/crates/ghostlight-dungeon/src/runtime.rs b/crates/ghostlight-dungeon/src/runtime.rs index 135e12a..6b1fb48 100644 --- a/crates/ghostlight-dungeon/src/runtime.rs +++ b/crates/ghostlight-dungeon/src/runtime.rs @@ -1696,15 +1696,22 @@ async fn run_cover_tick(state: &AppState) { let permits = state.controller_permits.clone(); let quarantined = state.controller_quarantined.clone(); running.spawn(async move { - // Quarantine has a clean edge: no permit is granted after the flag - // is set, and turns already holding one finish on their own - // bindings. + // Checked once up front, as a fast path that skips contending for + // a permit at all, and once more after acquiring one: a cell + // already parked behind the pool when a sibling's fault raises + // the flag must not proceed just because it queued before the + // flag flipped. A permit already held when the flag is set still + // finishes on its own binding — quarantine stops the *next* cell + // to reach either check, not one already mid-turn. if quarantined.load(Ordering::SeqCst) { return; } let Ok(_permit) = permits.acquire().await else { return; }; + if quarantined.load(Ordering::SeqCst) { + return; + } match runner.run_cell(&cell).await { // A tick that reports nothing is a tick nobody can debug. One // line per coarse cell, naming what it consumed and what it @@ -2870,17 +2877,20 @@ mod tests { } } - /// `run_cover_tick`'s per-cell task checks `controller_quarantined` once, - /// before it ever contends for a permit — spawned together in one - /// `JoinSet`, both of this tick's cells pass that check before either can - /// reach the port and raise it. The flag's real teeth are at the top of - /// `run_cover_tick` itself: once a cell's fault sets it mid-tick, the - /// *next* tick returns before deriving a cover at all, so no further cell - /// of any later tick ever reaches the port again. That is what this test - /// proves, rather than an intra-tick race between two sibling tasks that - /// this driver does not resolve. + /// `run_cover_tick`'s per-cell task checks `controller_quarantined` twice: + /// once up front, before contending for a permit at all, and once more + /// right after acquiring one. With the pool sized to one, only one of + /// this tick's two cells can ever hold the permit at a time, and the + /// second cannot be granted it until the first has fully returned — + /// which, for a faulting cell, means the flag is already set. So the + /// second cell's post-acquire check always sees it and returns without + /// ever reaching the port, deterministically, regardless of which cell + /// happened to acquire first. The flag's cross-tick teeth are separate: + /// `run_cover_tick`'s own top-of-function guard means the *next* tick + /// never derives a cover at all once quarantined, so no cell of any later + /// tick reaches the port either. Both edges are asserted here. #[tokio::test] - async fn quarantine_raised_mid_tick_stops_every_later_tick_from_reaching_the_port() { + async fn quarantine_raised_mid_tick_stops_the_sibling_cell_and_every_later_tick() { let fixture = fixture().await; active_two_cell_world(&fixture.state, &fixture.cookie).await; @@ -2894,6 +2904,10 @@ mod tests { Arc::new(AlwaysFreshWorkStore), test_controller_models(), ))); + // One permit, two cells: the second cell cannot even attempt the + // port until the first has returned, which is what makes the + // post-acquire recheck deterministic rather than a race. + state.controller_permits = Arc::new(Semaphore::new(1)); run_cover_tick(&state).await; assert!( @@ -2901,9 +2915,10 @@ mod tests { "an integrity-violating fault must quarantine the cognition organ" ); let calls_after_first_tick = port.calls.load(Ordering::SeqCst); - assert!( - calls_after_first_tick >= 1, - "the faulting cell must have reached the port before quarantine could be observed" + assert_eq!( + calls_after_first_tick, 1, + "the sibling cell waiting on the permit must not reach the port \ + once the first cell's fault raised the flag" ); run_cover_tick(&state).await;