Recovery: Save cancels a document's writes to any folder

Save, Revert and Close now cancel the document's background writes to
every folder, including one named with `file.recovery.save {dir}` that
is not in its entries yet, and remove a snapshot such a write already
published even when its result has not been taken. A cancel that comes
after the write published retires its result, so the removed snapshot
is not recorded as a checkpoint after a later edit. Cancels apply only
to writes the same session queued.

The test that closes a run's lock and expects it free no longer runs
while a test starts its child process, which held a copy of the lock
until the child ran its program.
This commit is contained in:
dotCooCoo committed 2026-11-11 20:24:55 -08:00
1 parent 43088a87c0
commit 7b91cad608
2 files changed
+146 -14

No files matched your search

+57 -7
View File
@@ -514,6 +514,15 @@ pub(crate) fn discard_document(s: &mut Session, index: usize, default_dir: Optio
{
entries.push((dir.to_path_buf(), uid));
}
// Background writes still queued or running for it, in any folder (one named by
// `file.recovery.save {dir}` is not in its entries until the write's result is taken): none
// may publish after this point, and what one already published is removed below.
#[cfg(not(target_arch = "wasm32"))]
for dir in worker::cancel_document(uid, s.recovery_owner.0) {
if !entries.iter().any(|(entry_dir, entry_uid)| *entry_uid == uid && same_directory(entry_dir, &dir)) {
entries.push((dir, uid));
}
}
for (dir, uid) in entries {
// Session ownership governs deletion as well as writes: never consume another live
// document's only source merely because its old uid matches this unrelated document.
@@ -861,7 +870,7 @@ pub(crate) fn finished(_s: &mut Session) -> Finished {
/// gigabytes of placed files is written without stopping the interface.
#[cfg(not(target_arch = "wasm32"))]
mod worker {
use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::mpsc::{Receiver, Sender};
use std::sync::{Condvar, Mutex, OnceLock};
@@ -884,8 +893,9 @@ mod worker {
pub(super) done: Mutex<Receiver<Done>>,
/// Results taken from `done` by a session that did not queue them, kept for their own.
pub(super) undelivered: Mutex<Vec<Done>>,
/// Documents queued or being written: a second write of one still holds nothing newer.
running: Mutex<BTreeSet<(PathBuf, u64)>>,
/// Documents queued or being written, with the session that queued each: a second write
/// of one still holds nothing newer.
running: Mutex<BTreeMap<(PathBuf, u64), u64>>,
/// A document saved, reverted or closed while its write was queued or running: the
/// snapshot it is producing must not be published.
cancelled: Mutex<BTreeSet<(PathBuf, u64)>>,
@@ -928,7 +938,7 @@ mod worker {
// A rename under way finishes first, so the caller's cleanup comes after it; a later
// one sees the mark.
let _publishing = super::PUBLISH.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
if !self.running.lock().is_ok_and(|running| running.contains(&key)) {
if !self.running.lock().is_ok_and(|running| running.contains_key(&key)) {
return;
}
if let Ok(mut cancelled) = self.cancelled.lock() {
@@ -936,6 +946,35 @@ mod worker {
}
}
/// [`Writer::cancel`] for every folder a write of `uid` queued by session `owner` is queued
/// or running for. Returns those folders, and the folders of its finished writes whose
/// results nobody has taken.
fn cancel_document(&self, uid: u64, owner: u64) -> Vec<PathBuf> {
// Held while results are read: the writer checks for a cancel and sends its result
// under it, so a result sent after this sees the cancel.
let _publishing = super::PUBLISH.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let keys: Vec<(PathBuf, u64)> = self
.running
.lock()
.map(|running| running.iter().filter(|((_, u), o)| *u == uid && **o == owner).map(|(key, _)| key.clone()).collect())
.unwrap_or_default();
if let Ok(mut cancelled) = self.cancelled.lock() {
cancelled.extend(keys.iter().cloned());
}
let mut dirs: Vec<PathBuf> = keys.into_iter().map(|(dir, _)| dir).collect();
// Results stay queued for their session; only their folders are read here.
let done = self.done.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let mut undelivered = self.undelivered.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
undelivered.extend(done.try_iter());
for d in undelivered.iter_mut().filter(|d| d.uid == uid && d.owner == owner && d.published) {
dirs.push(d.dir.clone());
// The caller removes that snapshot: its result must not record it as a checkpoint
// once the document has changed again.
d.published = false;
}
dirs
}
fn cancelled(&self, dir: &Path, uid: u64) -> bool {
self.cancelled.lock().is_ok_and(|cancelled| cancelled.contains(&(dir.to_path_buf(), uid)))
}
@@ -954,7 +993,12 @@ mod worker {
// The result before the document reads as idle: a writer that is not busy
// has already delivered it, so the interface never stops polling with an
// answer still on its way.
let sent = done_tx.send(Done { dir: dir.clone(), uid, error, published, owner }).is_ok();
let sent = {
let _publishing = super::PUBLISH.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
// Cancelled after it was published: the canceller removes the snapshot.
let published = published && !cancelled(&dir, uid);
done_tx.send(Done { dir: dir.clone(), uid, error, published, owner }).is_ok()
};
if let Some(w) = WRITER.get().and_then(Option::as_ref) {
w.finish(&dir, uid);
}
@@ -967,7 +1011,7 @@ mod worker {
jobs,
done: Mutex::new(done_rx),
undelivered: Mutex::new(Vec::new()),
running: Mutex::new(BTreeSet::new()),
running: Mutex::new(BTreeMap::new()),
cancelled: Mutex::new(BTreeSet::new()),
idle: Condvar::new(),
})
@@ -987,6 +1031,11 @@ mod worker {
}
}
/// Do not publish any write of `uid` in flight (see [`Writer::cancel_document`]).
pub(super) fn cancel_document(uid: u64, owner: u64) -> Vec<PathBuf> {
started().map(|w| w.cancel_document(uid, owner)).unwrap_or_default()
}
/// Whether that write is already marked as cancelled (see [`Writer::cancelled`]).
pub(super) fn cancelled(dir: &Path, uid: u64) -> bool {
started().is_some_and(|w| w.cancelled(dir, uid))
@@ -1016,9 +1065,10 @@ mod worker {
let w = writer()?;
let mut running = w.running.lock().ok()?;
let key = (entry.dir.clone(), entry.uid);
if !running.insert(key.clone()) {
if running.contains_key(&key) {
return Some(false);
}
running.insert(key.clone(), entry.owner);
// A write is wanted again: whatever cancelled the last one does not apply to this one.
if let Ok(mut cancelled) = w.cancelled.lock() {
cancelled.remove(&key);
+89 -7
View File
@@ -184,12 +184,57 @@ fn a_document_saved_while_its_write_is_held_leaves_no_snapshot() {
let saved = dir.path().join("saved.designcraft");
s.execute("file.saveAs", &json!({"path": saved.to_string_lossy()})).unwrap();
held.release();
assert_eq!(finished_writes(&mut s), 0);
// Before the session takes the result: a crash now must not find the stale snapshot.
testing::wait_until_idle();
assert!(!files(dir.path(), uid).0.exists(), "the stale snapshot was not published");
assert!(!files(dir.path(), uid).1.exists(), "nor its metadata");
assert_eq!(finished_writes(&mut s), 0);
assert!(list(dir.path()).is_empty());
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_document_saved_while_its_write_to_a_named_folder_is_held_leaves_no_snapshot() {
// A folder given to `file.recovery.save` is not the session's, and is not in the document's
// entries until the write's result is taken.
let _turn = background();
let named = tempfile::tempdir().unwrap();
let mut s = dirty_session();
let uid = s.docs[0].uid;
let held = testing::hold_next_write(named.path());
let queued = s.execute("file.recovery.save", &json!({"background": true, "dir": named.path().to_string_lossy()})).unwrap();
assert_eq!(queued["queued"], 1, "{queued}");
held.wait_until_held();
let saved = named.path().join("saved.designcraft");
s.execute("file.saveAs", &json!({"path": saved.to_string_lossy()})).unwrap();
held.release();
// Before the session takes the result: a crash now must not find the stale snapshot.
testing::wait_until_idle();
assert!(!files(named.path(), uid).0.exists(), "the stale snapshot was not published");
assert_eq!(finished_writes(&mut s), 0);
assert!(list(named.path()).is_empty());
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn saving_removes_a_finished_write_to_a_named_folder_whose_result_was_not_taken() {
let _turn = background();
let named = tempfile::tempdir().unwrap();
let mut s = dirty_session();
let uid = s.docs[0].uid;
s.execute("file.recovery.save", &json!({"background": true, "dir": named.path().to_string_lossy()})).unwrap();
testing::wait_until_idle();
assert!(files(named.path(), uid).0.exists(), "the write finished");
let saved = named.path().join("saved.designcraft");
s.execute("file.saveAs", &json!({"path": saved.to_string_lossy()})).unwrap();
assert!(!files(named.path(), uid).0.exists(), "Save removed the snapshot before its result was taken");
// Edited again before the result is taken: the removed snapshot is not its checkpoint.
s.execute("frame.create", &json!({"rect": [20, 20, 60, 60]})).unwrap();
assert_eq!(finished_writes(&mut s), 0);
assert!(!s.docs[0].recovery_entries.iter().any(|(d, _)| same_directory(d, named.path())), "no checkpoint for a removed snapshot");
assert!(list(named.path()).is_empty());
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn a_background_result_goes_to_the_session_that_queued_it() {
@@ -211,6 +256,28 @@ fn a_background_result_goes_to_the_session_that_queued_it() {
discard(dir.path(), uid);
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn saving_in_one_session_leaves_another_sessions_write_alone() {
let _turn = background();
let dir = tempfile::tempdir().unwrap();
let mut queued_by = dirty_session();
queued_by.recovery_dir = Some(dir.path().to_path_buf());
let uid = queued_by.docs[0].uid;
// The same uid in another session (recovered documents keep the uids of their entries).
let mut other = dirty_session();
other.docs[0].uid = uid;
let held = testing::hold_next_write(dir.path());
queued_by.execute("file.recovery.save", &json!({"background": true})).unwrap();
held.wait_until_held();
let elsewhere = tempfile::tempdir().unwrap();
other.execute("file.saveAs", &json!({"path": elsewhere.path().join("other.designcraft").to_string_lossy()})).unwrap();
held.release();
assert_eq!(finished_writes(&mut queued_by), 1);
assert!(files(dir.path(), uid).0.exists(), "the other session's save did not cancel this write");
discard(dir.path(), uid);
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn results_for_a_session_that_is_gone_are_not_kept() {
@@ -709,6 +776,11 @@ fn restart_recovery_child() {
}
}
/// A child process starts with a copy of every open file of this one until it runs its program, so
/// a lock file closed meanwhile stays locked: a test that closes a lock and expects it free does
/// not run while a child is started.
static SPAWN: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn run_restart_case(mode: &str) {
let source = restart_fixture();
if mode == "max_uid" {
@@ -717,12 +789,20 @@ fn run_restart_case(mode: &str) {
std::fs::rename(doc, max_doc).unwrap();
std::fs::rename(meta, max_meta).unwrap();
}
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "recovery::tests::restart_recovery_child", "--ignored", "--nocapture"])
.env("DESIGNCRAFT_TEST_RECOVERY_SOURCE", source.path())
.env("DESIGNCRAFT_TEST_RECOVERY_MODE", mode)
.output()
.unwrap();
// `spawn` returns once the child runs its program, and its copies of this process's files
// are closed by then.
let child = {
let _spawning = SPAWN.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "recovery::tests::restart_recovery_child", "--ignored", "--nocapture"])
.env("DESIGNCRAFT_TEST_RECOVERY_SOURCE", source.path())
.env("DESIGNCRAFT_TEST_RECOVERY_MODE", mode)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap()
};
let output = child.wait_with_output().unwrap();
assert!(output.status.success(), "fresh child failed: {}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr));
let child_stdout = String::from_utf8_lossy(&output.stdout);
assert!(child_stdout.contains("test result: ok. 1 passed; 0 failed;"), "the fresh-process helper did not run exactly once: {child_stdout}");
@@ -953,6 +1033,8 @@ fn documents_of_two_apps_that_ended_are_both_recovered() {
fn a_running_apps_recovery_data_is_left_alone() {
let root = tempfile::tempdir().unwrap();
let running = run_left(root.path(), "run-7-0", "still open", 1);
// No child starts while the lock is open, so closing it frees it (see `SPAWN`).
let _no_spawn = SPAWN.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let lock = std::fs::File::open(running.join(LOCK)).unwrap();
lock.try_lock().unwrap();
let own = run_dir(root.path()).unwrap();