sql-server panics fatally on shutdown when an external process deletes a database directory; recovery from resulting journal state is unreachable
Summary
When an external process deletes a per-database directory under the dolt server's data dir while the server has the database open, the server fatally panics during `CloseAllLocalDatabases` with:
```
fatal error: open <data_dir>/
After that panic, every subsequent server start emits `possible data loss detected in journal file at offset NNNN: corrupted journal` and fails to come up. We observed >1000 crash-loop attempts before the operator noticed and intervened. There is no built-in path to recover the journal from this state without manual surgery.
We are aware that the originating action (deleting a DB dir under a running server) is incorrect — we have a separate fix for the orchestrator that did this. This report is about dolt's failure mode being unnecessarily destructive: a missing manifest from one database during shutdown takes down the whole server and corrupts the journal for every other database it had loaded.
Repro outline
- Start `dolt sql-server` against a multi-database data_dir (e.g. `db_a`, `db_b`).
- While the server is up, externally `rm -rf <data_dir>/db_b/`.
- Issue any operation that exercises `db_b`, then send the server SIGTERM.
- Observe panic during `CloseAllLocalDatabases` → `(*GenerationalNBS).Close` → `(*NomsBlockStore).Close`.
- Restart server. Observe repeated `possible data loss detected in journal file at offset N` and inability to recover.
Stack trace from our incident (dolt 1.86.6)
``` panic: goroutine 1 [running]: runtime/debug.Stack() runtime/debug/stack.go:26 +0x64 github.com/dolthub/dolt/go/libraries/utils/errors.Fatalf(0xf47c08a8600?, {0x35975f4, 0x27}, {0xf48356a55f0, 0x1, 0x7959ac?}) github.com/dolthub/dolt/go/libraries/utils/errors/panic.go:39 +0x38 github.com/dolthub/dolt/go/store/nbs.(*NomsBlockStore).Close(0xf4767c7ea80) github.com/dolthub/dolt/go/store/nbs/store.go:1673 +0xc8 github.com/dolthub/dolt/go/store/nbs.(*GenerationalNBS).Close(0xf4767af9428) github.com/dolthub/dolt/go/store/nbs/generational_chunk_store.go:343 +0x38 github.com/dolthub/dolt/go/store/types.(*ValueStore).Close(...) github.com/dolthub/dolt/go/store/types/value_store.go:806 github.com/dolthub/dolt/go/store/datas.(*database).Close(0x0?) github.com/dolthub/dolt/go/store/datas/database_common.go:199 +0x28 github.com/dolthub/dolt/go/libraries/doltcore/dbfactory.CloseAllLocalDatabases() github.com/dolthub/dolt/go/libraries/doltcore/dbfactory/file.go:97 +0x120 main.runMain() github.com/dolthub/dolt/go/cmd/dolt/dolt.go:579 +0x2574 main.main() github.com/dolthub/dolt/go/cmd/dolt/dolt.go:227 +0x3c fatal error: open .../db_b/.dolt/noms/nbs_manifest_289032723: no such file or directory: fatal error closing table persister ```
What would help
- *Don't `Fatalf` from `(NomsBlockStore).Close` for ENOENT on the manifest temp. The DB is gone; the close has nothing left to flush. Log + skip that DB and let `CloseAllLocalDatabases` continue with the others. The whole-process kill on shutdown poisons every other DB's journal.
- Recoverable journal state for missing-DB references. Today `possible data loss detected` is sticky — the operator has to manually quarantine. A documented `fsck --revive-journal-with-data-loss`-style escape hatch (or auto-truncation behind a flag) would unblock recovery without dropping back to a JSONL snapshot.
Related: #10837 covers a downstream symptom (`dolt log --oneline` hang after `fsck --revive-journal-with-data-loss` on a recovered repo).
Why I'm filing this
Our orchestrator (gas-city) had a recurring corruption that we have a separate fix for. But: even with the orchestrator fixed, any operator action — manual cleanup, container churn, an unlucky `rm` — that disturbs a DB dir while the server is up should be a "lose that DB" event, not a "lose the entire dolt instance and require JSONL re-imports for everything" event. The current behavior is a steep cliff for what should be a recoverable misconfiguration.
@vbtcl what is your motivation for deleting a dolt internal database directory while a dolt server is running?
Our initial feeling here is that crashing on no longer being able to access the files in db_b is totally fine.
Crashing in this case should absolutely not corrupt the contents of db_a though. We need more information about what the state of db_a was and how it got into that state in order to further debug this issue. Do you still have the corrupted journal file from the equivalent of db_a when you saw this? Do you know if there were there any other manipulations of dolt-internal file-system state issued by external tools related to db_a or other databases on the server?
Thanks both — context below.
@coffeegoddd: motivation for the deletion
The Dolt server runs as the data plane for Gas Town, an orchestration platform we run for autonomous-agent coding workflows. It hosts many small Dolt databases (one per project / per coordination role: `beads_hq`, `beads_witness`, `pa`, `partcl`, `partcleda_site`, `nucleus`, `echo`, etc.) plus orphan testdb_* DBs accumulated from agent test runs.
We have an in-platform "Dolt orphan cleanup" tool that's supposed to identify and remove abandoned testdb-pattern DBs to keep the server lean. The bug: under one specific code path (a stale-DB sweep variant) it failed to lock-check the running server and proceeded to `rm -rf` a per-database dir while the server still had it open. The database it deleted in this incident was `partcleda_site` (a small, mostly-cold legacy Partcl marketing-site DB), based on the panic message:
``` fatal error: open /home/vamshi/gas-city/.beads/dolt/partcleda_site/.dolt/noms/nbs_manifest_289032723: no such file or directory ```
That orchestrator code path is what we have a fix for separately — agreed the originating action is on us. Filing this issue is purely about the blast radius when it does happen: one cold DB's missing manifest taking down every other DB the server had loaded.
@reltuk: state of `db_a` (= beads_hq) before/after
Identifying which DBs were affected. The panic backtrace shows `partcleda_site` as the trigger. After the crash, on every restart the server logged:
``` possible data loss detected in journal file at offset 66038046: corrupted journal ```
…followed immediately by:
``` connectionDb=beads_hq … error="database not found: beads_hq" … "table not found: issues" … "table not found: metadata" … "table not found: config" … "table not found: custom_statuses" ```
So the corrupted journal was `beads_hq`'s — the busiest database on the server, our HQ-level issue/mail tracker. It was hosting active write traffic up to the moment of the panic (the line right before the panic in the log is a connection on `beads_hq` writing).
`pa` (the partcl rig's beads DB) was also being written to in parallel during that time but came back up cleanly on the next restart, so the corruption was scoped to `beads_hq`.
External manipulations of `beads_hq`. None that we know of. The cleanup tool was scoped to testdb-pattern names; `beads_hq` doesn't match. No other process on the box runs `rm` / `rsync` / etc. against the Dolt data dir. All access to `beads_hq` was via SQL connections to the running server.
Do we still have the original corrupted journal? Honestly: no, sorry. We crash-looped >1000 times before noticing, then manually intervened to get the server back up — that intervention overwrote/rebuilt the journal. The current `vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv` file is from the subsequent rebuild, not the original. We don't have nightly Dolt backups configured (lesson learned, separately).
What we can still confirm, if useful:
- The `offset 66038046` value was stable across all 1000+ crash-loop log lines — same offset every time, same "corrupted journal" verdict.
- We can capture a fresh repro on a non-production test data dir if you want a deterministic test case: spin up a server with two DBs, kill one's dir mid-flight, SIGTERM, observe both the panic and the post-restart journal corruption. We hit it ~100% with the same shape.
What would be most valuable to us
The cross-DB blast radius is the actual ask. Concretely:
- `CloseAllLocalDatabases` should not panic the process when one DB's close fails — log + skip + continue closing the rest cleanly.
- A recovery path when journal corruption is detected on startup, even if it's "drop the tail past the corruption offset" with a clear opt-in flag. Currently 1000+ crash-loops and zero forward progress; the only way out was manual surgery on the journal (we ended up restoring from a JSONL export we'd taken pre-crash).
Happy to spin up a clean repro env if it'd help your team reproduce locally.
@vbtcl Yes, it would be helpful to have the repro env. Also any information on which version of dolthub/driver is implicated here.
This simple test doesn't repro on close-to-dolthub/dolt-main, but it doesn't exercise concurrent writes on the shutdown and panic path:
#!/bin/bash
set -eo pipefail
rm -rf one two
dolt sql-server &
server_pid=$!
echo "server_pid=$server_pid"
sleep 1
dolt sql -q 'create database one; use one; create table vals (id int primary key); insert into vals values (1); call dolt_commit("-Am", "commit some values");'
dolt sql -q 'create database two; use two; create table vals (id int primary key); insert into vals values (1); call dolt_commit("-Am", "commit some values");'
dolt sql -q 'use one; select sleep(2); insert into vals values (2); call dolt_commit("-Am", "commit some more values");' &
sql_pid=$!
echo "sql_pid=$sql_pid"
sleep 1
rm -rf one
wait "$sql_pid"
echo sql_pid done
dolt sql -q 'use two; insert into vals values (2); call dolt_commit("-Am", "commit another value");'
kill "$server_pid"
wait || echo sql-server crashed
echo server_pid done
dolt sql-server &
server_pid=$!
echo "server_pid=$server_pid"
sleep 1
dolt sql -q 'use two; select * from vals;'
kill "$server_pid"
wait
From a downstream orchestrator (Gas City) that manages an external dolt sql-server: a related signal that would help here is for the server to surface — via a log line or a metric — when one of its own open chunk files has been unlinked (deleted-inode-open) on the data directory it is serving. Today an orchestrator can only detect this out-of-band (Linux /proc/<pid>/fd inode inspection), and by the time the fatal shutdown path in this issue triggers it's too late to drain cleanly.
This is adjacent to the root cause already under discussion here (one deleted DB directory poisoning the whole instance): if the server could report "a data file I have open is now deleted" before the panic-on-shutdown path, an external manager could drain/restart gracefully instead of hitting the fatal exit. Raising it as a comment rather than a separate issue since it shares this thread's root cause. (We had separately considered a generic /health endpoint, but #7428 already settled that in favor of query-probing, so we're not re-opening that.)
What does it mean to "drain cleanly" here? In general, if a filesystem directory or file which the database server had exclusive ownership of and which it was using to persist database state disappears out from under it, we have typically been fine with Dolt crashing as soon as possible and preferably loudly. If that "as soon as possible" happens to be when it is shutting down, we still think that's better than no crash at all.
Once the file is gone, Dolt cannot fulfill its contract to durably store the database state going forward. It also cannot reasonably expect to establish application-level invariants which allow it to continue behaving in a way where it can surface to clients errors. The safest and most expedient thing to do is just to crash.
Even if Dolt installed something like an fs watch to detect that the file had been deleted out from under it, I think I would want it to crash, not report and allow some external process to tell it to exit cleanly.