fix(loader): stage prepared images in per-writer files
write_if_changed used a shared <hash>.tmp file, so two concurrent cold launches of the same image could truncate each other's staging bytes or fail the rename after the other had published. Move publication into a portable module that stages under a unique pid/sequence name and removes it on failure. Two hermetic tests cover concurrent publishers and a failed rename. (cherry picked from commit 1aac20aa8fd97237053c985b942225d54c0194a7)
This commit is contained in:
1 parent
98b36e12a2
commit
7a4f0018b5
3 files changed
+79
-7
No files matched your search
@@ -25,6 +25,7 @@
|
|||||||
mod console_insn;
|
mod console_insn;
|
||||||
mod crash;
|
mod crash;
|
||||||
pub mod prepare;
|
pub mod prepare;
|
||||||
|
pub(crate) mod publish;
|
||||||
pub mod resolve;
|
pub mod resolve;
|
||||||
#[cfg(all(windows, feature = "diagnostics"))]
|
#[cfg(all(windows, feature = "diagnostics"))]
|
||||||
mod stacks;
|
mod stacks;
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
//! Portable publication of derived files into a shared cache directory.
|
||||||
|
//!
|
||||||
|
//! Several copies of the runner can prepare the same title at once. Each
|
||||||
|
//! writer stages into its own file, so two of them cannot truncate each
|
||||||
|
//! other's bytes or rename a file the other already published. The final
|
||||||
|
//! rename is atomic within the directory, and the staging file is removed
|
||||||
|
//! if publication fails.
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
|
static NEXT_STAGING: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
/// Writes `bytes` to `path` unless it is already there, using a staging file
|
||||||
|
/// unique to this process and call.
|
||||||
|
pub fn write_if_changed(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
|
||||||
|
if std::fs::read(path).is_ok_and(|existing| existing == bytes) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let name = path
|
||||||
|
.file_name()
|
||||||
|
.and_then(|name| name.to_str())
|
||||||
|
.unwrap_or("image");
|
||||||
|
let sequence = NEXT_STAGING.fetch_add(1, Ordering::Relaxed);
|
||||||
|
let staging = path.with_file_name(format!(
|
||||||
|
".{name}.{}.{sequence}.tmp",
|
||||||
|
std::process::id()
|
||||||
|
));
|
||||||
|
std::fs::write(&staging, bytes)?;
|
||||||
|
if let Err(error) = std::fs::rename(&staging, path) {
|
||||||
|
let _ = std::fs::remove_file(&staging);
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// Two publishers staging the same destination at once both succeed,
|
||||||
|
/// and neither leaves a staging file behind.
|
||||||
|
#[test]
|
||||||
|
fn concurrent_publishers_do_not_share_a_staging_file() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let path = dir.path().join("image.bin");
|
||||||
|
let bytes = b"a prepared image".to_vec();
|
||||||
|
let (first, second) = (path.clone(), path.clone());
|
||||||
|
let (first_bytes, second_bytes) = (bytes.clone(), bytes.clone());
|
||||||
|
let (one, two) = (
|
||||||
|
std::thread::spawn(move || write_if_changed(&first, &first_bytes)),
|
||||||
|
std::thread::spawn(move || write_if_changed(&second, &second_bytes)),
|
||||||
|
);
|
||||||
|
one.join().unwrap().unwrap();
|
||||||
|
two.join().unwrap().unwrap();
|
||||||
|
assert_eq!(std::fs::read(&path).unwrap(), bytes);
|
||||||
|
let leftovers: Vec<_> = std::fs::read_dir(dir.path())
|
||||||
|
.unwrap()
|
||||||
|
.filter_map(|entry| entry.ok())
|
||||||
|
.filter(|entry| entry.path() != path)
|
||||||
|
.collect();
|
||||||
|
assert!(leftovers.is_empty(), "staging files were removed: {leftovers:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_failed_publication_removes_its_staging_file() {
|
||||||
|
let dir = tempfile::tempdir().unwrap();
|
||||||
|
let path = dir.path().join("missing-parent").join("image.bin");
|
||||||
|
assert!(write_if_changed(&path, b"bytes").is_err());
|
||||||
|
let leftovers: Vec<_> = std::fs::read_dir(dir.path())
|
||||||
|
.unwrap()
|
||||||
|
.filter_map(|entry| entry.ok())
|
||||||
|
.collect();
|
||||||
|
assert!(leftovers.is_empty(), "no staging file survives an error");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -183,13 +183,8 @@ impl ImportResolver for WithTitleDlls<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn write_if_changed(path: &Path, bytes: &[u8]) -> Result<()> {
|
fn write_if_changed(path: &Path, bytes: &[u8]) -> Result<()> {
|
||||||
if std::fs::read(path).is_ok_and(|existing| existing == bytes) {
|
crate::publish::write_if_changed(path, bytes)
|
||||||
return Ok(());
|
.with_context(|| format!("publishing {}", path.display()))
|
||||||
}
|
|
||||||
let tmp = path.with_extension("tmp");
|
|
||||||
std::fs::write(&tmp, bytes).with_context(|| format!("writing {}", tmp.display()))?;
|
|
||||||
std::fs::rename(&tmp, path).with_context(|| format!("renaming to {}", path.display()))?;
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LoadedImage {
|
impl LoadedImage {
|
||||||
|
|||||||
Reference in new issue
Block a user