Files
next.js/turbopack/crates/turbo-tasks-backend/benches/scope_stress.rs
Benjamin WoodruffandGitHub 89f3649a00 Turbopack: Panic if a top-level task attempts an eventually consistent read (#89735)
We've had some really bad bugs in the past, which can be hard to root-cause due to eventually-consistent reads a the top-level, e.g. https://github.com/vercel/next.js/pull/77511

Outside of tests, there's no good justification for doing an eventually-consistent read at the top level. It may leak incomplete computations, which can contain errors that would otherwise be resolved by a strongly consistent read.

The basic idea here is simple:

- Store a flag in `CURRENT_TASK_STATE` indicating that we're in a top-level task.
- When reading a task output or cell, check for that flag.

But, there are a few complications:

- `RawVc`'s read and resolution logic is wrong, and internally uses multiple eventually consistent operations after a strongly consistent read.
    - For this to be correct, it needs to happen as one atomic operation inside of the task backend, but it would change the backend API contract, so it's not straightforward to do that.
    - I added a `SUPPRESS_EVENTUAL_CONSISTENCY_TOP_LEVEL_TASK_CHECK` flag that I'm using for now.
- There are a ton of places that are doing eventually consistent reads for no good reason. These aren't causing problems today, but we should fix them instead of suppressing them. Claude helped with this, but it's still a lot.
- Some tests actually do need eventually-consistent reads because they're testing some internal behavior of turbo-tasks. I added an intentionally long-named `unmark_top_level_task_may_leak_eventually_consistent_state` helper for this.

I manually tested `turbopack-cli` against our bundler-benchmark repository that uses it.
2026-02-26 16:14:30 -08:00

84 lines
3.0 KiB
Rust

use anyhow::Result;
use criterion::{BenchmarkId, Criterion};
use turbo_tasks::{Completion, TryJoinIterExt, TurboTasks, Vc};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
pub fn scope_stress(c: &mut Criterion) {
if matches!(
std::env::var("TURBOPACK_BENCH_STRESS").ok().as_deref(),
None | Some("") | Some("no") | Some("false")
) {
return;
}
let mut group = c.benchmark_group("turbo_tasks_backend_scope_stress");
group.sample_size(20);
for size in [5, 10, 15, 20, 25, 30, 100, 200, 300] {
group.throughput(criterion::Throughput::Elements(
/* tasks for fib from 0 to size - 1 = */
(size as u64) * (size as u64) +
/* root tasks = */
(2 * size as u64)
- 1,
));
group.bench_with_input(
BenchmarkId::new("rectangle", format_args!("{size} x {size}")),
&size,
|b, size| {
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
let size = *size;
b.to_async(rt).iter_with_large_drop(move || {
let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
storage_mode: None,
..Default::default()
},
noop_backing_storage(),
));
async move {
(0..size)
.map(|a| (a, size - 1))
.chain((0..size - 1).map(|b| (size - 1, b)))
.map(|(a, b)| {
let tt = &tt;
async move {
Ok(tt
.run(async move {
rectangle_operation(a, b)
.read_strongly_consistent()
.await?;
Ok(())
})
.await?)
}
})
.try_join()
.await
.unwrap();
tt
}
})
},
);
}
}
/// This fills a rectagle from (0, 0) to (a, b) by
/// first filling (0, 0) to (a - 1, b) and then (0, 0) to (a, b - 1) recursively
#[turbo_tasks::function(operation)]
async fn rectangle_operation(a: u32, b: u32) -> Result<Vc<Completion>> {
if a > 0 {
rectangle_operation(a - 1, b).connect().await?;
}
if b > 0 {
rectangle_operation(a, b - 1).connect().await?;
}
Ok(Completion::new())
}