From a73d59e07f2d97f8cb7255d2b4cf14b39003e667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20G=C3=BCne=C5=9F?= Date: Thu, 26 Nov 2026 00:50:35 +0300 Subject: [PATCH] Pipeline: accept existing entry when concurrent cache publication races on Windows (#1324) * Pipeline: accept existing entry when concurrent cache publication races on Windows * Pipeline: exercise the blocked Windows cache publication path Keep the published entry open without delete sharing and assert that replacement is blocked. The regression fails with the pre-fix publication code and passes with the current implementation. Validation: 5 pipeline tests passed on x86_64-pc-windows-msvc; pipeline Clippy with warnings denied and formatting checks passed. * Pipeline: adapt concurrent publication test to new tempfile_in signature --- crates/victory-pipeline/src/cache.rs | 68 ++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/crates/victory-pipeline/src/cache.rs b/crates/victory-pipeline/src/cache.rs index 9692d29..17cfa4c 100644 --- a/crates/victory-pipeline/src/cache.rs +++ b/crates/victory-pipeline/src/cache.rs @@ -63,11 +63,16 @@ impl Cache { let (tmp, mut file) = tempfile_in(dir)?; let written = file.write_all(&output); drop(file); - let published = written.and_then(|()| std::fs::rename(&tmp, &path)); - if published.is_err() { + if let Err(err) = written { let _ = std::fs::remove_file(&tmp); + return Err(err.into()); + } + if let Err(err) = std::fs::rename(&tmp, &path) { + let _ = std::fs::remove_file(&tmp); + if !path.is_file() { + return Err(err.into()); + } } - published?; Ok(path) } } @@ -156,6 +161,63 @@ mod tests { ); } + #[cfg(windows)] + #[test] + fn accepts_entry_published_concurrently_while_open() { + use std::os::windows::fs::OpenOptionsExt; + + struct RacingUpper { + cache: Cache, + held: std::sync::Mutex>, + } + + impl Converter for RacingUpper { + fn id(&self) -> &'static str { + "test.upper" + } + fn version(&self) -> u32 { + 1 + } + fn convert(&self, input: &[u8]) -> Result> { + let output = input.to_ascii_uppercase(); + let key = CacheKey::new(self, input); + let path = self.cache.path_for(&key); + let dir = path.parent().unwrap(); + std::fs::create_dir_all(dir)?; + std::fs::write(&path, &output)?; + // FILE_SHARE_READ permits readers but prevents replacement while held. + *self.held.lock().unwrap() = Some( + std::fs::OpenOptions::new() + .read(true) + .share_mode(1) + .open(&path)?, + ); + let (probe, mut probe_file) = tempfile_in(dir)?; + probe_file.write_all(&output)?; + drop(probe_file); + assert!( + std::fs::rename(&probe, &path).is_err(), + "the open reader must prevent replacing the published entry" + ); + std::fs::remove_file(probe)?; + Ok(output) + } + } + + let tmp = tempfile::tempdir().unwrap(); + let cache = Cache::open(tmp.path().join("cache")).unwrap(); + let conv = RacingUpper { + cache: cache.clone(), + held: std::sync::Mutex::new(None), + }; + let path = cache.get_or_convert(&conv, b"abc").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"ABC"); + assert_eq!( + std::fs::read_dir(path.parent().unwrap()).unwrap().count(), + 1 + ); + } + #[test] fn staging_retries_collisions_without_changing_existing_files() { let dir = tempfile::tempdir().unwrap();