From 18ddace84eeff82b37185558a6c941234d855038 Mon Sep 17 00:00:00 2001 From: Elvis33LE Date: Thu, 8 Oct 2026 09:49:34 +0200 Subject: [PATCH] Autosave: a thread that fails to start is reported, not a silent no-op Autosaver::new discarded the spawn result with .ok(): under thread or resource exhaustion the recovery channel pointed at a dead receiver, every send failed silently and last_result stayed None - crash recovery was off while looking on. The failure is now recorded where last_result/flush report it (via the same note helper the worker uses), so health checks and flush surface it. --- crates/format/src/autosave.rs | 38 +++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/format/src/autosave.rs b/crates/format/src/autosave.rs index 91c22e3939..367af9722c 100644 --- a/crates/format/src/autosave.rs +++ b/crates/format/src/autosave.rs @@ -21,7 +21,7 @@ use photocraft_doc::Document; use serde::{Deserialize, Serialize}; use crate::store::write_atomic; -use crate::{PcraftWriter, Result, SaveOptions, SaveStats}; +use crate::{FormatError, PcraftWriter, Result, SaveOptions, SaveStats}; /// Sidecar describing an autosave. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -73,7 +73,16 @@ impl Autosaver { let bundle = dir.join(format!("{key}.pcraft")); let sidecar = dir.join(format!("{key}.json")); let last2 = last.clone(); - let handle = std::thread::Builder::new().name(format!("autosave-{key}")).spawn(move || worker(rx, bundle, sidecar, last2)).ok(); + // A thread that never started must not look like a working autosave: record the + // failure where `last_result`/`flush` report it, so recovery health checks see it. + let spawned = std::thread::Builder::new().name(format!("autosave-{key}")).spawn(move || worker(rx, bundle, sidecar, last2)); + let handle = match spawned { + Ok(h) => Some(h), + Err(e) => { + note(&last, Err(FormatError::Io(std::io::Error::other(format!("the autosave thread could not start: {e}"))))); + None + } + }; Autosaver { dir, key, tx: Some(tx), handle, last } } @@ -250,3 +259,28 @@ fn remove_entry(dir: &Path, key: &str) -> Result<()> { } Ok(()) } + +/// Record the outcome of the last save attempt where `last_result`/`flush` report it. A +/// poisoned lock keeps the previous result rather than panicking in a crash-recovery path. +fn note(last: &Arc>>>, r: Result) { + if let Ok(mut g) = last.lock() { + *g = Some(r); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_spawn_failure_is_reported_not_silent() { + // What the spawn-failure arm of `Autosaver::new` records: recovery health checks + // read this through `last_result`/`flush` instead of seeing a working autosave. + let last = Arc::new(Mutex::new(None)); + note(&last, Err(FormatError::Io(std::io::Error::other("the autosave thread could not start: resources")))); + let g = last.lock().expect("lock"); + let err = g.as_ref().and_then(|r| r.as_ref().err()).expect("the failure is recorded"); + let msg = err.to_string(); + assert!(msg.contains("could not start"), "{msg}"); + } +}