GPU: serialize pipeline library atomic writes with unique temp files
save() releases LAST and library locks before calling write_atomically(), which always wrote to the same fixed host-pipelines.tmp path. Concurrent worker saves and shutdown save(true) can truncate or unlink each other's temp file on Windows. - Serialize write_atomically() with a mutex and unique per-call temp filename (.tmp-<pid>-<n>), cleaning up the temp file on rename failure. - Add a 4-thread concurrent write_atomically stress test. (cherry picked from commit be824d9d218320c71f2623ed4b14045210785db5)
This commit is contained in:
1 parent
9c63a43431
commit
a5c98fe072
1 file changed
+38
-4
@@ -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");
|
use std::sync::atomic::AtomicU64;
|
||||||
std::fs::write(&tmp, bytes)?;
|
static WRITING: Mutex<()> = Mutex::new(());
|
||||||
std::fs::rename(&tmp, path)
|
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]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user