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

turbo-tasks: copy tasks on their first modification during a snapshot (#99649)

### What?

During a snapshot, a task's first modification now copies the task's
pre-mutation state whenever any of its categories is part of the
snapshot, whichever category the modification touches. A `None` marker
is inserted only for tasks with no modified category.

### Why?

`track_modification` used to decide based on the touched category only.
That let a snapshot persist inconsistent state. Take a task with dirty
meta and clean data:

1. The snapshot begins.
2. Data is modified. Data wasn't part of the snapshot, so only a `None`
marker is inserted.
3. Meta is modified. A marker is already present, so nothing is copied.
4. Persistence encodes the live meta, already mutated by step 3.

### How?

The design is otherwise unchanged: `*_modified_during_snapshot` bits,
`None` markers and the promotion in `end_snapshot` all stay, and nothing
new happens inside the operation exclusion. Only the first-modification
check in `track_modification_internal` changes, from "is the touched
category modified" to "is any category modified". The copy still encodes
with the live modified bits, so categories that weren't part of the
snapshot aren't persisted.

A regression test covers the sequence above. It checks that the
persisted item carries the pre-snapshot meta and no data, and that both
modifications are left for the next snapshot.

<!-- NEXT_JS_LLM -->


<!-- fleet 98a896e8-00fa-4f81-97f0-48bd49e2a9a2 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent 0c729c51
...@@ -2367,7 +2367,7 @@ mod cell_data_tracking_tests { ...@@ -2367,7 +2367,7 @@ mod cell_data_tracking_tests {
use crate::{ use crate::{
backend::{ backend::{
TaskDataCategory, TaskDataCategory,
storage::{Storage, StorageOptions, encode_task_contents}, storage::{Storage, StorageOptions, encode_snapshot_item, encode_task_contents},
storage_schema::TaskStorageAccessors, storage_schema::TaskStorageAccessors,
}, },
backing_storage::SnapshotItem, backing_storage::SnapshotItem,
...@@ -2590,6 +2590,68 @@ mod cell_data_tracking_tests { ...@@ -2590,6 +2590,68 @@ mod cell_data_tracking_tests {
assert_ne!(live, expected, "the shared cell value really changed"); assert_ne!(live, expected, "the shared cell value really changed");
} }
/// Regression test: a task that is part of the snapshot through one category must be copied
/// on its first modification during the snapshot, even when that modification touches a
/// category that isn't part of the snapshot.
///
/// Meta is modified before the snapshot, data is clean. During the snapshot, data is modified
/// first and then meta. If the data modification only inserted a `None` marker, the meta
/// modification would find a marker already present and not copy either, so the snapshot
/// would persist the already mutated meta.
#[tokio::test(flavor = "multi_thread")]
async fn modify_other_category_first_during_snapshot_preserves_snapshot_meta() {
let storage = Storage::new(StorageOptions::for_tests());
let task_id = persistent_task(1);
{
let mut g = guard_for(&storage, task_id);
let _ = g.track_modification(SpecificTaskDataCategory::Meta, "test");
assert!(!g.data_modified());
}
let expected_meta = {
let task = storage.access_mut(task_id);
encode_task_contents(
task_id,
&task,
SpecificTaskDataCategory::Meta,
&mut TurboBincodeBuffer::new(),
)
.unwrap()
};
let (snapshot_guard, has_modifications) = storage.start_snapshot();
assert!(has_modifications);
// Encodes the live state, like the backend does for tasks that weren't copied.
let process = |task_id: TaskId, task: &TaskStorage, buffer: &mut TurboBincodeBuffer| {
encode_snapshot_item(task_id, task, buffer).unwrap()
};
let shards = storage.take_snapshot(snapshot_guard, &process, &|_| {}, false);
{
let mut g = guard_for(&storage, task_id);
let cell = cell_of::<PersistableV>(0);
g.insert_cell_data(cell, dummy_ref(), persistence_of(&cell));
assert!(g.add_children(persistent_task(2)));
}
let items: Vec<_> = shards.into_iter().flatten().collect();
assert_eq!(items.len(), 1);
let SnapshotItem::Put { meta, data, .. } = &items[0] else {
panic!("expected a Put item");
};
assert_eq!(
meta.as_deref(),
Some(&expected_meta[..]),
"the snapshot must persist the meta from before the snapshot"
);
assert!(data.is_none(), "data wasn't modified before the snapshot");
// Both modifications made during the snapshot are left for the next snapshot.
let task = storage.access_mut(task_id);
assert!(task.flags.meta_modified());
assert!(task.flags.data_modified());
assert!(!task.flags.any_modified_during_snapshot());
}
/// A pre-encoded (copy-on-write) item carries the cache size stats of the state it encoded, /// A pre-encoded (copy-on-write) item carries the cache size stats of the state it encoded,
/// not the live state after the mutation that triggered the copy. /// not the live state after the mutation that triggered the copy.
#[cfg(feature = "print_cache_item_size")] #[cfg(feature = "print_cache_item_size")]
......
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