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 {
let shard = get_shard(&self.storage.task_cache, hash);
let mut ctx = self.execute_context(turbo_tasks);
let mut created_new = false;
// 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
// may re-enter task_cache with a write lock, so we must not hold a write lock here.
......@@ -1818,7 +1819,12 @@ impl TurboTasksBackend {
get_in_shard(shard, hash, |k| k.eq_components(native_fn, this, arg_ref))
{
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;
}
......@@ -1885,6 +1891,7 @@ impl TurboTasksBackend {
// The entry closure has returned, so the task_cache shard lock is released before
// cache tracking or aggregation updates can re-enter the backend.
created_new = created;
if created {
self.track_cache_miss_by_fn(native_fn);
// Update the aggregation number before connecting the child. We don't need this on
......@@ -1916,7 +1923,9 @@ impl TurboTasksBackend {
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
}
......@@ -3532,7 +3541,12 @@ impl TurboTasksBackend {
turbo_tasks: &TurboTasks<TurboTasksBackend>,
) {
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 {
......
......@@ -318,6 +318,7 @@ pub enum AggregationUpdateJob {
// upon attempted serialization) similar to #[serde(skip)] on variants
#[bincode(skip, default = "unreachable_decode")]
task: TaskId,
release_construction_ref: bool,
},
/// Increases the active counters of the tasks
IncreaseActiveCounts {
......@@ -1498,12 +1499,17 @@ impl AggregationUpdateQueue {
}
}
}
AggregationUpdateJob::IncreaseActiveCount { task } => {
self.increase_active_count(ctx, task);
AggregationUpdateJob::IncreaseActiveCount {
task,
release_construction_ref,
} => {
self.increase_active_count(ctx, task, release_construction_ref);
}
AggregationUpdateJob::IncreaseActiveCounts { mut task_ids } => {
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() {
self.jobs.push_front(AggregationUpdateJobItem::new(
AggregationUpdateJob::IncreaseActiveCounts { task_ids },
......@@ -1760,7 +1766,10 @@ impl AggregationUpdateQueue {
let has_active_count =
upper.get_activeness().is_some_and(|a| a.active_counter > 0);
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
......@@ -3057,6 +3066,7 @@ impl AggregationUpdateQueue {
if has_active_count {
self.push(AggregationUpdateJob::IncreaseActiveCount {
task: new_follower_id,
release_construction_ref: false,
});
}
......@@ -3216,7 +3226,12 @@ impl AggregationUpdateQueue {
/// Increases the active count of a task.
///
/// 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")]
let _span = trace_span!("increase active count").entered();
......@@ -3233,6 +3248,9 @@ impl AggregationUpdateQueue {
let is_new = state.is_empty();
let is_positive_now = state.increment_active_counter();
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
if is_empty {
task.take_activeness();
......
......@@ -58,6 +58,11 @@ pub(super) fn resurrect_deleted<'e, C: ExecuteContext<'e>>(
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)]
#[allow(clippy::large_enum_variant)]
pub enum ConnectChildOperation {
......@@ -72,6 +77,7 @@ impl ConnectChildOperation {
pub fn run(
parent_task_id: Option<TaskId>,
child_task_id: TaskId,
release_construction_ref: bool,
mut ctx: impl ExecuteContext<'_>,
) {
if let Some(parent_task_id) = parent_task_id {
......@@ -85,6 +91,10 @@ impl ConnectChildOperation {
// Quick skip if the child was already connected before
// We defer the insert until after the aggregation queue is processed.
if new_children.contains(&child_task_id) {
drop(parent_task);
if release_construction_ref {
self::release_construction_ref(child_task_id, &mut ctx);
}
return;
}
......@@ -98,6 +108,10 @@ impl ConnectChildOperation {
unreachable!();
};
new_children.insert(child_task_id);
drop(parent_task);
if release_construction_ref {
self::release_construction_ref(child_task_id, &mut ctx);
}
return;
}
}
......@@ -118,6 +132,7 @@ impl ConnectChildOperation {
}
queue.push(AggregationUpdateJob::IncreaseActiveCount {
task: child_task_id,
release_construction_ref,
});
} else {
// First connect of this child: its id is minted but the storage entry may not exist
......@@ -152,6 +167,9 @@ impl ConnectChildOperation {
{
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() {
......
......@@ -290,6 +290,7 @@ impl Storage {
let mut task = self.access_mut(task_id);
task.flags.set_restored(TaskDataCategory::All);
task.flags.set_new_task(true);
task.gc_pin_for_construction();
if let Some(task_type) = task_type {
task.set_persistent_task_type(task_type);
if !task_id.is_transient() {
......@@ -1153,6 +1154,18 @@ mod tests {
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
/// silently skip items via the "encoding failed" error path.
fn dummy_process(
......
......@@ -858,6 +858,12 @@ impl TaskStorage {
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
/// pins, aggregation edges, or dependency edges.
///
......
......@@ -180,6 +180,20 @@ async fn gc_does_not_collect_pinned_task() {
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
/// 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.
......
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