diff --git a/crates/victory-pipeline/src/cache.rs b/crates/victory-pipeline/src/cache.rs index 81e57de..9692d29 100644 --- a/crates/victory-pipeline/src/cache.rs +++ b/crates/victory-pipeline/src/cache.rs @@ -1,5 +1,7 @@ -use std::io::Write; +use std::fs::File; +use std::io::{self, Write}; use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; use anyhow::{Context, Result}; @@ -58,11 +60,7 @@ impl Cache { let dir = path.parent().expect("hashed path has a parent"); std::fs::create_dir_all(dir)?; // Write-then-rename so readers never see partial output. - let tmp = tempfile_in(dir); - let mut file = std::fs::OpenOptions::new() - .write(true) - .create_new(true) - .open(&tmp)?; + 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)); @@ -74,11 +72,31 @@ impl Cache { } } -fn tempfile_in(dir: &Path) -> PathBuf { - use std::sync::atomic::{AtomicU64, Ordering}; +fn tempfile_in(dir: &Path) -> io::Result<(PathBuf, File)> { static COUNTER: AtomicU64 = AtomicU64::new(0); - let n = COUNTER.fetch_add(1, Ordering::Relaxed); - dir.join(format!(".tmp-{}-{n}", std::process::id())) + create_tempfile_in(dir, &COUNTER) +} + +fn create_tempfile_in(dir: &Path, counter: &AtomicU64) -> io::Result<(PathBuf, File)> { + // An interrupted writer can leave a file behind, and process IDs are + // reused. Keep it intact and try a new name rather than failing a miss. + for _ in 0..128 { + let n = counter.fetch_add(1, Ordering::Relaxed); + let path = dir.join(format!(".tmp-{}-{n}", std::process::id())); + match std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&path) + { + Ok(file) => return Ok((path, file)), + Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e), + } + } + Err(io::Error::new( + io::ErrorKind::AlreadyExists, + "could not create a unique cache staging file after 128 attempts", + )) } #[cfg(test)] @@ -138,6 +156,65 @@ mod tests { ); } + #[test] + fn staging_retries_collisions_without_changing_existing_files() { + let dir = tempfile::tempdir().unwrap(); + let counter = AtomicU64::new(0); + for n in 0..2 { + std::fs::write( + dir.path().join(format!(".tmp-{}-{n}", std::process::id())), + b"stale", + ) + .unwrap(); + } + + let (path, mut file) = create_tempfile_in(dir.path(), &counter).unwrap(); + file.write_all(b"fresh").unwrap(); + drop(file); + assert_eq!( + path.file_name().unwrap(), + format!(".tmp-{}-2", std::process::id()).as_str() + ); + assert_eq!(std::fs::read(&path).unwrap(), b"fresh"); + for n in 0..2 { + assert_eq!( + std::fs::read(dir.path().join(format!(".tmp-{}-{n}", std::process::id()))).unwrap(), + b"stale" + ); + } + assert_eq!(counter.load(Ordering::Relaxed), 3); + } + + #[test] + fn staging_collision_retries_are_bounded() { + let dir = tempfile::tempdir().unwrap(); + let counter = AtomicU64::new(0); + for n in 0..128 { + std::fs::write( + dir.path().join(format!(".tmp-{}-{n}", std::process::id())), + b"stale", + ) + .unwrap(); + } + + let error = create_tempfile_in(dir.path(), &counter).unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::AlreadyExists); + assert_eq!(counter.load(Ordering::Relaxed), 128); + assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 128); + } + + #[test] + fn staging_does_not_retry_other_io_errors() { + let dir = tempfile::tempdir().unwrap(); + let counter = AtomicU64::new(0); + let missing = dir.path().join("missing"); + + let error = create_tempfile_in(&missing, &counter).unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::NotFound); + assert_eq!(counter.load(Ordering::Relaxed), 1); + assert!(!missing.exists()); + } + #[test] fn version_bump_invalidates() { let v1 = Upper {