From 1ba0f193a9c33857023de21a8393aa68d26a8844 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Mon, 21 Nov 2022 16:20:14 -0500 Subject: [PATCH] fix: fsync parent directory before returning that new segment file was created --- wal/src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/wal/src/lib.rs b/wal/src/lib.rs index 3682614a27..24e8bc7321 100644 --- a/wal/src/lib.rs +++ b/wal/src/lib.rs @@ -99,6 +99,11 @@ pub enum Error { source: std::io::Error, path: PathBuf, }, + + OpenSegmentDirectory { + source: std::io::Error, + path: PathBuf, + }, } /// A specialized `Result` for WAL-related errors @@ -394,8 +399,13 @@ struct OpenSegmentFile { impl OpenSegmentFile { async fn new_in_directory(dir: impl Into) -> Result { let dir = dir.into(); + let dir_for_closure = dir.clone(); let (tx, rx) = mpsc::channel(10); - let task = tokio::task::spawn_blocking(|| Self::task_main(rx, dir)); + let task = tokio::task::spawn_blocking(move || Self::task_main(rx, dir_for_closure)); + std::fs::File::open(&dir) + .context(OpenSegmentDirectorySnafu { path: dir })? + .sync_all() + .expect("fsync failure"); Ok(Self { tx, task }) }