Unverified Commit 6cfca45f authored by Jimmy Miller's avatar Jimmy Miller Committed by GitHub

Pin tasks during construction (#98688)

I got a failure in CI for the stress test that pointed to this as the
cause. During construction, since the task has no references to it, it
can be collected. Setting the transient ref prevents this bad case from
happening.

@lukesandberg I'm going to work on the gc at each suspend point in a
follow up PR.
parent f1a7e1db
...@@ -1811,6 +1811,7 @@ impl TurboTasksBackend { ...@@ -1811,6 +1811,7 @@ impl TurboTasksBackend {
let shard = get_shard(&self.storage.task_cache, hash); let shard = get_shard(&self.storage.task_cache, hash);
let mut ctx = self.execute_context(turbo_tasks); let mut ctx = self.execute_context(turbo_tasks);
let mut created_new = false;
// Step 1: Fast read-only cache lookup (read lock, no allocation). // Step 1: Fast read-only cache lookup (read lock, no allocation).
// Use a read lock rather than a write lock to avoid contention. connect_child // Use a read lock rather than a write lock to avoid contention. connect_child
// may re-enter task_cache with a write lock, so we must not hold a write lock here. // may re-enter task_cache with a write lock, so we must not hold a write lock here.
...@@ -1818,7 +1819,12 @@ impl TurboTasksBackend { ...@@ -1818,7 +1819,12 @@ impl TurboTasksBackend {
get_in_shard(shard, hash, |k| k.eq_components(native_fn, this, arg_ref)) get_in_shard(shard, hash, |k| k.eq_components(native_fn, this, arg_ref))
{ {
self.track_cache_hit_by_fn(native_fn); self.track_cache_hit_by_fn(native_fn);
operation::ConnectChildOperation::run(parent_task, task_id, ctx); operation::ConnectChildOperation::run(
parent_task,
task_id,
/* release_construction_ref */ false,
ctx,
);
return task_id; return task_id;
} }
...@@ -1885,6 +1891,7 @@ impl TurboTasksBackend { ...@@ -1885,6 +1891,7 @@ impl TurboTasksBackend {
// The entry closure has returned, so the task_cache shard lock is released before // The entry closure has returned, so the task_cache shard lock is released before
// cache tracking or aggregation updates can re-enter the backend. // cache tracking or aggregation updates can re-enter the backend.
created_new = created;
if created { if created {
self.track_cache_miss_by_fn(native_fn); self.track_cache_miss_by_fn(native_fn);
// Update the aggregation number before connecting the child. We don't need this on // Update the aggregation number before connecting the child. We don't need this on
...@@ -1916,7 +1923,9 @@ impl TurboTasksBackend { ...@@ -1916,7 +1923,9 @@ impl TurboTasksBackend {
task_id task_id
}; };
operation::ConnectChildOperation::run(parent_task, task_id, ctx); // New tasks carry a transient ref so they survive construction. Release it while
// connecting the task to the graph.
operation::ConnectChildOperation::run(parent_task, task_id, created_new, ctx);
task_id task_id
} }
...@@ -3532,7 +3541,12 @@ impl TurboTasksBackend { ...@@ -3532,7 +3541,12 @@ impl TurboTasksBackend {
turbo_tasks: &TurboTasks<TurboTasksBackend>, turbo_tasks: &TurboTasks<TurboTasksBackend>,
) { ) {
self.assert_not_persistent_calling_transient(parent_task, task, None); self.assert_not_persistent_calling_transient(parent_task, task, None);
ConnectChildOperation::run(parent_task, task, self.execute_context(turbo_tasks)); ConnectChildOperation::run(
parent_task,
task,
/* release_construction_ref */ false,
self.execute_context(turbo_tasks),
);
} }
fn create_transient_task(&self, task_type: TransientTaskType) -> TaskId { fn create_transient_task(&self, task_type: TransientTaskType) -> TaskId {
......
...@@ -318,6 +318,7 @@ pub enum AggregationUpdateJob { ...@@ -318,6 +318,7 @@ pub enum AggregationUpdateJob {
// upon attempted serialization) similar to #[serde(skip)] on variants // upon attempted serialization) similar to #[serde(skip)] on variants
#[bincode(skip, default = "unreachable_decode")] #[bincode(skip, default = "unreachable_decode")]
task: TaskId, task: TaskId,
release_construction_ref: bool,
}, },
/// Increases the active counters of the tasks /// Increases the active counters of the tasks
IncreaseActiveCounts { IncreaseActiveCounts {
...@@ -1498,12 +1499,17 @@ impl AggregationUpdateQueue { ...@@ -1498,12 +1499,17 @@ impl AggregationUpdateQueue {
} }
} }
} }
AggregationUpdateJob::IncreaseActiveCount { task } => { AggregationUpdateJob::IncreaseActiveCount {
self.increase_active_count(ctx, task); task,
release_construction_ref,
} => {
self.increase_active_count(ctx, task, release_construction_ref);
} }
AggregationUpdateJob::IncreaseActiveCounts { mut task_ids } => { AggregationUpdateJob::IncreaseActiveCounts { mut task_ids } => {
if let Some(task_id) = task_ids.pop() { if let Some(task_id) = task_ids.pop() {
self.increase_active_count(ctx, task_id); self.increase_active_count(
ctx, task_id, /* release_construction_ref */ false,
);
if !task_ids.is_empty() { if !task_ids.is_empty() {
self.jobs.push_front(AggregationUpdateJobItem::new( self.jobs.push_front(AggregationUpdateJobItem::new(
AggregationUpdateJob::IncreaseActiveCounts { task_ids }, AggregationUpdateJob::IncreaseActiveCounts { task_ids },
...@@ -1760,7 +1766,10 @@ impl AggregationUpdateQueue { ...@@ -1760,7 +1766,10 @@ impl AggregationUpdateQueue {
let has_active_count = let has_active_count =
upper.get_activeness().is_some_and(|a| a.active_counter > 0); upper.get_activeness().is_some_and(|a| a.active_counter > 0);
if has_active_count { if has_active_count {
self.push(AggregationUpdateJob::IncreaseActiveCount { task: task_id }); self.push(AggregationUpdateJob::IncreaseActiveCount {
task: task_id,
release_construction_ref: false,
});
} }
} }
// notify uppers about new follower // notify uppers about new follower
...@@ -3057,6 +3066,7 @@ impl AggregationUpdateQueue { ...@@ -3057,6 +3066,7 @@ impl AggregationUpdateQueue {
if has_active_count { if has_active_count {
self.push(AggregationUpdateJob::IncreaseActiveCount { self.push(AggregationUpdateJob::IncreaseActiveCount {
task: new_follower_id, task: new_follower_id,
release_construction_ref: false,
}); });
} }
...@@ -3216,7 +3226,12 @@ impl AggregationUpdateQueue { ...@@ -3216,7 +3226,12 @@ impl AggregationUpdateQueue {
/// Increases the active count of a task. /// Increases the active count of a task.
/// ///
/// Only used when activeness is tracked. /// Only used when activeness is tracked.
fn increase_active_count(&mut self, ctx: &mut impl ExecuteContext<'_>, task_id: TaskId) { fn increase_active_count(
&mut self,
ctx: &mut impl ExecuteContext<'_>,
task_id: TaskId,
release_construction_ref: bool,
) {
#[cfg(feature = "trace_aggregation_update")] #[cfg(feature = "trace_aggregation_update")]
let _span = trace_span!("increase active count").entered(); let _span = trace_span!("increase active count").entered();
...@@ -3233,6 +3248,9 @@ impl AggregationUpdateQueue { ...@@ -3233,6 +3248,9 @@ impl AggregationUpdateQueue {
let is_new = state.is_empty(); let is_new = state.is_empty();
let is_positive_now = state.increment_active_counter(); let is_positive_now = state.increment_active_counter();
let is_empty = state.is_empty(); let is_empty = state.is_empty();
if release_construction_ref {
task.update_and_get_transient_ref_count(-1);
}
// This can happen if active count was negative before // This can happen if active count was negative before
if is_empty { if is_empty {
task.take_activeness(); task.take_activeness();
......
...@@ -58,6 +58,11 @@ pub(super) fn resurrect_deleted<'e, C: ExecuteContext<'e>>( ...@@ -58,6 +58,11 @@ pub(super) fn resurrect_deleted<'e, C: ExecuteContext<'e>>(
task task
} }
fn release_construction_ref<'e, C: ExecuteContext<'e>>(task_id: TaskId, ctx: &mut C) {
let mut task = ctx.task(task_id, TaskDataCategory::Meta);
task.update_and_get_transient_ref_count(-1);
}
#[derive(Encode, Decode, Clone, Default)] #[derive(Encode, Decode, Clone, Default)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum ConnectChildOperation { pub enum ConnectChildOperation {
...@@ -72,6 +77,7 @@ impl ConnectChildOperation { ...@@ -72,6 +77,7 @@ impl ConnectChildOperation {
pub fn run( pub fn run(
parent_task_id: Option<TaskId>, parent_task_id: Option<TaskId>,
child_task_id: TaskId, child_task_id: TaskId,
release_construction_ref: bool,
mut ctx: impl ExecuteContext<'_>, mut ctx: impl ExecuteContext<'_>,
) { ) {
if let Some(parent_task_id) = parent_task_id { if let Some(parent_task_id) = parent_task_id {
...@@ -85,6 +91,10 @@ impl ConnectChildOperation { ...@@ -85,6 +91,10 @@ impl ConnectChildOperation {
// Quick skip if the child was already connected before // Quick skip if the child was already connected before
// We defer the insert until after the aggregation queue is processed. // We defer the insert until after the aggregation queue is processed.
if new_children.contains(&child_task_id) { if new_children.contains(&child_task_id) {
drop(parent_task);
if release_construction_ref {
self::release_construction_ref(child_task_id, &mut ctx);
}
return; return;
} }
...@@ -98,6 +108,10 @@ impl ConnectChildOperation { ...@@ -98,6 +108,10 @@ impl ConnectChildOperation {
unreachable!(); unreachable!();
}; };
new_children.insert(child_task_id); new_children.insert(child_task_id);
drop(parent_task);
if release_construction_ref {
self::release_construction_ref(child_task_id, &mut ctx);
}
return; return;
} }
} }
...@@ -118,6 +132,7 @@ impl ConnectChildOperation { ...@@ -118,6 +132,7 @@ impl ConnectChildOperation {
} }
queue.push(AggregationUpdateJob::IncreaseActiveCount { queue.push(AggregationUpdateJob::IncreaseActiveCount {
task: child_task_id, task: child_task_id,
release_construction_ref,
}); });
} else { } else {
// First connect of this child: its id is minted but the storage entry may not exist // First connect of this child: its id is minted but the storage entry may not exist
...@@ -152,6 +167,9 @@ impl ConnectChildOperation { ...@@ -152,6 +167,9 @@ impl ConnectChildOperation {
{ {
ctx.schedule_task(&child_task, ctx.get_current_task_priority()); ctx.schedule_task(&child_task, ctx.get_current_task_priority());
} }
if release_construction_ref {
child_task.update_and_get_transient_ref_count(-1);
}
} }
if !queue.is_empty() { if !queue.is_empty() {
......
...@@ -290,6 +290,7 @@ impl Storage { ...@@ -290,6 +290,7 @@ impl Storage {
let mut task = self.access_mut(task_id); let mut task = self.access_mut(task_id);
task.flags.set_restored(TaskDataCategory::All); task.flags.set_restored(TaskDataCategory::All);
task.flags.set_new_task(true); task.flags.set_new_task(true);
task.gc_pin_for_construction();
if let Some(task_type) = task_type { if let Some(task_type) = task_type {
task.set_persistent_task_type(task_type); task.set_persistent_task_type(task_type);
if !task_id.is_transient() { if !task_id.is_transient() {
...@@ -1153,6 +1154,18 @@ mod tests { ...@@ -1153,6 +1154,18 @@ mod tests {
TaskId::new(id).expect("id must be non-zero") TaskId::new(id).expect("id must be non-zero")
} }
#[test]
fn new_task_is_pinned_during_construction() {
let storage = Storage::new(2, true);
let task_id = non_transient_task(1);
storage.initialize_new_task(task_id, None);
let task = storage.access_mut(task_id);
assert_eq!(task.gc_transient_ref_count(), 1);
assert!(!task.gc_maybe_collectible());
}
/// A process fn that returns a non-empty SnapshotItem so the iterator doesn't /// A process fn that returns a non-empty SnapshotItem so the iterator doesn't
/// silently skip items via the "encoding failed" error path. /// silently skip items via the "encoding failed" error path.
fn dummy_process( fn dummy_process(
......
...@@ -858,6 +858,12 @@ impl TaskStorage { ...@@ -858,6 +858,12 @@ impl TaskStorage {
self.get_transient_ref_count().copied().unwrap_or(0) self.get_transient_ref_count().copied().unwrap_or(0)
} }
/// Pins a newly initialized task until its construction operation connects it to the graph.
pub fn gc_pin_for_construction(&mut self) {
debug_assert_eq!(self.gc_transient_ref_count(), 0);
self.set_transient_ref_count(1);
}
/// Whether a GC pass may collect this task: nothing references it, via parents, transient /// Whether a GC pass may collect this task: nothing references it, via parents, transient
/// pins, aggregation edges, or dependency edges. /// pins, aggregation edges, or dependency edges.
/// ///
......
...@@ -180,6 +180,20 @@ async fn gc_does_not_collect_pinned_task() { ...@@ -180,6 +180,20 @@ async fn gc_does_not_collect_pinned_task() {
tt.stop_and_wait().await; tt.stop_and_wait().await;
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn construction_pin_is_released_after_connecting_task() {
let (tt, _persistence_dir) = create_tt("construction_pin_is_released");
let task_id = turbo_tasks::run_once(tt.clone(), async move {
let task_id = TaskId::try_from(*leaf_task_id(7).read_strongly_consistent().await?)?;
anyhow::Ok(task_id)
})
.await
.unwrap();
assert_eq!(tt.backend().transient_ref_count_for_testing(task_id), 0);
tt.stop_and_wait().await;
}
/// Disposing a root task (as `RootTask::Drop` does when JS stops listening to a subscription) must /// 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 /// 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. /// safe after the backend has stopped, which `RootTask::Drop` relies on.
......
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