From 4807518c4bd7fcaf47a7f110d9027a6957ced9b8 Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Mon, 12 May 2025 09:37:04 -0700 Subject: [PATCH] Call fsync before close --- src/util.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/util.rs b/src/util.rs index 9f6689d..7bee368 100644 --- a/src/util.rs +++ b/src/util.rs @@ -179,6 +179,12 @@ pub fn write(path: impl AsRef, contents: impl AsRef<[u8]>) -> Result<()> { } // Close and rename the tmpfile. + // In some cases, errors from the last write() are reported only on close(). + // Rust ignores errors from close(), since it occurs inside `Drop`. To + // catch these errors, we manually call `File::sync_all()` first. + tmp_file + .sync_all() + .with_context(|| format!("could not sync writes to file: {}", tmp_path.display()))?; mem::drop(tmp_file); rename(&tmp_path, path) })();