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).
This commit is contained in:
@@ -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::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
struct SetOnDrop(Arc<AtomicBool>);
|
||||||
|
|
||||||
|
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
|
/// A child that holds its slot until `release` flips, without parking
|
||||||
/// machinery: busy-yield keeps the scheduler moving and the slot occupied.
|
/// machinery: busy-yield keeps the scheduler moving and the slot occupied.
|
||||||
fn holder(release: Arc<AtomicBool>) -> impl FnOnce() + Send + 'static {
|
fn holder(release: Arc<AtomicBool>) -> impl FnOnce() + Send + 'static {
|
||||||
@@ -130,6 +138,12 @@ fn racing_try_spawns_claim_exactly_the_free_slots() {
|
|||||||
const SPAWNERS: usize = 4;
|
const SPAWNERS: usize = 4;
|
||||||
smarm::runtime::init(Config::exact(4).max_actors(MAX)).run(|| {
|
smarm::runtime::init(Config::exact(4).max_actors(MAX)).run(|| {
|
||||||
let release = Arc::new(AtomicBool::new(false));
|
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 won = Arc::new(AtomicUsize::new(0));
|
||||||
let done = 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();
|
let mut spawners = Vec::new();
|
||||||
for _ in 0..SPAWNERS {
|
for _ in 0..SPAWNERS {
|
||||||
let release = release.clone();
|
let release = release.clone();
|
||||||
|
let go = go.clone();
|
||||||
let won = won.clone();
|
let won = won.clone();
|
||||||
let done = done.clone();
|
let done = done.clone();
|
||||||
spawners.push(spawn(move || {
|
spawners.push(spawn(move || {
|
||||||
|
while !go.load(Ordering::Acquire) {
|
||||||
|
yield_now();
|
||||||
|
}
|
||||||
loop {
|
loop {
|
||||||
match try_spawn(holder(release.clone())) {
|
match try_spawn(holder(release.clone())) {
|
||||||
Ok(h) => {
|
Ok(h) => {
|
||||||
@@ -160,6 +178,7 @@ fn racing_try_spawns_claim_exactly_the_free_slots() {
|
|||||||
done.fetch_add(1, Ordering::AcqRel);
|
done.fetch_add(1, Ordering::AcqRel);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
go.store(true, Ordering::Release);
|
||||||
// Wait for every racer to hit AtCapacity.
|
// Wait for every racer to hit AtCapacity.
|
||||||
while done.load(Ordering::Acquire) < SPAWNERS {
|
while done.load(Ordering::Acquire) < SPAWNERS {
|
||||||
yield_now();
|
yield_now();
|
||||||
|
|||||||
Reference in New Issue
Block a user