mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
### What? Replaces the silent "make root node" promotion in the turbo-tasks backend with a panic that enforces tasks to already have the `root` attribute before performing strongly consistent reads, reading task collectibles, or removing collectibles. ### Why? Previously, the backend would silently promote any non-root task to a root node (aggregation number `u32::MAX`) when these operations were requested. This masked incorrect task configuration — tasks that needed root-level aggregation weren't explicitly declared as such, making it harder to reason about the aggregation graph and hiding potential performance issues from implicit promotions. ### How? **Backend enforcement (3 locations):** - **Strongly consistent read** (`backend/mod.rs`): Checks `NativeFunction.is_root` on the target task. If it's a persistent task without `root`, panics with both the target and reader task descriptions. - **Read task collectibles** (`backend/mod.rs`): Same check when reading collectibles from a task. - **Remove collectibles** (`operation/update_collectible.rs`): Same check when removing collectibles (count < 0). All three locations drop the task guard before panicking to avoid deadlocking with `debug_get_task_description`. **Macro support for `root` on methods:** - `value_impl_macro.rs`: Now reads the `root` attribute from `#[turbo_tasks::function(root)]` on inherent impl and trait impl methods (previously hardcoded to `false`). - `value_trait_macro.rs`: Same for trait default methods. **`root` attribute additions:** - All `#[turbo_tasks::function(operation)]` in test files, benchmarks, and fuzz code - Production operations in `crates/next-api/` that are read with strong consistency - Regular functions and methods in tests that use `.strongly_consistent()` - Trait methods in `value_impl` and `value_trait` blocks used with strong consistency **New test:** - `non_root_task_panic.rs`: Verifies the panic fires when attempting a strongly consistent read on a non-root operation task. Captures the panic from the worker thread via a panic hook since the panic propagates as a channel error to the test thread. <!-- NEXT_JS_LLM_PR --> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
122 lines
3.8 KiB
Rust
122 lines
3.8 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::{collections::HashSet, mem::take, sync::Mutex};
|
|
|
|
use anyhow::Result;
|
|
use turbo_tasks::{
|
|
Invalidator, TraitRef, Vc, get_invalidator,
|
|
unmark_top_level_task_may_leak_eventually_consistent_state, with_turbo_tasks,
|
|
};
|
|
use turbo_tasks_testing::{Registration, register, run_once};
|
|
|
|
static REGISTRATION: Registration = register!();
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn trait_ref() {
|
|
run_once(®ISTRATION, || async {
|
|
unmark_top_level_task_may_leak_eventually_consistent_state();
|
|
let counter = Counter::cell(Counter {
|
|
value: Mutex::new((0, Default::default())),
|
|
});
|
|
|
|
let counter_value = counter.get_value();
|
|
|
|
assert_eq!(*counter.get_value().strongly_consistent().await?, 0);
|
|
assert_eq!(*counter_value.strongly_consistent().await?, 0);
|
|
|
|
counter.await?.incr();
|
|
|
|
assert_eq!(*counter.get_value().strongly_consistent().await?, 1);
|
|
assert_eq!(*counter_value.strongly_consistent().await?, 1);
|
|
|
|
// `ref_counter` will still point to the same `counter` instance as `counter`.
|
|
let trait_ref_counter = Vc::upcast::<Box<dyn CounterTrait>>(counter)
|
|
.into_trait_ref()
|
|
.await?;
|
|
let ref_counter = TraitRef::cell(trait_ref_counter.clone());
|
|
let ref_counter_value = ref_counter.get_value();
|
|
|
|
// However, `local_counter_value` will point to the value of `counter_value`
|
|
// at the time it was turned into a trait reference (just like a `ReadRef`
|
|
// would).
|
|
let local_counter_value = TraitRef::cell(
|
|
Vc::upcast::<Box<dyn CounterValueTrait>>(counter_value)
|
|
.into_trait_ref()
|
|
.await?,
|
|
)
|
|
.get_value();
|
|
|
|
counter.await?.incr();
|
|
assert_eq!(trait_ref_counter.get_value_sync().0, 2);
|
|
assert_eq!(*counter.get_value().strongly_consistent().await?, 2);
|
|
assert_eq!(*counter_value.strongly_consistent().await?, 2);
|
|
assert_eq!(*ref_counter_value.strongly_consistent().await?, 2);
|
|
assert_eq!(*local_counter_value.strongly_consistent().await?, 1);
|
|
|
|
anyhow::Ok(())
|
|
})
|
|
.await
|
|
.unwrap()
|
|
}
|
|
|
|
#[turbo_tasks::value(transparent)]
|
|
#[derive(Copy, Clone)]
|
|
struct CounterValue(usize);
|
|
|
|
#[turbo_tasks::value(serialization = "skip", evict = "never", cell = "new", eq = "manual")]
|
|
struct Counter {
|
|
#[turbo_tasks(debug_ignore, trace_ignore)]
|
|
value: Mutex<(usize, HashSet<Invalidator>)>,
|
|
}
|
|
|
|
impl Counter {
|
|
fn incr(&self) {
|
|
with_turbo_tasks(|tt| {
|
|
let mut lock = self.value.lock().unwrap();
|
|
lock.0 += 1;
|
|
let invalidators = take(&mut lock.1);
|
|
for i in invalidators {
|
|
i.invalidate(&**tt);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
#[turbo_tasks::value_trait]
|
|
trait CounterTrait {
|
|
#[turbo_tasks::function(root)]
|
|
fn get_value(&self) -> Vc<CounterValue>;
|
|
|
|
fn get_value_sync(&self) -> CounterValue;
|
|
}
|
|
|
|
#[turbo_tasks::value_impl]
|
|
impl CounterTrait for Counter {
|
|
#[turbo_tasks::function(root)]
|
|
fn get_value(&self) -> Result<Vc<CounterValue>> {
|
|
let mut lock = self.value.lock().unwrap();
|
|
lock.1.insert(get_invalidator().unwrap());
|
|
Ok(Vc::cell(lock.0))
|
|
}
|
|
|
|
fn get_value_sync(&self) -> CounterValue {
|
|
CounterValue(self.value.lock().unwrap().0)
|
|
}
|
|
}
|
|
|
|
#[turbo_tasks::value_trait]
|
|
trait CounterValueTrait {
|
|
#[turbo_tasks::function(root)]
|
|
fn get_value(&self) -> Vc<CounterValue>;
|
|
}
|
|
|
|
#[turbo_tasks::value_impl]
|
|
impl CounterValueTrait for CounterValue {
|
|
#[turbo_tasks::function(root)]
|
|
fn get_value(self: Vc<Self>) -> Vc<Self> {
|
|
self
|
|
}
|
|
}
|