Unverified Commit 645cae03 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-tasks-backend: simplify the collectibility predicate and collect transient tasks (#98615)

Fix refcounting for transient tasks
- ensure transient tasks refcount each other correctly, and are eligible
for collection
- ensure root tasks are born with a transient_ref_count of 1

Also drop some ultimately harmful conditions from the gc predicate
- don't consider `cell_dependent`. cell dependencies form cycles and are
subsumed by ancestors
- to read a cell you have to read it from a child (covered by
parent_count) or be passed it from a parent. For the parent to have
passed it to you it must have a child dependency on the producer or be
passed it by its parent (recursively). So the task that passed you the
vc must be holding ownership over both tasks.
- therefore a stale cell_dependency can only exist within a task that is
effectively dead, however because we do not reliably clean up all tasks
in a session (due to leaking root tasks), we cannot fully exclude the
existence of stale cell_dependency edges. We just know they must only
exist within unreachable tasks.

---------
Co-authored-by: 's avatarClaude Opus 5 (1M context) <noreply@anthropic.com>
parent cc7e804c
......@@ -435,12 +435,10 @@ impl TurboTasksBackend {
}
}
/// The number of persistent (non-transient) tasks resident in the map. Test-only hook; see
/// [`Storage::resident_persistent_task_count_for_testing`] for why the metric excludes
/// transient tasks.
/// The number oftasks resident in the map.
#[doc(hidden)]
pub fn resident_persistent_task_count_for_testing(&self) -> usize {
self.storage.resident_persistent_task_count_for_testing()
pub fn resident_task_count_for_testing(&self) -> usize {
self.storage.resident_task_count_for_testing()
}
/// The persistent `parent_count` of a resident task (0 if absent or not resident). Test-only
......
......@@ -160,27 +160,33 @@ impl CleanupOldEdgesOperation {
});
let mut task = ctx.task(task_id, TaskDataCategory::All);
let mut removed_persistent_children =
SmallVec::<[TaskId; 4]>::new();
// Mirror `ConnectChildrenOperation`'s split exactly: an edge
// counted as durable is released from `parent_count`, everything
// else from `transient_ref_count`. Getting this wrong either
// strands a task forever or underflows the count.
let parent_is_transient = task_id.is_transient();
let mut removed_durable = SmallVec::<[TaskId; 4]>::new();
let mut removed_transient = SmallVec::<[TaskId; 4]>::new();
for child_id in children.iter() {
if task.remove_children(child_id) && !child_id.is_transient() {
removed_persistent_children.push(*child_id);
if task.remove_children(child_id) {
if parent_is_transient || child_id.is_transient() {
removed_transient.push(*child_id);
} else {
removed_durable.push(*child_id);
}
}
}
// Each removed persistent child loses a parent.
if !removed_persistent_children.is_empty() {
let job = if task_id.is_transient() {
AggregationUpdateJob::AdjustTransientRefCount {
task_ids: removed_persistent_children,
delta: -1,
}
} else {
AggregationUpdateJob::AdjustParentCount {
task_ids: removed_persistent_children,
delta: -1,
}
};
queue.push(job);
if !removed_durable.is_empty() {
queue.push(AggregationUpdateJob::AdjustParentCount {
task_ids: removed_durable,
delta: -1,
});
}
if !removed_transient.is_empty() {
queue.push(AggregationUpdateJob::AdjustTransientRefCount {
task_ids: removed_transient,
delta: -1,
});
}
if is_aggregating_node(get_aggregation_number(&task)) {
drop(task);
......
......@@ -87,6 +87,14 @@ impl ConnectChildOperation {
release_construction_ref: bool,
mut ctx: impl ExecuteContext<'_>,
) {
if parent_task_id.is_none() {
// All parentless tasks receive a transient ref when connected: their lifetime cannot be
// constrained by turbo-tasks and needs to be managed by the caller. If the caller
// doesn't manage it, the GC root TTL handles it in a later session.
let mut child_task =
ctx.open_or_create_task_storage(child_task_id, TaskDataCategory::Meta);
child_task.update_and_get_transient_ref_count(1);
}
if let Some(parent_task_id) = parent_task_id {
let mut parent_task = ctx.task(parent_task_id, TaskDataCategory::Meta);
let Some(InProgressState::InProgress(InProgressStateInner { new_children, .. })) =
......
......@@ -71,12 +71,16 @@ pub fn connect_children(
"connect_children parent_count + dirty",
|mut child, ctx| {
// Bump before `make_task_dirty_internal`, which consumes the guard.
if !child.id().is_transient() {
if parent_is_transient {
child.update_and_get_transient_ref_count(1);
} else {
child.update_and_get_parent_count(1);
}
//
// `parent_count` is the *durable* count, so it may only track an edge that will
// itself be persisted: a persistent parent holding a persistent child. Every
// other combination is session-only and belongs in `transient_ref_count` --
// including a transient child, whose incoming edges can never outlive the
// session no matter what kind of parent holds them.
if parent_is_transient || child.id().is_transient() {
child.update_and_get_transient_ref_count(1);
} else {
child.update_and_get_parent_count(1);
}
if !child.has_output() {
make_task_dirty_internal(
......
......@@ -1582,15 +1582,10 @@ pub trait TaskGuard: Debug + TaskStorageAccessors {
new_value
}
/// Whether a GC pass may collect this task: it is non-transient and nothing references it.
///
/// How much this proves depends on the guard's category — with only `Meta` open it is a sound
/// pre-filter that cannot see dependency edges, and with `All` open it is authoritative. See
/// Whether a GC pass may collect this task: nothing references it.
fn is_gc_collectible(&self) -> bool {
// Transient-ness is a property of the id, not the storage; transient tasks are never
// collected.
self.check_access(SpecificTaskDataCategory::Meta);
!self.id().is_transient() && self.typed().gc_maybe_collectible()
self.typed().gc_maybe_collectible()
}
fn invalidate_serialization(&mut self);
......
......@@ -544,21 +544,10 @@ impl Storage {
Some(f(task.value()))
}
/// The number of **persistent** (non-transient) tasks resident in the map. Use this to assert
/// GC returns to a flat baseline across re-rooting: GC never collects transient tasks (e.g.
/// `run_once`/Once roots), so their count is not expected to settle.
/// The number of tasks resident in the map.
#[doc(hidden)]
pub fn resident_persistent_task_count_for_testing(&self) -> usize {
let mut persistent = 0;
for shard in self.map.shards() {
let shard = shard.read();
for (task_id, _) in shard.iter() {
if !task_id.is_transient() {
persistent += 1;
}
}
}
persistent
pub fn resident_task_count_for_testing(&self) -> usize {
self.map.len()
}
/// The number of shards in the resident map. GC seeds one `ScanShard` job per index; the slice
......@@ -568,32 +557,17 @@ impl Storage {
self.map.shards().len()
}
/// Iterates the non-transient tasks of a **single** shard of the resident map by index, under
/// that shard's read lock.
fn for_each_resident_persistent_in_shard(
&self,
index: usize,
mut f: impl FnMut(TaskId, &TaskStorage),
) {
/// Scans a **single** shard by index, invoking `on_candidate` for each resident task whose
/// storage passes [`TaskStorage::gc_maybe_collectible`].
pub fn gc_scan_shard(&self, index: usize, mut on_candidate: impl FnMut(TaskId)) {
let shard = self.map.shards()[index].read();
for (task_id, task) in shard.iter() {
if task_id.is_transient() {
continue;
if task.gc_maybe_collectible() {
on_candidate(*task_id);
}
f(*task_id, task);
}
}
/// Scans a **single** shard by index, invoking `on_candidate` for each resident, non-transient
/// task whose storage passes the cheap [`TaskStorage::gc_maybe_collectible`] pre-filter.
pub fn gc_scan_shard(&self, index: usize, mut on_candidate: impl FnMut(TaskId)) {
self.for_each_resident_persistent_in_shard(index, |task_id, storage| {
if storage.gc_maybe_collectible() {
on_candidate(task_id);
}
});
}
/// Return the set of all known live roots.
pub fn gc_scan_roots(&self) -> impl Iterator<Item = TaskId> {
// Roots that failed the "held by a transient pin" expectation, with the referencing tasks
......@@ -605,20 +579,21 @@ impl Storage {
let per_shard: Vec<Vec<TaskId>> =
parallel::map_collect(&(0..self.shard_count()).collect::<Vec<_>>(), |&index| {
let mut roots = Vec::new();
self.for_each_resident_persistent_in_shard(index, |task_id, storage| {
if storage.gc_is_root() {
// The `is_root` criteria is conservative, in debug assert that we aren't
// marking things as roots for surprising reasons
let shard = self.map.shards()[index].read();
for (task_id, task) in shard.iter() {
if !task_id.is_transient() && task.gc_is_root() {
// The `is_root` criteria is conservative, in debug assert that we
// aren't marking things as roots for surprising reasons
#[cfg(debug_assertions)]
if !storage.gc_is_held_by_transient_pin() {
if !task.gc_is_held_by_transient_pin() {
unexpected
.lock()
.unwrap()
.push((task_id, storage.gc_root_holders()));
.push((*task_id, task.gc_root_holders()));
}
roots.push(task_id);
roots.push(*task_id);
}
});
}
roots
});
......@@ -743,20 +718,22 @@ impl Storage {
}
};
shard.retain(|(task_id, task)| {
if task_id.is_transient() {
// Transient tasks can not be evicted at all, unless they are fully
// delete by the GC.
if task_id.is_transient() && !task.flags.deleted() {
evicted.unevictable_reasons[UnevictableReason::Transient.index()] += 1;
return true;
}
// GC'd tasks were tombstoned during the snapshot so we can drop them fully now.
// All GC'd tasks were tombstoned during the snapshot (or are not persisted) so we
// can drop them fully now.
if task.flags.deleted() {
let task_type = task
.get_persistent_task_type()
.expect("GC deleted tasks must have a task type");
remove_from_task_cache(
&mut evicted,
&mut deferred_task_cache_removals,
task_type,
);
if let Some(task_type) = task.get_persistent_task_type() {
remove_from_task_cache(
&mut evicted,
&mut deferred_task_cache_removals,
task_type,
);
}
evicted.full += 1;
return false;
}
......
......@@ -868,14 +868,26 @@ impl TaskStorage {
}
/// Whether a GC pass may collect this task: nothing references it, via parents, transient
/// pins, aggregation edges, or dependency edges.
/// pins, or aggregation edges.
///
/// Precision depends on what the caller restored. Meta alone cannot see the three Data-category
/// dependent sets, so the answer is a sound *pre-filter*: a `false` is definitive, a `true` may
/// still have dependents. With Meta + Data it is the full predicate. That one-directional
/// conservatism lets the cheap Meta-only shard scan and the authoritative under-guard recheck
/// (which opens `TaskDataCategory::All`, and is what actually gates collection) share this
/// single predicate.
/// Only reads `Meta`, so the shard scan and the under-guard recheck get the same answer -- it
/// is exact in both, not a pre-filter.
///
/// The `Data`-category dependent sets are deliberately not consulted:
///
/// - `cell_dependents` / `cell_dependents_hashed` are redundant with ancestry. A cell dependent
/// is either a child, whose child edge already orders the teardown, or a sibling reached by
/// passing a `ResolvedVc` laterally, which needs a common ancestor that collects both in the
/// same pass. Counting them deadlocked the caller/callee cycle `NftJsonAsset::content` ->
/// `all_assets_from_entries_filtered`, whose tasks could then never be collected.
/// - `output_dependent` is redundant with `parent_count`. It records a read of a task's
/// *output*, which is the `OperationVc` representation, and those reads go through
/// `connect()` -- so the reader is already a child. (A `ResolvedVc` read, the one that
/// travels laterally as an argument, lands in `cell_dependents` instead.)
///
/// Removing the cell sets exposed a race in the GC cascade -- rebalancing running while other
/// workers were still collecting -- which `gc_collect` now avoids by deferring all rebalance
/// work until the parallel phase is quiescent.
pub fn gc_maybe_collectible(&self) -> bool {
// None of the predicates below are correct without this.
self.flags.is_restored(TaskDataCategory::Meta)
......@@ -890,18 +902,12 @@ impl TaskStorage {
// It is rare for an upper to be present when the ref counts are 0, but it
// happens transiently during a concurrent GC pass as uppers move around in the cascade.
&& self.upper().is_empty()
// `collectibles_dependents` is Meta, so it is always checkable here.
// It would be rare for a collectibles dependent to be the only thing holding a task,
// but the invalidation that disconnected the task may not have bubbled all the way up
// yet.
&& self
.collectibles_dependents()
.is_none_or(|d| d.is_empty())
// The remaining dependent sets are Data; skipped (leaving this a pre-filter) when Data
// is not restored.
&& (!self.flags.is_restored(TaskDataCategory::Data)
|| (self.output_dependent().is_empty()
&& self.cell_dependents().is_none_or(|d| d.is_empty())
&& self
.cell_dependents_hashed()
.is_none_or(|d| d.is_empty())))
}
/// Whether this task is a GC **root**: parent-less, but pinned for some reason
......
......@@ -259,7 +259,16 @@ async fn dispose_root_task_releases_anchored_subgraph() {
turbo_tasks::run_once(tt.clone(), async move { anyhow::Ok(()) })
.await
.unwrap();
assert_eq!(tt.backend().gc_for_testing(&tt), 1);
// Two: the leaf, and the disposed root_task` itself. The `root_task` is a transient task, and
// transient tasks are collectible, so releasing the last reference to the subgraph reclaims
// both.
assert_eq!(
tt.backend()
.snapshot_and_evict_for_testing(&tt)
.gc_stats()
.collected,
2
);
// Disposal after the backend has stopped (the whole task map is dropped by `stop`), as a
// `RootTask` finalized during Node worker teardown would be.
......
......@@ -115,11 +115,11 @@ async fn gc_rebalances_aggregation_and_cascades_in_one_pass() {
result.unwrap();
// Baseline resident count with the reader subtree disconnected but not yet collected.
let baseline = tt2.backend().resident_persistent_task_count_for_testing();
let baseline = tt2.backend().resident_task_count_for_testing();
let collected = tt2.backend().gc_for_testing(&tt2);
tt2.backend().snapshot_and_evict_for_testing(&tt2);
let after = tt2.backend().resident_persistent_task_count_for_testing();
let after = tt2.backend().resident_task_count_for_testing();
assert_eq!(
collected,
......@@ -172,11 +172,11 @@ async fn gc_diamond_forward_dep_no_resurrection() {
.await;
result.unwrap();
let baseline = tt2.backend().resident_persistent_task_count_for_testing();
let baseline = tt2.backend().resident_task_count_for_testing();
let collected = tt2.backend().gc_for_testing(&tt2);
tt2.backend().snapshot_and_evict_for_testing(&tt2);
let after = tt2.backend().resident_persistent_task_count_for_testing();
let after = tt2.backend().resident_task_count_for_testing();
assert_eq!(
collected,
......
......@@ -66,7 +66,7 @@ async fn gc_re_rooting_stays_flat() {
// independently of GC — including them would mask the real signal.
(
collected,
tt.backend().resident_persistent_task_count_for_testing(),
tt.backend().resident_task_count_for_testing(),
interrupted,
)
}
......
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