Unverified Commit a2fb5e3b authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

turbo-tasks-backend: resolve task priority when creating child contexts (#100767)

### What?

`ExecuteContext::child_context()` in `turbo-tasks-backend` now reads the
current task priority on the calling thread. The resulting
`ChildExecuteContext` stores a priority that is already known, instead
of an optional one.

### Why?

Both users of `child_context()` hand the child context to
`scope_bounded` helper threads:

- the parallel path of `connect_children` (≥10k new children)
- the parallel invalidation of output dependents

Before this change the child context copied an override that is usually
unset. On a helper thread, `get_current_task_priority()` then fell back
to the task-local task state. That state only exists on the thread
running the task, so helper threads got `TaskPriority::initial()`. Tasks
scheduled or made dirty from those chunks (e.g. via
`make_task_dirty_internal`) therefore got a priority that depended on
which thread ran the chunk.

### How?

The priority is resolved in `child_context()`, on the thread that
creates the context. An explicit override still wins; otherwise the
task-local priority is read before the work moves to another thread.
Making the field non-optional means a child context can no longer carry
an unresolved priority.

Putting the fix in `child_context()` covers both parallel users, and any
future one. Neither user relied on the late lookup, because the child
context always runs on another thread. Paths that don't go parallel are
unchanged.

Verification:

- `cargo check -p turbo-tasks-backend --all-targets`
- `cargo clippy -p turbo-tasks-backend --all-targets`
- `cargo nextest run -p turbo-tasks-backend` (329 passed)

This PR does not add a regression test for priority on the parallel
paths. Such a test could build on the existing
`dirty_parent_priority_for_testing` hook used in
`tests/invalidate_all.rs`.

<!-- NEXT_JS_LLM -->


<!-- fleet 458ef681-a3c9-47b5-a9fe-b400713e9995 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 7812b031
...@@ -113,7 +113,7 @@ pub struct ExecuteContext<'e> { ...@@ -113,7 +113,7 @@ pub struct ExecuteContext<'e> {
task_lock_counter: TaskLockCounter, task_lock_counter: TaskLockCounter,
/// Overrides the current task priority, which is otherwise read from the task-local task /// Overrides the current task priority, which is otherwise read from the task-local task
/// state. Set for work that runs off the calling task's thread (see /// state. Set for work that runs off the calling task's thread (see
/// [`ExecuteContext::with_current_task_priority`]). /// [`ExecuteContext::with_current_task_priority`] and [`ExecuteContext::child_context`]).
current_task_priority: Option<TaskPriority>, current_task_priority: Option<TaskPriority>,
} }
...@@ -948,6 +948,13 @@ fn apply_restore_result( ...@@ -948,6 +948,13 @@ fn apply_restore_result(
} }
impl<'e> ExecuteContext<'e> { impl<'e> ExecuteContext<'e> {
/// Creates a context that can be sent to another thread (e.g. a `scope_bounded` helper
/// thread) to continue work on behalf of this context.
///
/// The current task priority is resolved here, on the calling thread, because the task-local
/// task state it is otherwise read from is not available on other threads. Without this,
/// tasks scheduled from the child context would get a priority that depends on which thread
/// the work happens to run on.
pub fn child_context<'l>(&self) -> ChildExecuteContext<'l> pub fn child_context<'l>(&self) -> ChildExecuteContext<'l>
where where
'e: 'l, 'e: 'l,
...@@ -955,7 +962,7 @@ impl<'e> ExecuteContext<'e> { ...@@ -955,7 +962,7 @@ impl<'e> ExecuteContext<'e> {
ChildExecuteContext { ChildExecuteContext {
backend: self.backend, backend: self.backend,
turbo_tasks: self.turbo_tasks, turbo_tasks: self.turbo_tasks,
current_task_priority: self.current_task_priority, current_task_priority: self.get_current_task_priority(),
} }
} }
...@@ -1245,7 +1252,9 @@ impl<'e> ExecuteContext<'e> { ...@@ -1245,7 +1252,9 @@ impl<'e> ExecuteContext<'e> {
pub struct ChildExecuteContext<'e> { pub struct ChildExecuteContext<'e> {
backend: &'e TurboTasksBackend, backend: &'e TurboTasksBackend,
turbo_tasks: &'e TurboTasks<TurboTasksBackend>, turbo_tasks: &'e TurboTasks<TurboTasksBackend>,
current_task_priority: Option<TaskPriority>, /// The priority of the task that created this context, resolved on its thread (see
/// [`ExecuteContext::child_context`]).
current_task_priority: TaskPriority,
} }
impl<'e> ChildExecuteContext<'e> { impl<'e> ChildExecuteContext<'e> {
...@@ -1259,7 +1268,7 @@ impl<'e> ChildExecuteContext<'e> { ...@@ -1259,7 +1268,7 @@ impl<'e> ChildExecuteContext<'e> {
// already waits for, so it needs no shutdown guard of its own. // already waits for, so it needs no shutdown guard of its own.
_shutdown_guard: None, _shutdown_guard: None,
task_lock_counter: TaskLockCounter::new(), task_lock_counter: TaskLockCounter::new(),
current_task_priority: self.current_task_priority, current_task_priority: Some(self.current_task_priority),
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment