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
This commit is contained in:
1 parent
57f13cf10d
commit
a73d59e07f
1 file changed
+65
-3
@@ -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<Option<std::fs::File>>,
|
||||
}
|
||||
|
||||
impl Converter for RacingUpper {
|
||||
fn id(&self) -> &'static str {
|
||||
"test.upper"
|
||||
}
|
||||
fn version(&self) -> u32 {
|
||||
1
|
||||
}
|
||||
fn convert(&self, input: &[u8]) -> Result<Vec<u8>> {
|
||||
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();
|
||||
|
||||
Reference in new issue
Block a user