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 {
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(
&self,
task: TaskId,
......@@ -566,7 +571,7 @@ pub trait Backend: Sync + Send {
#[cfg(feature = "verify_determinism")] stateful: bool,
has_invalidator: bool,
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) -> bool;
) -> Option<TaskPriority>;
type BackendJob: Send + 'static;
......
......@@ -1185,70 +1185,66 @@ impl<B: Backend> Executor<TurboTasks<B>, ScheduledTask, TaskPriority> for TurboT
let this = this.clone();
let future = async move {
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
// for an assert
let execution_id = this.execution_id_factory.wrapping_get();
let current_task_state = Arc::new(RwLock::new(CurrentTaskState::new(
task_id,
execution_id,
priority,
false, // in_top_level_task
)));
let single_execution_future = async {
if this.stopped.load(Ordering::Acquire) {
this.backend.task_execution_canceled(task_id, &*this);
return false;
}
let Some(TaskExecutionSpec { future, span }) = this
.backend
.try_start_task_execution(task_id, priority, &*this)
else {
return false;
// it's okay for execution ids to overflow and wrap, they're just used
// for an assert
let execution_id = this.execution_id_factory.wrapping_get();
let current_task_state = Arc::new(RwLock::new(CurrentTaskState::new(
task_id,
execution_id,
priority,
false, // in_top_level_task
)));
let single_execution_future = async {
if this.stopped.load(Ordering::Acquire) {
this.backend.task_execution_canceled(task_id, &*this);
return None;
}
let TaskExecutionSpec { future, span } = this
.backend
.try_start_task_execution(task_id, priority, &*this)?;
async {
let result = CaptureFuture::new(future).await;
// 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 result = CaptureFuture::new(future).await;
// 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)))
}
};
let finished_state = this.finish_current_task_state();
let cell_counters = CURRENT_TASK_STATE.with(|ts| {
ts.write().unwrap().cell_counters.take().unwrap()
});
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;
let finished_state = this.finish_current_task_state();
let cell_counters = CURRENT_TASK_STATE
.with(|ts| ts.write().unwrap().cell_counters.take().unwrap());
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
};
if let Some(stale_priority) = CURRENT_TASK_STATE
.scope(current_task_state, single_execution_future)
.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();
})
......
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