From 855568fa341ea352a1064211f20a15ad92c8a633 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Mon, 13 May 2024 19:37:35 -0400 Subject: [PATCH] Synchronize files after writing (#10735) fsync(2) is a somewhat expensive operation that flushes writes to the underlying disk/SSD. It's typically used by databases to ensure that writes survive very hard failure scenarios like your cat kicking the plug out of the wall. Synchronizing isn't automatically done by `flush`ing (from the `std::io::Write` or `tokio::io::AsyncWriteExt` traits). From the [`tokio::fs::File`] moduledocs: > To ensure that a file is closed immediately when it is dropped, you > should call `flush` before dropping it. Note that this does not ensure > that the file has been fully written to disk; the operating system > might keep the changes around in an in-memory buffer. See the > `sync_all` method for telling the OS to write the data to disk. [`tokio::fs::File`]: https://docs.rs/tokio/latest/tokio/fs/struct.File.html --- helix-view/src/document.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 3393fbed7633..b8d318bb8690 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -930,6 +930,7 @@ impl Document { let write_result: anyhow::Result<_> = async { let mut dst = tokio::fs::File::create(&write_path).await?; to_writer(&mut dst, encoding_with_bom_info, &text).await?; + dst.sync_all().await?; Ok(()) } .await;