mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-16 07:12:43 +00:00
Recovery: a clean document stops its background writes; a failed folder sync keeps the snapshot tracked
- recovery::save cancels the background writes the session queued for a clean document and removes what a finished one left, so an older snapshot is not published after Undo returns the document to its saved state. - Cancelling a document's writes leaves a write another session queued to follow in place. - When the folder sync fails after the rename, the snapshot is reported as published with the error, so it is tracked and Save or Close removes it.
This commit is contained in:
1 parent
966c8a587c
commit
1eda04663b
2 files changed
+156
-4
No files matched your search
@@ -580,6 +580,12 @@ pub fn save(s: &Session, dir: &Path) -> Result<usize> {
|
||||
for d in &s.docs {
|
||||
// New Document's preview is not a document until Create makes it one.
|
||||
if !d.is_dirty() || s.new_document_preview() == Some(d.uid) {
|
||||
// A background write this session queued of an older state must not publish after
|
||||
// this discard, and what one already wrote in another folder is removed too.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
for other in worker::cancel_document(d.uid, s.recovery_owner.0) {
|
||||
discard(&other, d.uid);
|
||||
}
|
||||
discard(dir, d.uid);
|
||||
continue;
|
||||
}
|
||||
@@ -762,6 +768,8 @@ fn write_snapshot(entry: &Snapshot, cancelled: impl Fn() -> bool) -> Written {
|
||||
#[cfg(any(test, feature = "testing"))]
|
||||
testing::gate(&entry.dir);
|
||||
let (doc, meta) = files(&entry.dir, entry.uid);
|
||||
// Set at the rename: an error after it (syncing the folder) leaves the entry on disk.
|
||||
let landed = std::cell::Cell::new(false);
|
||||
let published = publish_recovery_written(
|
||||
&doc,
|
||||
entry.own,
|
||||
@@ -774,6 +782,7 @@ fn write_snapshot(entry: &Snapshot, cancelled: impl Fn() -> bool) -> Written {
|
||||
// and remove (Save and Close must clean up the snapshot they wrote). Recorded with the
|
||||
// rename, so a cleanup after it can remove the entry.
|
||||
|| {
|
||||
landed.set(true);
|
||||
if !entry.own {
|
||||
WRITTEN.lock().unwrap_or_else(std::sync::PoisonError::into_inner).insert((entry.key.clone(), entry.uid));
|
||||
}
|
||||
@@ -785,6 +794,8 @@ fn write_snapshot(entry: &Snapshot, cancelled: impl Fn() -> bool) -> Written {
|
||||
// reports an error, which is the cancel's and not a failure.
|
||||
Ok(false) => return Written { published: false, error: None },
|
||||
Err(_) if cancelled() => return Written { published: false, error: None },
|
||||
// Renamed into place, then the folder sync failed: the entry is there to track and clean up.
|
||||
Err(e) if landed.get() => return Written { published: true, error: Some(e) },
|
||||
Err(e) => return Written { published: false, error: Some(e) },
|
||||
}
|
||||
let metadata = json!({"path": entry.path, "title": entry.title, "saved": designcraft_doc::vars::now()});
|
||||
@@ -1048,10 +1059,15 @@ mod worker {
|
||||
let mut dirs = Vec::new();
|
||||
if let Ok(mut running) = self.running.lock() {
|
||||
let mut keys = Vec::new();
|
||||
for (key, q) in running.iter_mut().filter(|((_, u), q)| *u == uid && q.owner == owner) {
|
||||
keys.push(key.clone());
|
||||
dirs.push(q.dir.clone());
|
||||
q.next = None;
|
||||
for (key, q) in running.iter_mut().filter(|((_, u), _)| *u == uid) {
|
||||
// A write another session queued to follow stays queued.
|
||||
if q.next.as_ref().is_some_and(|n| n.owner == owner) {
|
||||
q.next = None;
|
||||
}
|
||||
if q.owner == owner {
|
||||
keys.push(key.clone());
|
||||
dirs.push(q.dir.clone());
|
||||
}
|
||||
}
|
||||
if let Ok(mut cancelled) = self.cancelled.lock() {
|
||||
cancelled.extend(keys);
|
||||
@@ -1315,6 +1331,23 @@ pub mod testing {
|
||||
w.wait_until_idle();
|
||||
}
|
||||
}
|
||||
|
||||
static FAIL_SYNC: Mutex<Option<PathBuf>> = Mutex::new(None);
|
||||
|
||||
/// Make the next folder sync after a recovery file is renamed into `dir` fail.
|
||||
pub fn fail_next_sync(dir: &Path) {
|
||||
*FAIL_SYNC.lock().unwrap_or_else(PoisonError::into_inner) = Some(dir.to_path_buf());
|
||||
}
|
||||
|
||||
/// Whether this sync of `dir` is the one [`fail_next_sync`] asked to fail.
|
||||
pub(super) fn sync_fails(dir: &Path) -> bool {
|
||||
let mut slot = FAIL_SYNC.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
if slot.as_deref() == Some(dir) {
|
||||
*slot = None;
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -1460,6 +1493,10 @@ fn is_link_like(metadata: &std::fs::Metadata) -> bool {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn sync_recovery_dir(dir: &Path) -> Result<()> {
|
||||
validate_recovery_dir(dir)?;
|
||||
#[cfg(any(test, feature = "testing"))]
|
||||
if testing::sync_fails(dir) {
|
||||
return Err(EngineError::Other(format!("{}: sync failed", dir.display())));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
|
||||
@@ -466,6 +466,121 @@ fn a_snapshot_whose_metadata_failed_is_still_the_documents_to_remove() {
|
||||
assert!(s.docs[0].recovery_entries.is_empty());
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_snapshot_whose_folder_sync_failed_is_still_the_documents_to_remove() {
|
||||
// The rename put the snapshot in place before the folder sync failed: it is tracked, so Save
|
||||
// removes it.
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let dir = run_dir(root.path()).unwrap();
|
||||
let mut s = dirty_session();
|
||||
s.recovery_dir = Some(dir.clone());
|
||||
let uid = s.docs[0].uid;
|
||||
let (document, _) = files(&dir, uid);
|
||||
testing::fail_next_sync(&dir);
|
||||
let err = save_tracked(&mut s, &dir).unwrap_err();
|
||||
assert!(err.to_string().contains("sync failed"), "{err}");
|
||||
assert!(document.exists(), "the snapshot landed");
|
||||
assert!(s.docs[0].recovery_entries.iter().any(|(d, u)| *u == uid && same_directory(d, &dir)), "and is tracked");
|
||||
let saved = root.path().join("saved.designcraft");
|
||||
s.execute("file.saveAs", &json!({"path": saved.to_string_lossy()})).unwrap();
|
||||
assert!(!document.exists(), "Save removed the snapshot");
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_document_undone_to_its_saved_state_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();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(!s.docs[0].is_dirty(), "back to the state it had");
|
||||
// A save of recovery data now drops the clean document's checkpoint, and the held write of
|
||||
// the older state must not publish after it.
|
||||
save(&s, dir.path()).unwrap();
|
||||
held.release();
|
||||
testing::wait_until_idle();
|
||||
assert!(!files(dir.path(), uid).0.exists(), "the older snapshot was not published");
|
||||
let _ = s.execute("file.recovery.progress", &json!({}));
|
||||
assert!(list(dir.path()).is_empty());
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_document_undone_to_its_saved_state_loses_the_snapshot_a_finished_write_left_in_another_folder() {
|
||||
let _turn = background();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let other = tempfile::tempdir().unwrap();
|
||||
let mut s = dirty_session();
|
||||
s.recovery_dir = Some(other.path().to_path_buf());
|
||||
let uid = s.docs[0].uid;
|
||||
s.execute("file.recovery.save", &json!({"background": true})).unwrap();
|
||||
// Written, but its result not yet taken by the session.
|
||||
testing::wait_until_idle();
|
||||
assert!(files(other.path(), uid).0.exists());
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
save(&s, dir.path()).unwrap();
|
||||
assert!(!files(other.path(), uid).0.exists(), "the snapshot of the older state is removed");
|
||||
let _ = s.execute("file.recovery.progress", &json!({}));
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_clean_document_leaves_another_sessions_write_queued_behind_its_own() {
|
||||
let _turn = background();
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut a = dirty_session();
|
||||
a.recovery_dir = Some(dir.path().to_path_buf());
|
||||
let uid = a.docs[0].uid;
|
||||
let mut b = dirty_session();
|
||||
b.recovery_dir = Some(dir.path().to_path_buf());
|
||||
b.docs[0].uid = uid;
|
||||
b.execute("frame.create", &json!({"rect": [20, 20, 60, 60]})).unwrap();
|
||||
let held = testing::hold_next_write(dir.path());
|
||||
a.execute("file.recovery.save", &json!({"background": true})).unwrap();
|
||||
held.wait_until_held();
|
||||
// A write now cancels the held one; the other session's write is queued to follow it.
|
||||
a.execute("file.recovery.save", &json!({"dir": dir.path().to_string_lossy()})).unwrap();
|
||||
b.execute("file.recovery.save", &json!({"background": true})).unwrap();
|
||||
a.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(!a.docs[0].is_dirty());
|
||||
save(&a, dir.path()).unwrap();
|
||||
held.release();
|
||||
assert_eq!(finished_writes(&mut b), 1, "the other session's write ran");
|
||||
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(), 2, "its snapshot was kept");
|
||||
discard(dir.path(), uid);
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_clean_document_in_another_session_leaves_this_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, clean, in another session.
|
||||
let mut other = Session::new();
|
||||
other.execute("file.new", &json!({})).unwrap();
|
||||
other.docs[0].uid = uid;
|
||||
assert!(!other.docs[0].is_dirty());
|
||||
let held = testing::hold_next_write(dir.path());
|
||||
queued_by.execute("file.recovery.save", &json!({"background": true})).unwrap();
|
||||
held.wait_until_held();
|
||||
save(&other, dir.path()).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 a_background_snapshot_whose_metadata_failed_is_still_the_documents_to_remove() {
|
||||
|
||||
Reference in new issue
Block a user