|
@@ -673,11 +673,22 @@ fn replace_library(dir: &std::path::Path, tmp: &std::path::Path) -> std::io::Res
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Writes `bytes` to a temporary file beside `path`, then renames it over
|
|
/// Writes `bytes` to a temporary file beside `path`, then renames it over
|
|
|
-/// `path`: a reader never sees a part.
|
|
|
|
|
|
|
+/// `path`: a reader never sees a part. Serialised so concurrent worker and
|
|
|
|
|
+/// shutdown saves do not race on the same staging file or target rename.
|
|
|
fn write_atomically(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()> {
|
|
fn write_atomically(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()> {
|
|
|
- let tmp = path.with_extension("tmp");
|
|
|
|
|
- std::fs::write(&tmp, bytes)?;
|
|
|
|
|
- std::fs::rename(&tmp, path)
|
|
|
|
|
|
|
+ use std::sync::atomic::AtomicU64;
|
|
|
|
|
+ static WRITING: Mutex<()> = Mutex::new(());
|
|
|
|
|
+ static NEXT: AtomicU64 = AtomicU64::new(0);
|
|
|
|
|
+ let _guard = WRITING.lock().unwrap_or_else(|e| e.into_inner());
|
|
|
|
|
+ let n = NEXT.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
+ let mut name = path.file_name().unwrap_or_default().to_os_string();
|
|
|
|
|
+ name.push(format!(".tmp-{}-{n}", std::process::id()));
|
|
|
|
|
+ let tmp = path.with_file_name(name);
|
|
|
|
|
+ let res = std::fs::write(&tmp, bytes).and_then(|()| std::fs::rename(&tmp, path));
|
|
|
|
|
+ if res.is_err() {
|
|
|
|
|
+ let _ = std::fs::remove_file(&tmp);
|
|
|
|
|
+ }
|
|
|
|
|
+ res
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Title pipeline keys requested by draws or dispatches (this run and the last).
|
|
/// Title pipeline keys requested by draws or dispatches (this run and the last).
|
|
@@ -848,4 +859,27 @@ mod tests {
|
|
|
assert!(bytes == &expected[..], "page {page} of the replaced library no longer reads as the old file");
|
|
assert!(bytes == &expected[..], "page {page} of the replaced library no longer reads as the old file");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn concurrent_write_atomically_never_fails_or_truncates() {
|
|
|
|
|
+ let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
+ let target = dir.path().join("host-pipelines.bin");
|
|
|
|
|
+ let threads: Vec<_> = (0..4)
|
|
|
|
|
+ .map(|t| {
|
|
|
|
|
+ let target = target.clone();
|
|
|
|
|
+ std::thread::spawn(move || {
|
|
|
|
|
+ let payload = vec![b'a' + t as u8; 4096];
|
|
|
|
|
+ for _ in 0..30 {
|
|
|
|
|
+ write_atomically(&target, &payload).unwrap();
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ .collect();
|
|
|
|
|
+ for t in threads {
|
|
|
|
|
+ t.join().unwrap();
|
|
|
|
|
+ }
|
|
|
|
|
+ let final_bytes = std::fs::read(&target).unwrap();
|
|
|
|
|
+ assert_eq!(final_bytes.len(), 4096);
|
|
|
|
|
+ assert!(final_bytes.iter().all(|&b| b == final_bytes[0]));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|