From 164a83eb732b6a930f650f4c40b6bb656e8ed36e Mon Sep 17 00:00:00 2001 From: smarm-agent Date: Sun, 18 Oct 2026 09:20:15 +0200 Subject: [PATCH] test(try_spawn): gate the racers, and never hang on a failed race racing_try_spawns_claim_exactly_the_free_slots failed ~1 in 4 runs on a 2-vCPU box (same rate on v0.8.0). With 4 scheduler threads the first spawner started claiming while root was still spawning the rest, so the precondition "every spawner holds its slot before the race" was false: root's own spawn hit the full table and panicked. When root panicked with a spawner still mid-race, the run hung instead of failing: that spawner's holders sit under a live parent at root exit, so the sweep skips them, and they spin on `release` forever. Spawners now wait on a start gate that root opens after spawning all of them, and a drop guard sets `release` if root unwinds. 400/400 passes (200 idle, 200 under CPU load). --- tests/try_spawn.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/try_spawn.rs b/tests/try_spawn.rs index 859125a..1de3c88 100644 --- a/tests/try_spawn.rs +++ b/tests/try_spawn.rs @@ -12,6 +12,14 @@ use smarm::{spawn, try_spawn, try_spawn_under_with, yield_now, SpawnError, Spawn use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::Arc; +struct SetOnDrop(Arc); + +impl Drop for SetOnDrop { + fn drop(&mut self) { + self.0.store(true, Ordering::Release); + } +} + /// A child that holds its slot until `release` flips, without parking /// machinery: busy-yield keeps the scheduler moving and the slot occupied. fn holder(release: Arc) -> impl FnOnce() + Send + 'static { @@ -130,6 +138,12 @@ fn racing_try_spawns_claim_exactly_the_free_slots() { const SPAWNERS: usize = 4; smarm::runtime::init(Config::exact(4).max_actors(MAX)).run(|| { let release = Arc::new(AtomicBool::new(false)); + // A failing assert below must fail the test, not hang it: holders spawned by a + // still-running spawner are not reached by the root-exit sweep. + let _release_on_unwind = SetOnDrop(release.clone()); + // Racers start only once every spawner holds its slot; otherwise the first ones + // can drain the table while root is still spawning the rest. + let go = Arc::new(AtomicBool::new(false)); let won = Arc::new(AtomicUsize::new(0)); let done = Arc::new(AtomicUsize::new(0)); @@ -144,9 +158,13 @@ fn racing_try_spawns_claim_exactly_the_free_slots() { let mut spawners = Vec::new(); for _ in 0..SPAWNERS { let release = release.clone(); + let go = go.clone(); let won = won.clone(); let done = done.clone(); spawners.push(spawn(move || { + while !go.load(Ordering::Acquire) { + yield_now(); + } loop { match try_spawn(holder(release.clone())) { Ok(h) => { @@ -160,6 +178,7 @@ fn racing_try_spawns_claim_exactly_the_free_slots() { done.fetch_add(1, Ordering::AcqRel); })); } + go.store(true, Ordering::Release); // Wait for every racer to hit AtCapacity. while done.load(Ordering::Acquire) < SPAWNERS { yield_now();