From 74ea0be88fa0b9ffbc892aac1ac5a2b70c044d4f Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Wed, 28 Oct 2026 22:29:21 +0530 Subject: [PATCH] Disk cache: a failed free-space reading clears the last one (#2551) Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/system/sysinfo.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/system/sysinfo.rs b/crates/engine/src/system/sysinfo.rs index fe94df09e..c02e68773 100644 --- a/crates/engine/src/system/sysinfo.rs +++ b/crates/engine/src/system/sysinfo.rs @@ -56,9 +56,8 @@ impl Watch { } let (latest, busy) = (self.latest.clone(), self.busy.clone()); let read = move || { - if let Some(m) = read() { - *latest.lock().unwrap_or_else(PoisonError::into_inner) = Some(m); - } + // A failed reading clears the last one: a stale value is worse than none. + *latest.lock().unwrap_or_else(PoisonError::into_inner) = read(); busy.store(false, Ordering::Release); }; #[cfg(not(target_arch = "wasm32"))] @@ -72,7 +71,7 @@ impl Watch { } } - /// The last reading (`None` until one finished). + /// The last reading (`None` until one finished or when the last one failed). pub fn latest(&self) -> Option { *self.latest.lock().unwrap_or_else(PoisonError::into_inner) } @@ -288,6 +287,26 @@ mod tests { assert_eq!(m.available, 1000); } + #[test] + fn a_failed_reading_clears_the_last_one() { + let w = Watch::::default(); + let wait = || { + for _ in 0..500 { + if !w.busy.load(Ordering::Acquire) { + return; + } + std::thread::sleep(std::time::Duration::from_millis(2)); + } + panic!("the reading never finished"); + }; + w.poll_with("ec-test", || Some(7)); + wait(); + assert_eq!(w.latest(), Some(7)); + w.poll_with("ec-test", || None); + wait(); + assert_eq!(w.latest(), None); + } + #[test] fn parses_df() { let linux = "Filesystem 1024-blocks Used Available Capacity Mounted on\n/dev/nvme0n1p2 488281250 123456789 364824461 26% /\n";