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.
73 lines
2.1 KiB
Rust
73 lines
2.1 KiB
Rust
use core::panic;
|
|
use std::{
|
|
panic::{set_hook, take_hook},
|
|
sync::{
|
|
Arc, LazyLock,
|
|
atomic::{AtomicBool, Ordering},
|
|
},
|
|
};
|
|
|
|
use anyhow::Result;
|
|
use regex::Regex;
|
|
use turbo_tasks::{
|
|
Vc,
|
|
backend::TurboTasksExecutionError,
|
|
panic_hooks::{handle_panic, register_panic_hook},
|
|
unmark_top_level_task_may_leak_eventually_consistent_state,
|
|
};
|
|
use turbo_tasks_testing::{Registration, register, run_once_without_cache_check};
|
|
|
|
static REGISTRATION: Registration = register!();
|
|
|
|
static FILE_PATH_REGEX: LazyLock<Regex> =
|
|
LazyLock::new(|| Regex::new(r"panics\.rs:\d+:\d+$").unwrap());
|
|
|
|
// DO NOT ADD MORE TESTS TO THIS FILE!
|
|
//
|
|
// This test depends on the process-wide global panic handler. This test must be run in its own
|
|
// process in isolation of any other tests.
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn test_panic_hook() {
|
|
let prev_hook = take_hook();
|
|
set_hook(Box::new(move |info| {
|
|
handle_panic(info);
|
|
prev_hook(info);
|
|
}));
|
|
|
|
let hook_was_called = Arc::new(AtomicBool::new(false));
|
|
let _hook_guard = register_panic_hook({
|
|
let hook_was_called = hook_was_called.clone();
|
|
Box::new(move |_| hook_was_called.store(true, Ordering::SeqCst))
|
|
});
|
|
|
|
let result = run_once_without_cache_check(®ISTRATION, async move {
|
|
unmark_top_level_task_may_leak_eventually_consistent_state();
|
|
anyhow::Ok(*double(3).await?)
|
|
})
|
|
.await;
|
|
|
|
assert!(hook_was_called.load(Ordering::SeqCst));
|
|
|
|
let error = result.unwrap_err();
|
|
let root_cause = error.root_cause();
|
|
|
|
let Some(panic) = root_cause.downcast_ref::<TurboTasksExecutionError>() else {
|
|
panic!("Expected a TurboTasksExecutionError");
|
|
};
|
|
|
|
let TurboTasksExecutionError::Panic(panic) = panic else {
|
|
panic!("Expected a TurboTasksExecutionError::Panic");
|
|
};
|
|
|
|
assert!(
|
|
FILE_PATH_REGEX.is_match(panic.location.as_ref().unwrap()),
|
|
"Panic location '{}' should be a line in panics.rs",
|
|
panic.location.as_ref().unwrap()
|
|
);
|
|
}
|
|
|
|
#[turbo_tasks::function]
|
|
fn double(_val: u64) -> Result<Vc<u64>> {
|
|
panic!("oh no");
|
|
}
|