Unverified Commit 97e7d901 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-tasks: explicit GC root anchoring + cross-session orphan reclamation (#96857)

### What?

Reworks how the reference-counting GC decides what is a **root** (a task
that must not be collected), and adds a durable path to reclaim
cross-session orphaned roots

Currently GC roots are persisted and the only way to get collected is
for another session to revive them, and then for their ref-counts to
drop. This provides a new mechanism.

1. **In-session** — Top level operations get reference counted so they
can be dropped in session.
2. **Cross-session** — live roots are recorded along with a simple aging
mechanism so they can still be collected even if a later session never
restores it.

### Why?
This closes the final GC gap. Top level operations are ambiguous
currently and need tracking. With cross session aging we close a basic
gap where if you delete a route between sessions we never clean up the
tasks. Now it will eventually age out.

### How?

**Roots are anchored explicitly**. Via pins and RAII guards like
`GcRoot`

**Persisted roots set + TTL age-out** A new Infra-keyspace `GcRoots`
entry persists `Vec<(TaskId, ttl)>`. That allows us to detect when roots
are stale and can be dropped.

**Dispose transient root tasks on drop.** `RootTask::Drop` now disposes
tasks and cleans up references allowing the GC to work
parent b0b7a429
......@@ -4,8 +4,9 @@ use next_core::emit_assets;
use rustc_hash::{FxHashMap, FxHashSet};
use turbo_rcstr::RcStr;
use turbo_tasks::{
FxIndexSet, NonLocalValue, OperationValue, OperationVc, ResolvedVc, State, TryFlatJoinIterExt,
TryJoinIterExt, Vc, debug::ValueDebugFormat, trace::TraceRawVcs, turbobail,
FxIndexSet, GcRoot, NonLocalValue, OperationValue, OperationVc, ResolvedVc, State,
TryFlatJoinIterExt, TryJoinIterExt, Vc, debug::ValueDebugFormat, trace::TraceRawVcs,
turbo_tasks, turbobail,
};
use turbo_tasks_fs::{FileContent, FileSystemPath};
use turbopack_core::{
......@@ -44,15 +45,24 @@ pub struct PathToOutputOperation(
FxHashMap<FileSystemPath, ExpandedOutputAssetsOperationSet>,
);
/// The operations that produce the assets at one path.
#[derive(Clone, Default, TraceRawVcs, PartialEq, Eq, ValueDebugFormat, Debug, NonLocalValue)]
struct ExpandedOutputAssetsOperationSet(FxIndexSet<OperationVc<ExpandedOutputAssets>>);
struct ExpandedOutputAssetsOperationSet(FxIndexSet<GcRoot<ExpandedOutputAssets>>);
// HACK: This is technically incorrect because the map's key contains a `ResolvedVc`...
unsafe impl OperationValue for PathToOutputOperation {}
/// The pinned assets operation and the compute entry derived from it.
#[derive(
Clone, TraceRawVcs, PartialEq, Eq, ValueDebugFormat, Debug, NonLocalValue, OperationValue,
)]
struct ComputeEntry {
assets_operation: GcRoot<ExpandedOutputAssets>,
compute_entry: GcRoot<OptionMapEntry>,
}
// A precomputed map for quick access to output asset by filepath
type OutputOperationToComputeEntry =
FxHashMap<OperationVc<ExpandedOutputAssets>, OperationVc<OptionMapEntry>>;
type OutputOperationToComputeEntry = FxHashMap<OperationVc<ExpandedOutputAssets>, ComputeEntry>;
/// Tracks all the output assets produced in a session. This allows us to compute fine grained
/// change information which drives HMR sessions.
......@@ -174,7 +184,22 @@ impl VersionedContentMap {
client_output_path,
);
this.map_op_to_compute_entry.update_conditionally(|map| {
map.insert(assets_operation, compute_entry) != Some(compute_entry)
if let Some(existing) = map.get(&assets_operation)
&& *existing.compute_entry == compute_entry
{
// No-op update.
return false;
}
let tt = turbo_tasks();
map.insert(
assets_operation,
ComputeEntry {
assets_operation: GcRoot::pin(tt.clone(), assets_operation),
compute_entry: GcRoot::pin(tt, compute_entry),
},
);
true
});
Ok(())
}
......@@ -198,30 +223,31 @@ impl VersionedContentMap {
self.map_path_to_op.update_conditionally(|map| {
let mut changed = false;
let tt = turbo_tasks();
// get current map's keys, subtract keys that don't exist in operation
let mut stale_assets = map.0.keys().cloned().collect::<FxHashSet<_>>();
for (k, _) in entries.iter().flatten() {
let res = map
.0
.entry(k.clone())
.or_default()
.0
.insert(assets_operation);
let set = &mut map.0.entry(k.clone()).or_default().0;
let inserted = !set.contains(&assets_operation);
if inserted {
set.insert(GcRoot::pin(tt.clone(), assets_operation));
}
stale_assets.remove(k);
changed = changed || res;
changed = changed || inserted;
}
// Make more efficient with reverse map
// Make more efficient with reverse map. Removing the entry drops its pin.
for k in &stale_assets {
let res = map
let removed = map
.0
.get_mut(k)
// guaranteed
.unwrap()
.0
.swap_remove(&assets_operation);
changed = changed || res
changed = changed || removed
}
changed
});
......@@ -305,7 +331,7 @@ impl VersionedContentMap {
fn raw_get(&self, path: FileSystemPath) -> Vc<OptionMapEntry> {
let assets = {
let map = &self.map_path_to_op.get().0;
map.get(&path).and_then(|m| m.0.iter().next().copied())
map.get(&path).and_then(|m| m.0.first().map(|pin| **pin))
};
let Some(assets) = assets else {
return Vc::cell(None);
......@@ -315,7 +341,7 @@ impl VersionedContentMap {
let compute_entry = {
let map = self.map_op_to_compute_entry.get();
map.get(&assets).copied()
map.get(&assets).map(|entry| *entry.compute_entry)
};
let Some(compute_entry) = compute_entry else {
return Vc::cell(None);
......
......@@ -23,8 +23,8 @@ use turbo_tasks::{
use turbopack_core::issue::{IssueFilter, PlainIssue};
use crate::next_api::utils::{
DetachedVc, NapiIssue, RootTask, TurbopackResult, strongly_consistent_catch_collectables,
subscribe,
DetachedVc, NapiIssue, SubscriptionTask, TurbopackResult,
strongly_consistent_catch_collectables, subscribe,
};
#[napi(object)]
......@@ -195,7 +195,7 @@ pub fn endpoint_server_changed_subscribe(
TurbopackResult<()>,
(),
>,
) -> napi::Result<External<RootTask>> {
) -> napi::Result<External<SubscriptionTask>> {
let turbopack_ctx = endpoint.turbopack_ctx().clone();
let endpoint = ****endpoint;
subscribe(
......@@ -282,7 +282,7 @@ pub fn endpoint_client_changed_subscribe(
TurbopackResult<()>,
(),
>,
) -> napi::Result<External<RootTask>> {
) -> napi::Result<External<SubscriptionTask>> {
let turbopack_ctx = endpoint.turbopack_ctx().clone();
let endpoint_op = ****endpoint;
subscribe(
......
......@@ -52,9 +52,9 @@ use tracing::Instrument;
use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
Effects, FxIndexSet, OperationValue, OperationVc, PrettyPrintError, ReadRef, ResolvedVc,
TransientInstance, TryJoinIterExt, TurboTasksApi, TurboTasksCallApi, UpdateInfo, Vc,
mark_top_level_task,
Effects, FxIndexSet, GcRoot, OperationValue, OperationVc, PrettyPrintError, ReadRef,
ResolvedVc, TransientInstance, TryJoinIterExt, TurboTasksApi, TurboTasksCallApi, UpdateInfo,
Vc, mark_top_level_task,
message_queue::{CompilationEvent, Severity},
read_strongly_consistent_and_apply_effects, take_effects,
trace::TraceRawVcs,
......@@ -93,7 +93,7 @@ use crate::{
NextTurboTasks, NextTurbopackContext, create_turbo_tasks,
},
utils::{
DetachedVc, NapiIssue, NapiUsedFeature, RootTask, TurbopackResult, get_issues,
DetachedVc, NapiIssue, NapiUsedFeature, SubscriptionTask, TurbopackResult, get_issues,
strongly_consistent_catch_collectables, subscribe,
},
},
......@@ -419,6 +419,8 @@ pub struct ProjectInstance {
container: ResolvedVc<ProjectContainer>,
// Never locked across an await point.
exit_receiver: Mutex<Option<ExitReceiver>>,
// Pin the ProjectContainer for as long as this struct survives.
_container_gc_root: GcRoot<ProjectContainer>,
}
#[napi(ts_return_type = "Promise<{ __napiType: \"Project\" }>")]
......@@ -613,14 +615,17 @@ pub fn project_new<'env>(
let options = ProjectOptions::from(options);
let is_dev = options.dev;
let root_path = options.root_path.clone();
let container = turbo_tasks
let (container, container_op) = turbo_tasks
.run(async move {
let container_op = ProjectContainer::new_operation(rcstr!("next.js"), is_dev);
ProjectContainer::initialize(container_op, options).await?;
container_op.resolve().strongly_consistent().await
let container = container_op.resolve().strongly_consistent().await?;
// Return the operation itself so we can pin it below
Ok((container, container_op))
})
.or_else(|e| turbopack_ctx.throw_turbopack_internal_result(&e.into()))
.await?;
let container_gc_root = GcRoot::pin(turbo_tasks.clone(), container_op);
if is_dev {
Handle::current().spawn({
......@@ -661,6 +666,7 @@ pub fn project_new<'env>(
turbopack_ctx,
container,
exit_receiver: Mutex::new(Some(exit_receiver)),
_container_gc_root: container_gc_root,
}))
}
.instrument(tracing::info_span!("create project")),
......@@ -1766,7 +1772,7 @@ pub fn project_entrypoints_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: &External<ProjectInstance>,
#[napi(ts_arg_type = "(err: Error, value: TurbopackResult<Partial<NapiEntrypoints>>) => void")]
func: FunctionRef<TurbopackResult<Option<NapiEntrypoints>>, ()>,
) -> napi::Result<External<RootTask>> {
) -> napi::Result<External<SubscriptionTask>> {
let turbopack_ctx = project.turbopack_ctx.clone();
let container = project.container;
subscribe(
......@@ -2010,7 +2016,7 @@ pub fn project_client_hmr_events(
TurbopackResult<Unknown<'static>>,
(),
>,
) -> napi::Result<External<RootTask>> {
) -> napi::Result<External<SubscriptionTask>> {
let container = project.container;
let session = TransientInstance::new(());
subscribe(
......@@ -2144,7 +2150,7 @@ pub fn project_client_hmr_chunk_names_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: &External<ProjectInstance>,
#[napi(ts_arg_type = "(err: Error, value: TurbopackResult<HmrChunkNames>) => void")]
func: FunctionRef<TurbopackResult<HmrChunkNames>, ()>,
) -> napi::Result<External<RootTask>> {
) -> napi::Result<External<SubscriptionTask>> {
let container = project.container;
subscribe(
project.turbopack_ctx.clone(),
......
......@@ -21,7 +21,7 @@ use regex::Regex;
use rustc_hash::FxHashMap;
use serde::Serialize;
use turbo_rcstr::RcStr;
use turbo_tasks::{Effects, OperationVc, ReadRef, TaskId, Vc, VcValueType, take_effects};
use turbo_tasks::{Effects, GcRoot, OperationVc, ReadRef, TaskId, Vc, VcValueType, take_effects};
use turbo_tasks_fs::FileContent;
use turbopack_core::{
issue::{
......@@ -43,17 +43,23 @@ use crate::next_api::turbopack_ctx::NextTurbopackContext;
/// [`turbo_tasks::OperationValue`] and should be dereferenced to an [`OperationVc`] before being
/// passed to a [`turbo_tasks::function`].
//
// TODO: If we add a tracing garbage collector to turbo-tasks, this should be tracked as a GC root.
#[derive(Clone)]
/// A `DetachedVc` holds its operation's task alive against garbage collection for as long as the
/// handle exists.
pub struct DetachedVc<T> {
turbopack_ctx: NextTurbopackContext,
/// The Vc. Must be unresolved, otherwise you are referencing an inactive operation.
vc: OperationVc<T>,
/// Pins the operation to prevent GC, and holds the `Vc` itself. Must be unresolved, otherwise
/// you are referencing an inactive operation.
gc_root: GcRoot<T>,
}
impl<T> DetachedVc<T> {
pub fn new(turbopack_ctx: NextTurbopackContext, vc: OperationVc<T>) -> Self {
Self { turbopack_ctx, vc }
// Pin the operation's task so GC treats this out-of-graph handle as a root.
let gc_root = GcRoot::pin(turbopack_ctx.turbo_tasks().clone(), vc);
Self {
turbopack_ctx,
gc_root,
}
}
pub fn turbopack_ctx(&self) -> &NextTurbopackContext {
......@@ -65,7 +71,7 @@ impl<T> Deref for DetachedVc<T> {
type Target = OperationVc<T>;
fn deref(&self) -> &Self::Target {
&self.vc
&self.gc_root
}
}
......@@ -73,33 +79,36 @@ impl<T> Deref for DetachedVc<T> {
/// [`turbo_tasks::TurboTasks::spawn_root_task`] that can be passed back and forth to JS across the
/// [`napi`][mod@napi] boundary via [`External`].
///
/// JavaScript code receiving this value **must** call [`root_task_dispose`] in a `try...finally`
/// block to avoid leaking root tasks.
/// JavaScript code should call [`root_task_dispose`] in a `try...finally` block to dispose the root
/// task promptly. If it doesn't, [`Drop`] disposes it as a backstop.
///
/// This is used by [`subscribe`] to create a computation that re-executes when dependencies change.
//
// TODO: If we add a tracing garbage collector to turbo-tasks, this should be tracked as a GC root.
pub struct RootTask {
pub struct SubscriptionTask {
turbopack_ctx: NextTurbopackContext,
task_id: Option<TaskId>,
}
impl Drop for RootTask {
impl SubscriptionTask {
fn dispose(&mut self) {
if let Some(task) = self.task_id.take() {
self.turbopack_ctx.turbo_tasks().dispose_root_task(task);
}
}
}
impl Drop for SubscriptionTask {
fn drop(&mut self) {
// TODO stop the root task
self.dispose();
}
}
#[napi]
pub fn root_task_dispose(
#[napi(ts_arg_type = "{ __napiType: \"RootTask\" }")] mut root_task: ExternalRef<RootTask>,
#[napi(ts_arg_type = "{ __napiType: \"RootTask\" }")] mut root_task: ExternalRef<
SubscriptionTask,
>,
) -> napi::Result<()> {
if let Some(task) = root_task.task_id.take() {
root_task
.turbopack_ctx
.turbo_tasks()
.dispose_root_task(task);
}
root_task.dispose();
Ok(())
}
......@@ -435,7 +444,7 @@ pub fn subscribe<
func: &FunctionRef<V, ()>,
handler: impl 'static + Sync + Send + Clone + Fn() -> F,
mapper: impl 'static + Sync + Send + FnMut(ThreadsafeCallContext<T>) -> napi::Result<V>,
) -> napi::Result<External<RootTask>> {
) -> napi::Result<External<SubscriptionTask>> {
let js_func = func.borrow_back(env)?;
let func: ThreadsafeFunction<T, (), V, Status, true> = js_func
.build_threadsafe_function::<T>()
......@@ -463,7 +472,7 @@ pub fn subscribe<
}
}
});
Ok(External::new(RootTask {
Ok(External::new(SubscriptionTask {
turbopack_ctx: ctx,
task_id: Some(task_id),
}))
......
......@@ -14,6 +14,7 @@ use std::{
use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode};
use parking_lot::RwLockReadGuard;
use tracing::info_span;
#[cfg(feature = "trace_prepare_tasks")]
use tracing::trace_span;
......@@ -231,6 +232,9 @@ pub struct ExecuteContextImpl<'e> {
backend: &'e TurboTasksBackend,
turbo_tasks: &'e TurboTasks<TurboTasksBackend>,
phase: ExecutePhase<'e>,
/// Held by contexts built through `TurboTasksBackend::try_execute_context`, so that storage
/// teardown in `stop()` waits for this context to be dropped.
_shutdown_guard: Option<RwLockReadGuard<'e, bool>>,
task_lock_counter: TaskLockCounter,
}
......@@ -245,6 +249,26 @@ impl<'e> ExecuteContextImpl<'e> {
phase: ExecutePhase::Normal {
_guard: backend.start_operation(),
},
_shutdown_guard: None,
task_lock_counter: TaskLockCounter::new(),
}
}
/// Like [`ExecuteContextImpl::new`], but owns the shutdown read guard that
/// [`TurboTasksBackend::try_execute_context`] acquired, keeping `stop()` out until this
/// context is dropped.
pub(super) fn new_with_shutdown_guard(
backend: &'e TurboTasksBackend,
turbo_tasks: &'e TurboTasks<TurboTasksBackend>,
shutdown_guard: RwLockReadGuard<'e, bool>,
) -> Self {
Self {
backend,
turbo_tasks,
phase: ExecutePhase::Normal {
_guard: backend.start_operation(),
},
_shutdown_guard: Some(shutdown_guard),
task_lock_counter: TaskLockCounter::new(),
}
}
......@@ -265,6 +289,7 @@ impl<'e> ExecuteContextImpl<'e> {
backend,
turbo_tasks,
phase: ExecutePhase::Gc(gc_collectible),
_shutdown_guard: None,
task_lock_counter: TaskLockCounter::new(),
}
}
......@@ -1243,6 +1268,9 @@ impl<'e> ChildExecuteContext<'e> for ChildExecuteContextImpl<'e> {
backend: self.backend,
turbo_tasks: self.turbo_tasks,
phase: ExecutePhase::Child,
// A child context runs inside its parent's execution, which the foreground drain
// already waits for, so it needs no shutdown guard of its own.
_shutdown_guard: None,
task_lock_counter: TaskLockCounter::new(),
}
}
......
......@@ -551,17 +551,49 @@ impl Storage {
self.map.shards().len()
}
/// 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)) {
/// 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),
) {
let shard = self.map.shards()[index].read();
for (task_id, task) in shard.iter() {
if !task_id.is_transient() && task.gc_maybe_collectible() {
on_candidate(*task_id);
if task_id.is_transient() {
continue;
}
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> {
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 m
storage.gc_debug_assert_root_held_by_transient_pin();
roots.push(task_id);
}
});
roots
});
per_shard.into_iter().flatten()
}
pub fn access_pair_mut(
&self,
key1: TaskId,
......
......@@ -867,10 +867,6 @@ impl TaskStorage {
/// 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.
///
/// Refusing to collect a task with dependents is what makes task-id reuse safe: a hard-deleted
/// id can be handed out again, so a surviving dependent edge would silently resolve to an
/// unrelated live task instead of tripping `MustExist`.
pub fn gc_maybe_collectible(&self) -> bool {
// None of the predicates below are correct without this.
self.flags.is_restored(TaskDataCategory::Meta)
......@@ -898,6 +894,31 @@ impl TaskStorage {
.cell_dependents_hashed()
.is_none_or(|d| d.is_empty())))
}
/// Whether this task is a GC **root**: parent-less, but pinned for some reason
///
/// NOTE: this is a conservative classification. The typical reason is that there is a
/// [`TaskStorage::transient_ref`] live, but this will return true if there is merely an
/// `upper/follower`.
pub fn gc_is_root(&self) -> bool {
self.flags.is_restored(TaskDataCategory::Meta)
&& !self.flags.deleted()
&& self.gc_parent_count() == 0
&& !self.gc_maybe_collectible()
}
/// Assert that a task classified by [`TaskStorage::gc_is_root`] is held by a pin that eviction
/// cannot drop.
pub fn gc_debug_assert_root_held_by_transient_pin(&self) {
debug_assert!(self.gc_is_root()); // sanity for our caller
debug_assert!(
self.gc_transient_ref_count() > 0
|| self.get_in_progress().is_some()
|| self.get_activeness().is_some(),
"GC root is held by a non-transient pin: {self:?}.\nBeing held by another kind of \
reference implies a bug in GC or the aggregation graph."
);
}
}
/// Counts for aggregation tree and collectibles fields.
......
......@@ -18,7 +18,7 @@ use turbo_tasks::{
use crate::{
GitVersionInfo,
backend::{AnyOperation, SpecificTaskDataCategory, storage_schema::TaskStorage},
backend::{AnyOperation, SpecificTaskDataCategory, TtlCounter, storage_schema::TaskStorage},
backing_storage::{SnapshotItem, SnapshotMeta, compute_task_type_hash_from_components},
database::{
db_invalidation::{StartupCacheState, check_db_invalidation_and_cleanup, invalidate_db},
......@@ -30,8 +30,34 @@ use crate::{
db_invalidation::invalidation_reasons,
};
const META_KEY_OPERATIONS: u32 = 0;
const META_KEY_NEXT_FREE_TASK_ID: u32 = 1;
/// The fixed keys in the [`KeySpace::Infra`] keyspace.
#[derive(Clone, Copy)]
#[repr(u8)]
enum InfraKey {
Operations = 0,
NextFreeTaskId = 1,
GcRoots = 2,
}
impl InfraKey {
fn key(self) -> ByteKey {
ByteKey::new(self as u8)
}
}
struct ByteKey([u8; 1]);
impl ByteKey {
fn new(value: u8) -> Self {
Self([value])
}
}
impl AsRef<[u8]> for ByteKey {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
struct IntKey([u8; 4]);
......@@ -183,9 +209,9 @@ impl TurboBackingStorageInner {
}
/// Used to read the next free task ID from the database.
fn get_infra_u32(&self, key: u32) -> Result<Option<u32>> {
fn get_infra_u32(&self, key: InfraKey) -> Result<Option<u32>> {
self.database
.get(KeySpace::Infra, IntKey::new(key).as_ref())?
.get(KeySpace::Infra, key.key().as_ref())?
.map(as_u32)
.transpose()
}
......@@ -206,7 +232,7 @@ impl TurboBackingStorage {
pub(crate) fn next_free_task_id(&self) -> Result<TaskId> {
Ok(self
.inner
.get_infra_u32(META_KEY_NEXT_FREE_TASK_ID)
.get_infra_u32(InfraKey::NextFreeTaskId)
.context("Unable to read next free task id from database")?
.map_or(Ok(TaskId::MIN), TaskId::try_from)?)
}
......@@ -214,7 +240,7 @@ impl TurboBackingStorage {
pub(crate) fn uncompleted_operations(&self) -> Result<Vec<AnyOperation>> {
fn get(database: &TurboKeyValueDatabase) -> Result<Vec<AnyOperation>> {
let Some(operations) =
database.get(KeySpace::Infra, IntKey::new(META_KEY_OPERATIONS).as_ref())?
database.get(KeySpace::Infra, InfraKey::Operations.key().as_ref())?
else {
return Ok(Vec::new());
};
......@@ -224,9 +250,23 @@ impl TurboBackingStorage {
get(&self.inner.database).context("Unable to read uncompleted operations from database")
}
/// Reads the persisted GC roots set (see [`InfraKey::GcRoots`]). Empty on a fresh database.
pub(crate) fn roots(&self) -> Result<Vec<(TaskId, TtlCounter)>> {
fn get(database: &TurboKeyValueDatabase) -> Result<Vec<(TaskId, TtlCounter)>> {
let Some(roots) = database.get(KeySpace::Infra, InfraKey::GcRoots.key().as_ref())?
else {
return Ok(Vec::new());
};
let roots = turbo_bincode_decode(roots.borrow())?;
Ok(roots)
}
get(&self.inner.database).context("Unable to read GC roots from database")
}
pub(crate) fn save_snapshot<I>(
&self,
operations: Vec<Arc<AnyOperation>>,
roots: Option<Vec<(TaskId, TtlCounter)>>,
snapshots: Vec<I>,
) -> Result<SnapshotMeta>
where
......@@ -327,7 +367,7 @@ impl TurboBackingStorage {
let mut next_task_id = get_next_free_task_id(&batch)?;
next_task_id = next_task_id.max(snapshot_meta.max_next_task_id + 1);
save_infra(&batch, next_task_id, operations)?;
save_infra(&batch, next_task_id, operations, roots)?;
{
let _span = tracing::trace_span!("commit").entered();
// Byte totals are the physical on-disk bytes (post-compression, including .sst /
......@@ -444,10 +484,7 @@ impl TurboBackingStorage {
fn get_next_free_task_id(batch: &TurboWriteBatch<'_>) -> Result<u32, anyhow::Error> {
Ok(
match batch.get(
KeySpace::Infra,
IntKey::new(META_KEY_NEXT_FREE_TASK_ID).as_ref(),
)? {
match batch.get(KeySpace::Infra, InfraKey::NextFreeTaskId.key().as_ref())? {
Some(bytes) => u32::from_le_bytes(Borrow::<[u8]>::borrow(&bytes).try_into()?),
None => 1,
},
......@@ -458,11 +495,12 @@ fn save_infra(
batch: &TurboWriteBatch<'_>,
next_task_id: u32,
operations: Vec<Arc<AnyOperation>>,
roots: Option<Vec<(TaskId, TtlCounter)>>,
) -> Result<(), anyhow::Error> {
batch
.put(
KeySpace::Infra,
WriteBuffer::Borrowed(IntKey::new(META_KEY_NEXT_FREE_TASK_ID).as_ref()),
WriteBuffer::Borrowed(InfraKey::NextFreeTaskId.key().as_ref()),
WriteBuffer::Borrowed(&next_task_id.to_le_bytes()),
)
.context("Unable to write next free task id")?;
......@@ -474,11 +512,22 @@ fn save_infra(
batch
.put(
KeySpace::Infra,
WriteBuffer::Borrowed(IntKey::new(META_KEY_OPERATIONS).as_ref()),
WriteBuffer::Borrowed(InfraKey::Operations.key().as_ref()),
WriteBuffer::SmallVec(operations),
)
.context("Unable to write operations")?;
}
if let Some(roots) = roots {
let _span = tracing::trace_span!("update roots", roots = roots.len()).entered();
let roots = turbo_bincode_encode(&roots).context("Unable to serialize GC roots")?;
batch
.put(
KeySpace::Infra,
WriteBuffer::Borrowed(InfraKey::GcRoots.key().as_ref()),
WriteBuffer::SmallVec(roots),
)
.context("Unable to write GC roots")?;
}
// Safety: save_infra is called after all concurrent writes to Infra are done.
unsafe { batch.flush(KeySpace::Infra)? };
Ok(())
......@@ -680,6 +729,7 @@ mod tests {
// Snapshot with no task data, just the one deletion.
storage.save_snapshot(
Vec::new(),
None,
vec![vec![SnapshotItem::Delete {
task_id: deleted_id,
task_type_hash: collision_hash.to_le_bytes(),
......
......@@ -16,7 +16,7 @@ use turbo_persistence::{CompactConfig, TurboPersistence};
use crate::database::turbo::{self, TurboKeyValueDatabase};
pub use crate::{
backend::{BackendOptions, EvictionMode, StorageMode, TurboTasksBackend},
backend::{BackendOptions, EvictionMode, StorageMode, TtlCounter, TurboTasksBackend},
database::{
db_invalidation,
db_invalidation::StartupCacheState,
......
......@@ -2,12 +2,20 @@
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
mod gc_fixture;
mod util;
use std::sync::Arc;
use anyhow::Result;
use turbo_tasks::{ResolvedVc, State, TaskId, Vc, prevent_gc};
use turbo_tasks::{
ResolvedVc, TaskId, Vc, prevent_gc, unmark_top_level_task_may_leak_eventually_consistent_state,
};
use crate::util::create_tt;
use crate::{
gc_fixture::{Selector, create_selector},
util::create_tt,
};
/// The `TaskId` backing a resolved `Vc` (its `TaskOutput` node).
fn task_id_of<T>(vc: Vc<T>) -> TaskId {
......@@ -16,14 +24,6 @@ fn task_id_of<T>(vc: Vc<T>) -> TaskId {
.expect("a resolved Vc should be backed by a task")
}
#[turbo_tasks::value(transparent)]
struct Selector(State<bool>);
#[turbo_tasks::function(operation, root)]
fn create_selector(initial: bool) -> Vc<Selector> {
Selector(State::new(initial)).cell()
}
#[turbo_tasks::function]
fn leaf(n: u32) -> Vc<u32> {
Vc::cell(n)
......@@ -180,6 +180,61 @@ async fn gc_does_not_collect_pinned_task() {
tt.stop_and_wait().await;
}
/// Disposing a root task (as `RootTask::Drop` does when JS stops listening to a subscription) must
/// release the anchor its child edges placed on the tasks it read. Disposal is also idempotent and
/// safe after the backend has stopped, which `RootTask::Drop` relies on.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dispose_root_task_releases_anchored_subgraph() {
let (tt, _persistence_dir) = create_tt("dispose_root_task_releases_anchored_subgraph");
// The root sends the leaf's id out via a oneshot rather than a `run_once` probe: a probe is its
// own transient `Once` task that would also connect the leaf, and `Once` tasks are never
// disposed, so the leaf's count would never drop to 0.
let (tx, rx) = tokio::sync::oneshot::channel();
let tx = Arc::new(std::sync::Mutex::new(Some(tx)));
let root_id = tt.spawn_root_task(move || {
let tx = tx.lock().unwrap().take();
Box::pin(async move {
// The root body runs as a top-level task, as `subscribe`'s HMR handler does.
unmark_top_level_task_may_leak_eventually_consistent_state();
let leaf_vc = leaf(88);
let value = *leaf_vc.await?;
if let Some(tx) = tx {
let _ = tx.send(task_id_of(leaf_vc.resolve().await?));
}
anyhow::Ok(Vc::<u32>::cell(value))
})
});
let leaf_id = rx.await.unwrap();
for _ in 0..100 {
if tt.backend().transient_ref_count_for_testing(leaf_id) > 0 {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
assert_eq!(tt.backend().parent_count_for_testing(leaf_id), 0);
assert_eq!(tt.backend().transient_ref_count_for_testing(leaf_id), 1);
assert_eq!(tt.backend().gc_for_testing(&tt), 0);
// Disposing twice must not panic or underflow the child's count: JS could dispose explicitly
// multiple times
tt.dispose_root_task(root_id);
tt.dispose_root_task(root_id);
assert_eq!(tt.backend().transient_ref_count_for_testing(leaf_id), 0);
// A `run_once` keeps touched tasks active until it returns, so GC has to run after it.
turbo_tasks::run_once(tt.clone(), async move { anyhow::Ok(()) })
.await
.unwrap();
assert_eq!(tt.backend().gc_for_testing(&tt), 1);
// Disposal after the backend has stopped (the whole task map is dropped by `stop`), as a
// `RootTask` finalized during Node worker teardown would be.
tt.stop_and_wait().await;
tt.dispose_root_task(root_id);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn unpin_after_stop_does_not_panic() {
let (tt, _persistence_dir) = create_tt("unpin_after_stop_does_not_panic");
......
#![feature(arbitrary_self_types)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
//! Cross-session collection of GC roots.
mod gc_fixture;
mod util;
use std::{
sync::{
Arc,
atomic::{AtomicU32, Ordering},
},
time::Duration,
};
use anyhow::Result;
use turbo_tasks::{GcRoot, TurboTasks, Vc};
use turbo_tasks_backend::TurboTasksBackend;
use crate::{
gc_fixture::{create_constant, diamond_root_op},
util::{create_persistence_dir, reopen_tt_with_gc, reopen_tt_with_gc_ttl},
};
/// Counts executions of [`orphan_leaf`], keyed by its argument. A collected task re-executes when
/// next requested; a merely evicted one restores from disk without executing.
static LEAF_EXECUTIONS: [AtomicU32; 3] = [AtomicU32::new(0), AtomicU32::new(0), AtomicU32::new(0)];
fn leaf_executions(n: u32) -> u32 {
LEAF_EXECUTIONS[n as usize].load(Ordering::Relaxed)
}
/// A leaf keyed by `n`, so each root gets a distinct child.
#[turbo_tasks::function]
fn orphan_leaf(n: u32) -> Vc<u32> {
LEAF_EXECUTIONS[n as usize].fetch_add(1, Ordering::Relaxed);
Vc::cell(n)
}
/// A two-task "root -> subtree": read at the top level of a session this op has no persistent
/// parent, so it is a durable GC root and its leaf gets `parent_count 1`.
#[turbo_tasks::function(operation, root)]
async fn root_with_child(n: u32) -> Result<Vc<u32>> {
Ok(Vc::cell(*orphan_leaf(n).await? + 1))
}
/// Runs GC until at least `want` tasks have been collected, or gives up after 20 passes.
///
/// A loop rather than a fixed pass count because age-out needs `elapsed > TTL` strictly, so a pass
/// sharing a millisecond with the demotion collects nothing.
async fn gc_until_collected(tt: &Arc<TurboTasks<TurboTasksBackend>>, want: usize) -> usize {
let mut collected = 0usize;
for _ in 0..20 {
collected += tt.backend().gc_for_testing(tt);
if collected >= want {
break;
}
tokio::time::sleep(Duration::from_millis(5)).await;
}
collected
}
/// Width of the diamond: the fanout gives many chances for the interleaving where an `A` scrubs a
/// not-yet-restored `B`.
const DIAMOND_FANOUT: u32 = 64;
/// A root is kept alive by being used and reclaimed by not being used, across process restarts.
///
/// Both roots have `parent_count == 0` forever, so only the cross-session roots map distinguishes
/// the one that keeps being requested from the one that is abandoned. The TTL is forced to 0 so the
/// age-out lands inside the test rather than days later.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn reused_root_survives_sessions_that_abandon_its_sibling() {
let dir = create_persistence_dir("reused_root_survives_sessions_that_abandon_its_sibling");
// Session 1: build both subtrees and pin each root, the way an embedder holds a live handle to
// a route. The pin makes it a durable root, recorded in the persisted roots map on shutdown.
{
let tt = reopen_tt_with_gc(&dir);
let ops = turbo_tasks::run_once(tt.clone(), async move {
let kept = root_with_child(1);
let dropped = root_with_child(2);
assert_eq!(*kept.read_strongly_consistent().await?, 2);
assert_eq!(*dropped.read_strongly_consistent().await?, 3);
anyhow::Ok((kept, dropped))
})
.await
.unwrap();
let kept_pin = GcRoot::pin(tt.clone(), ops.0);
let dropped_pin = GcRoot::pin(tt.clone(), ops.1);
tt.backend().snapshot_and_evict_for_testing(&tt);
drop(kept_pin);
drop(dropped_pin);
tt.stop_and_wait().await;
}
// Sessions 2 and 3: only root 1 is ever requested again. Session 2's first pass demotes root 2
// and a later pass ages it out; session 3 proves the collection stuck.
let mut total_collected = 0usize;
for session in 2..=3 {
let tt = reopen_tt_with_gc_ttl(&dir, Duration::ZERO);
let kept_op = turbo_tasks::run_once(tt.clone(), async move {
// Re-request root 1 only, so it has a resident entry to pin.
let kept = root_with_child(1);
assert_eq!(
*kept.read_strongly_consistent().await?,
2,
"the reused root must still compute in session {session}"
);
anyhow::Ok(kept)
})
.await
.unwrap();
let kept_pin = GcRoot::pin(tt.clone(), kept_op);
total_collected += gc_until_collected(&tt, 2).await;
drop(kept_pin);
tt.stop_and_wait().await;
}
// Exactly the abandoned root and its leaf: root 1 is re-pinned every session and its leaf has
// a parent, so nothing else here is collectible.
assert_eq!(total_collected, 2);
// Session 4: the survivor must still be cached, the collected one must rebuild.
{
let tt = reopen_tt_with_gc(&dir);
let kept_before = leaf_executions(1);
let dropped_before = leaf_executions(2);
let result = turbo_tasks::run_once(tt.clone(), async move {
// A dangling edge left by the collection, or a resurrected half-deleted task, would
// surface as a wrong value or a panic here.
assert_eq!(*root_with_child(1).read_strongly_consistent().await?, 2);
assert_eq!(*root_with_child(2).read_strongly_consistent().await?, 3);
// The survivor's leaf comes from the persisted cache; the collected one re-executes.
assert_eq!(leaf_executions(1), kept_before);
assert_eq!(leaf_executions(2), dropped_before + 1);
anyhow::Ok(())
})
.await;
tt.stop_and_wait().await;
result.unwrap();
}
}
/// Collecting an orphaned root whose subtree holds a forward cell-dependency on a **disk-only**
/// target (never restored this session) must scrub that stale reverse edge by restoring the live
/// target.
///
/// Session 2 ages the root out and cascades its `A`/`B` children concurrently, so an `A`'s
/// `CleanupOldEdges` can open its target `B` before `B` has been restored from disk. Unlike
/// `gc_resurrection::gc_diamond_forward_dep_no_resurrection`, where every target is resident, only
/// a restart can reach the disk-only case.
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn gc_collect_scrubs_disk_only_forward_dep_target() {
let dir = create_persistence_dir("gc_collect_scrubs_disk_only_forward_dep_target");
// Session 1: build the diamond and persist it. Without the pin the root is never a tracked
// root, so nothing is collected and the cascade under test never runs.
{
let tt = reopen_tt_with_gc(&dir);
let root_op = turbo_tasks::run_once(tt.clone(), async move {
let constant_op = create_constant();
let constant_vc = constant_op.resolve().strongly_consistent().await?;
let root_op = diamond_root_op(constant_vc, DIAMOND_FANOUT);
root_op.read_strongly_consistent().await?;
anyhow::Ok(root_op)
})
.await
.unwrap();
let root_pin = GcRoot::pin(tt.clone(), root_op);
// A GC pass is what admits a live root to the persisted map; the snapshot alone leaves
// nothing for session 2 to age out.
tt.backend().gc_for_testing(&tt);
tt.backend().snapshot_and_evict_for_testing(&tt);
drop(root_pin);
tt.stop_and_wait().await;
}
// Session 2: the root is never re-requested, so it ages out and cascades to every A/B pair.
{
let tt = reopen_tt_with_gc_ttl(&dir, Duration::ZERO);
let tt2 = tt.clone();
turbo_tasks::run_once(tt.clone(), async move {
// No panic in these passes is the real assertion. The diamond is a root plus
// DIAMOND_FANOUT A/B pairs; the cascade may also reach the constant op above it.
let collected = gc_until_collected(&tt2, 2 * DIAMOND_FANOUT as usize + 1).await;
assert!(
collected > 2 * DIAMOND_FANOUT as usize,
"the orphaned diamond subtree should be collected (got {collected})"
);
anyhow::Ok(())
})
.await
.unwrap();
tt.stop_and_wait().await;
}
// Session 3: a clean recompute, proving no dangling reverse edge survived.
{
let tt = reopen_tt_with_gc(&dir);
let result = turbo_tasks::run_once(tt.clone(), async move {
let constant_op = create_constant();
let constant_vc = constant_op.resolve().strongly_consistent().await?;
diamond_root_op(constant_vc, DIAMOND_FANOUT)
.read_strongly_consistent()
.await?;
anyhow::Ok(())
})
.await;
tt.stop_and_wait().await;
result.unwrap();
}
}
//! Shared graph shapes for the GC tests.
use anyhow::Result;
use turbo_tasks::{ResolvedVc, State, Vc};
/// A flag a test flips to disconnect a subtree.
#[turbo_tasks::value(transparent)]
pub struct Selector(State<bool>);
#[turbo_tasks::function(operation, root)]
pub fn create_selector(initial: bool) -> Vc<Selector> {
Selector(State::new(initial)).cell()
}
/// 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`).
#[turbo_tasks::value(transparent)]
pub struct Constant(State<u32>);
#[turbo_tasks::function(operation, root)]
pub fn create_constant() -> Vc<Constant> {
Constant(State::new(0)).cell()
}
// --- Diamond fixture ---
//
// Reader `A` forward-cell-depends on target `B` without `B` being its child; both are children of
// the root. So collecting the root drives `A` and `B` to `parent_count 0` at once and they cascade
// concurrently, while `A` still has a forward-dep on `B` to scrub.
/// The forward-dependency target `B`.
#[turbo_tasks::function]
pub async fn diamond_target(constant: ResolvedVc<Constant>, index: u32) -> Result<Vc<u32>> {
let base = *constant.await?.get();
Ok(Vc::cell(base.wrapping_add(index).wrapping_mul(7)))
}
/// The diamond reader `A`: `B` arrives already resolved, so reading it records a forward dependency
/// without making `B` a child (only calling a task creates a child edge).
#[turbo_tasks::function]
pub async fn diamond_reader(target: ResolvedVc<u32>) -> Result<Vc<u32>> {
Ok(Vc::cell(1 + *target.await?))
}
/// Parents each `A`/`B` pair as siblings. `fanout` is part of the cache key, so each caller gets
/// distinct task instances.
#[turbo_tasks::function]
pub async fn diamond_root(constant: ResolvedVc<Constant>, fanout: u32) -> Result<Vc<u32>> {
let mut sum = 0u32;
for index in 0..fanout {
let target = diamond_target(*constant, index).to_resolved().await?;
sum = sum.wrapping_add(*target.await?);
sum = sum.wrapping_add(*diamond_reader(*target).await?);
}
Ok(Vc::cell(sum))
}
/// [`diamond_root`] as an op, for tests that need a `task_id()` to pin as a durable root.
#[turbo_tasks::function(operation, root)]
pub async fn diamond_root_op(constant: ResolvedVc<Constant>, fanout: u32) -> Result<Vc<u32>> {
Ok(diamond_root(*constant, fanout))
}
......@@ -14,34 +14,18 @@
//! subtree must also be disconnected *cleanly* — drop the parent's reference, don't invalidate,
//! since invalidation runs `cleanup_old_edges` and strips the outgoing deps first.
mod gc_fixture;
mod util;
use std::sync::atomic::{AtomicU32, Ordering};
use anyhow::Result;
use turbo_tasks::{ResolvedVc, State, Vc};
use turbo_tasks::{ResolvedVc, Vc};
use crate::util::create_tt;
#[turbo_tasks::value(transparent)]
struct Selector(State<bool>);
#[turbo_tasks::function(operation, root)]
fn create_selector(initial: bool) -> Vc<Selector> {
Selector(State::new(initial)).cell()
}
/// A long-lived State whose *value never changes*, read by the leaves purely to make each leaf
/// **mutable**, so a reader records a real dependency edge on it. The state task is a root and
/// stays alive; the leaves reach it via a dependency edge, not a child edge, so disconnecting the
/// leaves as children still lets them lose activeness.
#[turbo_tasks::value(transparent)]
struct Constant(State<u32>);
#[turbo_tasks::function(operation, root)]
fn create_constant() -> Vc<Constant> {
Constant(State::new(0)).cell()
}
use crate::{
gc_fixture::{Constant, Selector, create_constant, create_selector, diamond_root},
util::create_tt,
};
/// The forward-dependency *target*: mutable because it reads `constant`'s State. `FANOUT` distinct
/// leaves per reader give many chances for the racing interleaving.
......@@ -66,38 +50,6 @@ async fn reader(constant: ResolvedVc<Constant>) -> Result<Vc<u32>> {
Ok(Vc::cell(sum))
}
/// A *sibling* forward-dependency target for the diamond fixture (`B`).
#[turbo_tasks::function]
async fn diamond_target(constant: ResolvedVc<Constant>, index: u32) -> Result<Vc<u32>> {
let base = *constant.await?.get();
Ok(Vc::cell(base.wrapping_add(index).wrapping_mul(7)))
}
/// A diamond *reader* (`A`): reads the cell of a `diamond_target` (`B`) that is **passed in as an
/// already-resolved `Vc`** — so `A` records a forward (cell) dependency on `B` WITHOUT connecting
/// `B` as `A`'s child (a child edge is only created by *calling* a task; here `B` was called by
/// `diamond_root`). This decoupling is the crux: `B`'s only parent is `diamond_root`, so when the
/// root is collected BOTH `A` and `B` reach `parent_count 0` at the same time and cascade-collect
/// concurrently — while `A` still holds a forward-dep on `B` to scrub.
#[turbo_tasks::function]
async fn diamond_reader(target: ResolvedVc<u32>) -> Result<Vc<u32>> {
Ok(Vc::cell(1 + *target.await?))
}
/// The diamond root: parents both `A` and `B` as siblings, so collecting the root cascades a
/// `Collect` for every `A` and `B` at once — a `B` can therefore be collected before the
/// `Collect(A)` whose `CleanupOldEdges` opens it.
#[turbo_tasks::function]
async fn diamond_root(constant: ResolvedVc<Constant>) -> Result<Vc<u32>> {
let mut sum = 0u32;
for index in 0..FANOUT {
let target = diamond_target(*constant, index).to_resolved().await?;
sum = sum.wrapping_add(*target.await?);
sum = sum.wrapping_add(*diamond_reader(*target).await?);
}
Ok(Vc::cell(sum))
}
/// A root that reads `reader()` only while the selector is `false`. Flipping the selector to `true`
/// drops `reader` (and, transitively, all `sd_leaf`s) from the live graph **without invalidating
/// them** — so they stay clean and retain their outgoing dependency edges, then all become
......@@ -125,7 +77,7 @@ async fn select_diamond(
) -> Result<Vc<u32>> {
let use_diamond = !*selector.await?.get();
let value = if use_diamond {
*diamond_root(*constant).await?
*diamond_root(*constant, FANOUT).await?
} else {
0u32
};
......@@ -180,6 +132,13 @@ async fn gc_rebalances_aggregation_and_cascades_in_one_pass() {
"resident count must drop by exactly the collected subtree"
);
// Only the three top-level `(operation, root)` tasks may be tracked as roots. A leaf that
// reached the post-drain scan still holding a dangling `upper` edge to the deleted `reader`
// would land in the map as `MostRecent`, which never ages out. If this fires it is a finding
// about the aggregation graph, not a reason to narrow `gc_is_root`.
let roots = tt2.backend().persisted_gc_roots_for_testing();
assert_eq!(roots.len(), 3, "unexpected roots tracked: {roots:?}");
tt.stop_and_wait().await;
}
......
......@@ -6,8 +6,12 @@
//! `Arc<dyn TurboTasksApi>`, which erases the concrete backend type and makes `tt.backend()`
//! unreachable. It also owns the session loop (re-running the body and comparing results), whereas
//! these tests need to drive snapshots — and sometimes a DB reopen — on their own schedule.
//!
//! This module is compiled into each test binary separately, so any helper a given binary doesn't
//! call reads as dead code there.
#![allow(dead_code)]
use std::{path::Path, sync::Arc};
use std::{path::Path, sync::Arc, time::Duration};
use turbo_tasks::TurboTasks;
use turbo_tasks_backend::{
......@@ -19,6 +23,21 @@ use turbo_tasks_backend::{
/// Reusing the same `path` (after the previous backend has been stopped) reopens the persisted
/// database, which is how a test can assert that state survives a restart.
fn open_tt_at(path: &Path, num_workers: usize) -> Arc<TurboTasks<TurboTasksBackend>> {
open_tt_at_with_gc(path, num_workers, Some(true), None)
}
/// Like [`open_tt_at`], but forces the GC on or off for this backend instead of deriving it from
/// the `TURBO_ENGINE_GC` env var, which every test in the binary would share.
///
/// A test that depends on the persisted GC roots map must force it on: the map is only written by
/// the GC branch of `snapshot_and_persist`, so with GC off a session persists an empty root set and
/// the cross-session behaviour under test silently never engages.
fn open_tt_at_with_gc(
path: &Path,
num_workers: usize,
gc: Option<bool>,
gc_root_ttl: Option<Duration>,
) -> Arc<TurboTasks<TurboTasksBackend>> {
TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
num_workers: Some(num_workers),
......@@ -27,9 +46,8 @@ fn open_tt_at(path: &Path, num_workers: usize) -> Arc<TurboTasks<TurboTasksBacke
// snapshot_and_evict_for_testing manually.
storage_mode: Some(turbo_tasks_backend::StorageMode::ReadWriteOnShutdown),
eviction_mode: EvictionMode::Full,
// `gc_for_testing` requires a GC-enabled backend. Set per-backend rather than via
// `TURBO_ENGINE_GC` so parallel tests in the same binary don't share the setting.
gc: Some(true),
gc,
gc_root_ttl,
..Default::default()
},
turbo_tasks_backend::turbo_backing_storage(
......@@ -49,15 +67,37 @@ fn open_tt_at(path: &Path, num_workers: usize) -> Arc<TurboTasks<TurboTasksBacke
))
}
/// A persistence directory that outlives the backends opened on it, so a test can stop one backend
/// and open another on the same path to simulate a restart.
pub fn create_persistence_dir(name: &str) -> tempfile::TempDir {
tempfile::Builder::new()
.prefix(&format!("{name}-"))
.tempdir()
.unwrap()
}
/// Opens a new session over an existing persistence directory, with the GC forced on (see
/// [`open_tt_at_with_gc`]). The previous backend must already be stopped so its shutdown snapshot
/// has been flushed.
pub fn reopen_tt_with_gc(dir: &tempfile::TempDir) -> Arc<TurboTasks<TurboTasksBackend>> {
open_tt_at_with_gc(dir.path(), 2, Some(true), None)
}
/// [`reopen_tt_with_gc`] with the GC root TTL pinned, so a test can age a root out inside the test
/// rather than days later. Set at construction, where the TTL is resolved.
pub fn reopen_tt_with_gc_ttl(
dir: &tempfile::TempDir,
gc_root_ttl: Duration,
) -> Arc<TurboTasks<TurboTasksBackend>> {
open_tt_at_with_gc(dir.path(), 2, Some(true), Some(gc_root_ttl))
}
/// A fresh persistent backend in its own temp directory, with `num_workers` workers.
pub fn create_tt_with_workers(
name: &str,
num_workers: usize,
) -> (Arc<TurboTasks<TurboTasksBackend>>, tempfile::TempDir) {
let dir = tempfile::Builder::new()
.prefix(&format!("{name}-"))
.tempdir()
.unwrap();
let dir = create_persistence_dir(name);
let tt = open_tt_at(dir.path(), num_workers);
(tt, dir)
}
......
......@@ -102,7 +102,7 @@ pub use crate::{
},
join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt},
manager::{
CurrentCellRef, InputResolution, ReadCellTracking, ReadConsistency, ReadTracking,
CurrentCellRef, GcRoot, InputResolution, ReadCellTracking, ReadConsistency, ReadTracking,
ScheduleKey, TaskPersistence, TaskPriority, TurboTasks, TurboTasksApi, TurboTasksCallApi,
Unused, UpdateInfo, dynamic_call, emit, get_serialization_invalidator, mark_finished,
mark_stateful, mark_top_level_task, prevent_gc, run, run_once, run_once_with_reason,
......
use std::{
borrow::Borrow,
cell::Cell,
cmp::Reverse,
fmt::{Debug, Display},
future::Future,
hash::{BuildHasher, BuildHasherDefault},
hash::{BuildHasher, BuildHasherDefault, Hash},
mem::take,
ops::Deref,
panic::AssertUnwindSafe,
......@@ -30,9 +31,9 @@ use tracing::{Instrument, Span, instrument};
use turbo_tasks_hash::{DeterministicHash, hash_xxh3_hash128};
use crate::{
CellId, Completion, InvalidationReason, InvalidationReasonSet, OutputContent, RawVc,
ReadCellOptions, ReadOutcome, ReadOutputOptions, ResolvedVc, SharedReference, TaskId,
TraitMethod, ValueTypeId, Vc, VcRead, VcValueTrait, VcValueType,
CellId, Completion, InvalidationReason, InvalidationReasonSet, NonLocalValue, OperationValue,
OperationVc, OutputContent, RawVc, ReadCellOptions, ReadOutcome, ReadOutputOptions, ResolvedVc,
SharedReference, TaskId, TraitMethod, ValueTypeId, Vc, VcRead, VcValueTrait, VcValueType,
backend::{
Backend, CellContent, CellHash, TaskCollectiblesMap, TaskExecutionSpec, TransientTaskType,
TurboTasksExecutionError, TypedCellContent, VerificationMode,
......@@ -2262,6 +2263,89 @@ pub fn prevent_gc() {
}
}
/// An RAII guard that pins an [`OperationVc`]'s task against garbage collection.
pub struct GcRoot<T: ?Sized> {
tt: Arc<dyn TurboTasksApi>,
vc: OperationVc<T>,
}
impl<T: ?Sized> GcRoot<T> {
/// Pins `vc`'s task, returning a guard that unpins it on drop.
pub fn pin(tt: Arc<dyn TurboTasksApi>, vc: OperationVc<T>) -> Self {
tt.pin_task_for_gc(vc.task_id());
Self { tt, vc }
}
}
/// A guard derefs to the operation it pins, so [`OperationVc`]'s own methods can be called on it
/// directly and `*guard` recovers the operation itself.
impl<T: ?Sized> Deref for GcRoot<T> {
type Target = OperationVc<T>;
fn deref(&self) -> &Self::Target {
&self.vc
}
}
impl<T: ?Sized> Clone for GcRoot<T> {
fn clone(&self) -> Self {
Self::pin(self.tt.clone(), self.vc)
}
}
impl<T: ?Sized> Drop for GcRoot<T> {
fn drop(&mut self) {
self.tt.unpin_task_for_gc(self.vc.task_id());
}
}
impl<T: ?Sized> Debug for GcRoot<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GcRoot").field("vc", &self.vc).finish()
}
}
impl<T: ?Sized> PartialEq for GcRoot<T> {
/// Compares the pinned operation only. Two guards for the same operation are interchangeable
/// as far as reachability is concerned, even though each holds its own pin.
fn eq(&self, other: &Self) -> bool {
self.vc == other.vc
}
}
impl<T: ?Sized> Eq for GcRoot<T> {}
impl<T: ?Sized> Hash for GcRoot<T> {
/// Hashes the pinned operation, consistently with [`PartialEq`], so a guard can be looked up
/// in a set by the [`OperationVc`] it pins (see the [`Borrow`] impl).
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.vc.hash(state);
}
}
/// Lets a collection keyed on guards be queried with the bare operation: `Borrow` plus the
/// matching [`Hash`]/[`Eq`] impls give `OperationVc<T>: Equivalent<GcRoot<T>>`, so e.g.
/// `IndexSet<GcRoot<T>>::swap_remove` accepts an `&OperationVc<T>`.
impl<T: ?Sized> Borrow<OperationVc<T>> for GcRoot<T> {
fn borrow(&self) -> &OperationVc<T> {
&self.vc
}
}
impl<T: ?Sized> TraceRawVcs for GcRoot<T> {
fn trace_raw_vcs(&self, trace_context: &mut crate::trace::TraceRawVcsContext) {
self.vc.trace_raw_vcs(trace_context);
}
}
/// Safety: a `GcRoot` contains exactly one [`OperationVc`] and no [`Vc`] or [`ResolvedVc`], which
/// is what [`OperationValue`] asserts.
unsafe impl<T: ?Sized + Send> OperationValue for GcRoot<T> {}
/// Safety: mirrors the [`OperationVc`] impl — a `GcRoot` holds no task-local data beyond the
/// operation it pins.
unsafe impl<T: NonLocalValue + ?Sized> NonLocalValue for GcRoot<T> {}
pub fn emit<T: VcValueTrait + ?Sized>(collectible: ResolvedVc<T>) {
with_turbo_tasks(|tt| {
let raw_vc = collectible.node.node;
......
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