From 228f5a4596fe6cf05c4bf0225631ca370909a487 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 18:12:43 -0800 Subject: [PATCH] craft-cloud-local: the concurrent-save test always reaches the locked recheck The second save now signals once its temporary file is written, before it takes WRITE_LOCK, and the test releases the first save only then. The second save therefore always passes its early check and is refused by the recheck under the lock, and the test fails when that recheck is removed. --- crates/cloud-local/src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/cloud-local/src/lib.rs b/crates/cloud-local/src/lib.rs index f79271499e..08144a6ca5 100644 --- a/crates/cloud-local/src/lib.rs +++ b/crates/cloud-local/src/lib.rs @@ -394,6 +394,8 @@ fn write_replacing(dir: &Dir, rel: &Path, bytes: &[u8], check: impl FnOnce() -> let _ = dir.remove_file(&temp_rel); return Err(fault); } + #[cfg(test)] + tests::before_lock(); let published = { let _serialized = WRITE_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); check().and_then(|()| { @@ -475,6 +477,17 @@ mod tests { } } + /// Run once by the next [`write_replacing`] to finish its temporary file, + /// before it takes [`WRITE_LOCK`], then cleared. + static BEFORE_LOCK: Mutex> = Mutex::new(None); + + pub(super) fn before_lock() { + let hook = BEFORE_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take(); + if let Some(hook) = hook { + hook(); + } + } + fn entry_for(id: &str) -> Entry { Entry { room: format!("local:{id}"), @@ -512,10 +525,16 @@ mod tests { // The first save has passed its check and not yet renamed. A second // save must not be able to reach its own check in between. assert!(WRITE_LOCK.try_lock().is_err(), "the check and the rename must run under one lock"); + // The second save passes its early check and writes its temporary + // file before the first one renames, so only the recheck under the + // lock can refuse it. + let (ready_tx, ready_rx) = mpsc::channel::<()>(); + *BEFORE_LOCK.lock().unwrap() = Some(Box::new(move || ready_tx.send(()).unwrap())); let second = { let (root, entry) = (root.path().to_owned(), read_entry.clone()); std::thread::spawn(move || write(&root, &entry, b"second save")) }; + ready_rx.recv().unwrap(); go_tx.send(()).unwrap(); assert!(first.join().unwrap().is_ok());