Repository navigation
fix(state): heal state.db before reading settings - #1902
Merged
Merged
Conversation
Boot read settings from state.db (config.Load) before state.Open ran the snapshot heal, so a damaged state.db stopped every start. Boot now restores state.db from a usable snapshot first when the settings read fails with SQLite corruption, even if the verified-good marker is present. Without a usable snapshot nothing changes. When state.Open restores an older snapshot after Load, the snapshot's settings run and /api/health reports the step back instead of Core exiting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Item F2 of the stable plan.
Problem
Boot reads settings from state.db (
config.Load, main.go:412) beforestate.Open(main.go:507), and onlystate.Openruns the snapshot heal. Damage to the first 4 KiB of state.db makesLoadfail withread stored settings: file is not a database (26), Core exits, and systemd restarts it into the same failure every 5 s. A goodstate.db.snapshotsits next to it unused.The verified-good marker makes it worse.
state.db.cleansurvives crashes, andopenCheckedskips the check while it exists. After a power loss on a box that booted before, evenstate.Openalone would not heal.A second path: if
Loadreads revision R from a damaged but readable file andstate.Openthen restores a snapshot at revision R-1,InitializeStorageexits with "database recovery lost current settings". The next boot then runs R-1 anyway, and no longer reports the heal.Change
state.HealBeforeLoad(path)reads the settings row. Only on a SQLite corrupt (11) or not-a-database (26) error, and only when the snapshot passes quick_check, it removes the marker and runsopenChecked, which restores the snapshot. A missing, busy or readable file, or one without a usable snapshot, is left alone, so a healthy boot does no extra checks and the owner's damaged file is never swapped for an empty one beforeLoad.config.HealSettingsDatabase(seed)finds the database the seed names and calls it. main.go calls it beforeconfig.Loadand adds the event to the store, so it shows on/api/health.InitializeStorageaccepts an older settings revision only when this boot restored state.db from its snapshot. It runs the snapshot's settings and adds a heal event that names both revisions. Every other mismatch still exits.Store.HealEventsand the newRecordHealEventtakehealMu.Tests
TestCorruptStateDBWithGoodSnapshotHealsBeforeLoadis the audit repro (caa-133), renamed. It fails on master in both subtests (marker present and marker removed) and passes here.TestOlderSnapshotSettingsRunAndAreReportedfails with "database recovery lost current settings" without theInitializeStoragechange and passes with it.TestHealthyStateDBIsNotHealedBeforeLoad,TestHealBeforeLoadWithoutSnapshotChangesNothing,TestHealBeforeLoadIgnoresMissingDatabase.go test -race -count=1 ./internal/state/... ./internal/config/... ./cmd/ftw/... ./internal/api/...: ok.go vet ./...andgo build ./...: ok.Risk
Medium. The new code runs only after a settings read has failed with SQLite corruption, a case where Core exited before. The snapshot check and restore then run before the API port binds. The snapshot leaves out bulky history tables, so the check is short. A restore can bring back settings older than the last save;
/api/healthnow says so on the boot that does it. If the API port changed between the two revisions, the box keeps the newer port until the next restart.🤖 Generated with Claude Code