Unverified Commit 952b5afb authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

turbo-tasks: make scope_unbounded worker churn test deterministic (#100515)

### What?

Makes the turbo-tasks test
`scope_unbounded::tests::test_unbounded_busy_queue_does_not_churn_workers`
deterministic, and tightens its worker-lifetime bound from 16 to 4.

### Why?

The test failed intermittently with errors like `a saturated queue
should not churn drainers, got 20 lifetimes for 20000 items`. The
calling thread enqueued trivial items one at a time while the scope
workers drained them immediately. If the producer was preempted for
longer than `WORKER_IDLE_TIMEOUT` (100µs), the workers idled out and the
next enqueue respawned them. Under CPU load this exceeded the loose
bound in about 1 of 5 runs.

The old shape also didn't prove much: with the idle timeout set to 0 it
still usually passed.

### How?

The test still processes 20,000 items, but the queue is now kept
saturated by construction rather than by out-racing the scheduler:

- **Parents.** Half the items are seeded as parents. Each parent waits
until the seed iterator is exhausted, so no worker can sit on an empty
queue while the producer is still seeding.
- **Children.** Each parent spawns one child, so a worker that exits too
early will be re-armed by a later `spawn`.
- **Child gate.** Children wait until every parent's `spawn` call has
returned. The queue is FIFO, so all parents are already in flight by
then and the wait cannot deadlock. While any `spawn` is still pending,
no drainer can find the queue empty.

With this, a correct implementation has at most 4 accumulator lifetimes:
3 scope workers plus the calling thread. The test now checks that
workers are not torn down while work is queued. It no longer measures
the exact idle timeout, which can't be asserted without depending on
scheduler timing.

Verification:

- On an 8-vCPU Linux machine: 100/100 runs idle and 100/100 runs with 8
busy-loop processes. More stress runs also passed (300/300 under 8
busy-loop processes, 400/400 across 4 concurrent test loops under 16).
- Mutation check: making scope workers exit after each item makes the
test fail with thousands of lifetimes (2,940 in the recorded run).
- All `scope_unbounded` tests pass, and `cargo fmt --check` and `cargo
clippy -p turbo-tasks --tests -- -D warnings` are clean.

<!-- NEXT_JS_LLM -->


<!-- fleet 8bf6a21a-27a8-41a9-bb36-ee50675a54d6 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 1b689957
......@@ -922,7 +922,23 @@ mod tests {
);
}
/// Sustained work keeps workers alive rather than churning them:
/// Workers are not torn down while work is still queued.
///
/// The queue is kept saturated deterministically instead of by racing a producer, because a
/// preempted producer would let workers hit `WORKER_IDLE_TIMEOUT` and be re-armed:
///
/// - Every parent (initial item) blocks until the `initial` iterator is exhausted. While the
/// calling thread is still enqueueing, each live worker is therefore busy inside `run`.
/// - Each parent then spawns one child. Children block until every parent's `spawn` returns.
/// The queue is FIFO, so every parent is already in flight by the time any child is held;
/// this cannot deadlock. While a `spawn` is still pending, every drainer is either holding an
/// item or has thousands of children left to pop, so no worker can find the queue empty.
///
/// A correct implementation never re-arms a worker, whereas one that drops workers while work
/// is still queued is re-armed by later `spawn` calls and churns.
///
/// This does not time `WORKER_IDLE_TIMEOUT` itself; that cannot be asserted without depending
/// on scheduler timing.
#[cfg_attr(
target_family = "wasm",
ignore = "intermittently stalls under the full wasm suite"
......@@ -931,17 +947,38 @@ mod tests {
#[cfg(not(miri))]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_unbounded_busy_queue_does_not_churn_workers() {
// Total items processed: `ITEMS / 2` initial items, each spawning one child.
const ITEMS: usize = 20_000;
const INITIAL: usize = ITEMS / 2;
let inits = Arc::new(AtomicUsize::new(0));
let counted = inits.clone();
// Set once the calling thread has enqueued every parent.
let released = Arc::new(AtomicBool::new(false));
let release = released.clone();
// Number of parents whose `spawn` call has returned.
let parents_spawned = AtomicUsize::new(0);
let processed = tokio::task::spawn_blocking(move || {
scope_unbounded_with(
0..ITEMS,
(0..INITIAL).chain(std::iter::from_fn(move || {
release.store(true, Ordering::Release);
None
})),
move || {
counted.fetch_add(1, Ordering::SeqCst);
0usize
},
|_spawner, _item, acc| {
move |spawner, item, acc| {
if item < INITIAL {
while !released.load(Ordering::Acquire) {
thread::yield_now();
}
spawner.spawn(item + INITIAL);
parents_spawned.fetch_add(1, Ordering::Release);
} else {
while parents_spawned.load(Ordering::Acquire) < INITIAL {
thread::yield_now();
}
}
*acc += 1;
ControlFlow::Continue(())
},
......@@ -951,13 +988,13 @@ mod tests {
.await
.unwrap();
assert_eq!(processed, ITEMS, "every item must run");
// 4 runtime workers => 3 scope workers + the calling thread at any instant. The bound is
// loose because a worker can still lose a race to the last queued item and be re-armed, and
// the rest of the suite competes for the same threads — but a churning implementation lands
// in the thousands, so anything near the worker count proves the timeout is doing its job.
// 4 runtime workers => at most 3 scope workers + the calling thread. A worker that has
// received an item only exits once the queue is empty, which cannot happen until the last
// `spawn` has been made. Each drainer slot therefore hosts at most one lifetime that
// receives an item.
let count = inits.load(Ordering::SeqCst);
assert!(
count <= 16,
count <= 4,
"a saturated queue should not churn drainers, got {count} lifetimes for {ITEMS} items"
);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment