From 6b4a658a06b40b921c6892bc72f18120d66379d1 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Fri, 20 Nov 2026 21:35:22 -0600 Subject: [PATCH 1/2] fix(id): fsync temp files before rename in SequenceFile, registry and list-store saves Route next_sequence, save_registry and ListVersionStore::save through one durable_write helper: sync the temp file before rename and sync the parent directory after, matching local_claims.rs and world_store.rs. Refs #12152. --- pkgs/id/crates/id-core/src/durable_write.rs | 42 +++++++++++++++++++ pkgs/id/crates/id-core/src/lib.rs | 1 + pkgs/id/crates/id-core/src/sanctions_store.rs | 4 +- .../id-core/src/transport_advertisement.rs | 3 +- pkgs/id/crates/id-store/src/tags.rs | 8 +--- 5 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 pkgs/id/crates/id-core/src/durable_write.rs diff --git a/pkgs/id/crates/id-core/src/durable_write.rs b/pkgs/id/crates/id-core/src/durable_write.rs new file mode 100644 index 0000000000..f0f726b5df --- /dev/null +++ b/pkgs/id/crates/id-core/src/durable_write.rs @@ -0,0 +1,42 @@ +//! Crash-safe replacement of a small state file: the bytes reach disk before the +//! rename makes them visible at `path`, so a power loss never leaves an empty or +//! torn file at the real path. +use std::fs::File; +use std::io::Write as _; +use std::path::Path; + +/// Writes `bytes` to `tmp`, syncs it, renames it over `path`, then syncs the parent directory. +pub fn write_durable(path: &Path, tmp: &Path, bytes: &[u8]) -> std::io::Result<()> { + { + let mut file = File::create(tmp)?; + file.write_all(bytes)?; + file.sync_all()?; + } + std::fs::rename(tmp, path)?; + if let Some(dir) = path.parent().filter(|dir| !dir.as_os_str().is_empty()) + && let Ok(dir) = File::open(dir) + { + let _ = dir.sync_all(); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::write_durable; + + #[test] + fn replaces_file_and_leaves_no_temp() { + let dir = std::env::temp_dir().join(format!("durable-write-{}", std::process::id())); + std::fs::create_dir_all(&dir).expect("create dir"); + let path = dir.join("state.json"); + let tmp = dir.join("state.json.tmp"); + + write_durable(&path, &tmp, b"first").expect("first write"); + write_durable(&path, &tmp, b"second").expect("second write"); + + assert_eq!(std::fs::read(&path).expect("read"), b"second"); + assert!(!tmp.exists()); + std::fs::remove_dir_all(&dir).expect("cleanup"); + } +} diff --git a/pkgs/id/crates/id-core/src/lib.rs b/pkgs/id/crates/id-core/src/lib.rs index 29d8ea1570..6aafef241a 100644 --- a/pkgs/id/crates/id-core/src/lib.rs +++ b/pkgs/id/crates/id-core/src/lib.rs @@ -30,6 +30,7 @@ pub mod boundary_surfaces; pub mod canonical_cbor; pub mod connectivity; pub mod discovery; +pub mod durable_write; pub mod frames; pub mod graph; pub mod hex; diff --git a/pkgs/id/crates/id-core/src/sanctions_store.rs b/pkgs/id/crates/id-core/src/sanctions_store.rs index 0bd76b93d6..4cc566bddd 100644 --- a/pkgs/id/crates/id-core/src/sanctions_store.rs +++ b/pkgs/id/crates/id-core/src/sanctions_store.rs @@ -508,8 +508,8 @@ impl ListVersionStore { let bytes = serde_json::to_vec_pretty(self).map_err(|e| ListStoreError::Codec(e.to_string()))?; let tmp = path.with_extension("tmp"); - std::fs::write(&tmp, &bytes).map_err(|e| ListStoreError::Io(e.to_string()))?; - std::fs::rename(&tmp, path).map_err(|e| ListStoreError::Io(e.to_string()))?; + crate::durable_write::write_durable(path, &tmp, &bytes) + .map_err(|e| ListStoreError::Io(e.to_string()))?; Ok(()) } diff --git a/pkgs/id/crates/id-core/src/transport_advertisement.rs b/pkgs/id/crates/id-core/src/transport_advertisement.rs index a59f29b8e7..6e63078873 100644 --- a/pkgs/id/crates/id-core/src/transport_advertisement.rs +++ b/pkgs/id/crates/id-core/src/transport_advertisement.rs @@ -346,8 +346,7 @@ impl SequenceFile { pub fn next_sequence(&self) -> std::io::Result { let next = self.last_issued()?.saturating_add(1).max(1); let tmp = self.path.with_extension("tmp"); - std::fs::write(&tmp, next.to_string())?; - std::fs::rename(&tmp, &self.path)?; + crate::durable_write::write_durable(&self.path, &tmp, next.to_string().as_bytes())?; Ok(next) } } diff --git a/pkgs/id/crates/id-store/src/tags.rs b/pkgs/id/crates/id-store/src/tags.rs index cdd38f8fcd..07f74e8605 100644 --- a/pkgs/id/crates/id-store/src/tags.rs +++ b/pkgs/id/crates/id-store/src/tags.rs @@ -1744,14 +1744,8 @@ async fn save_registry(path: &Path, registry: &Registry) -> Result<()> { .await .context("creating registry parent directory")?; } - // Atomic write: temp file then rename. let tmp = path.with_extension("json.tmp"); - tokio::fs::write(&tmp, &json) - .await - .context("writing registry temp file")?; - tokio::fs::rename(&tmp, path) - .await - .context("renaming registry temp to final")?; + id_core::durable_write::write_durable(path, &tmp, &json).context("writing registry")?; Ok(()) } From d86e9c944da9fdcacefcf6c1b5e2c94289df571f Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Fri, 20 Nov 2026 22:02:35 -0600 Subject: [PATCH 2/2] fix(id): sync current dir for bare-filename durable writes; allow test unwraps --- pkgs/id/crates/id-core/src/durable_write.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkgs/id/crates/id-core/src/durable_write.rs b/pkgs/id/crates/id-core/src/durable_write.rs index f0f726b5df..41eb8d98de 100644 --- a/pkgs/id/crates/id-core/src/durable_write.rs +++ b/pkgs/id/crates/id-core/src/durable_write.rs @@ -13,15 +13,18 @@ pub fn write_durable(path: &Path, tmp: &Path, bytes: &[u8]) -> std::io::Result<( file.sync_all()?; } std::fs::rename(tmp, path)?; - if let Some(dir) = path.parent().filter(|dir| !dir.as_os_str().is_empty()) - && let Ok(dir) = File::open(dir) - { + let dir = path + .parent() + .filter(|dir| !dir.as_os_str().is_empty()) + .unwrap_or(Path::new(".")); + if let Ok(dir) = File::open(dir) { let _ = dir.sync_all(); } Ok(()) } #[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used)] mod tests { use super::write_durable;