mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
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.
109 lines
3.3 KiB
Rust
109 lines
3.3 KiB
Rust
#![feature(arbitrary_self_types)]
|
|
#![feature(arbitrary_self_types_pointers)]
|
|
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
|
|
|
|
use std::time::Duration;
|
|
|
|
use anyhow::{Result, bail};
|
|
use turbo_rcstr::RcStr;
|
|
use turbo_tasks::{CollectiblesSource, ReadRef, ResolvedVc, State, ValueToString, Vc, emit};
|
|
use turbo_tasks_testing::{Registration, register, run_once};
|
|
|
|
static REGISTRATION: Registration = register!();
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn test_dirty_in_progress() {
|
|
run_once(®ISTRATION, || async {
|
|
let cases = [
|
|
(1, 3, 2, 2, ""),
|
|
(11, 13, 12, 42, "12"),
|
|
(1, 13, 11, 42, "11"),
|
|
(1, 3, 11, 42, "11"),
|
|
(11, 3, 2, 2, ""),
|
|
(11, 13, 2, 2, ""),
|
|
];
|
|
for (a, b, c, value, collectible) in cases {
|
|
println!("{a} -> {b} -> {c} = {value} {collectible}");
|
|
let input_val = ReadRef::new_owned(ChangingInput {
|
|
state: State::new(a),
|
|
});
|
|
let input_vc = ReadRef::resolved_cell(input_val.clone());
|
|
let output_op = compute_operation(input_vc);
|
|
output_op.read_strongly_consistent().await?;
|
|
println!("update to {b}");
|
|
input_val.state.set(b);
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
println!("update to {c}");
|
|
input_val.state.set(c);
|
|
let read = output_op.read_strongly_consistent().await?;
|
|
assert_eq!(read.value, value);
|
|
assert_eq!(read.collectible, collectible);
|
|
println!("\n");
|
|
}
|
|
Ok(())
|
|
})
|
|
.await
|
|
.unwrap()
|
|
}
|
|
|
|
#[turbo_tasks::value]
|
|
struct ChangingInput {
|
|
state: State<u32>,
|
|
}
|
|
|
|
#[turbo_tasks::value]
|
|
struct Output {
|
|
value: u32,
|
|
collectible: String,
|
|
}
|
|
|
|
#[turbo_tasks::value]
|
|
struct Collectible {
|
|
value: u32,
|
|
}
|
|
|
|
#[turbo_tasks::value_impl]
|
|
impl ValueToString for Collectible {
|
|
#[turbo_tasks::function]
|
|
fn to_string(&self) -> Vc<RcStr> {
|
|
Vc::cell(self.value.to_string().into())
|
|
}
|
|
}
|
|
|
|
#[turbo_tasks::function(operation)]
|
|
async fn inner_compute(input: ResolvedVc<ChangingInput>) -> Result<Vc<u32>> {
|
|
println!("start inner_compute");
|
|
let value = *input.await?.state.get();
|
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
|
if value > 10 {
|
|
let collectible: ResolvedVc<Box<dyn ValueToString>> =
|
|
ResolvedVc::upcast(Collectible { value }.resolved_cell());
|
|
emit(collectible);
|
|
|
|
println!("end inner_compute with collectible");
|
|
Ok(Vc::cell(42))
|
|
} else {
|
|
println!("end inner_compute without collectible");
|
|
Ok(Vc::cell(value))
|
|
}
|
|
}
|
|
|
|
#[turbo_tasks::function(operation)]
|
|
async fn compute_operation(input: ResolvedVc<ChangingInput>) -> Result<Vc<Output>> {
|
|
println!("start compute");
|
|
let operation = inner_compute(input);
|
|
let value = *operation.connect().await?;
|
|
let collectibles = operation.peek_collectibles::<Box<dyn ValueToString>>();
|
|
if collectibles.len() > 1 {
|
|
bail!("expected 0..1 collectible, found {}", collectibles.len());
|
|
}
|
|
let first = collectibles.iter().next();
|
|
let collectible = if let Some(first) = first {
|
|
first.to_string().await?.to_string()
|
|
} else {
|
|
"".to_string()
|
|
};
|
|
println!("end compute");
|
|
Ok(Output { value, collectible }.cell())
|
|
}
|