From c0acf00a022a759001c220083210937a2a983165 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Fri, 6 Nov 2026 17:58:34 +0100 Subject: [PATCH] merkle: wait for a dropped session's warm-up worker to stop With `warm_up` enabled, each session runs its warm-up on a detached thread that holds a `Store`, and with it the database directory lock. Dropping a session without finishing it only disconnected the worker's channels, so the worker could still be running when the `Nomt` was dropped. Reopening the same path right away then failed with "Failed to lock directory: Resource temporarily unavailable": in 226 of 300 tries of `drop(session); drop(nomt); Nomt::open(..)`. Give `WarmUpHandle` a `Drop` that disconnects the channels and waits for the worker to report back, as `ReverseDeltaBuilder` already does for the rollback worker. The worker drops its `Store` before reporting. A finished session's output has already been taken, so its drop returns at once. Dropping an unfinished session now takes ~20-40us instead of ~0. Found by the concurrent_sessions fuzz target. Co-Authored-By: Claude Opus 5.5 --- nomt/src/merkle/mod.rs | 13 ++++++++ nomt/tests/reopen_after_drop.rs | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 nomt/tests/reopen_after_drop.rs diff --git a/nomt/src/merkle/mod.rs b/nomt/src/merkle/mod.rs index 56a7298d4a..b8b895e46b 100644 --- a/nomt/src/merkle/mod.rs +++ b/nomt/src/merkle/mod.rs @@ -616,6 +616,19 @@ struct WarmUpHandle { output_rx: Receiver>>, } +impl Drop for WarmUpHandle { + fn drop(&mut self) { + // Wait for the worker to stop. It holds a `Store`, and with it the database directory + // lock, so a session dropped without finishing must not leave it running past `Nomt`: + // reopening the database would then fail to take the lock. Disconnecting the channels + // tells the worker its session is gone. After a finished session the output has already + // been taken and this returns at once. + self.finish_tx = channel::bounded(0).0; + self.warmup_tx = channel::unbounded().0; + let _ = self.output_rx.recv(); + } +} + /// The warm-up runs until its session finishes, so it gets a thread of its own rather than one of /// the commit workers: coexisting sessions would otherwise occupy all of them. fn spawn_warm_up(params: worker::WarmUpParams) -> WarmUpHandle { diff --git a/nomt/tests/reopen_after_drop.rs b/nomt/tests/reopen_after_drop.rs new file mode 100644 index 0000000000..083881c4a0 --- /dev/null +++ b/nomt/tests/reopen_after_drop.rs @@ -0,0 +1,58 @@ +//! Once a `Nomt` and its sessions are dropped, the database can be reopened right away. + +use nomt::{hasher::Blake3Hasher, KeyReadWrite, Nomt, Options, SessionParams}; +use nomt_test_utils::account_path; + +fn open(path: &std::path::Path, warm_up: bool) -> Nomt { + let mut o = Options::new(); + o.path(path); + o.bitbox_seed([0; 16]); + o.hashtable_buckets(4096); + o.io_workers(1); + o.commit_concurrency(1); + o.warm_up(warm_up); + o.rollback(true); + Nomt::open(o).expect("reopening right after dropping the database") +} + +#[test] +fn reopen_right_after_dropping_sessions() { + let dir = tempfile::tempdir().unwrap(); + + // Give the warm-ups some pages to fetch. + let db = open(dir.path(), false); + let session = db.begin_session(SessionParams::default()); + let mut writes = (0..1000) + .map(|i| (account_path(i), KeyReadWrite::Write(Some(vec![1; 64])))) + .collect::>(); + writes.sort_by_key(|(key, _)| *key); + session.finish(writes).unwrap().commit(&db).unwrap(); + drop(db); + + for round in 0..50 { + let db = open(dir.path(), true); + + let key = account_path(round); + let session = db.begin_session(SessionParams::default()); + session.warm_up(key); + session + .finish(vec![(key, KeyReadWrite::Write(Some(vec![round as u8])))]) + .unwrap() + .commit(&db) + .unwrap(); + + // Sessions dropped without finishing, with warm-ups in flight and idle, right before the + // database itself. + let session = db.begin_session(SessionParams::default()); + for i in 0..100 { + session.warm_up(account_path(round * 10 + i)); + session.preserve_prior_value(account_path(round * 10 + i)); + } + drop(session); + drop(db.begin_session(SessionParams::default())); + drop(db); + } + + let db = open(dir.path(), true); + assert_eq!(db.read(account_path(49)).unwrap(), Some(vec![49])); +}