fix(sync): reap stale running rows on primary startup#10
Merged
Conversation
A sync_status row can be left in 'running' state forever if the primary process is killed mid-cycle before RecordSyncComplete runs — typically during a rolling deploy that terminates the old primary before it finishes syncing. The phantom row then shows up in /ui/about and /readyz queries that search by status, even though nothing is actually in flight. Worker.running is an in-memory atomic reset on process start, so no future sync is actually blocked by the stale row — the fix is purely cosmetic, transitioning orphans to 'failed' with an explanatory error_message on every primary startup. Safe under Consul lease semantics: only one primary runs at a time, and the reap fires BEFORE the first sync tick, so there's no overlap with a legitimate in-flight row. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Code Metrics Report
Code coverage of files in pull request scope (29.9%)
Reported by octocov |
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.
Summary
ReapStaleRunningRowshelper ininternal/sync/status.gothat transitions everystatus='running'row tostatus='failed'with an explanatoryerror_messagevia a single SQLUPDATEcmd/peeringdb-plus/main.goon the primary at startup, immediately afterInitStatusTableMotivation
We currently have a stale row 929 in the production
sync_statustable inrunningstate. It's left over from the rolling deploy that destroyed the original LHR primary machine (801e9df646e918) — that process was killed mid-cycle beforeRecordSyncCompleteran, and the running row has no process to clean it up. Phantom rows like this show up in/ui/aboutfreshness queries and/readyzfallback logic, even thoughWorker.runningis a per-process atomic and nothing is actually in flight.Safety
Safe under Consul lease semantics:
main.goBEFORE the first sync tick, so there's no overlap with a legitimate in-flight rowRecordSyncComplete's eventualUPDATE ... WHERE id = ?would win anyway if there were an overlap (latest-write-wins)Test plan
go test -race ./internal/sync/... ./cmd/peeringdb-plus/...go vet ./...golangci-lint run ./...(0 issues)After merge + deploy, the existing prod row 929 should be transitioned to
failedwitherror_message='startup reap: process restarted before sync completed'.🤖 Generated with Claude Code