Unverified Commit fed3b4be authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Simplify context-bound task guard signatures

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 6703879a
...@@ -909,8 +909,9 @@ impl TurboTasksBackend { ...@@ -909,8 +909,9 @@ impl TurboTasksBackend {
drop(task); drop(task);
} }
// End every task guard's shared context borrow before executing queued work or // The `if let` scrutinee can hold a task guard until the whole branch ends, even
// converting errors through mutable context access. // after dropping its binding. Finish that scope and release the outer optional guard
// before borrowing the context mutably for queue execution or error conversion.
drop(reader_task); drop(reader_task);
if let Some(queue) = queue_to_execute { if let Some(queue) = queue_to_execute {
queue.execute(&mut ctx); queue.execute(&mut ctx);
......
...@@ -114,20 +114,16 @@ pub trait ExecuteContext<'e>: Sized { ...@@ -114,20 +114,16 @@ pub trait ExecuteContext<'e>: Sized {
/// ///
/// The check applies only to persistent tasks; a `MustExist` open of a transient id falls /// The check applies only to persistent tasks; a `MustExist` open of a transient id falls
/// through to create. See `ExecuteContextImpl::open_task`. /// through to create. See `ExecuteContextImpl::open_task`.
fn task<'ctx>( fn task(&self, task_id: TaskId, category: TaskDataCategory) -> Self::TaskGuardImpl<'_>;
&'ctx self,
task_id: TaskId,
category: TaskDataCategory,
) -> Self::TaskGuardImpl<'ctx>;
/// Opens a task that may legitimately be gone, returning `None` if it is. /// Opens a task that may legitimately be gone, returning `None` if it is.
/// ///
/// Gone covers both a task that exists nowhere and one that is soft-deleted: the caller cannot /// Gone covers both a task that exists nowhere and one that is soft-deleted: the caller cannot
/// tell those apart, since only the timing of the next eviction separates them. /// tell those apart, since only the timing of the next eviction separates them.
fn try_get_task<'ctx>( fn try_get_task(
&'ctx self, &self,
task_id: TaskId, task_id: TaskId,
category: TaskDataCategory, category: TaskDataCategory,
) -> Option<Self::TaskGuardImpl<'ctx>>; ) -> Option<Self::TaskGuardImpl<'_>>;
/// Opens a task, materializing an in-memory storage entry for it if one is not resident yet /// Opens a task, materializing an in-memory storage entry for it if one is not resident yet
/// (inserting a blank, then restoring `category` from disk if present). Use only where the /// (inserting a blank, then restoring `category` from disk if present). Use only where the
/// task's storage may not be resident: the first connect of a freshly-minted child (threads can /// task's storage may not be resident: the first connect of a freshly-minted child (threads can
...@@ -136,11 +132,11 @@ pub trait ExecuteContext<'e>: Sized { ...@@ -136,11 +132,11 @@ pub trait ExecuteContext<'e>: Sized {
/// This creates *storage for* an already-minted `TaskId`; it does not mint one. Compare /// This creates *storage for* an already-minted `TaskId`; it does not mint one. Compare
/// `TurboTasksBackend::get_or_create_task`, which takes a function and arguments and returns a /// `TurboTasksBackend::get_or_create_task`, which takes a function and arguments and returns a
/// new `TaskId`. /// new `TaskId`.
fn open_or_create_task_storage<'ctx>( fn open_or_create_task_storage(
&'ctx self, &self,
task_id: TaskId, task_id: TaskId,
category: TaskDataCategory, category: TaskDataCategory,
) -> Self::TaskGuardImpl<'ctx>; ) -> Self::TaskGuardImpl<'_>;
/// Prepares (as in fetches from persistent storage) a list of tasks. /// Prepares (as in fetches from persistent storage) a list of tasks.
/// The iterator should not have duplicates, as this would cause over-fetching. /// The iterator should not have duplicates, as this would cause over-fetching.
fn prepare_tasks( fn prepare_tasks(
...@@ -181,12 +177,12 @@ pub trait ExecuteContext<'e>: Sized { ...@@ -181,12 +177,12 @@ pub trait ExecuteContext<'e>: Sized {
/// Opens two tasks that must **already exist** under a single lock acquisition (to atomically /// Opens two tasks that must **already exist** under a single lock acquisition (to atomically
/// read/mutate an edge between them). Both ids are opened `MustExist` — an edge only exists /// read/mutate an edge between them). Both ids are opened `MustExist` — an edge only exists
/// between already-materialized tasks. /// between already-materialized tasks.
fn task_pair<'ctx>( fn task_pair(
&'ctx self, &self,
task_id1: TaskId, task_id1: TaskId,
task_id2: TaskId, task_id2: TaskId,
category: TaskDataCategory, category: TaskDataCategory,
) -> (Self::TaskGuardImpl<'ctx>, Self::TaskGuardImpl<'ctx>); ) -> (Self::TaskGuardImpl<'_>, Self::TaskGuardImpl<'_>);
fn schedule_task(&self, task: &impl TaskGuard, parent_priority: TaskPriority); fn schedule_task(&self, task: &impl TaskGuard, parent_priority: TaskPriority);
fn get_current_task_priority(&self) -> TaskPriority; fn get_current_task_priority(&self) -> TaskPriority;
fn operation_suspend_point<T>(&mut self, op: &T) fn operation_suspend_point<T>(&mut self, op: &T)
...@@ -350,12 +346,12 @@ impl<'e> ExecuteContextImpl<'e> { ...@@ -350,12 +346,12 @@ impl<'e> ExecuteContextImpl<'e> {
} }
} }
fn open_task<'ctx>( fn open_task(
&'ctx self, &self,
task_id: TaskId, task_id: TaskId,
category: TaskDataCategory, category: TaskDataCategory,
access: TaskAccess, access: TaskAccess,
) -> Option<TaskGuardImpl<'ctx>> { ) -> Option<TaskGuardImpl<'_>> {
self.task_lock_counter.acquire(); self.task_lock_counter.acquire();
let mut task = OpenedTask::Owned(self.backend.storage.access_entry_mut(task_id)); let mut task = OpenedTask::Owned(self.backend.storage.access_entry_mut(task_id));
...@@ -1114,28 +1110,24 @@ impl<'e> ExecuteContext<'e> for ExecuteContextImpl<'e> { ...@@ -1114,28 +1110,24 @@ impl<'e> ExecuteContext<'e> for ExecuteContextImpl<'e> {
} }
} }
fn task<'ctx>( fn task(&self, task_id: TaskId, category: TaskDataCategory) -> Self::TaskGuardImpl<'_> {
&'ctx self,
task_id: TaskId,
category: TaskDataCategory,
) -> Self::TaskGuardImpl<'ctx> {
self.open_task(task_id, category, TaskAccess::MustExist) self.open_task(task_id, category, TaskAccess::MustExist)
.expect("a MustExist open either yields a task or panics") .expect("a MustExist open either yields a task or panics")
} }
fn try_get_task<'ctx>( fn try_get_task(
&'ctx self, &self,
task_id: TaskId, task_id: TaskId,
category: TaskDataCategory, category: TaskDataCategory,
) -> Option<Self::TaskGuardImpl<'ctx>> { ) -> Option<Self::TaskGuardImpl<'_>> {
self.open_task(task_id, category, TaskAccess::AllowMissing) self.open_task(task_id, category, TaskAccess::AllowMissing)
} }
fn open_or_create_task_storage<'ctx>( fn open_or_create_task_storage(
&'ctx self, &self,
task_id: TaskId, task_id: TaskId,
category: TaskDataCategory, category: TaskDataCategory,
) -> Self::TaskGuardImpl<'ctx> { ) -> Self::TaskGuardImpl<'_> {
self.open_task(task_id, category, TaskAccess::MaybeCreate) self.open_task(task_id, category, TaskAccess::MaybeCreate)
.expect("a MaybeCreate open always yields a task") .expect("a MaybeCreate open always yields a task")
} }
...@@ -1177,12 +1169,12 @@ impl<'e> ExecuteContext<'e> for ExecuteContextImpl<'e> { ...@@ -1177,12 +1169,12 @@ impl<'e> ExecuteContext<'e> for ExecuteContextImpl<'e> {
); );
} }
fn task_pair<'ctx>( fn task_pair(
&'ctx self, &self,
task_id1: TaskId, task_id1: TaskId,
task_id2: TaskId, task_id2: TaskId,
category: TaskDataCategory, category: TaskDataCategory,
) -> (Self::TaskGuardImpl<'ctx>, Self::TaskGuardImpl<'ctx>) { ) -> (Self::TaskGuardImpl<'_>, Self::TaskGuardImpl<'_>) {
self.task_lock_counter.acquire_multiple(2); self.task_lock_counter.acquire_multiple(2);
let (mut task1, mut task2) = self.backend.storage.access_pair_mut(task_id1, task_id2); let (mut task1, mut task2) = self.backend.storage.access_pair_mut(task_id1, task_id2);
...@@ -2542,27 +2534,3 @@ mod cell_data_tracking_tests { ...@@ -2542,27 +2534,3 @@ mod cell_data_tracking_tests {
); );
} }
} }
#[cfg(test)]
mod task_guard_lifetime_tests {
#[cfg(debug_assertions)]
use super::TaskLockCounter;
#[cfg(debug_assertions)]
#[test]
#[cfg_attr(
target_family = "wasm",
ignore = "WASI uses panic=abort; catch_unwind cannot catch the expected panic"
)]
fn debug_counter_rejects_nested_independent_locks_and_tracks_pairs() {
let counter = TaskLockCounter::new();
counter.acquire();
assert!(
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| counter.acquire())).is_err()
);
counter.release();
counter.acquire_multiple(2);
counter.release();
counter.release();
}
}
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