Files
5d0dab5fc6 [turbopack] Enforce root attribute for strongly consistent reads and collectibles (#93114)
### 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]>
2026-05-12 18:56:02 +00: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, root)]
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(root)]
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(()))
}