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

turbo-tasks-gc: Invalidating a deleted task should be a no-op (#98614)

Make invalidating deleted tasks a no-op.

Invalidators now represent a 'weak' reference to a task, and
invalidations don't assert task existence. We know that the task exists
when the invalidator is created but by the time an invalidation occurs
it might not exist anymore. So treat the dependency as weak.

Take care to not create 'blank' tasks when querying for them, this can
confuse assertions that occur later.


## Why is this safe?

Currently it is safe because we never reuse task ids, so invalidating a
deleted task is perfectly reasonable, much like invalidating a
non-active task, there is nothing to do

In the future if we start reusing task ids, then this becomes an ABA
problem. An invalidator can point at a new task. This is also not too
bad, a spurious invalidation is self healing. Also most invalidators are
not actually persisted which limits the risk.

So we can either decide to build a mechanism to tear down these stale
edges or tolerate spurious invalidations. I think we could make
invalidator users subscribe to a 'deleted task id bus' which would allow
them to drop references, this could be useful and then the few cases
where we _persist_ invalidators (by way of `State` objects) would need
to devise a new mechanism (or decide to tolerate the spurious
invalidations)

---------
Co-authored-by: 's avatarClaude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: 's avatarvercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
parent b9be0685
......@@ -60,7 +60,7 @@ impl Operation for InvalidateOperation {
} => {
let mut queue = AggregationUpdateQueue::new();
for task_id in task_ids {
make_task_dirty(
try_make_task_dirty(
task_id,
#[cfg(feature = "task_dirty_cause")]
cause.clone(),
......@@ -88,6 +88,7 @@ impl Operation for InvalidateOperation {
}
}
/// Marks a task dirty. The task must exist.
pub fn make_task_dirty(
task_id: TaskId,
#[cfg(feature = "task_dirty_cause")] cause: TaskDirtyCause,
......@@ -105,6 +106,28 @@ pub fn make_task_dirty(
);
}
/// Marks a task dirty, doing nothing if it no longer exists.
///
/// Intended for invalidation usecases.
fn try_make_task_dirty(
task_id: TaskId,
#[cfg(feature = "task_dirty_cause")] cause: TaskDirtyCause,
queue: &mut AggregationUpdateQueue,
ctx: &mut impl ExecuteContext<'_>,
) {
let Some(mut task) = ctx.try_get_task(task_id, TaskDataCategory::All) else {
return;
};
make_task_dirty_internal(
&mut task,
true,
#[cfg(feature = "task_dirty_cause")]
cause,
queue,
ctx,
);
}
/// Requires the guard to be allocated with [TaskDataCategory::All]
pub fn make_task_dirty_internal<'e, E: ExecuteContext<'e>>(
task: &mut E::TaskGuardImpl,
......
......@@ -522,6 +522,20 @@ impl Storage {
}
}
/// Like [`Self::access_mut`], but keeps the map entry so the caller can still remove it.
pub fn access_entry_mut(&self, key: TaskId) -> TaskEntryGuard<'_> {
let entry = match self.map.entry(key) {
dashmap::mapref::entry::Entry::Occupied(e) => e,
dashmap::mapref::entry::Entry::Vacant(e) => {
e.insert_entry(Box::new(TaskStorage::new()))
}
};
TaskEntryGuard {
storage: self,
entry,
}
}
/// Read-only access to an already resident task. Returns `None` if the task isnt in memory
/// resident. The closure runs while a shard read lock is held, so it must be cheap and must
/// not re-enter the map.
......@@ -831,6 +845,44 @@ impl Storage {
}
}
/// A write guard that still owns its map entry, so the task can be removed under the lock that is
/// already held.
///
/// Use [`Storage::access_entry_mut`] to obtain one. Convert it with [`Self::into_write_guard`] once
/// removal is no longer a possibility, or call [`Self::discard`] to drop the entry outright.
pub struct TaskEntryGuard<'a> {
storage: &'a Storage,
entry: dashmap::mapref::entry::OccupiedEntry<'a, TaskId, Box<TaskStorage>>,
}
impl<'a> TaskEntryGuard<'a> {
/// Removes this task's entry.
pub fn discard(self) {
self.entry.remove();
}
/// Gives up the ability to remove the entry, yielding an ordinary write guard.
pub fn into_write_guard(self) -> StorageWriteGuard<'a> {
StorageWriteGuard {
storage: self.storage,
inner: self.entry.into_ref().into(),
}
}
}
impl Deref for TaskEntryGuard<'_> {
type Target = TaskStorage;
fn deref(&self) -> &Self::Target {
self.entry.get()
}
}
impl DerefMut for TaskEntryGuard<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.entry.get_mut()
}
}
pub struct StorageWriteGuard<'a> {
storage: &'a Storage,
inner: RefMut<'a, TaskId, Box<TaskStorage>>,
......
......@@ -11,7 +11,7 @@ use anyhow::Result;
use turbo_tasks::{ResolvedVc, TaskId, Vc, prevent_gc};
use crate::{
gc_fixture::{Selector, create_selector},
gc_fixture::{Constant, Selector, create_constant, create_selector},
util::create_tt,
};
......@@ -47,6 +47,29 @@ async fn branch_b() -> Result<Vc<u32>> {
Ok(Vc::cell(2 + *leaf(20).await?))
}
/// Reads `observed` so the task registers an invalidator on it, then goes out of the live graph
/// when the selector flips. Whether that invalidator outlives the task is the point of
/// `mutating_a_state_read_by_a_collected_task_does_not_panic`.
#[turbo_tasks::function]
async fn state_reader(observed: ResolvedVc<Constant>) -> Result<Vc<u32>> {
Ok(Vc::cell(*observed.await?.get()))
}
/// Reads `state_reader` only while the selector is false, so flipping it disconnects the reader.
#[turbo_tasks::function(operation, root)]
async fn select_state_reader(
selector: ResolvedVc<Selector>,
observed: ResolvedVc<Constant>,
) -> Result<Vc<u32>> {
let use_b = *selector.await?.get();
let value = if use_b {
*branch_b().await?
} else {
*state_reader(*observed).await?
};
Ok(Vc::cell(value))
}
/// A task that pins itself against GC while executing. Once pinned it must survive collection even
/// after it is disconnected.
#[turbo_tasks::function]
......@@ -114,7 +137,6 @@ async fn gc_collects_disconnected_subtree() {
);
// Flipping back must recompute branch_a fresh, since it was collected.
let tt3 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
let selector_op = create_selector(true);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
......@@ -123,7 +145,6 @@ async fn gc_collects_disconnected_subtree() {
assert_eq!(*output.read_strongly_consistent().await?, 22);
selector.set(false);
assert_eq!(*output.read_strongly_consistent().await?, 11);
let _ = &tt3;
anyhow::Ok(())
})
.await;
......@@ -266,3 +287,58 @@ async fn unpin_after_stop_does_not_panic() {
tt.unpin_task_for_gc(leaf_id);
}
/// A `State` keeps an `Invalidator` for every task that read it, and those entries are plain task
/// ids with nothing keeping the task alive. Collecting a reader therefore leaves a dangling
/// invalidator behind, this test ensures that that doesn't cause a panic.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn mutating_a_state_read_by_a_collected_task_does_not_panic() {
let (tt, _persistence_dir) =
create_tt("mutating_a_state_read_by_a_collected_task_does_not_panic");
let tt2 = tt.clone();
let result = 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?;
let observed_vc = create_constant().resolve().strongly_consistent().await?;
// `state_reader` reads `observed`, registering an invalidator on that State.
let output = select_state_reader(selector_vc, observed_vc);
output.read_strongly_consistent().await?;
// Flip so `state_reader` leaves the live graph; its invalidator stays on `observed`.
selector.set(true);
output.read_strongly_consistent().await?;
anyhow::Ok(())
})
.await;
result.unwrap();
let collected = tt2.backend().gc_for_testing(&tt2);
assert!(
collected > 0,
"the disconnected state reader should have been collected"
);
// Required, and not just for realism: the evict is what selects the code path under test.
// GC only soft-deletes, leaving the task resident, and a weak open of a resident-but-deleted
// task returns early on the `deleted` flag. Evicting drops it from memory (and tombstones it on
// disk), so the open below instead reaches the exists-nowhere case in
// `ExecuteContextImpl::open_task` -- nothing restored, nothing found on disk -- which is the
// one that would panic under `MustExist`. Drop this line and the test still passes, but it
// stops covering that path.
tt2.backend().snapshot_and_evict_for_testing(&tt2);
// Mutating the State now walks its invalidator list, which still names the collected reader.
let result = turbo_tasks::run_once(tt.clone(), async move {
let observed = create_constant().read_strongly_consistent().await?;
observed.set(1);
anyhow::Ok(())
})
.await;
result.unwrap();
tt.stop_and_wait().await;
}
......@@ -230,7 +230,6 @@ async fn gc_resurrect_on_reconnect() {
// Reconnect the subtree (selector back to false) BEFORE any snapshot. Reading `reader` again
// connects it, which must resurrect it (and its leaves, as it re-reads them).
let tt3 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
......@@ -244,7 +243,6 @@ async fn gc_resurrect_on_reconnect() {
expected,
"resurrected reader must recompute the correct value"
);
let _ = &tt3;
anyhow::Ok(())
})
.await;
......@@ -252,7 +250,6 @@ async fn gc_resurrect_on_reconnect() {
// A snapshot+evict now must NOT have tombstoned/hard-deleted the resurrected subtree.
tt2.backend().snapshot_and_evict_for_testing(&tt2);
let tt4 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
......@@ -260,7 +257,6 @@ async fn gc_resurrect_on_reconnect() {
let constant_vc = constant_op.resolve().strongly_consistent().await?;
let output = select_reader(selector_vc, constant_vc);
assert_eq!(*output.read_strongly_consistent().await?, expected);
let _ = &tt4;
anyhow::Ok(())
})
.await;
......@@ -319,7 +315,6 @@ async fn select_imm_reader(selector: ResolvedVc<Selector>) -> Result<Vc<u32>> {
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn gc_resurrect_immutable_recomputes() {
let (tt, _persistence_dir) = create_tt("gc_resurrect_immutable_recomputes");
let tt2 = tt.clone();
let expected: u32 = (0..IMM_FANOUT).fold(0u32, |a, b| a.wrapping_add(b * 3));
let result = turbo_tasks::run_once(tt.clone(), async move {
......@@ -349,7 +344,7 @@ async fn gc_resurrect_immutable_recomputes() {
// Collect the disconnected subtree. The entries stay resident (no snapshot yet), so the tasks
// are soft-deleted rather than gone.
let collected = tt2.backend().gc_for_testing(&tt2);
let collected = tt.backend().gc_for_testing(&tt);
assert_eq!(
collected,
IMM_FANOUT as usize + 1,
......@@ -360,10 +355,8 @@ async fn gc_resurrect_immutable_recomputes() {
// This must recompute it rather than serve a stale value. Done before the reconnect below,
// while the subtree is still collected — afterwards the leaves are live again and a read would
// legitimately hit a fresh cell, proving nothing.
let tt_direct = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
assert_eq!(*read_imm_leaf(0).read_strongly_consistent().await?, 0);
let _ = &tt_direct;
anyhow::Ok(())
})
.await;
......@@ -377,7 +370,6 @@ async fn gc_resurrect_immutable_recomputes() {
// Reconnect BEFORE any snapshot. These tasks were never persisted, so there is nothing on disk
// to restore — the only way back to a correct value is re-execution.
let tt3 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
......@@ -389,7 +381,7 @@ async fn gc_resurrect_immutable_recomputes() {
expected,
"a resurrected immutable task must recompute the correct value"
);
let _ = &tt3;
anyhow::Ok(())
})
.await;
......@@ -404,14 +396,12 @@ async fn gc_resurrect_immutable_recomputes() {
// A snapshot + evict must not have tombstoned the resurrected subtree, and the restored data
// must survive the round trip.
tt2.backend().snapshot_and_evict_for_testing(&tt2);
let tt4 = tt.clone();
tt.backend().snapshot_and_evict_for_testing(&tt);
let result = turbo_tasks::run_once(tt.clone(), async move {
let selector_op = create_selector(false);
let selector_vc = selector_op.resolve().strongly_consistent().await?;
let output = select_imm_reader(selector_vc);
assert_eq!(*output.read_strongly_consistent().await?, expected);
let _ = &tt4;
anyhow::Ok(())
})
.await;
......
......@@ -35,7 +35,6 @@ async fn gc_re_rooting_stays_flat() {
tt: &Arc<TurboTasks<TurboTasksBackend>>,
gen_value: u32,
) -> (usize, usize, bool) {
let tt_inner = tt.clone();
turbo_tasks::run_once(tt.clone(), async move {
// `create_generation` is a cached root operation, so this returns the same task each
// round.
......@@ -47,7 +46,6 @@ async fn gc_re_rooting_stays_flat() {
}
let output = wide_root(generation_vc, WIDTH);
output.read_strongly_consistent().await?;
let _ = &tt_inner;
anyhow::Ok(())
})
.await
......@@ -119,14 +117,13 @@ async fn gc_re_rooting_stays_flat() {
);
// The live graph must still compute correctly after all the churn.
let tt3 = tt.clone();
let result = turbo_tasks::run_once(tt.clone(), async move {
let generation_op = create_generation();
let generation_vc = generation_op.resolve().strongly_consistent().await?;
let output = wide_root(generation_vc, WIDTH);
let expected: u32 = expected_value(ROUNDS, WIDTH);
assert_eq!(*output.read_strongly_consistent().await?, expected);
let _ = &tt3;
anyhow::Ok(())
})
.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