From 6934ade56c2b1b9fa796b98d41e34d7e00af606f Mon Sep 17 00:00:00 2001 From: Nathaniel Ledford Date: Thu, 19 Nov 2026 11:20:28 -0500 Subject: [PATCH] catalog: removing the log's file ends its writer thread `FsStore::remove` kept the log thread after unlinking `catalog.log`, so the next append found a file of another length and the thread held its records back. It now writes what the thread holds first, and ends the thread once the file is gone (the next append starts one), as the same method does on the segments branch. Co-Authored-By: Claude --- crates/catalog/src/store.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/catalog/src/store.rs b/crates/catalog/src/store.rs index 9ae07f2593..03c0194c95 100644 --- a/crates/catalog/src/store.rs +++ b/crates/catalog/src/store.rs @@ -414,12 +414,24 @@ impl Store for FsStore { } fn remove(&mut self, name: &str) -> io::Result<()> { + // What was handed to the log's thread is written first (a failure is the caller's: nothing is dropped). The thread + // goes only once the file is gone (it would read a missing file as another log), and the next append starts one. + #[cfg(not(target_arch = "wasm32"))] + let thread = self.log_for(name).is_some(); + #[cfg(not(target_arch = "wasm32"))] + if thread { + self.flush_log()?; + } self.folder.check()?; match std::fs::remove_file(self.path(name)) { Ok(()) => {} Err(e) if e.kind() == io::ErrorKind::NotFound => {} Err(e) => return Err(e), } + #[cfg(not(target_arch = "wasm32"))] + if thread { + self.log = None; + } // (a removed log is a log of no bytes: what the next append is checked against) self.reset_sync_log(name, 0); Ok(()) @@ -592,4 +604,22 @@ mod tests_failed_write { assert!(!dir.join("catalog.log.tmp").exists(), "a staging file was left behind"); let _ = std::fs::remove_dir_all(&dir); } + + /// A log that is removed while its thread runs ends the thread: the next append starts one for the new, empty file, and is + /// written (a thread that outlived its file would find another file than the one it wrote and hold the record back). + #[test] + fn removing_the_log_ends_its_writer() { + let dir = std::env::temp_dir().join(format!("lc-remove-log-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let mut store = FsStore::open(&dir).unwrap(); + store.append("catalog.log", b"abc\n").unwrap(); + store.flush().unwrap(); + store.remove("catalog.log").unwrap(); + assert!(!dir.join("catalog.log").exists()); + store.append("catalog.log", b"de\n").unwrap(); + store.flush().unwrap(); + assert!(store.unwritten().is_none(), "nothing is held back"); + assert_eq!(std::fs::read(dir.join("catalog.log")).unwrap(), b"de\n"); + let _ = std::fs::remove_dir_all(&dir); + } }