From 9127563914fa6c77587b058bed20400ef48bde36 Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Sat, 31 Jan 2026 17:46:11 +0100 Subject: [PATCH] fix: cleanup entire temp directory when using testing_node (#18399) Co-authored-by: Matthias Seitz Co-authored-by: Amp --- crates/node/builder/src/builder/mod.rs | 4 ++- crates/storage/db/src/lib.rs | 38 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/node/builder/src/builder/mod.rs b/crates/node/builder/src/builder/mod.rs index ef28ad641a..204b67630c 100644 --- a/crates/node/builder/src/builder/mod.rs +++ b/crates/node/builder/src/builder/mod.rs @@ -251,6 +251,8 @@ impl NodeBuilder { } /// Creates a preconfigured node for testing purposes with a specific datadir. + /// + /// The entire `datadir` will be cleaned up when the node is dropped. #[cfg(feature = "test-utils")] pub fn testing_node_with_datadir( mut self, @@ -268,7 +270,7 @@ impl NodeBuilder { let data_dir = path.unwrap_or_chain_default(self.config.chain.chain(), self.config.datadir.clone()); - let db = reth_db::test_utils::create_test_rw_db_with_path(data_dir.db()); + let db = reth_db::test_utils::create_test_rw_db_with_datadir(data_dir.data_dir()); WithLaunchContext { builder: self.with_database(db), task_executor } } diff --git a/crates/storage/db/src/lib.rs b/crates/storage/db/src/lib.rs index cd37f50d61..be823616ac 100644 --- a/crates/storage/db/src/lib.rs +++ b/crates/storage/db/src/lib.rs @@ -203,6 +203,26 @@ pub mod test_utils { Arc::new(TempDatabase::new(db, path)) } + /// Create read/write database for testing within a data directory. + /// + /// The database is created at `datadir/db`, and `TempDatabase` will clean up the entire + /// `datadir` on drop. + #[track_caller] + pub fn create_test_rw_db_with_datadir>( + datadir: P, + ) -> Arc> { + let datadir = datadir.as_ref().to_path_buf(); + let db_path = datadir.join("db"); + let emsg = format!("{ERROR_DB_CREATION}: {db_path:?}"); + let db = init_db( + &db_path, + DatabaseArguments::new(ClientVersion::default()) + .with_max_read_transaction_duration(Some(MaxReadTransactionDuration::Unbounded)), + ) + .expect(&emsg); + Arc::new(TempDatabase::new(db, datadir)) + } + /// Create read only database for testing #[track_caller] pub fn create_test_ro_db() -> Arc> { @@ -235,6 +255,24 @@ mod tests { use std::time::Duration; use tempfile::tempdir; + #[test] + fn test_temp_database_cleanup() { + // Test that TempDatabase properly cleans up its directory when dropped + let temp_path = { + let db = crate::test_utils::create_test_rw_db(); + let path = db.path().to_path_buf(); + assert!(path.exists(), "Database directory should exist while TempDatabase is alive"); + path + // TempDatabase dropped here + }; + + // Verify the directory was cleaned up + assert!( + !temp_path.exists(), + "Database directory should be cleaned up after TempDatabase is dropped" + ); + } + #[test] fn db_version() { let path = tempdir().unwrap();