Skip to content

Commit 2a10c23

Browse files
fix: atomically replace cache manifests via temp-file rename
Signed-off-by: Andrew Paprotsky <apaprotskyi@nvidia.com>
1 parent a3c3b89 commit 2a10c23

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

modelexpress_common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fd-lock = { workspace = true }
4040
[dev-dependencies]
4141
google-cloud-gax = { workspace = true }
4242
mockall = "0.13"
43-
tempfile = "3.20"
43+
tempfile = { workspace = true }
4444
tokio-test = "0.4"
4545
wiremock = "0.6.5"
4646

modelexpress_common/src/providers/gcs.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -351,27 +351,15 @@ impl<'a> ModelDir<'a> {
351351

352352
fs::write(&temp_path, payload)
353353
.with_context(|| format!("Failed to write manifest '{}'", temp_path.display()))?;
354-
match fs::remove_file(&manifest_path) {
355-
Ok(()) => {}
356-
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
357-
Err(err) => {
358-
let _ = fs::remove_file(&temp_path);
359-
return Err(anyhow::anyhow!(
360-
"Failed to replace manifest '{}': could not remove existing file: {}",
361-
manifest_path.display(),
362-
err
363-
));
364-
}
365-
}
366-
367-
if let Err(err) = fs::rename(&temp_path, &manifest_path) {
368-
let _ = fs::remove_file(&temp_path);
369-
return Err(anyhow::anyhow!(
370-
"Failed to move manifest '{}' into '{}': {}",
354+
if let Err(err) = fs::rename(&temp_path, &manifest_path).with_context(|| {
355+
format!(
356+
"Failed to move manifest '{}' into '{}'",
371357
temp_path.display(),
372-
manifest_path.display(),
373-
err
374-
));
358+
manifest_path.display()
359+
)
360+
}) {
361+
let _ = fs::remove_file(&temp_path);
362+
return Err(err);
375363
}
376364

377365
Ok(())
@@ -2377,6 +2365,10 @@ mod tests {
23772365
)
23782366
.expect("Expected manifest JSON");
23792367
assert_eq!(parsed, final_manifest, "scenario={name}");
2368+
assert!(
2369+
!model_dir_state.manifest_temp_path().exists(),
2370+
"scenario={name}"
2371+
);
23802372
}
23812373
}
23822374

0 commit comments

Comments
 (0)