diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 3d43a80e0..bb67875b4 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -412,6 +412,9 @@ pub struct Session { pub clipboard_origin: f64, /// Crash-recovery folder (the app sets it; saving or closing a document removes its entry). pub recovery_dir: Option, + /// This session's number for the background recovery writer, which returns each result to + /// the session that queued it. + pub(crate) recovery_owner: recovery::Owner, /// Copied text with its formatting (a frameless story slice), its plain text, and what pasting /// it needs from its document: a copy holding only the stories its anchored text frames show /// (and theirs) and the graphics the text and those stories place. @@ -477,6 +480,7 @@ impl Session { clipboard: None, clipboard_origin: 0.0, recovery_dir: None, + recovery_owner: recovery::Owner::new(), text_clipboard: None, link_checks: Default::default(), ui_requests: vec![], diff --git a/crates/engine/src/recovery.rs b/crates/engine/src/recovery.rs index 420d1795f..dc7d63ed7 100644 --- a/crates/engine/src/recovery.rs +++ b/crates/engine/src/recovery.rs @@ -649,6 +649,32 @@ struct Snapshot { own: bool, /// The folder's canonical key, for recording that a shared entry is this run's now. key: PathBuf, + /// The session that queued a background write ([`Session::recovery_owner`]), which takes its + /// result. + #[cfg_attr(target_arch = "wasm32", allow(dead_code))] + owner: u64, +} + +/// A number for each session, so background results go back to the session that queued them. +/// Dropping it retires the number: results still queued for it are then dropped. +pub(crate) struct Owner(pub(crate) u64); + +/// The numbers of the sessions that exist. +static LIVE_OWNERS: std::sync::Mutex> = std::sync::Mutex::new(std::collections::BTreeSet::new()); + +impl Owner { + pub(crate) fn new() -> Self { + static NEXT_OWNER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); + let id = NEXT_OWNER.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + LIVE_OWNERS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).insert(id); + Owner(id) + } +} + +impl Drop for Owner { + fn drop(&mut self) { + LIVE_OWNERS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).remove(&self.0); + } } impl Snapshot { @@ -662,6 +688,7 @@ impl Snapshot { // An entry this run did not write is never replaced, even one made since it checked. own: access.owns(d.uid), key: access.key.clone(), + owner: 0, } } } @@ -700,7 +727,16 @@ fn write_snapshot(entry: &Snapshot, cancelled: impl Fn() -> bool) -> Written { WRITTEN.lock().unwrap_or_else(std::sync::PoisonError::into_inner).insert((entry.key.clone(), entry.uid)); } let metadata = json!({"path": entry.path, "title": entry.title, "saved": designcraft_doc::vars::now()}); - Written { published: true, error: publish_recovery_file(&meta, metadata.to_string().as_bytes(), entry.own).err() } + // A cancel after the primary landed removes it; the metadata must not be left without it. + let bytes = metadata.to_string(); + let error = publish_recovery_written( + &meta, + entry.own, + |out| out.write_all(bytes.as_bytes()).map_err(|e| EngineError::Other(format!("{}: {e}", meta.display()))), + &cancelled, + ) + .err(); + Written { published: true, error } } /// Write recovery entries for dirty documents on a background thread and drop clean checkpoints @@ -729,7 +765,8 @@ pub(crate) fn start_tracked(s: &mut Session, dir: &Path) -> Result { continue; } let Some(document) = s.docs.get(index) else { continue }; - let entry = Snapshot::of(&access, dir, document); + let mut entry = Snapshot::of(&access, dir, document); + entry.owner = s.recovery_owner.0; // A write still running for this document already holds a snapshot of it; the next // timer tick catches up with the edits made since. match worker::submit(entry) { @@ -787,8 +824,20 @@ pub(crate) fn finished(s: &mut Session) -> Finished { let Some(w) = worker::started() else { return out }; // Before draining: the worker sends a result before it clears the document as running, so a writer idle now has every result in the channel already. let busy = w.busy(); - let done = w.done.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - while let Ok(d) = done.try_recv() { + // Results go to the session that queued them: another session has none of those documents, + // and would remove their snapshots as if they had been closed. + let mine: Vec = { + let done = w.done.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let mut others = w.undelivered.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let mut all = std::mem::take(&mut *others); + all.extend(done.try_iter()); + let (mine, rest): (Vec<_>, Vec<_>) = all.into_iter().partition(|d| d.owner == s.recovery_owner.0); + // A session that is gone takes nothing: its results are dropped rather than kept for ever. + let live = LIVE_OWNERS.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + *others = rest.into_iter().filter(|d| live.contains(&d.owner)).collect(); + mine + }; + for d in mine { // A primary that landed belongs to this run even if its optional metadata failed: record // it, so Save and Close remove the snapshot instead of leaving it in the folder for ever. if d.published && record_checkpoint(s, d.dir, d.uid) { @@ -826,11 +875,15 @@ mod worker { /// The primary document reached the folder (its optional metadata may have failed): the /// checkpoint must still be recorded, or Save and Close could not remove it. pub(super) published: bool, + /// The session that queued the write. + pub(super) owner: u64, } pub(super) struct Writer { jobs: Sender, pub(super) done: Mutex>, + /// Results taken from `done` by a session that did not queue them, kept for their own. + pub(super) undelivered: Mutex>, /// Documents queued or being written: a second write of one still holds nothing newer. running: Mutex>, /// A document saved, reverted or closed while its write was queued or running: the @@ -872,6 +925,9 @@ mod worker { /// as small as the work in flight. fn cancel(&self, dir: &Path, uid: u64) { let key = (dir.to_path_buf(), uid); + // 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)) { return; } @@ -894,11 +950,11 @@ mod worker { let spawned = std::thread::Builder::new().name("designcraft-recovery".into()).spawn(move || { while let Ok(job) = job_rx.recv() { let (published, error) = guarded_write(&job); - let (dir, uid) = (job.dir, job.uid); + let (dir, uid, owner) = (job.dir, job.uid, job.owner); // 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 }).is_ok(); + let sent = 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); } @@ -910,6 +966,7 @@ mod worker { spawned.ok().map(|_| Writer { jobs, done: Mutex::new(done_rx), + undelivered: Mutex::new(Vec::new()), running: Mutex::new(BTreeSet::new()), cancelled: Mutex::new(BTreeSet::new()), idle: Condvar::new(), @@ -1124,6 +1181,11 @@ fn publish_recovery_file(path: &Path, bytes: &[u8], replace: bool) -> Result<()> .map(|_| ()) } +/// Serializes the last check for a cancelled write with the rename that publishes it, and with +/// [`worker::cancel`]. +#[cfg(not(target_arch = "wasm32"))] +static PUBLISH: std::sync::Mutex<()> = std::sync::Mutex::new(()); + /// [`publish_recovery_file`], with `write` filling the temporary file: a large document is written /// straight to disk instead of being built in memory first. Returns whether it was published; /// `cancelled` is asked after the bytes are written and before the rename, so a document saved or @@ -1155,16 +1217,21 @@ fn publish_recovery_written( out.flush().map_err(|e| EngineError::Other(format!("{}: {e}", path.display())))?; } tmp.as_file_mut().sync_all().map_err(|e| EngineError::Other(format!("{}: {e}", path.display())))?; - // Last moment: the document was saved, reverted or closed while the bytes were written. The - // temporary file is dropped, so nothing of the stale snapshot stays. - if cancelled() { - return Ok(false); + { + // Held from the check to the rename: a cancel waits for a rename already under way (the + // cleanup after it then removes the file), and a rename after a cancel sees it. + let _publishing = PUBLISH.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + // Last moment: the document was saved, reverted or closed while the bytes were written. + // The temporary file is dropped, so nothing of the stale snapshot stays. + if cancelled() { + return Ok(false); + } + // Validate the non-followed leaf again immediately before renaming the randomized + // temporary file to its fixed sibling destination. + validate_recovery_dir(dir)?; + let published = if replace { tmp.persist(path) } else { tmp.persist_noclobber(path) }; + published.map_err(|e| EngineError::Other(format!("{}: {}", path.display(), e.error)))?; } - // Validate the non-followed leaf again immediately before renaming the randomized temporary - // file to its fixed sibling destination. - validate_recovery_dir(dir)?; - let published = if replace { tmp.persist(path) } else { tmp.persist_noclobber(path) }; - published.map_err(|e| EngineError::Other(format!("{}: {}", path.display(), e.error)))?; sync_recovery_dir(dir)?; Ok(true) } diff --git a/crates/engine/src/recovery/tests.rs b/crates/engine/src/recovery/tests.rs index 3b7c2303d..79644ca3d 100644 --- a/crates/engine/src/recovery/tests.rs +++ b/crates/engine/src/recovery/tests.rs @@ -170,6 +170,65 @@ fn a_background_save_returns_while_its_write_is_held() { discard(dir.path(), uid); } +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_document_saved_while_its_write_is_held_leaves_no_snapshot() { + let _turn = background(); + let dir = tempfile::tempdir().unwrap(); + let mut s = dirty_session(); + s.recovery_dir = Some(dir.path().to_path_buf()); + let uid = s.docs[0].uid; + let held = testing::hold_next_write(dir.path()); + s.execute("file.recovery.save", &json!({"background": true})).unwrap(); + held.wait_until_held(); + 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); + assert!(!files(dir.path(), uid).0.exists(), "the stale snapshot was not published"); + assert!(!files(dir.path(), uid).1.exists(), "nor its metadata"); + assert!(list(dir.path()).is_empty()); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_background_result_goes_to_the_session_that_queued_it() { + 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; + let mut other = dirty_session(); + queued_by.execute("file.recovery.save", &json!({"background": true})).unwrap(); + testing::wait_until_idle(); + // Another session has none of these documents: it must neither take the result nor remove + // the snapshot as if its document had been closed. + let v = other.execute("file.recovery.progress", &json!({})).unwrap(); + assert_eq!(v["written"], 0, "{v}"); + assert!(files(dir.path(), uid).0.exists(), "the snapshot is still there"); + assert_eq!(finished_writes(&mut queued_by), 1); + assert!(queued_by.docs[0].recovery_entries.iter().any(|(d, u)| *u == uid && same_directory(d, dir.path())), "the checkpoint is recorded"); + discard(dir.path(), uid); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn results_for_a_session_that_is_gone_are_not_kept() { + let _turn = background(); + let dir = tempfile::tempdir().unwrap(); + let mut gone = dirty_session(); + gone.recovery_dir = Some(dir.path().to_path_buf()); + let owner = gone.recovery_owner.0; + gone.execute("file.recovery.save", &json!({"background": true})).unwrap(); + testing::wait_until_idle(); + drop(gone); + let mut other = dirty_session(); + other.execute("file.recovery.progress", &json!({})).unwrap(); + let writer = worker::started().unwrap(); + let kept = writer.undelivered.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + assert!(!kept.iter().any(|d| d.owner == owner), "a result nobody can take was kept"); +} + #[test] fn a_background_save_rejects_a_background_that_is_not_true_or_false() { let dir = tempfile::tempdir().unwrap();