Unverified Commit a82d9ffc authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

turbo-tasks: clarify abort recovery

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 32647f72
...@@ -2042,22 +2042,6 @@ impl TurboTasksBackend { ...@@ -2042,22 +2042,6 @@ impl TurboTasksBackend {
drop(in_progress_cells); drop(in_progress_cells);
} }
fn rollback_aborted_collectibles(
&self,
task_id: TaskId,
collectible_deltas: FxHashMap<CollectibleRef, i32>,
turbo_tasks: &TurboTasks<TurboTasksBackend>,
) {
for (collectible, delta) in collectible_deltas {
operation::UpdateCollectibleOperation::run_rollback(
task_id,
collectible,
-delta,
self.execute_context(turbo_tasks),
);
}
}
fn task_execution_aborted( fn task_execution_aborted(
&self, &self,
task_id: TaskId, task_id: TaskId,
...@@ -2084,7 +2068,7 @@ impl TurboTasksBackend { ...@@ -2084,7 +2068,7 @@ impl TurboTasksBackend {
self.task_statistics self.task_statistics
.map(|stats| stats.increment_abort_observed(native_fn, abort_reason)); .map(|stats| stats.increment_abort_observed(native_fn, abort_reason));
Span::current().record("abort_trigger", abort_reason.as_str()); Span::current().record("abort_trigger", abort_reason.as_str());
let aborted_as_unneeded = in_progress.abort_when_unneeded(); let aborted_dead = in_progress.abort_when_unneeded();
// `outdated_collectibles` is generation-local bookkeeping initialized at execution start. // `outdated_collectibles` is generation-local bookkeeping initialized at execution start.
// The aborted generation will never reach completion cleanup, so discard it before // The aborted generation will never reach completion cleanup, so discard it before
// reversing that generation's actual current-collectible deltas below. // reversing that generation's actual current-collectible deltas below.
...@@ -2104,39 +2088,35 @@ impl TurboTasksBackend { ...@@ -2104,39 +2088,35 @@ impl TurboTasksBackend {
} = *in_progress; } = *in_progress;
// Only children that were not already connected received a speculative active-count // Only children that were not already connected received a speculative active-count
// increment during this execution. // increment during this execution. See `ConnectChildOperation::run`.
for child in task.iter_children() { for child in task.iter_children() {
new_children.remove(&child); new_children.remove(&child);
} }
let in_progress_cells = if aborted_as_unneeded { let in_progress_cells = if aborted_dead {
task.take_in_progress_cells() task.take_in_progress_cells()
} else { } else {
None None
}; };
if let Some(cells) = &in_progress_cells { if let Some(cells) = &in_progress_cells {
// A dead task discards these cell states. Wake every waiter first so it retries against
// the dirty task; otherwise it could hang or read stale/missing in-progress cell data.
for state in cells.values() { for state in cells.values() {
state.event.notify(usize::MAX); state.event.notify(usize::MAX);
} }
} }
if !aborted_as_unneeded { enum Recovery {
debug_assert!(stale, "only stale or unneeded executions may be aborted"); Stale(TaskPriority),
let priority = compute_stale_priority(&task); Dead {
let old = task.set_in_progress(InProgressState::Scheduled { done_event: Event,
done_event, queue: Box<AggregationUpdateQueue>,
reason: TaskExecutionReason::Stale, },
});
debug_assert!(old.is_none(), "InProgress already exists");
drop(task);
self.rollback_aborted_collectibles(task_id, collectible_deltas, turbo_tasks);
decrease_active_counts_of_new_children(new_children, &mut ctx);
return Some(priority);
} }
let recovery = if aborted_dead {
// Discard the aborted execution state and make the task dirty without scheduling it while // Discard the aborted execution state and make the task dirty without scheduling it
// it is disconnected. Queue child decrements first, but execute all recursive aggregation // while it is disconnected. Queue child decrements first, but execute all recursive
// work only after releasing the parent guard. // aggregation work only after releasing the parent guard.
let mut queue = AggregationUpdateQueue::new(); let mut queue = AggregationUpdateQueue::new();
if !new_children.is_empty() { if !new_children.is_empty() {
queue.push(AggregationUpdateJob::DecreaseActiveCounts { queue.push(AggregationUpdateJob::DecreaseActiveCounts {
...@@ -2146,8 +2126,8 @@ impl TurboTasksBackend { ...@@ -2146,8 +2126,8 @@ impl TurboTasksBackend {
make_task_dirty_internal( make_task_dirty_internal(
&mut task, &mut task,
MakeTaskDirtyOptions { MakeTaskDirtyOptions {
// The aborted execution's in-progress state was already taken above, so there is // The aborted execution's in-progress state was already taken above, so there
// nothing left to mark stale. // is nothing left to mark stale.
make_stale: false, make_stale: false,
schedule_when_active: false, schedule_when_active: false,
#[cfg(feature = "task_dirty_cause")] #[cfg(feature = "task_dirty_cause")]
...@@ -2156,9 +2136,43 @@ impl TurboTasksBackend { ...@@ -2156,9 +2136,43 @@ impl TurboTasksBackend {
&mut queue, &mut queue,
&mut ctx, &mut ctx,
); );
Recovery::Dead {
done_event,
queue: Box::new(queue),
}
} else {
debug_assert!(stale, "only stale or dead executions may be aborted");
let priority = compute_stale_priority(&task);
let old = task.set_in_progress(InProgressState::Scheduled {
done_event,
reason: TaskExecutionReason::Stale,
});
debug_assert!(old.is_none(), "InProgress already exists");
Recovery::Stale(priority)
};
drop(task); drop(task);
self.rollback_aborted_collectibles(task_id, collectible_deltas, turbo_tasks);
queue.execute(&mut ctx); // Restore the collectible state of the last completed generation before either branch
// propagates its task-graph updates. Rollback takes its own task guard.
for (collectible, delta) in collectible_deltas {
operation::UpdateCollectibleOperation::run_rollback(
task_id,
collectible,
-delta,
self.execute_context(turbo_tasks),
);
}
let done_event = match recovery {
Recovery::Stale(priority) => {
decrease_active_counts_of_new_children(new_children, &mut ctx);
return Some(priority);
}
Recovery::Dead { done_event, queue } => {
(*queue).execute(&mut ctx);
done_event
}
};
// A connection may race with dropping the aborted future. Re-check liveness after the // A connection may race with dropping the aborted future. Re-check liveness after the
// dirty transition so a revived task is not left dirty but unscheduled. // dirty transition so a revived task is not left dirty but unscheduled.
......
...@@ -6,7 +6,10 @@ mod util; ...@@ -6,7 +6,10 @@ mod util;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use anyhow::Result; use anyhow::Result;
use tokio::sync::Notify; use tokio::{
sync::Notify,
time::{Duration, timeout},
};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
CollectiblesSource, NonLocalValue, ReadRef, ResolvedVc, State, TransientInstance, CollectiblesSource, NonLocalValue, ReadRef, ResolvedVc, State, TransientInstance,
...@@ -150,6 +153,16 @@ async fn conditional_parent( ...@@ -150,6 +153,16 @@ async fn conditional_parent(
Ok(Vc::cell(0)) Ok(Vc::cell(0))
} }
#[turbo_tasks::function(operation, root)]
async fn collectible_observer(
producer_input: ResolvedVc<ChangingInput>,
control: TransientInstance<ExecutionControl>,
) -> Result<Vc<u32>> {
let collectibles =
collectible_producer(producer_input, control).peek_collectibles::<Box<dyn ValueToString>>();
Ok(Vc::cell(collectibles.len() as u32))
}
#[turbo_tasks::function(operation, root)] #[turbo_tasks::function(operation, root)]
async fn conditional_non_root_parent( async fn conditional_non_root_parent(
selector: ResolvedVc<ChangingInput>, selector: ResolvedVc<ChangingInput>,
...@@ -183,23 +196,36 @@ async fn aborted_first_execution_does_not_report_collectible() { ...@@ -183,23 +196,36 @@ async fn aborted_first_execution_does_not_report_collectible() {
control.wait_for_emitted(1).await; control.wait_for_emitted(1).await;
selector.state.set(1); selector.state.set(1);
control.wait_for_dropped(1).await; control.wait_for_dropped(1).await;
// If reading collectibles revives the producer, its replacement emits nothing and // If observing collectibles revives the producer, its replacement emits nothing and
// therefore cannot hide the aborted generation's leaked collectible. // therefore cannot hide the aborted generation's leaked collectible.
producer_input.state.set(0); producer_input.state.set(0);
anyhow::Ok(()) anyhow::Ok(())
}; };
tokio::try_join!(read_parent, disconnect)?; tokio::try_join!(read_parent, disconnect)?;
let collectibles = collectible_producer(producer_input_vc, control.clone()) let producer = collectible_producer(producer_input_vc, control.clone());
.peek_collectibles::<Box<dyn ValueToString>>(); // The first read schedules the dirty producer. Starting its replacement proves the abort
// callback (including rollback) finished; waiting only for the old future's Drop is racy.
let _ = producer.peek_collectibles::<Box<dyn ValueToString>>();
timeout(Duration::from_secs(10), control.wait_for_started(2))
.await
.expect("replacement generation did not start");
assert!( assert!(
collectibles.is_empty(), producer
"an aborted generation reported {} collectible(s)", .peek_collectibles::<Box<dyn ValueToString>>()
collectibles.len() .is_empty(),
"an aborted generation reported a collectible"
); );
// Keep cleanup safe if a failure above leaves the execution parked. // Let the non-emitting replacement finish, then verify through a strongly-consistent root.
control.release.notify_one(); control.release.notify_one();
assert_eq!(
*collectible_observer(producer_input_vc, control.clone())
.read_strongly_consistent()
.await?,
0,
"a completed replacement reported the aborted generation's collectible"
);
anyhow::Ok(()) anyhow::Ok(())
}) })
.await .await
......
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