Files
next.js/turbopack/crates/turbo-tasks-backend/tests/bug.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

208 lines
6.0 KiB
Rust

#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
use anyhow::Result;
use bincode::{Decode, Encode};
use turbo_tasks::{NonLocalValue, Vc, trace::TraceRawVcs};
use turbo_tasks_testing::{Registration, register, run_once};
static REGISTRATION: Registration = register!();
#[derive(Debug, Clone, PartialEq, Eq, NonLocalValue, TraceRawVcs, Encode, Decode)]
struct TaskReferenceSpec {
task: u16,
read: bool,
read_strongly_consistent: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, NonLocalValue, TraceRawVcs, Encode, Decode)]
struct TaskSpec {
references: Vec<TaskReferenceSpec>,
}
#[turbo_tasks::value(transparent)]
struct TasksSpec(Vec<TaskSpec>);
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_graph_bug() {
let mut nonce = 0;
run_once(&REGISTRATION, move || async move {
// pass a nonce to re-run the test body on every turbo-tasks restart
nonce += 1;
test_graph_bug_operation(nonce)
.read_strongly_consistent()
.await
})
.await
.unwrap()
}
#[turbo_tasks::function(operation)]
async fn test_graph_bug_operation(nonce: u32) -> Result<Vc<()>> {
let _ = nonce; // ensure the nonce is part of our cache key
// see https://github.com/vercel/next.js/pull/79451
let spec = vec![
TaskSpec {
references: vec![
TaskReferenceSpec {
task: 3,
read: false,
read_strongly_consistent: true,
},
TaskReferenceSpec {
task: 1,
read: true,
read_strongly_consistent: false,
},
TaskReferenceSpec {
task: 12,
read: false,
read_strongly_consistent: true,
},
],
},
TaskSpec {
references: vec![TaskReferenceSpec {
task: 2,
read: true,
read_strongly_consistent: true,
}],
},
TaskSpec {
references: vec![TaskReferenceSpec {
task: 4,
read: false,
read_strongly_consistent: false,
}],
},
TaskSpec {
references: vec![TaskReferenceSpec {
task: 6,
read: false,
read_strongly_consistent: false,
}],
},
TaskSpec {
references: vec![
TaskReferenceSpec {
task: 5,
read: false,
read_strongly_consistent: false,
},
TaskReferenceSpec {
task: 13,
read: false,
read_strongly_consistent: false,
},
],
},
TaskSpec {
references: vec![
TaskReferenceSpec {
task: 11,
read: false,
read_strongly_consistent: true,
},
TaskReferenceSpec {
task: 14,
read: false,
read_strongly_consistent: false,
},
TaskReferenceSpec {
task: 7,
read: false,
read_strongly_consistent: false,
},
TaskReferenceSpec {
task: 8,
read: false,
read_strongly_consistent: false,
},
],
},
TaskSpec {
references: vec![TaskReferenceSpec {
task: 9,
read: false,
read_strongly_consistent: false,
}],
},
TaskSpec { references: vec![] },
TaskSpec {
references: vec![
TaskReferenceSpec {
task: 12,
read: false,
read_strongly_consistent: false,
},
TaskReferenceSpec {
task: 11,
read: false,
read_strongly_consistent: false,
},
],
},
TaskSpec {
references: vec![TaskReferenceSpec {
task: 10,
read: false,
read_strongly_consistent: false,
}],
},
TaskSpec {
references: vec![TaskReferenceSpec {
task: 12,
read: false,
read_strongly_consistent: false,
}],
},
TaskSpec { references: vec![] },
TaskSpec {
references: vec![TaskReferenceSpec {
task: 14,
read: false,
read_strongly_consistent: false,
}],
},
TaskSpec { references: vec![] },
TaskSpec {
references: vec![
TaskReferenceSpec {
task: 16,
read: false,
read_strongly_consistent: true,
},
TaskReferenceSpec {
task: 15,
read: false,
read_strongly_consistent: true,
},
],
},
TaskSpec { references: vec![] },
TaskSpec { references: vec![] },
];
let spec: Vc<TasksSpec> = Vc::cell(spec);
run_task(spec, 0).await?;
Ok(Vc::cell(()))
}
#[turbo_tasks::function]
async fn run_task(spec: Vc<TasksSpec>, task: u16) -> Result<Vc<()>> {
let spec_ref = spec.await?;
let task = &spec_ref[task as usize];
for reference in &task.references {
let call = run_task(spec, reference.task);
if reference.read {
call.await?;
}
if reference.read_strongly_consistent {
call.strongly_consistent().await?;
}
}
Ok(Vc::cell(()))
}