No-Panic Policy¶
BenoDB is a long-running database process. A single unwrap() on an
unexpected None/Err in a request path, an ingest loop, or a decode path
crashes the whole daemon: in-flight queries are dropped, the process restarts,
and the WAL has to recover. Panics are therefore treated as defects in
production code, not as an acceptable error strategy.
Scope¶
Code |
Rule |
|---|---|
Production library code ( |
No |
Production binaries ( |
Same rule |
|
Exempt — tests may unwrap freely |
|
Exempt |
|
Build scripts are exempt in spirit, but |
The rule¶
Prefer, in order:
Propagate:
?with the native error type (HyperstreamError,DataFusionError,anyhow::Error) viaok_or_else/context.Make the function total: where Arrow’s
data_type()(or an equivalent guard) already proves the downcast cannot fail, use.map(|a| ...).unwrap_or(<neutral>)orif let ... else { <neutral> }instead ofunwrap(). A violated invariant then degrades tonull/false/skiprather than panicking.Documented invariant: only where the API genuinely has no infallible constructor and failure is a programming error caught at startup, add a narrowly scoped
#[allow(clippy::unwrap_used, clippy::expect_used)]with a justification comment. Every such site must also be listed below.
Enforcement¶
CI runs scripts/no_panic_check.sh as the no-panic job in .github/workflows/ci.yml. It combines two checks:
1. Ratchet (scripts/no_panic_check.sh)¶
bash scripts/no_panic_check.sh # fail if production panic sites grew, then run hard gate
bash scripts/no_panic_check.sh --write # lower the baseline after a fix
The script compiles with -D clippy::unwrap_used -D clippy::expect_used -D clippy::panic across benodb (--lib), benodb-search (--lib), benodb-iceberg (--lib), benodb-flight (--bin benodb-flight), and benodb-mcp (--bin benodb-mcp). Using explicit non-test targets leaves #[cfg(test)] tests out while comparing the total un-exempted site count against scripts/no_panic_baseline.txt.
2. Library hard gate (--features no-panic)¶
src/lib.rs, benodb-search/src/lib.rs, and crates/benodb-iceberg/src/lib.rs carry:
#![cfg_attr(
all(not(test), feature = "no-panic"),
deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)
)]
After the ratchet passes, scripts/no_panic_check.sh runs cargo clippy --no-deps --lib --features no-panic so any new un-exempted unwrap(), expect(), or panic! in the core library fails CI directly. The benodb-iceberg crate carries its own no-panic feature (the root package’s flag does not reach it), and the script gates it separately with cargo clippy --no-deps -p benodb-iceberg --lib --features no-panic.
Baseline and inventory¶
Initial production baseline (measured before remediation): 289 sites
(247 core library + 42 search library). Current baseline: 0 — the no-panic job enforces both the 0-site ratchet and the --lib --features no-panic hard gate in CI.
Two module-scoped exemptions remain, both justified in source:
Exemption |
Sites |
Why |
|---|---|---|
|
69 |
Vendored |
|
13 |
|
Everything else in the core and search library crates — graph UDFs, the storage/read path, the ingest and index-build path, merge/compaction/catalog, the algorithms, the GPU backends, the Python/FFI bindings, and src/lib.rs — is clean under the --lib --features no-panic gate (with the narrowly scoped startup invariants in the allowlist below).
Remaining gaps (not panic-free end to end)¶
Vendored
hnsw_rsgraph invariants. The module exemption coversunwrap/expect/panicthat assert graph invariants in the builder and in the inner search loop — inputs the engine constructs itself. No deserialization path is exempt: every reachable sidecar decode either returnsResultor runs underdecode_guard(see below).hnswioreader.load_description,load_hnsw, andload_hnsw_with_dist(src/core/index/hnsw_rs/hnswio.rs) panic on short reads and are covered by the module exemption. They have no caller in the crate —grep -rn 'load_hnsw' --include='*.rs' src/matches only the definitions — because the reachable per-segment loader isHnswIvfIndex, which returnsResult. They are retained as vendored API surface, not as a code path.Dev/bench utilities.
src/bin/*is compiled by the hard gate (--bins) and reports zero sites, so the one-off tools (ingest_wikipedia,bench_*,verify_*,setup_test_data,relocate_table,fix_table_paths,check_table_paths,reindex_inverted) are covered rather than exempt. The narrowly scoped startup invariants in the allowlist below are the only remaining#[allow]s.
What v0.17.4 closes¶
Per-segment sidecar decode is guarded.
HnswIvfIndex::load_lazy_from_container,HnswIvfIndex::deserialize_single_cluster, andHnswIvfIndex::loadrun their bodies underdecode_guard, which converts a decoder panic intoErr, so a corrupt artifact degrades the query to an exact scan instead of aborting the process.corrupt_per_segment_sidecars_return_errors_not_panicsdrives garbage, empty, and non-Parquet blobs through the reachable entry points.The root binaries and
benodb-searchare gated.scripts/no_panic_check.shruns--lib --binsfor the root package, a separate--lib --binsgate forbenodb-search, and ratchets thebenodb-searchbinary alongsidebenodb-flightandbenodb-mcp.
Nothing on this page is deferred to a later release. The library gate and the binary gates now cover every target the engine ships.
Residual-invariant allowlist¶
These sites are permitted to keep a documented #[allow] because the API has no
infallible form and failure is a startup-time programming error, not a runtime
condition:
src/telemetry/metrics.rs—prometheusmetric constructors (IntGauge::new,IntCounterVec::new, …) returnResultand are only fallible on an invalid name or duplicate registration. All names/help strings are static constants. Failure happens once at startup before the server binds and is unreachable from a request. Guarded by#[allow(clippy::unwrap_used, clippy::expect_used)]onMetrics::new.Compile-time regex literals (e.g.
Regex::new(r"...")) — the pattern is a literal, so construction cannot fail. These may useexpectwith a message.chrono::NaiveDate::from_ymd_opt(1970, 1, 1)— replaced byNaiveDate::default()where possible (it is 1970-01-01), which is infallible.src/lib.rsjemalloc ctl —tikv_jemalloc_ctl::epoch::mib()is a process-lifetime singleton; failure indicates a broken allocator build.src/core/table/state.rsshared_fallback_runtime()— asynchronously constructed tables have no owned runtime;Table::runtime()now returns a shared process runtime instead of panicking, so the previous “Runtime not available on async Table” panic is gone (v0.17.4). Building that shared runtime has no infallible form and fails only if the OS refuses threads or event sources, so the singleexpectis a documented startup invariant, guarded by#[allow(clippy::expect_used)].src/python/helpers.rsTOKIO_RUNTIME—Runtime::new()has no infallible form and fails only if the OS cannot give the process a reactor and worker threads. If that happens the binding layer is unusable, so failing once at first use with a clear message is correct. Guarded by#[allow(clippy::expect_used)].src/core/segment.rsindexing_pool()andsrc/core/table/maintenance.rsmaintenance_pool()—rayon’sThreadPoolBuilder::build()has no infallible form and fails only on OS resource exhaustion (thread creation). The pools are process-lifetime singletons built on first use; a failure is unrecoverable at startup. Guarded by a function-level#[allow(clippy::expect_used)].
Anything not in this list should be converted to ?, made total, or the
allowlist entry justified in review.