Unverified Commit 191fd742 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

turbopack: reschedule stale tasks with correct invalidation priority (#92897)

### What?

When an in-progress task is invalidated during execution, it transitions
to a "stale" state. Previously, on completion it was directly
re-executed in the same worker slot — inheriting the original schedule
priority rather than the priority from the invalidation that made it
stale.

### Why?

A stale task that was invalidated at low priority was being re-executed
at whatever high priority the original schedule had. This caused
high-priority work to be unfairly blocked or deprioritized in the
scheduler.

### How?

**`backend.rs` trait:** `task_execution_completed` return type changed
from `bool` (reschedule yes/no) to `Option<TaskPriority>` — `None` means
done, `Some(priority)` means the task was stale and must be re-executed
at this priority.

**`backend/mod.rs`:** The three helper functions (`_prepare`,
`_connect`, `_finish`) and the main `task_execution_completed` all
propagate the invalidation priority on stale returns. In each stale
path, the priority is read from
`task.is_dirty().unwrap_or(TaskPriority::leaf())` before the task state
is mutated.

**`manager.rs`:** The executor no longer loops to directly re-execute
stale tasks. Instead, if `task_execution_completed` returns
`Some(stale_priority)`, the task is unconditionally re-scheduled through
the priority runner at that priority, so all tasks execute in the
correct priority order.

<!-- NEXT_JS_LLM_PR -->
parent defac920
...@@ -558,6 +558,11 @@ pub trait Backend: Sync + Send { ...@@ -558,6 +558,11 @@ pub trait Backend: Sync + Send {
fn task_execution_canceled(&self, task: TaskId, turbo_tasks: &dyn TurboTasksBackendApi<Self>); fn task_execution_canceled(&self, task: TaskId, turbo_tasks: &dyn TurboTasksBackendApi<Self>);
/// Called when a task's execution finishes.
///
/// Returns `Some(priority)` if the task was invalidated again while executing and must be
/// re-run. The caller is responsible for re-scheduling the task at the returned priority
/// (typically lower than the priority of the just-finished run).
fn task_execution_completed( fn task_execution_completed(
&self, &self,
task: TaskId, task: TaskId,
...@@ -566,7 +571,7 @@ pub trait Backend: Sync + Send { ...@@ -566,7 +571,7 @@ pub trait Backend: Sync + Send {
#[cfg(feature = "verify_determinism")] stateful: bool, #[cfg(feature = "verify_determinism")] stateful: bool,
has_invalidator: bool, has_invalidator: bool,
turbo_tasks: &dyn TurboTasksBackendApi<Self>, turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) -> bool; ) -> Option<TaskPriority>;
type BackendJob: Send + 'static; type BackendJob: Send + 'static;
......
...@@ -1185,8 +1185,6 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT ...@@ -1185,8 +1185,6 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT
let this = this.clone(); let this = this.clone();
let future = async move { let future = async move {
abort_on_panic(async { abort_on_panic(async {
let mut schedule_again = true;
while schedule_again {
// it's okay for execution ids to overflow and wrap, they're just used // it's okay for execution ids to overflow and wrap, they're just used
// for an assert // for an assert
let execution_id = this.execution_id_factory.wrapping_get(); let execution_id = this.execution_id_factory.wrapping_get();
...@@ -1199,15 +1197,12 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT ...@@ -1199,15 +1197,12 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT
let single_execution_future = async { let single_execution_future = async {
if this.stopped.load(Ordering::Acquire) { if this.stopped.load(Ordering::Acquire) {
this.backend.task_execution_canceled(task_id, &*this); this.backend.task_execution_canceled(task_id, &*this);
return false; return None;
} }
let Some(TaskExecutionSpec { future, span }) = this let TaskExecutionSpec { future, span } = this
.backend .backend
.try_start_task_execution(task_id, priority, &*this) .try_start_task_execution(task_id, priority, &*this)?;
else {
return false;
};
async { async {
let result = CaptureFuture::new(future).await; let result = CaptureFuture::new(future).await;
...@@ -1224,15 +1219,12 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT ...@@ -1224,15 +1219,12 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT
.map_err(|err| err.into()) .map_err(|err| err.into())
} }
Ok(Err(err)) => Err(err.into()), Ok(Err(err)) => Err(err.into()),
Err(err) => { Err(err) => Err(TurboTasksExecutionError::Panic(Arc::new(err))),
Err(TurboTasksExecutionError::Panic(Arc::new(err)))
}
}; };
let finished_state = this.finish_current_task_state(); let finished_state = this.finish_current_task_state();
let cell_counters = CURRENT_TASK_STATE.with(|ts| { let cell_counters = CURRENT_TASK_STATE
ts.write().unwrap().cell_counters.take().unwrap() .with(|ts| ts.write().unwrap().cell_counters.take().unwrap());
});
this.backend.task_execution_completed( this.backend.task_execution_completed(
task_id, task_id,
result, result,
...@@ -1246,9 +1238,13 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT ...@@ -1246,9 +1238,13 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT
.instrument(span) .instrument(span)
.await .await
}; };
schedule_again = CURRENT_TASK_STATE if let Some(stale_priority) = CURRENT_TASK_STATE
.scope(current_task_state, single_execution_future) .scope(current_task_state, single_execution_future)
.await; .await
{
// Task was stale; re-schedule at the correct invalidation priority so
// other tasks can run in the right priority order.
this.schedule(task_id, stale_priority);
} }
this.finish_foreground_job(); this.finish_foreground_job();
}) })
......
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