SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Crash: a clean exit is reported as an unclean exit (#12842) (#12891)
Homero Thompson1c1018aa6eda
diff --git a/crates/engine/src/crash.rs b/crates/engine/src/crash.rsindex 21bcda6999..ea590ff628 100644--- a/crates/engine/src/crash.rs+++ b/crates/engine/src/crash.rs@@ -318,25 +318,34 @@ pub fn install(dir: PathBuf, build: Build) -> Vec<Summary> {let _ = fs::write(&seen_file, all.iter().map(|s| s.id.as_str()).collect::<Vec<_>>().join("\n"));let known = all.into_iter().filter_map(|s| sig_of(&s.id).map(|sig| (sig, (0, s.id)))).collect();let journal = Journal::open(&dir, &build, started);- if STATE- .set(State {- dir,- build,- environment: Mutex::new(String::new()),- journal: Mutex::new(journal),- known: Mutex::new(known),- written: AtomicUsize::new(0),- started,- started_ms,- })- .is_ok()- {- let previous = std::panic::take_hook();- std::panic::set_hook(Box::new(move |info| {- if !hook(info) {- previous(info);+ match STATE.set(State {+ dir,+ build,+ environment: Mutex::new(String::new()),+ journal: Mutex::new(journal),+ known: Mutex::new(known),+ written: AtomicUsize::new(0),+ started,+ started_ms,+ }) {+ Ok(()) => {+ let previous = std::panic::take_hook();+ std::panic::set_hook(Box::new(move |info| {+ if !hook(info) {+ previous(info);+ }+ }));+ }+ Err(state) => {+ // The journal was already installed in this process: the one just opened is an+ // orphan nobody holds, and the next start must not call it an unclean exit.+ if let Ok(mut j) = state.journal.lock()+ && let Some(path) = j.path.take()+ {+ j.file = None;+ let _ = fs::remove_file(path);}- }));+ }}fresh}@@ -414,11 +423,16 @@ fn remove_stray_native_files(dir: &Path) {/// The app is closing normally: its journal goes, and repeat counts are written.pub fn end_session() {let Some(s) = STATE.get() else { return };- if let Ok(mut j) = s.journal.lock()- && let Some(path) = j.path.take()- {- j.file = None;- let _ = fs::remove_file(path);+ if let Ok(mut j) = s.journal.lock() {+ // The session ended cleanly: the journal says so before it goes. A journal that stays+ // (the removal failed, the machine stopped in between) is then not an unclean exit at+ // the next start: the `reported` flag says there is nothing left to report.+ j.record(&json!({ "reported": "clean" }));+ if let Some(path) = j.path.take() {+ j.file = None;+ // Best-effort: a journal that cannot go is not an unclean exit (it says so).+ let _ = fs::remove_file(path);+ }}let counts: Vec<(u64, String)> = s.known.lock().map(|k| k.values().filter(|(n, _)| *n > 0).cloned().collect()).unwrap_or_default();for (n, id) in counts {diff --git a/crates/engine/src/crash_tests.rs b/crates/engine/src/crash_tests.rsindex 85113e2230..61beb248cf 100644--- a/crates/engine/src/crash_tests.rs+++ b/crates/engine/src/crash_tests.rs@@ -18,7 +18,8 @@ fn child() {let (Some(dir), Some(mode)) = (solidcraft_geom::env::var_os("CRASH_CHILD_DIR"), solidcraft_geom::env::var("CRASH_CHILD")) else {return;};- let fresh = install(PathBuf::from(dir), build());+ let dir = PathBuf::from(dir);+ let fresh = install(dir.clone(), build());println!("FRESH {}", serde_json::to_string(&fresh).unwrap());// Dying while starting up, before any command: in a graphics driver, with or without what// a native crash handler caught.@@ -203,6 +204,24 @@ fn child() {s.execute("solid.sphere", &json!({"diameter": 8})).unwrap();let _ = s.execute("diagnostics.crash_test", &json!({"mode": "abort"}));}+ // A clean exit whose journal cannot be removed: the directory is read-only.+ "clean_read_only" => {+ let sessions = dir.join("sessions");+ #[cfg(unix)]+ {+ use std::os::unix::fs::PermissionsExt;+ fs::set_permissions(&sessions, fs::Permissions::from_mode(0o500)).unwrap();+ end_session();+ fs::set_permissions(&sessions, fs::Permissions::from_mode(0o700)).unwrap();+ }+ #[cfg(not(unix))]+ end_session();+ println!("CLEAN_READ_ONLY_OK");+ }+ // A second install in the same process: the journal it opens is an orphan.+ "install_twice" => {+ install(dir.clone(), build());+ }_ => {}}end_session();@@ -325,6 +344,37 @@ fn a_known_panic_that_ends_the_app_is_counted_not_reported_again_as_an_unclean_elet _ = fs::remove_dir_all(&d);}+/// A clean exit whose journal cannot be removed (its directory is read-only) is not an unclean+/// exit at the next start: the journal says the session ended cleanly.+#[test]+fn a_clean_exit_whose_journal_cannot_be_removed_is_not_reported_as_an_unclean_exit() {+ let d = fresh_dir("clean-exit");+ let (out, ok) = run_child(&d, "clean_read_only");+ assert!(ok && out.contains("CLEAN_READ_ONLY_OK"), "{out}");+ // The next start finds the journal the clean exit could not remove: it says nothing about it.+ let (out, ok) = run_child(&d, "none");+ assert!(ok);+ assert!(out.contains("FRESH []"), "{out}");+ let reports = list(&d);+ assert!(reports.is_empty(), "{reports:?}");+ let _ = fs::remove_dir_all(&d);+}++/// A second install in the same process leaves no journal behind: the next start has nothing to+/// report about it.+#[test]+fn a_journal_left_behind_by_a_second_install_is_not_reported_as_an_unclean_exit() {+ let d = fresh_dir("install-twice");+ let (out, ok) = run_child(&d, "install_twice");+ assert!(ok, "{out}");+ let (out, ok) = run_child(&d, "none");+ assert!(ok);+ assert!(out.contains("FRESH []"), "{out}");+ let reports = list(&d);+ assert!(reports.is_empty(), "{reports:?}");+ let _ = fs::remove_dir_all(&d);+}+#[test]fn an_abort_is_reported_at_the_next_start_with_a_replay() {let d = fresh_dir("abort");