Unverified Commit 1c50e09d authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

turbo-tasks-backend: Enforce that tasks exist when accessing them (#96043)

Change the semantics of `task` so that it enforces that tasks actually
exist (either in memory or storage). For the rare cases we expect to be
creating tasks, have callers call `get_or_create_task`

generally creating a task is _racy_ so it is always `get_or_create` but
when doing things like updating the aggregation graph or reading cells,
we _know_ it must exist so assert it.

Currently this is a `debug_assert` but with the GC we may update it to a
full assert.

---------
Co-authored-by: 's avatarClaude Opus 4.8 (1M context) <noreply@anthropic.com>
parent 8a473f45
...@@ -330,6 +330,20 @@ impl TurboTasksBackend { ...@@ -330,6 +330,20 @@ impl TurboTasksBackend {
(had_new_data, counts) (had_new_data, counts)
} }
/// Opens `task` with the must-exist [`ExecuteContext::task`] and drops the guard. Test-only
/// hook to exercise the non-fabricating existence guarantee: this panics (debug builds) if
/// `task` exists in neither memory nor persistent storage (rather than fabricating a
/// blank).
#[doc(hidden)]
pub fn assert_task_exists_for_testing(
&self,
task: TaskId,
turbo_tasks: &TurboTasks<TurboTasksBackend>,
) {
let mut ctx = self.execute_context(turbo_tasks);
let _ = ctx.task(task, TaskDataCategory::All);
}
fn should_restore(&self) -> bool { fn should_restore(&self) -> bool {
self.options.storage_mode.is_some() self.options.storage_mode.is_some()
} }
...@@ -1793,7 +1807,9 @@ impl TurboTasksBackend { ...@@ -1793,7 +1807,9 @@ impl TurboTasksBackend {
turbo_tasks: &TurboTasks<TurboTasksBackend>, turbo_tasks: &TurboTasks<TurboTasksBackend>,
) -> String { ) -> String {
let mut ctx = self.execute_context(turbo_tasks); let mut ctx = self.execute_context(turbo_tasks);
let task = ctx.task(task_id, TaskDataCategory::Data); // Diagnostic path: the caller may name any id, including one that no longer exists, so this
// must not assert existence. A nonexistent task falls through to the "unknown" case below.
let task = ctx.open_or_create_task_storage(task_id, TaskDataCategory::Data);
if let Some(value) = task.get_persistent_task_type() { if let Some(value) = task.get_persistent_task_type() {
value.to_string() value.to_string()
} else if let Some(value) = task.get_transient_task_type() { } else if let Some(value) = task.get_transient_task_type() {
......
...@@ -79,7 +79,10 @@ impl ConnectChildOperation { ...@@ -79,7 +79,10 @@ impl ConnectChildOperation {
task: child_task_id, task: child_task_id,
}); });
} else { } else {
let mut child_task = ctx.task(child_task_id, TaskDataCategory::Meta); // First connect of this child: its id is minted but the storage entry may not exist
// yet, and concurrent connects race to be the one that first touches it.
let mut child_task =
ctx.open_or_create_task_storage(child_task_id, TaskDataCategory::Meta);
let has_output = child_task.has_output(); let has_output = child_task.has_output();
// An already constructed top-level task was made a root when it was first connected. // An already constructed top-level task was made a root when it was first connected.
// It may still be dirty and need to run; this only avoids repeating the idempotent // It may still be dirty and need to run; this only avoids repeating the idempotent
......
...@@ -369,12 +369,16 @@ impl TurboBackingStorage { ...@@ -369,12 +369,16 @@ impl TurboBackingStorage {
Ok(task_ids) Ok(task_ids)
} }
/// Reads the stored `category` for `task_id`.
///
/// `None` means the database had no key for it. That is distinct from `Some` of an empty
/// [`TaskStorage`] (a key that decoded to nothing), which is what lets a `MustExist` open tell
/// "absent everywhere" from "present but empty".
pub(crate) fn lookup_data( pub(crate) fn lookup_data(
&self, &self,
task_id: TaskId, task_id: TaskId,
category: SpecificTaskDataCategory, category: SpecificTaskDataCategory,
storage: &mut TaskStorage, ) -> Result<Option<TaskStorage>> {
) -> Result<()> {
let inner = &*self.inner; let inner = &*self.inner;
let Some(bytes) = inner let Some(bytes) = inner
.database .database
...@@ -383,12 +387,14 @@ impl TurboBackingStorage { ...@@ -383,12 +387,14 @@ impl TurboBackingStorage {
format!("Looking up task storage for {task_id} from database failed") format!("Looking up task storage for {task_id} from database failed")
})? })?
else { else {
return Ok(()); return Ok(None);
}; };
let mut storage = TaskStorage::default();
let mut decoder = new_turbo_bincode_decoder(bytes.borrow()); let mut decoder = new_turbo_bincode_decoder(bytes.borrow());
storage storage
.decode(category, &mut decoder) .decode(category, &mut decoder)
.map_err(|e| anyhow::anyhow!("Failed to decode {category:?}: {e:?}")) .with_context(|| format!("Failed to decode {category:?}"))?;
Ok(Some(storage))
} }
pub(crate) fn batch_lookup_data( pub(crate) fn batch_lookup_data(
......
...@@ -13,21 +13,17 @@ use turbo_tasks::{ ...@@ -13,21 +13,17 @@ use turbo_tasks::{
}; };
use turbo_tasks_backend::{BackendOptions, EvictionMode, GitVersionInfo, TurboTasksBackend}; use turbo_tasks_backend::{BackendOptions, EvictionMode, GitVersionInfo, TurboTasksBackend};
/// Creates a fresh per-call persistence directory rooted under /// Creates a fresh per-call persistence directory in the OS temp dir, with the test `name` as a
/// `CARGO_TARGET_TMPDIR/.cache/`, with the test `name` as a prefix so failed /// prefix so a leaked directory from a failed run is identifiable. The unique suffix from
/// runs are easy to find on disk. The unique suffix from `tempfile` lets /// `tempfile` lets repeated or concurrent runs coexist without trampling each other's database.
/// multiple processes (or repeated invocations of the same test) run in
/// parallel without trampling each other's database.
/// ///
/// The returned [`tempfile::TempDir`] cleans up its contents on drop, so /// The returned [`tempfile::TempDir`] cleans up its contents on drop, so callers should keep it
/// callers should keep it alive at least until the `TurboTasks` it backs has /// alive at least until the `TurboTasks` it backs has finished shutting down (so the final snapshot
/// finished shutting down (so the final snapshot can flush to disk). /// can flush to disk).
fn create_test_persistence_dir(name: &str) -> tempfile::TempDir { fn create_test_persistence_dir(name: &str) -> tempfile::TempDir {
let parent = std::path::PathBuf::from(format!("{}/.cache", env!("CARGO_TARGET_TMPDIR")));
std::fs::create_dir_all(&parent).unwrap();
tempfile::Builder::new() tempfile::Builder::new()
.prefix(&format!("{name}-")) .prefix(&format!("{name}-"))
.tempdir_in(&parent) .tempdir()
.unwrap() .unwrap()
} }
......
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