Unverified Commit 1119ce6b authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-tasks-backend: stop dirtying dependents of GC-collected tasks (#99647)

### What?

When GC collects a task, it removes each dependent's forward edge to it,
as before, but no longer marks the dependent dirty.

### Why?

The dirty never helps, the dependent task's previous execution is still
valid. But it is also 'stale', no task should ever read it again, so
providing a hint that it should be recomputed is harmful. Such a task is
in a "dead but doesn't know it yet" state and so we can ignore it until
its parent disconnects it.

We still need to remove the edges so when the dependent is itself
collected it doesn't panic because its dependencies have disappeared.
parent ef6a1004
......@@ -31,7 +31,7 @@ use crate::{
backend::{
TurboTasksBackend,
operation::{
AggregationUpdateJob, AggregationUpdateQueue, ExecuteContext, capture_all_edges,
AggregationUpdateQueue, ExecuteContext, capture_all_edges,
cleanup_old_edges_deletions_only,
},
snapshot_coordinator::SnapshotPhase,
......@@ -129,9 +129,6 @@ pub struct GcPassResult {
deleted_roots: Vec<TaskId>,
/// Aggregation rebalance requests that were deferred from the main GC loop.
deferred_balance_edges: FxHashSet<(TaskId, TaskId)>,
/// Dependents of collected tasks whose forward edge was scrubbed, deferred from the main GC
/// loop. Dirtying propagates through the aggregation graph, so it must not race the cascade.
deferred_dirty_dependents: FxHashSet<TaskId>,
/// The gc loop was interrupted by competing work.
pub interrupted: bool,
}
......@@ -179,15 +176,6 @@ impl GcPassResult {
}
self.deferred_balance_edges
.extend(other.deferred_balance_edges);
// merge into the larger set and keep that one
if other.deferred_dirty_dependents.len() > self.deferred_dirty_dependents.len() {
std::mem::swap(
&mut self.deferred_dirty_dependents,
&mut other.deferred_dirty_dependents,
);
}
self.deferred_dirty_dependents
.extend(other.deferred_dirty_dependents);
self.interrupted |= other.interrupted;
self
}
......@@ -301,9 +289,6 @@ impl TurboTasksBackend {
// we defer all rebalancing to the end
let deferred = cleanup_old_edges_deletions_only(task_id, old_edges, &mut ctx);
result.deferred_balance_edges.extend(deferred.balance_edges);
result
.deferred_dirty_dependents
.extend(deferred.dirty_dependents);
ControlFlow::Continue(())
},
|(stats, result), (other_stats, other_result)| {
......@@ -327,21 +312,6 @@ impl TurboTasksBackend {
while !queue.process(&mut ctx) {}
}
// Dirty the dependents whose edges were scrubbed. After the rebalance above so the
// aggregation graph is settled, and before the root scan below because dirtying can change
// activeness and therefore rootness.
let dirty_dependents = std::mem::take(&mut result.deferred_dirty_dependents);
if !dirty_dependents.is_empty() {
let noop_collector = |_task_id| {};
let mut ctx = ExecuteContext::new_for_gc(self, turbo_tasks, phase, &noop_collector);
let mut queue = AggregationUpdateQueue::new();
// A dependent collected by this same pass is skipped: the job is weak by construction.
queue.push(AggregationUpdateJob::InvalidateDueToDependencyTornDown {
task_ids: dirty_dependents.into_iter().collect(),
});
while !queue.process(&mut ctx) {}
}
// Collect all active roots
// We don't do this in the GC pass because a task detected as a root 'early' might become a
// non-root later due to other operations (e.g. it might get promoted to a live aggregation
......
......@@ -41,9 +41,8 @@ use crate::{
backend::{
TaskDataCategory,
operation::{
ExecuteContext, TaskGuard,
connect_child::resurrect_deleted,
invalidate::{make_task_dirty_internal, try_make_task_dirty},
ExecuteContext, TaskGuard, connect_child::resurrect_deleted,
invalidate::make_task_dirty_internal,
},
storage_schema::TaskStorageAccessors,
},
......@@ -292,11 +291,6 @@ pub enum AggregationUpdateJob {
AdjustTransientRefCount { task_ids: TaskIdVec, delta: i32 },
/// Notifies an upper task about changed data from an inner task.
AggregatedDataUpdate(Box<AggregatedDataUpdateJob>),
/// Mark these tasks dirty because they have a dependency on a task being deleted by GC.
///
/// The id references are weak by construction: a dependent that was itself collected is
/// skipped.
InvalidateDueToDependencyTornDown { task_ids: TaskIdVec },
/// Invalidates tasks that are dependent on a collectible type of `collectibles_task`.
InvalidateDueToCollectiblesChange {
task_ids: TaskIdVec,
......@@ -1411,18 +1405,6 @@ impl AggregationUpdateQueue {
}
self.aggregated_data_update(upper_ids, ctx, update);
}
AggregationUpdateJob::InvalidateDueToDependencyTornDown { task_ids } => {
for task_id in task_ids {
// `try_*`: the dependent may itself have been collected in this cascade.
try_make_task_dirty(
task_id,
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::DependencyTornDown,
self,
ctx,
);
}
}
AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids,
collectibles_task,
......
#[cfg(feature = "trace_aggregation_update_stats")]
use std::mem::take;
use auto_hash_map::AutoSet;
use rustc_hash::{FxBuildHasher, FxHashSet};
use rustc_hash::FxHashSet;
use smallvec::SmallVec;
use turbo_tasks::TaskId;
......@@ -82,9 +82,6 @@ type Stats = ();
pub struct DeferredCleanup {
/// Rebalance jobs. `balance_edge` *adds* aggregation edges, which must not happen mid-cascade.
pub balance_edges: Vec<(TaskId, TaskId)>,
/// Dependents whose forward edge to the torn-down task was scrubbed. They must be dirtied: the
/// value they were derived from no longer exists, so their cached result cannot be trusted.
pub dirty_dependents: AutoSet<TaskId, FxBuildHasher, 2>,
}
pub fn cleanup_old_edges(
......@@ -100,8 +97,7 @@ pub fn cleanup_old_edges(
///
/// Returns the work the deletions produced but did not run, for the caller to replay once the
/// parallel collect is quiescent. Deletion is safe to run concurrently; the deferred work is
/// not. `balance_edge` *adds* edges, and dirtying propagates through the aggregation graph --
/// neither is safe while other workers are still collecting.
/// not: `balance_edge` *adds* edges, which is unsafe while other workers are still collecting.
pub fn cleanup_old_edges_deletions_only(
task_id: TaskId,
outdated: Vec<OutdatedEdge>,
......@@ -125,7 +121,6 @@ fn cleanup_old_edges_inner(
ctx: &mut ExecuteContext<'_>,
stop_when_only_rebalance_remains: bool,
) -> (Stats, Option<DeferredCleanup>) {
let mut dirty_dependents = AutoSet::default();
while let Some(edge) = outdated.pop() {
match edge {
OutdatedEdge::Child(child_id) => {
......@@ -306,9 +301,14 @@ fn cleanup_old_edges_inner(
}
}
// Handle reverse edges, we remove these when `task_id` is being
// deleted. The reverse dependents must exist and we dirty them as we
// go. We also don't bother removing cell_dependents from `task_id`
// since that task is being deleted.
// deleted. The reverse dependents must exist. We also don't bother
// removing cell_dependents from `task_id` since that task is being
// deleted.
//
// These dependents are 'stale' and will likely be deleted soon, so they
// are not dirtied. Removing their forward edge ensures that when they are deleted they
// don't panic trying to remove their dependencies (since they have already been
// deleted).
OutdatedEdge::CellDependentOfDeleted(CellRef {
task: dependent_task_id,
cell,
......@@ -322,8 +322,6 @@ fn cleanup_old_edges_inner(
let mut task = ctx.task(dependent_task_id, TaskDataCategory::Data);
task.remove_cell_dependencies(&forward);
task.remove_outdated_cell_dependencies(&forward);
drop(task);
dirty_dependents.insert(dependent_task_id);
}
OutdatedEdge::HashedCellDependentOfDeleted(
CellRef {
......@@ -339,23 +337,13 @@ fn cleanup_old_edges_inner(
let mut task = ctx.task(dependent_task_id, TaskDataCategory::Data);
task.remove_cell_dependencies_hashed(&(forward, key));
task.remove_outdated_cell_dependencies_hashed(&(forward, key));
drop(task);
dirty_dependents.insert(dependent_task_id);
}
OutdatedEdge::OutputDependentOfDeleted(dependent_task_id) => {
let mut task = ctx.task(dependent_task_id, TaskDataCategory::Data);
task.remove_output_dependencies(&task_id);
task.remove_outdated_output_dependencies(&task_id);
drop(task);
dirty_dependents.insert(dependent_task_id);
}
}
// Flush accumulated dirty dependents to the aggregation update queue.
if !stop_when_only_rebalance_remains && !dirty_dependents.is_empty() {
queue.push(AggregationUpdateJob::InvalidateDueToDependencyTornDown {
task_ids: take(&mut dirty_dependents).into_iter().collect(),
});
}
}
while !stop_when_only_rebalance_remains || !queue.only_rebalance_remains() {
......@@ -374,7 +362,6 @@ fn cleanup_old_edges_inner(
Default::default(),
Some(DeferredCleanup {
balance_edges: queue.take_deferred_balance_edges().collect(),
dirty_dependents,
}),
)
}
......@@ -355,19 +355,29 @@ async fn shared_cell_target_collected_before_its_second_reader() {
}
}
/// A task holding a collected task's `Vc` in its arguments fails with an error when it
/// re-executes, rather than panicking.
///
/// This is the stale-mapper shape: reader 2 receives the shared target as an argument and reads
/// it, but only the owning side is the target's parent. Once the owning side is collected, reader 2
/// can never re-execute successfully, and nothing guarantees its parent re-runs (and drops it)
/// first. Here the teardown dirties reader 2, which re-executes right away against the collected
/// target.
/// The stale-mapper shape: the target arrives as an argument (so this task is not its parent), and
/// `other` is a second, unrelated dependency that can invalidate it.
#[turbo_tasks::function]
async fn stale_reader(target: ResolvedVc<u32>, other: ResolvedVc<Constant>) -> Result<Vc<u32>> {
let other = *other.await?.get();
Ok(Vc::cell(other + *target.await?))
}
#[turbo_tasks::function(operation, root)]
async fn stale_reader_root(
target: ResolvedVc<u32>,
other: ResolvedVc<Constant>,
) -> Result<Vc<u32>> {
Ok(Vc::cell(*stale_reader(*target, *other).await?))
}
/// A dependent holding a collected task's `Vc` in its arguments fails with an error, rather than a
/// panic, when an unrelated dependency invalidates it before its parent drops it.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn reading_a_collected_task_is_an_error() {
let dir = create_persistence_dir("reading_a_collected_task_is_an_error");
let tt = reopen_tt_with_gc(&dir);
let borrowing_op = turbo_tasks::run_once(tt.clone(), async move {
let (reader_op, other) = turbo_tasks::run_once(tt.clone(), async move {
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
let selector = selector_op.read_strongly_consistent().await?;
......@@ -375,39 +385,49 @@ async fn reading_a_collected_task_is_an_error() {
let constant_op = create_constant();
let constant_vc = constant_op.resolve().strongly_consistent().await?;
// A second `Constant` cell, distinct from the owning side's, as the unrelated dependency.
let other_op = create_other_constant();
let other_vc = other_op.resolve().strongly_consistent().await?;
let other = other_op.read_strongly_consistent().await?;
let owning = select_owning(selector_vc, constant_vc);
let target = owning.resolve().strongly_consistent().await?;
assert_eq!(*target.await?, 41);
let borrowing = borrowing_root(target);
assert_eq!(*borrowing.read_strongly_consistent().await?, 43);
let reader = stale_reader_root(target, other_vc);
assert_eq!(*reader.read_strongly_consistent().await?, 41);
// Drop the owning subtree cleanly: no invalidation, so the target keeps its edges.
selector.set(true);
assert_eq!(*owning.read_strongly_consistent().await?, 0);
anyhow::Ok(borrowing)
anyhow::Ok((reader, other))
})
.await
.unwrap();
let borrowing_pin = GcRoot::pin(tt.clone(), borrowing_op);
// Pin the reader root so only the owning side is collectible.
let pin = GcRoot::pin(tt.clone(), reader_op);
let collected = gc_until_collected(&tt, 3).await;
assert!(
collected >= 3,
"the owning subtree, reader 1 and the shared target should be collected (got {collected})"
);
// Invalidate the reader through its other dependency, so it re-executes while it is still
// alive (here the pin keeps it; in a build its stale parent would).
let result = turbo_tasks::run_once(tt.clone(), async move {
borrowing_op.read_strongly_consistent().await?;
other.set(1);
reader_op.read_strongly_consistent().await?;
anyhow::Ok(())
})
.await;
let err = result.expect_err("reader 2 reads a collected task, so the root must fail");
let err = result.expect_err("the reader reads a collected task, so the root must fail");
assert!(
format!("{err:?}").contains("garbage collected"),
"expected the collected-task read error, got: {err:?}"
);
drop(borrowing_pin);
drop(pin);
tt.stop_and_wait().await;
}
......
......@@ -24,9 +24,6 @@ pub enum TaskDirtyCause {
/// Re-dirtied because a GC-soft-deleted task was resurrected by a new connection before its
/// hard-delete: its edges were scrubbed, so it must re-execute to rebuild them.
Resurrected,
/// Dirtied because a task this one depended on was torn down: the value this task's result was
/// derived from no longer exists, so the result cannot be trusted.
DependencyTornDown,
Unknown,
}
......@@ -90,7 +87,6 @@ impl std::fmt::Display for TaskDirtyCause {
}
TaskDirtyCause::Invalidator => write!(f, "invalidator"),
TaskDirtyCause::Resurrected => write!(f, "resurrected"),
TaskDirtyCause::DependencyTornDown => write!(f, "dependency torn down"),
TaskDirtyCause::Unknown => write!(f, "unknown"),
}
}
......
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