diff --git a/crates/engine/src/recovery.rs b/crates/engine/src/recovery.rs index 2a0422417..4f774b8d3 100644 --- a/crates/engine/src/recovery.rs +++ b/crates/engine/src/recovery.rs @@ -583,10 +583,33 @@ fn prepare_save(s: &Session, dir: &Path) -> Result { Ok(access) } +/// Refuse a write now while another session's background write of one of the dirty documents' +/// uids to `dir` is queued or running. A write now cancels only this session's writes. +#[cfg(not(target_arch = "wasm32"))] +fn check_no_other_write(s: &Session, dir: &Path) -> Result<()> { + let preview = s.new_document_preview(); + for document in s.docs.iter().filter(|d| d.is_dirty() && preview != Some(d.uid)) { + if worker::queued_by_other(dir, document.uid, s.recovery_owner.0) { + return Err(still_being_written(dir)); + } + } + Ok(()) +} + +#[cfg(not(target_arch = "wasm32"))] +fn still_being_written(dir: &Path) -> EngineError { + EngineError::Other(format!( + "{}: another document's recovery data is still being written here; try again or choose another recovery folder", + dir.display() + )) +} + /// Write recovery entries for dirty documents and drop clean checkpoints. Returns how many /// were written. Command callers use the tracked variant without configuring automatic recovery. pub fn save(s: &Session, dir: &Path) -> Result { let access = prepare_save(s, dir)?; + #[cfg(not(target_arch = "wasm32"))] + check_no_other_write(s, dir)?; let mut n = 0; for d in &s.docs { // New Document's preview is not a document until Create makes it one. @@ -601,9 +624,10 @@ pub fn save(s: &Session, dir: &Path) -> Result { continue; } // The caller asked for this write now: nothing has cancelled it. - // A background write of an older state must not land over this one. + // A background write of an older state this session queued must not land over this one, + // and no other session's write of the uid is queued until it is done. #[cfg(not(target_arch = "wasm32"))] - worker::cancel(dir, d.uid); + let _now = worker::write_now(dir, d.uid, s.recovery_owner.0).ok_or_else(|| still_being_written(dir))?; if let Some(e) = write_snapshot(&Snapshot::of(&access, dir, d), || false).error { return Err(e); } @@ -617,6 +641,8 @@ pub fn save(s: &Session, dir: &Path) -> Result { pub(crate) fn save_tracked(s: &mut Session, dir: &Path) -> Result { reject_overfull_checkpoints(s, dir)?; let access = prepare_save(s, dir)?; + #[cfg(not(target_arch = "wasm32"))] + check_no_other_write(s, dir)?; let mut n = 0; let preview = s.new_document_preview(); for index in 0..s.docs.len() { @@ -629,9 +655,10 @@ pub(crate) fn save_tracked(s: &mut Session, dir: &Path) -> Result { continue; } let Some(document) = s.docs.get(index) else { continue }; - // A background write of an older state must not land over this one. + // A background write of an older state this session queued must not land over this one, + // and no other session's write of the uid is queued until it is done. #[cfg(not(target_arch = "wasm32"))] - worker::cancel(dir, document.uid); + let _now = worker::write_now(dir, document.uid, s.recovery_owner.0).ok_or_else(|| still_being_written(dir))?; let entry = Snapshot::of(&access, dir, document); let written = write_snapshot(&entry, || false); // The primary is the recovery data; its metadata is optional. Track the primary before @@ -980,6 +1007,9 @@ mod worker { /// A document saved, reverted or closed while its write was queued or running: the /// snapshot it is producing must not be published. cancelled: Mutex>, + /// Documents a session is writing now on its own thread, with that session: no background + /// write of one is queued until it is done (see [`write_now`]). + writing_now: Mutex>, /// Notified each time a write finishes and leaves `running`. idle: Condvar, } @@ -1131,6 +1161,48 @@ mod worker { dirs } + /// Whether a session other than `owner` has a write of `dir`/`uid` queued or running that + /// is not cancelled, or queued to follow one. + fn queued_by_other(&self, dir: &Path, uid: u64, owner: u64) -> bool { + let running = self.running.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + self.other_in_flight(&running, dir, uid, owner) + } + + /// [`Writer::queued_by_other`] with `running` held, counting writes now as well. + fn other_in_flight(&self, running: &BTreeMap<(PathBuf, u64), Queued>, dir: &Path, uid: u64, owner: u64) -> bool { + let canonical = key(dir, uid); + running + .iter() + .filter(|(k, q)| **k == canonical || (k.1 == uid && q.dir == dir)) + .any(|(k, q)| (q.owner != owner && !self.cancelled(k)) || q.next.as_ref().is_some_and(|n| n.owner != owner)) + || self.writing_now.lock().is_ok_and(|now| now.get(&canonical).is_some_and(|by| *by != owner)) + } + + /// Start a write now of `dir`/`uid` by session `owner`. Returns `None` while another + /// session's write of it is in flight. Otherwise cancels this session's writes of it (as + /// [`Writer::cancel`]) and records the write now under the returned key, all under one + /// hold of `running`. + fn write_now(&self, dir: &Path, uid: u64, owner: u64) -> Option<(PathBuf, u64)> { + let canonical = key(dir, uid); + let _publishing = super::PUBLISH.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let mut running = self.running.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + if self.other_in_flight(&running, dir, uid, owner) { + return None; + } + let mut keys = Vec::new(); + for (k, q) in running.iter_mut().filter(|(k, q)| **k == canonical || (k.1 == uid && q.dir == dir)) { + q.next = None; + keys.push(k.clone()); + } + if let Ok(mut cancelled) = self.cancelled.lock() { + cancelled.extend(keys); + } + if let Ok(mut now) = self.writing_now.lock() { + now.insert(canonical.clone(), owner); + } + Some(canonical) + } + /// `key` as in [`Writer::finish`]. fn cancelled(&self, key: &(PathBuf, u64)) -> bool { self.cancelled.lock().is_ok_and(|cancelled| cancelled.contains(key)) @@ -1172,6 +1244,7 @@ mod worker { undelivered: Mutex::new(Vec::new()), running: Mutex::new(BTreeMap::new()), cancelled: Mutex::new(BTreeSet::new()), + writing_now: Mutex::new(BTreeMap::new()), idle: Condvar::new(), }) }) @@ -1183,20 +1256,41 @@ mod worker { WRITER.get().and_then(Option::as_ref) } - /// Do not publish the snapshot being written for `dir`/`uid` (see [`Writer::cancel`]). - pub(super) fn cancel(dir: &Path, uid: u64) { - if let Some(w) = started() { - w.cancel(dir, uid, None); - } - } - - /// [`cancel`] for the writes session `owner` queued. + /// Do not publish the snapshot session `owner` is having written for `dir`/`uid` (see + /// [`Writer::cancel`]). pub(super) fn cancel_owned(dir: &Path, uid: u64, owner: u64) { if let Some(w) = started() { w.cancel(dir, uid, Some(owner)); } } + /// A write now in progress; dropping it lets background writes of its document queue again. + pub(super) struct WriteNow(Option<(PathBuf, u64)>); + + impl Drop for WriteNow { + fn drop(&mut self) { + if let (Some(key), Some(w)) = (self.0.take(), started()) + && let Ok(mut now) = w.writing_now.lock() + { + now.remove(&key); + } + } + } + + /// See [`Writer::write_now`]. `None` while another session's write of `dir`/`uid` is in + /// flight. Without a writer thread there are no background writes to wait for. + pub(super) fn write_now(dir: &Path, uid: u64, owner: u64) -> Option { + match writer() { + Some(w) => w.write_now(dir, uid, owner).map(|key| WriteNow(Some(key))), + None => Some(WriteNow(None)), + } + } + + /// See [`Writer::queued_by_other`]. + pub(super) fn queued_by_other(dir: &Path, uid: u64, owner: u64) -> bool { + started().is_some_and(|w| w.queued_by_other(dir, uid, owner)) + } + /// See [`Writer::pending_folders`]. pub(super) fn pending_folders(uid: u64, owner: u64) -> Vec { started().map(|w| w.pending_folders(uid, owner)).unwrap_or_default() @@ -1238,6 +1332,10 @@ mod worker { let w = writer()?; let mut running = w.running.lock().ok()?; let key = (entry.key.clone(), entry.uid); + // A session is writing this document now: this write waits for a later turn. + if w.writing_now.lock().is_ok_and(|now| now.contains_key(&key)) { + return Some(false); + } if let Some(q) = running.get_mut(&key) { // The write in flight was cancelled (the document was saved since), so it holds // nothing to recover: this one follows it, in place of any that was waiting. diff --git a/crates/engine/src/recovery/tests.rs b/crates/engine/src/recovery/tests.rs index d1d03f162..a45cea1ce 100644 --- a/crates/engine/src/recovery/tests.rs +++ b/crates/engine/src/recovery/tests.rs @@ -336,6 +336,56 @@ fn saving_in_one_session_leaves_another_sessions_write_to_the_same_folder_alone( discard(dir.path(), uid); } +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_write_now_in_one_session_leaves_another_sessions_write_of_the_same_uid_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, with two objects instead of one. + let mut other = dirty_session(); + other.docs[0].uid = uid; + other.execute("frame.create", &json!({"rect": [20, 20, 60, 60]})).unwrap(); + let held = testing::hold_next_write(dir.path()); + queued_by.execute("file.recovery.save", &json!({"background": true})).unwrap(); + held.wait_until_held(); + let err = save(&other, dir.path()).unwrap_err(); + assert!(err.to_string().contains("still being written"), "{err}"); + let err = other.execute("file.recovery.save", &json!({"dir": dir.path().to_string_lossy()})).unwrap_err(); + assert!(err.to_string().contains("still being written"), "{err}"); + assert!(!worker::cancelled(dir.path(), uid), "the other session's write now cancelled this write"); + held.release(); + assert_eq!(finished_writes(&mut queued_by), 1); + let mut reopened = Session::new(); + assert_eq!(open(&mut reopened, dir.path()).unwrap().len(), 1); + assert_eq!(reopened.doc().unwrap().doc.spreads[0].items.len(), 1, "the queued session's snapshot was kept"); + discard(dir.path(), reopened.doc().unwrap().uid); + discard(dir.path(), uid); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn no_other_sessions_write_of_the_uid_is_queued_during_a_write_now() { + let _turn = background(); + let dir = tempfile::tempdir().unwrap(); + let writing = dirty_session(); + let uid = writing.docs[0].uid; + let mut other = dirty_session(); + other.recovery_dir = Some(dir.path().to_path_buf()); + other.docs[0].uid = uid; + let now = worker::write_now(dir.path(), uid, writing.recovery_owner.0).expect("nothing else is in flight"); + let queued = other.execute("file.recovery.save", &json!({"background": true})).unwrap(); + assert_eq!(queued["queued"], 0, "another session's write was queued during the write now: {queued}"); + assert!(worker::write_now(dir.path(), uid, other.recovery_owner.0).is_none(), "a second write now of the uid started"); + drop(now); + let queued = other.execute("file.recovery.save", &json!({"background": true})).unwrap(); + assert_eq!(queued["queued"], 1, "{queued}"); + assert_eq!(finished_writes(&mut other), 1); + discard(dir.path(), uid); +} + #[cfg(not(target_arch = "wasm32"))] #[test] fn a_write_now_cancels_a_background_write_to_the_same_folder_spelled_another_way() { @@ -733,7 +783,7 @@ fn a_cancel_stops_a_write_before_the_package_is_finished() { fn only_a_queued_write_is_cancelled() { // A mark for a document with no write in flight would stay in the writer's set for ever. let dir = tempfile::tempdir().unwrap(); - worker::cancel(dir.path(), 7); + worker::cancel_owned(dir.path(), 7, 1); assert!(!worker::cancelled(dir.path(), 7), "nothing was queued for it"); }