Recovery: closing or saving a document cancels only its own session's background writes

Discarding a document's recovery entries cancelled every background write of that uid in the folder, so another session's write of a document with the same uid was dropped without a replacement. The cancel now covers only writes the session itself queued.
This commit is contained in:
dotCooCoo committed 2026-11-11 23:09:49 -08:00
1 parent c4b2d73062
commit b9cba43f89
2 files changed
+46 -10

No files matched your search

+22 -10
View File
@@ -539,9 +539,10 @@ pub(crate) fn discard_document(s: &mut Session, index: usize, default_dir: Optio
if !s.docs.iter().enumerate().any(|(other_index, owner)| {
other_index != index && owner.recovery_entries.iter().any(|(entry_dir, entry_uid)| *entry_uid == uid && same_directory(entry_dir, &dir))
}) {
// A write of it still running must not publish its snapshot after this point.
// A write of it this session still runs must not publish its snapshot after this
// point; another session's write of the same uid is its own.
#[cfg(not(target_arch = "wasm32"))]
worker::cancel(&dir, uid);
worker::cancel_owned(&dir, uid, owner);
discard(&dir, uid, owner);
}
}
@@ -1041,22 +1042,26 @@ mod worker {
/// Do not publish the snapshot being written for `dir`/`uid`: the document was saved,
/// reverted or closed. Only a write that is queued or running is marked, so the set stays
/// as small as the work in flight.
fn cancel(&self, dir: &Path, uid: u64) {
/// as small as the work in flight. With `owner`, only writes that session queued are
/// marked: another session's write of the same uid stays.
fn cancel(&self, dir: &Path, uid: u64, owner: Option<u64>) {
let canonical = key(dir, uid);
let mine = |queued_by: u64| owner.is_none_or(|owner| owner == queued_by);
// 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);
// The folder's canonical path, or its name as given: a link in the path may lead
// elsewhere now than when the write was queued.
let Ok(mut running) = self.running.lock() else { return };
let keys: Vec<(PathBuf, u64)> =
running.iter().filter(|(k, q)| **k == canonical || (k.1 == uid && q.dir == dir)).map(|(k, _)| k.clone()).collect();
// A write queued to follow it is older than what the caller has now, too.
for k in &keys {
if let Some(q) = running.get_mut(k) {
let mut keys = Vec::new();
for (k, q) in running.iter_mut().filter(|(k, q)| **k == canonical || (k.1 == uid && q.dir == dir)) {
// A write queued to follow it is older than what the caller has now, too.
if q.next.as_ref().is_some_and(|n| mine(n.owner)) {
q.next = None;
}
if mine(q.owner) {
keys.push(k.clone());
}
}
if let Ok(mut cancelled) = self.cancelled.lock() {
cancelled.extend(keys);
@@ -1181,7 +1186,14 @@ mod worker {
/// 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);
w.cancel(dir, uid, None);
}
}
/// [`cancel`] for the writes session `owner` queued.
pub(super) fn cancel_owned(dir: &Path, uid: u64, owner: u64) {
if let Some(w) = started() {
w.cancel(dir, uid, Some(owner));
}
}
+24
View File
@@ -312,6 +312,30 @@ fn saving_in_one_session_leaves_another_sessions_write_alone() {
discard(dir.path(), uid);
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn saving_in_one_session_leaves_another_sessions_write_to_the_same_folder_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 and recovery folder in another session.
let mut other = dirty_session();
other.recovery_dir = Some(dir.path().to_path_buf());
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();
assert!(!worker::cancelled(dir.path(), uid), "the other session's save cancelled this write");
held.release();
assert_eq!(finished_writes(&mut queued_by), 1);
assert!(files(dir.path(), uid).0.exists(), "this session's snapshot was kept");
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() {