From ddcecda599c08916fe68ebc7a2fd167ddbf8ed05 Mon Sep 17 00:00:00 2001 From: BFlat Date: Fri, 4 Dec 2026 20:03:44 +0700 Subject: [PATCH] M12: restore test waits for a crashed run's folder lock to free before recovering (#3015) (#3016) * M12: restore test waits for a crashed run's folder lock to free before recovering (#3015) * M12: restore test also waits before the quit run takes the crashed run's folder back (#3015) --- apps/figcraft/src/recovery.rs | 13 +++++++++++++ apps/figcraft/src/restore_tests.rs | 7 +++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/figcraft/src/recovery.rs b/apps/figcraft/src/recovery.rs index 8a8485ca4..296b08df2 100644 --- a/apps/figcraft/src/recovery.rs +++ b/apps/figcraft/src/recovery.rs @@ -439,6 +439,19 @@ pub fn recover(root: &Path, s: &mut Session) -> usize { recover_all(root, s).copies } +/// Test hook: wait, up to a deadline, until no folder under `root` looks held. Another test's +/// child between its `fork` and its `exec` can hold an ended run's lock for longer than +/// `LOCK_WAIT` on a loaded machine (#3015). +#[cfg(test)] +pub fn wait_for_ended_runs(root: &Path) { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + let held = + || std::fs::read_dir(root).into_iter().flatten().flatten().any(|e| matches!(lock_folder(&e.path()), Err(std::fs::TryLockError::WouldBlock))); + while held() && std::time::Instant::now() < deadline { + std::thread::sleep(std::time::Duration::from_millis(10)); + } +} + /// Open the copies left by runs that ended without closing (folders under `root` whose lock is /// free, and copies directly in `root` from older versions) as unsaved documents. Each is kept /// until this run has its own copy of the document ([`Recovered::waiting`]); a copy of one of your diff --git a/apps/figcraft/src/restore_tests.rs b/apps/figcraft/src/restore_tests.rs index 3eadd8c92..fd1ca5198 100644 --- a/apps/figcraft/src/restore_tests.rs +++ b/apps/figcraft/src/restore_tests.rs @@ -12,7 +12,7 @@ use figcraft_engine::Session; use figcraft_ui_egui::FigApp; use serde_json::json; -use crate::recovery::{Recovery, recover}; +use crate::recovery::{Recovery, recover, wait_for_ended_runs}; use crate::services::{Desktop, Start, mark_restoring, start_session}; fn root(name: &str) -> PathBuf { @@ -205,6 +205,7 @@ fn after_a_crash_unsaved_documents_come_back_beside_the_restored_tabs_and_a_quit r.write(&s, true); // Killed: the lock goes with the process. The next start gets both back, and the tab. drop(r); + wait_for_ended_runs(&recovery); let mut killed = next(&dir); assert_eq!(recover(&recovery, &mut killed), 2); assert_eq!(start_session(Some(&dir), &mut killed, false), Start::Restore); @@ -212,7 +213,9 @@ fn after_a_crash_unsaved_documents_come_back_beside_the_restored_tabs_and_a_quit assert_eq!(killed.docs.iter().filter(|d| d.path.as_deref() == Some(paths[0].as_str()) && !d.is_dirty()).count(), 1, "the tab, as saved"); mark_restoring(&dir, false); // A normal quit instead (Don't save was the answer, or there was nothing to ask): the - // copies go, and the saved file is all that comes back. + // copies go, and the saved file is all that comes back. (This run takes back the folder the + // recover above let go of, once it is free.) + wait_for_ended_runs(&recovery); let mut r = Recovery::start(&recovery).unwrap(); r.write(&s, true); r.finish();