fix: handle missing rocksdb gracefully in read-only db commands (#22394)

Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Dan Cline
2026-02-20 12:18:43 -05:00
committed by GitHub
parent d23c244cd1
commit 1ff88e43cd
3 changed files with 33 additions and 5 deletions

View File

@@ -146,11 +146,24 @@ impl<C: ChainSpecParser> EnvironmentArgs<C> {
})
}
};
let rocksdb_provider = RocksDBProvider::builder(data_dir.rocksdb())
.with_default_tables()
.with_database_log_level(self.db.log_level)
.with_read_only(!access.is_read_write())
.build()?;
let rocksdb_provider = if !access.is_read_write() && !RocksDBProvider::exists(&rocksdb_path)
{
// RocksDB database doesn't exist yet (e.g. datadir restored from a snapshot
// or created before RocksDB storage). Create an empty one so read-only
// commands can proceed.
debug!(target: "reth::cli", ?rocksdb_path, "RocksDB not found, initializing empty database");
reth_fs_util::create_dir_all(&rocksdb_path)?;
RocksDBProvider::builder(data_dir.rocksdb())
.with_default_tables()
.with_database_log_level(self.db.log_level)
.build()?
} else {
RocksDBProvider::builder(data_dir.rocksdb())
.with_default_tables()
.with_database_log_level(self.db.log_level)
.with_read_only(!access.is_read_write())
.build()?
};
let provider_factory =
self.create_provider_factory(&config, db, sfp, rocksdb_provider, access, runtime)?;

View File

@@ -691,6 +691,14 @@ impl RocksDBProvider {
RocksDBBuilder::new(path)
}
/// Returns `true` if a `RocksDB` database exists at the given path.
///
/// Checks for the presence of the `CURRENT` file, which `RocksDB` creates
/// when initializing a database.
pub fn exists(path: impl AsRef<Path>) -> bool {
path.as_ref().join("CURRENT").exists()
}
/// Returns `true` if this provider is in read-only mode.
pub fn is_read_only(&self) -> bool {
matches!(self.0.as_ref(), RocksDBProviderInner::ReadOnly { .. })

View File

@@ -74,6 +74,13 @@ impl RocksDBProvider {
RocksDBBuilder::new(path)
}
/// Returns `true` if a `RocksDB` database exists at the given path (stub implementation).
///
/// Always returns `false` since `RocksDB` is not available.
pub fn exists(_path: impl AsRef<Path>) -> bool {
false
}
/// Check consistency of `RocksDB` tables (stub implementation).
///
/// Returns `None` since there is no `RocksDB` data to check when the feature is disabled.