Unverified Commit a8d6f7a0 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Fix two GC paths that leave half-torn-down dependency edges (#99902)

## Summary

Fixes two GC bugs that each lead to a reported Turbopack panic when GC
and persistent caching are both on:

```
task TaskId N (invalidate cell dependents, MustExist): task is missing in memory or persistent storage — a stale reference to an already-collected or never-created task
```

**Interrupted GC pass + eviction.** When a GC pass is interrupted, tasks
would be marked deleted but tombstones would not be written. Eviction
still dropped every `deleted` task anyway. The next snapshot then
persisted the neighbours' halves of the torn-down edges, while the
collected tasks' last snapshot stayed live on disk. A later lookup
restored such a task as live and clean, with children that GC had since
collected and tombstoned. Touching those children panics with
`MustExist`.

- Solution: eviction should not drop `deleted` tasks if the snapshot was
interrupted


**Resurrected task keeps stale reverse edges.** When GC collects a task
`P`, it scrubbed the forward edge from each dependent `D` but
deliberately left `P`'s own `cell_dependents` / `output_dependent`,
since `P` was being deleted. If `P` was then resurrected, that stale
entry survived. When `D` was collected later it had no forward edge to
`P`, so the entry was never cleared, and the next time `P` updated the
cell it panicked on the missing `D`. This only shows when `D` is
inactive: a live `D` gets dirtied by `P`'s update, re-executes, and
re-registers a fresh edge pair, which hides it.

- Solution: GC should not assume 'deleted' tasks are always deleted!
resurrection defeats it


<!-- NEXT_JS_LLM -->
parent 5a92748e
...@@ -35,7 +35,7 @@ use crate::{ ...@@ -35,7 +35,7 @@ use crate::{
cleanup_old_edges_deletions_only, cleanup_old_edges_deletions_only,
}, },
snapshot_coordinator::SnapshotPhase, snapshot_coordinator::SnapshotPhase,
storage::{SpecificTaskDataCategory, TaskDataCategory}, storage::TaskDataCategory,
storage_schema::TaskStorageAccessors, storage_schema::TaskStorageAccessors,
}, },
backing_storage::SnapshotItem, backing_storage::SnapshotItem,
...@@ -269,13 +269,6 @@ impl TurboTasksBackend { ...@@ -269,13 +269,6 @@ impl TurboTasksBackend {
// writes the tombstone. // writes the tombstone.
drop(task.take_cell_data()); drop(task.take_cell_data());
task.set_deleted(true); task.set_deleted(true);
if task.new_task() {
task.discard_modifications_for_gc_new_task();
} else {
// Persisted ensure it is marked modified so the next snapshot tombstones it.
// It is almost certainly already marked modified, so this is mostly a no-op.
let _ = task.track_modification(SpecificTaskDataCategory::Meta, "gc_deleted");
}
drop(task); // drop the lock so edge cleanup can run drop(task); // drop the lock so edge cleanup can run
stats.collected += 1; stats.collected += 1;
stats.edges_deleted += old_edges.len(); stats.edges_deleted += old_edges.len();
......
...@@ -1474,9 +1474,8 @@ impl TurboTasksBackend { ...@@ -1474,9 +1474,8 @@ impl TurboTasksBackend {
let task_count = task_snapshots.len(); let task_count = task_snapshots.len();
if task_snapshots.is_empty() && gc_roots_to_persist.is_none() { if task_snapshots.is_empty() && gc_roots_to_persist.is_none() {
// This should be impossible — if we got here, modified_count was nonzero or gc_roots // Rare: the modified counts are out of sync with the modified persistable tasks, which
// was present, and every modification that increments the count also failed // only happens if every modified task was both new and deleted.
// during encoding.
std::hint::cold_path(); std::hint::cold_path();
return Ok(Some((snapshot_time, false, gc_outcome))); return Ok(Some((snapshot_time, false, gc_outcome)));
} }
...@@ -2461,18 +2460,26 @@ impl TurboTasksBackend { ...@@ -2461,18 +2460,26 @@ impl TurboTasksBackend {
// Here at completion, we clean up only the OUTDATED deps (the "before" snapshot). // Here at completion, we clean up only the OUTDATED deps (the "before" snapshot).
// Using iter_* (active) instead would incorrectly clean up deps that are still valid, // Using iter_* (active) instead would incorrectly clean up deps that are still valid,
// breaking dependency tracking. // breaking dependency tracking.
old_edges.extend( old_edges.extend(task.iter_outdated_cell_dependencies().map(|cell| {
task.iter_outdated_cell_dependencies() OutdatedEdge::CellDependency {
.map(OutdatedEdge::CellDependency), dependent: task_id,
); cell,
}
}));
old_edges.extend( old_edges.extend(
task.iter_outdated_cell_dependencies_hashed() task.iter_outdated_cell_dependencies_hashed()
.map(|(r, k)| OutdatedEdge::HashedCellDependency(r, k)), .map(|(cell, key)| OutdatedEdge::HashedCellDependency {
); dependent: task_id,
old_edges.extend( cell,
task.iter_outdated_output_dependencies() key,
.map(OutdatedEdge::OutputDependency), }),
); );
old_edges.extend(task.iter_outdated_output_dependencies().map(|output_task| {
OutdatedEdge::OutputDependency {
dependent: task_id,
output_task,
}
}));
old_edges.extend( old_edges.extend(
task.iter_outdated_collectibles_dependencies() task.iter_outdated_collectibles_dependencies()
.map(OutdatedEdge::CollectiblesDependency), .map(OutdatedEdge::CollectiblesDependency),
......
...@@ -1225,7 +1225,9 @@ impl<'a> TaskGuard<'a> { ...@@ -1225,7 +1225,9 @@ impl<'a> TaskGuard<'a> {
#[inline] #[inline]
pub fn assert_not_deleted(&self, operation: &str) { pub fn assert_not_deleted(&self, operation: &str) {
debug_assert!( debug_assert!(
!self.deleted(), // Small hack to work around check_access. Technically a 'deleted' flag is never
// persisted so it cannot be recovered either.
!self.task.flags.deleted(),
"{operation} on GC-deleted task {} — a resurrection path was missed", "{operation} on GC-deleted task {} — a resurrection path was missed",
self.id() self.id()
); );
...@@ -1616,12 +1618,6 @@ impl<'a> TaskGuard<'a> { ...@@ -1616,12 +1618,6 @@ impl<'a> TaskGuard<'a> {
self.category = access; self.category = access;
} }
/// Clears all modified/new flags for a GC-collected task that was **never persisted**
/// (`new_task`).
pub fn discard_modifications_for_gc_new_task(&mut self) {
self.task.discard_modifications_for_gc_new_task();
}
pub fn invalidate_serialization(&mut self) { pub fn invalidate_serialization(&mut self) {
// TODO this causes race conditions, since we never know when a value is changed. We can't // TODO this causes race conditions, since we never know when a value is changed. We can't
// "snapshot" the value correctly. // "snapshot" the value correctly.
......
...@@ -235,7 +235,8 @@ struct TaskStorageSchema { ...@@ -235,7 +235,8 @@ struct TaskStorageSchema {
pub new_task: bool, pub new_task: bool,
/// GC soft-deletion marker. Set by the garbage collector when a task is marked for deletion. /// GC soft-deletion marker. Set by the garbage collector when a task is marked for deletion.
#[field(storage = "flag", category = "transient")] /// `meta` simply to ensure that setting/clearing it tracks a modification.
#[field(storage = "flag", category = "meta")]
deleted: bool, deleted: bool,
// ========================================================================= // =========================================================================
...@@ -477,6 +478,9 @@ pub enum UnevictableReason { ...@@ -477,6 +478,9 @@ pub enum UnevictableReason {
InProgress, InProgress,
/// Modified flags are set, or data/meta has not been restored yet. /// Modified flags are set, or data/meta has not been restored yet.
Modified, Modified,
/// GC deleted the task but its tombstone has not been persisted yet, e.g. because the GC pass
/// was interrupted and abandoned its snapshot.
MarkedForDeletion,
/// The task is transient /// The task is transient
Transient, Transient,
// Keep `NothingToEvict` last: `COUNT` is derived from its discriminant. // Keep `NothingToEvict` last: `COUNT` is derived from its discriminant.
...@@ -489,6 +493,7 @@ impl UnevictableReason { ...@@ -489,6 +493,7 @@ impl UnevictableReason {
pub const ALL: [UnevictableReason; Self::COUNT] = [ pub const ALL: [UnevictableReason; Self::COUNT] = [
UnevictableReason::InProgress, UnevictableReason::InProgress,
UnevictableReason::Modified, UnevictableReason::Modified,
UnevictableReason::MarkedForDeletion,
UnevictableReason::Transient, UnevictableReason::Transient,
UnevictableReason::NothingToEvict, UnevictableReason::NothingToEvict,
]; ];
...@@ -508,6 +513,7 @@ impl UnevictableReason { ...@@ -508,6 +513,7 @@ impl UnevictableReason {
match self { match self {
UnevictableReason::InProgress => "skipped_in_progress", UnevictableReason::InProgress => "skipped_in_progress",
UnevictableReason::Modified => "skipped_modified", UnevictableReason::Modified => "skipped_modified",
UnevictableReason::MarkedForDeletion => "skipped_marked_for_deletion",
UnevictableReason::Transient => "skipped_transient", UnevictableReason::Transient => "skipped_transient",
UnevictableReason::NothingToEvict => "skipped_nothing_to_evict", UnevictableReason::NothingToEvict => "skipped_nothing_to_evict",
} }
...@@ -1168,22 +1174,23 @@ mod tests { ...@@ -1168,22 +1174,23 @@ mod tests {
assert!(storage.flags.current_session_clean()); assert!(storage.flags.current_session_clean());
// Test persisted_bits only includes non-transient flags // Test persisted_bits only includes non-transient flags
// optimization_pending=bit 0 (meta, persisted) // optimization_pending=bit 0, deleted=bit 1 (meta, persisted)
// invalidator=bit 1, immutable=bit 2 (data, persisted) // invalidator=bit 2, immutable=bit 3 (data, persisted)
// current_session_clean=bit 3 (transient) // current_session_clean and the other transient flags come after
let persisted = storage.flags.persisted_bits(); let persisted = storage.flags.persisted_bits();
assert_eq!(persisted, 0b110); // invalidator + immutable assert_eq!(persisted, 0b1100); // invalidator + immutable
// Test TaskFlags constants // Test TaskFlags constants
assert_eq!(TaskFlags::PERSISTED_MASK, 0b111); // 3 persisted flags assert_eq!(TaskFlags::PERSISTED_MASK, 0b1111); // 4 persisted flags
// Test set_persisted_bits preserves transient flags // Test set_persisted_bits preserves transient flags
let mut storage2 = TaskStorage::new(); let mut storage2 = TaskStorage::new();
storage2.flags.set_current_session_clean(true); // Set transient flag storage2.flags.set_current_session_clean(true); // Set transient flag
storage2.flags.set_persisted_bits(0b100); // Set immutable only storage2.flags.set_persisted_bits(0b1000); // Set immutable only
assert!(storage2.flags.immutable()); assert!(storage2.flags.immutable());
assert!(!storage2.flags.invalidator()); assert!(!storage2.flags.invalidator());
assert!(!storage2.flags.optimization_pending()); assert!(!storage2.flags.optimization_pending());
assert!(!storage2.flags.deleted());
assert!(storage2.flags.current_session_clean()); // Transient flag preserved assert!(storage2.flags.current_session_clean()); // Transient flag preserved
} }
...@@ -1224,14 +1231,14 @@ mod tests { ...@@ -1224,14 +1231,14 @@ mod tests {
assert!(storage.flags.prefetched()); assert!(storage.flags.prefetched());
// Verify these are all transient (not in persisted_bits) // Verify these are all transient (not in persisted_bits)
// Only invalidator, immutable should be persisted // Only optimization_pending, deleted, invalidator and immutable should be persisted
let persisted = storage.flags.persisted_bits(); let persisted = storage.flags.persisted_bits();
assert_eq!(persisted, 0b00); // No persisted flags set assert_eq!(persisted, 0b00); // No persisted flags set
// Set a persisted flag and verify internal state flags are still transient // Set a persisted flag and verify internal state flags are still transient
storage.flags.set_immutable(true); storage.flags.set_immutable(true);
let persisted = storage.flags.persisted_bits(); let persisted = storage.flags.persisted_bits();
assert_eq!(persisted, 0b100); // Only immutable (bit 2) assert_eq!(persisted, 0b1000); // Only immutable (bit 3)
} }
// Helper to create encoder // Helper to create encoder
......
...@@ -16,6 +16,13 @@ pub fn create_selector(initial: bool) -> Vc<Selector> { ...@@ -16,6 +16,13 @@ pub fn create_selector(initial: bool) -> Vc<Selector> {
Selector(State::new(initial)).cell() Selector(State::new(initial)).cell()
} }
/// A second, independent [`Selector`] starting at `false`, for a test that disconnects two
/// subtrees at different times. `create_selector(false)` would return the same task.
#[turbo_tasks::function(operation, root)]
pub fn create_other_selector() -> Vc<Selector> {
Selector(State::new(false)).cell()
}
/// A never-changing State read by leaf tasks purely to make them mutable, so a reader records a /// A never-changing State read by leaf tasks purely to make them mutable, so a reader records a
/// real dependency edge (an immutable constant records none — see `add_cell_dependency`). /// real dependency edge (an immutable constant records none — see `add_cell_dependency`).
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
......
...@@ -23,7 +23,10 @@ use anyhow::Result; ...@@ -23,7 +23,10 @@ use anyhow::Result;
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc};
use crate::{ use crate::{
gc_fixture::{Constant, Selector, create_constant, create_selector, diamond_root}, gc_fixture::{
Constant, Selector, create_constant, create_other_selector, create_selector,
diamond_reader, diamond_root, diamond_target,
},
util::create_tt, util::create_tt,
}; };
...@@ -409,3 +412,149 @@ async fn gc_resurrect_immutable_recomputes() { ...@@ -409,3 +412,149 @@ async fn gc_resurrect_immutable_recomputes() {
tt.stop_and_wait().await; tt.stop_and_wait().await;
} }
/// Holds the producer `P` (`diamond_target`) as its only parent while the selector is `false`.
/// Returns `P`'s output so a caller can resolve it to `P`'s cell.
#[turbo_tasks::function(operation, root)]
async fn select_producer(
selector: ResolvedVc<Selector>,
constant: ResolvedVc<Constant>,
) -> Result<Vc<u32>> {
if *selector.await?.get() {
Ok(Vc::cell(u32::MAX))
} else {
Ok(diamond_target(*constant, 0))
}
}
/// Holds the dependent `D` (`diamond_reader`) as its only parent while the selector is `false`.
/// `D` reads `P`'s cell laterally, so it records a cell dependency on `P` without `P` being its
/// child, and the two can be collected independently.
#[turbo_tasks::function(operation, root)]
async fn select_dependent(
selector: ResolvedVc<Selector>,
target: ResolvedVc<u32>,
) -> Result<Vc<u32>> {
if *selector.await?.get() {
Ok(Vc::cell(u32::MAX))
} else {
Ok(Vc::cell(*diamond_reader(*target).await?))
}
}
/// A resurrected task must not keep the reverse dependency edges GC left behind.
///
/// Collecting `P` scrubs the forward edge from each dependent `D`, but deliberately leaves
/// `P.cell_dependents` alone since `P` is about to be deleted. If `P` is then resurrected, that
/// stale `(D, cell)` entry survives with no forward edge backing it, so collecting `D` later never
/// clears it. Once `D` is gone, invalidating `P` walks `cell_dependents`, opens `D` with
/// `MustExist`, and panics with `task is missing in memory or persistent storage`.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn gc_resurrected_producer_has_no_stale_cell_dependents() {
let (tt, _persistence_dir) = create_tt("gc_resurrected_producer_has_no_stale_cell_dependents");
// Build `P` and `D`, then disconnect `P` alone. `D` keeps its cell dependency on `P`.
let target = turbo_tasks::run_once(tt.clone(), async move {
let producer_selector_op = create_selector(false);
let producer_selector_vc = producer_selector_op.resolve().strongly_consistent().await?;
let dependent_selector_op = create_other_selector();
let dependent_selector_vc = dependent_selector_op
.resolve()
.strongly_consistent()
.await?;
let constant_op = create_constant();
let constant_vc = constant_op.resolve().strongly_consistent().await?;
let target = select_producer(producer_selector_vc, constant_vc)
.resolve()
.strongly_consistent()
.await?;
assert_eq!(
*select_dependent(dependent_selector_vc, target)
.read_strongly_consistent()
.await?,
1
);
let producer_selector = producer_selector_op.read_strongly_consistent().await?;
producer_selector.set(true);
select_producer(producer_selector_vc, constant_vc)
.read_strongly_consistent()
.await?;
anyhow::Ok(target)
})
.await
.unwrap();
assert_eq!(
tt.backend().gc_for_testing(&tt),
1,
"only the producer should be collected; the dependent is still connected"
);
// Disconnect `D`, then reconnect `P` before any snapshot. `P` is resurrected and re-executes;
// its update finds the stale `D` entry and dirties `D`, but `D` is no longer active, so it
// never re-executes and re-registers a forward edge that would clear the entry. A dependent
// that is still live does exactly that, which hides the bug.
turbo_tasks::run_once(tt.clone(), async move {
let dependent_selector_op = create_other_selector();
let dependent_selector_vc = dependent_selector_op
.resolve()
.strongly_consistent()
.await?;
dependent_selector_op
.read_strongly_consistent()
.await?
.set(true);
select_dependent(dependent_selector_vc, target)
.read_strongly_consistent()
.await?;
let producer_selector_op = create_selector(false);
let producer_selector_vc = producer_selector_op.resolve().strongly_consistent().await?;
let constant_vc = create_constant().resolve().strongly_consistent().await?;
producer_selector_op
.read_strongly_consistent()
.await?
.set(false);
assert_eq!(
*select_producer(producer_selector_vc, constant_vc)
.read_strongly_consistent()
.await?,
0
);
anyhow::Ok(())
})
.await
.unwrap();
// Collect `D`, then snapshot and evict so it is gone from memory and disk.
assert_eq!(
tt.backend().gc_for_testing(&tt),
1,
"only the dependent should be collected; the producer is connected again"
);
tt.backend().snapshot_and_evict_for_testing(&tt);
// Invalidate `P`. Its cell changes, so `update_cell` walks `cell_dependents`.
turbo_tasks::run_once(tt.clone(), async move {
let producer_selector_vc = create_selector(false)
.resolve()
.strongly_consistent()
.await?;
let constant_op = create_constant();
let constant_vc = constant_op.resolve().strongly_consistent().await?;
constant_op.read_strongly_consistent().await?.set(1);
assert_eq!(
*select_producer(producer_selector_vc, constant_vc)
.read_strongly_consistent()
.await?,
7
);
anyhow::Ok(())
})
.await
.unwrap();
tt.stop_and_wait().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