Unverified Commit eb85307e authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[lts-active] turbo-tasks snapshot consistency fixes (#99644, #99732, #99649) (#100002)

## Summary

Backports three snapshot-consistency fixes to `releases/lts/active`, in
canary order:

- #99644 turbo-tasks: bincode-encode copy-on-write snapshots instead of
cloning
- #99732 turbo-tasks: disable persisting after a failed snapshot
- #99649 turbo-tasks: copy tasks on their first modification during a
snapshot

16.4.0 can persist inconsistent task state when a mutation races a
background snapshot. The main case is #99649. A task that is meta-dirty
and data-clean when the snapshot starts, then writes data before meta,
has its live (post-mutation) meta persisted. One example is an
invalidated task that re-runs while an idle snapshot is writing.
`children` and `parent_count` are both meta, so the cache on disk can
list a child edge without the child's matching `parent_count`. `next
dev` doesn't persist on shutdown, so the next session restores that
state. When the parent later drops the edge, the server aborts with
`parent_count underflow: decremented below the number of persistent
parents`. We have one report of this from a 16.4.0 dev run.

The cherry-picks apply without conflicts. The six touched files are
identical to `canary` at #99649.

## Verification

- `cargo test -p turbo-tasks-backend --lib` on this branch: 119 passed.
This includes #99649's regression test
`modify_other_category_first_during_snapshot_preserves_snapshot_meta`
and #99732's `failed_persist_disables_persisting`.
- Locally on macOS this needed
`CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER=cc` and `RUSTFLAGS` set to the
repo's shared flags without `--icf=all`. The branch's pinned
`nightly-2026-08-20` ships a `rust-lld` that can't read the current
macOS SDK's `.tbd` files. This is unrelated to the change, and CI uses
its normal config.

<!-- NEXT_JS_LLM -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 7217011a
...@@ -21,6 +21,9 @@ pub enum SnapshotItem { ...@@ -21,6 +21,9 @@ pub enum SnapshotItem {
data: Option<TurboBincodeBuffer>, data: Option<TurboBincodeBuffer>,
/// Task type for new tasks that need to be added to the task cache /// Task type for new tasks that need to be added to the task cache
task_type_hash: Option<TaskTypeHash>, task_type_hash: Option<TaskTypeHash>,
/// Cache size statistics captured when the item was encoded.
#[cfg(feature = "print_cache_item_size")]
stats: Box<SnapshotItemStats>,
}, },
Delete { Delete {
task_id: TaskId, task_id: TaskId,
...@@ -30,6 +33,18 @@ pub enum SnapshotItem { ...@@ -30,6 +33,18 @@ pub enum SnapshotItem {
}, },
} }
/// Per-task cache size statistics, captured by `encode_snapshot_item` from the same task state
/// that was encoded, so pre-encoded (copy-on-write) items report what was actually persisted.
#[cfg(feature = "print_cache_item_size")]
#[derive(Default)]
pub struct SnapshotItemStats {
/// The task name, used as the stats grouping key.
pub task_name: String,
pub counts: crate::backend::storage_schema::MetaCounts,
/// Size of the encoded task output, if any.
pub output_size: usize,
}
impl SnapshotItem { impl SnapshotItem {
/// The task this item persists or tombstones. (Currently only used by tests, which assert on /// The task this item persists or tombstones. (Currently only used by tests, which assert on
/// the id of items yielded by the snapshot iterator.) /// the id of items yielded by the snapshot iterator.)
......
...@@ -276,6 +276,7 @@ impl TurboBackingStorage { ...@@ -276,6 +276,7 @@ impl TurboBackingStorage {
meta, meta,
data, data,
task_type_hash, task_type_hash,
..
} => { } => {
let key = IntKey::new(*task_id); let key = IntKey::new(*task_id);
let key = key.as_ref(); let key = key.as_ref();
......
...@@ -898,18 +898,6 @@ impl GroupedFields { ...@@ -898,18 +898,6 @@ impl GroupedFields {
// Code Generation Helpers // Code Generation Helpers
// ============================================================================= // =============================================================================
/// Generate inline field clone assignments: `snapshot.field = self.field.clone();`
fn gen_clone_inline_fields<'a>(fields: impl Iterator<Item = &'a FieldInfo>) -> Vec<TokenStream> {
fields
.map(|field| {
let field_name = &field.field_name;
quote! {
snapshot.#field_name = self.#field_name.clone();
}
})
.collect()
}
fn gen_restore_inline_field(field: &FieldInfo) -> TokenStream { fn gen_restore_inline_field(field: &FieldInfo) -> TokenStream {
let field_name = &field.field_name; let field_name = &field.field_name;
if !field.filter_transient { if !field.filter_transient {
...@@ -3844,25 +3832,6 @@ fn generate_decode_lazy_fields(fields: &[&FieldInfo]) -> TokenStream { ...@@ -3844,25 +3832,6 @@ fn generate_decode_lazy_fields(fields: &[&FieldInfo]) -> TokenStream {
} }
} }
/// Generate clone inline statements for a category.
fn gen_clone_inline_for_category(
grouped_fields: &GroupedFields,
category: Category,
) -> Vec<TokenStream> {
gen_clone_inline_fields(grouped_fields.persistent_inline(category))
}
/// Generate clone lazy match arms for a category.
fn gen_clone_lazy_arms_for_category(
grouped_fields: &GroupedFields,
category: Category,
) -> Vec<TokenStream> {
gen_lazy_match_arms(grouped_fields.persistent_lazy(category), |_, field| {
let variant_name = &field.variant_name;
quote! { snapshot.lazy.push(LazyField::#variant_name(data.clone())); }
})
}
/// Generate restore inline statements for a category. /// Generate restore inline statements for a category.
fn gen_restore_inline_for_category( fn gen_restore_inline_for_category(
grouped_fields: &GroupedFields, grouped_fields: &GroupedFields,
...@@ -3874,11 +3843,9 @@ fn gen_restore_inline_for_category( ...@@ -3874,11 +3843,9 @@ fn gen_restore_inline_for_category(
.collect() .collect()
} }
/// Generate snapshot clone and restore methods for TaskStorage. /// Generate snapshot restore methods for TaskStorage.
/// ///
/// Generates: /// Generates:
/// - `clone_meta_snapshot(&self) -> TaskStorage` - Clone only persistent meta fields
/// - `clone_data_snapshot(&self) -> TaskStorage` - Clone only persistent data fields
/// - `restore_from(&mut self, source, category)` - Restore data by category from decoded storage /// - `restore_from(&mut self, source, category)` - Restore data by category from decoded storage
/// - `restore_meta_from(&mut self, source)` - Restore meta fields from source /// - `restore_meta_from(&mut self, source)` - Restore meta fields from source
/// - `restore_data_from(&mut self, source)` - Restore data fields from source /// - `restore_data_from(&mut self, source)` - Restore data fields from source
...@@ -3886,14 +3853,8 @@ fn gen_restore_inline_for_category( ...@@ -3886,14 +3853,8 @@ fn gen_restore_inline_for_category(
fn generate_snapshot_restore_methods(grouped_fields: &GroupedFields) -> TokenStream { fn generate_snapshot_restore_methods(grouped_fields: &GroupedFields) -> TokenStream {
let has_meta_flags = grouped_fields.persisted_meta_flags().next().is_some(); let has_meta_flags = grouped_fields.persisted_meta_flags().next().is_some();
let has_data_flags = grouped_fields.persisted_data_flags().next().is_some(); let has_data_flags = grouped_fields.persisted_data_flags().next().is_some();
let has_any_flags = has_meta_flags || has_data_flags;
// Generate field operations by category // Generate field operations by category
let clone_meta_inline = gen_clone_inline_for_category(grouped_fields, Category::Meta);
let clone_data_inline = gen_clone_inline_for_category(grouped_fields, Category::Data);
let clone_meta_lazy_arms = gen_clone_lazy_arms_for_category(grouped_fields, Category::Meta);
let clone_data_lazy_arms = gen_clone_lazy_arms_for_category(grouped_fields, Category::Data);
let restore_meta_inline = gen_restore_inline_for_category(grouped_fields, Category::Meta); let restore_meta_inline = gen_restore_inline_for_category(grouped_fields, Category::Meta);
let restore_data_inline = gen_restore_inline_for_category(grouped_fields, Category::Data); let restore_data_inline = gen_restore_inline_for_category(grouped_fields, Category::Data);
...@@ -3912,15 +3873,6 @@ fn generate_snapshot_restore_methods(grouped_fields: &GroupedFields) -> TokenStr ...@@ -3912,15 +3873,6 @@ fn generate_snapshot_restore_methods(grouped_fields: &GroupedFields) -> TokenStr
.map(|(idx, f)| gen_restore_lazy_merge_arm(f, idx as u8)) .map(|(idx, f)| gen_restore_lazy_merge_arm(f, idx as u8))
.collect(); .collect();
let clone_all_flags = if has_any_flags {
quote! {
// Clone all persisted flags
snapshot.flags.set_persisted_bits(self.flags.persisted_bits());
}
} else {
quote! {}
};
// Generate flags handling for restore - per category // Generate flags handling for restore - per category
let restore_meta_flags = if has_meta_flags { let restore_meta_flags = if has_meta_flags {
quote! { quote! {
...@@ -3943,34 +3895,6 @@ fn generate_snapshot_restore_methods(grouped_fields: &GroupedFields) -> TokenStr ...@@ -3943,34 +3895,6 @@ fn generate_snapshot_restore_methods(grouped_fields: &GroupedFields) -> TokenStr
quote! { quote! {
#[automatically_derived] #[automatically_derived]
impl TaskStorage { impl TaskStorage {
/// Create a snapshot containing all persistent fields
pub fn clone_snapshot(&self) -> TaskStorage {
let mut snapshot = TaskStorage::new();
// Clone inline meta fields
#(#clone_meta_inline)*
// Clone inline data fields
#(#clone_data_inline)*
#clone_all_flags
// Clone all persistent lazy fields (both meta and data).
// (No pre-`reserve`: the schema has ≤24 lazy fields, so at most 3 grows
// (0→4→8→16→24) total — cheaper than complicating the public API surface
// of `TinyVec`.)
for field in &self.lazy {
match field {
#(#clone_data_lazy_arms)*
#(#clone_meta_lazy_arms)*
// Skip transient fields
_ => {}
}
}
snapshot
}
/// Restore persisted data from a decoded TaskStorage. /// Restore persisted data from a decoded TaskStorage.
/// ///
/// This is used during restore operations to copy decoded persisted data /// This is used during restore operations to copy decoded persisted data
......
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