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,70 +1185,66 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT ...@@ -1185,70 +1185,66 @@ 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; // it's okay for execution ids to overflow and wrap, they're just used
while schedule_again { // for an assert
// it's okay for execution ids to overflow and wrap, they're just used let execution_id = this.execution_id_factory.wrapping_get();
// for an assert let current_task_state = Arc::new(RwLock::new(CurrentTaskState::new(
let execution_id = this.execution_id_factory.wrapping_get(); task_id,
let current_task_state = Arc::new(RwLock::new(CurrentTaskState::new( execution_id,
task_id, priority,
execution_id, false, // in_top_level_task
priority, )));
false, // in_top_level_task let single_execution_future = async {
))); if this.stopped.load(Ordering::Acquire) {
let single_execution_future = async { this.backend.task_execution_canceled(task_id, &*this);
if this.stopped.load(Ordering::Acquire) { return None;
this.backend.task_execution_canceled(task_id, &*this); }
return false;
} let TaskExecutionSpec { future, span } = this
.backend
let Some(TaskExecutionSpec { future, span }) = this .try_start_task_execution(task_id, priority, &*this)?;
.backend
.try_start_task_execution(task_id, priority, &*this) async {
else { let result = CaptureFuture::new(future).await;
return false;
// wait for all spawned local tasks using `local` to finish
wait_for_local_tasks().await;
let result = match result {
Ok(Ok(raw_vc)) => {
// This is safe because we waited for all local tasks to
// complete above
raw_vc
.to_non_local_unchecked_sync(&*this)
.map_err(|err| err.into())
}
Ok(Err(err)) => Err(err.into()),
Err(err) => Err(TurboTasksExecutionError::Panic(Arc::new(err))),
}; };
async { let finished_state = this.finish_current_task_state();
let result = CaptureFuture::new(future).await; let cell_counters = CURRENT_TASK_STATE
.with(|ts| ts.write().unwrap().cell_counters.take().unwrap());
// wait for all spawned local tasks using `local` to finish this.backend.task_execution_completed(
wait_for_local_tasks().await; task_id,
result,
let result = match result { &cell_counters,
Ok(Ok(raw_vc)) => { #[cfg(feature = "verify_determinism")]
// This is safe because we waited for all local tasks to finished_state.stateful,
// complete above finished_state.has_invalidator,
raw_vc &*this,
.to_non_local_unchecked_sync(&*this) )
.map_err(|err| err.into()) }
} .instrument(span)
Ok(Err(err)) => Err(err.into()), .await
Err(err) => { };
Err(TurboTasksExecutionError::Panic(Arc::new(err))) if let Some(stale_priority) = CURRENT_TASK_STATE
} .scope(current_task_state, single_execution_future)
}; .await
{
let finished_state = this.finish_current_task_state(); // Task was stale; re-schedule at the correct invalidation priority so
let cell_counters = CURRENT_TASK_STATE.with(|ts| { // other tasks can run in the right priority order.
ts.write().unwrap().cell_counters.take().unwrap() this.schedule(task_id, stale_priority);
});
this.backend.task_execution_completed(
task_id,
result,
&cell_counters,
#[cfg(feature = "verify_determinism")]
finished_state.stateful,
finished_state.has_invalidator,
&*this,
)
}
.instrument(span)
.await
};
schedule_again = CURRENT_TASK_STATE
.scope(current_task_state, single_execution_future)
.await;
} }
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