Repository navigation
fix(db-init): the fail-closed check reads the list without a pipeline - #391
Merged
Merged
Conversation
printf '%s\n' "$fail_closed" | grep -Fqx "$name" stops reading as soon as grep -q finds the match; if printf is still writing the rest of the list when grep closes its end of the pipe, printf dies of SIGPIPE and under set -uo pipefail the if sees that non-zero exit instead of grep's match, so a migration that must stop the adoption is swallowed as 'not found'. Same class as #389's migrate.sh filename check. Replaced with a case match against the newline-delimited list, which never pipes into anything. A test stalls a stub printf between the matching line and the rest to turn the real ~0.05% race into something that reproduces every run, and shows the fixed form never misses.
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.
What
docker/db-init/adopt-ledger.sh:40had the same defect class PR #389 fixed inbackend/scripts/migrate.sh:printf '%s\n' "$fail_closed" | grep -Fqx "$name"stops reading as soon asgrep -qfinds the match. Ifprintfis still writing the rest of the list when grep closes its end of the pipe, printf dies of SIGPIPE, and underset -uo pipefailtheifsees that non-zero exit instead of grep's match — so a migration that must stop the adoption is silently swallowed as "not found", and the script goes on to record a failed migration as applied.Measured during the #389 review: 3/6000 runs on the real (small, 17-name) list; 3/4000 with a failing psql stub reproducing the exact swallow.
Fix
Replaced the pipeline with a
casematch against the newline-delimited list — no pipe, so the SIGPIPE race cannot occur.docker/db-init/adopt-ledger.sh:25(the single ~20-byte-linegrep -q memory_filescheck, measured 0/6000) is untouched, as is everything else in the file.Test
Added
backend/src/__tests__/adoptLedgerFailClosedSigpipe.test.ts: a stubprintfthat writes the matching line, sleeps, then the rest, turns the real ~0.05% race into a deterministic repro. It shows the oldprintf|grep -qform swallows the match every run (25/25) and the newcaseform never does (0/25), plus a regression check that the file no longer pipes into grep at that line.Scope
Local
docker-compose.ymlonly —adopt-ledger.shis mounted there and nowhere else. Production (deploy/prod/compose.yaml) runs onlymigrate.sh upand is untouched. No migration is touched.Verification
bash -n docker/db-init/adopt-ledger.sh→ 0npx vitest run src/__tests__/adoptLedgerFailClosedSigpipe.test.ts src/__tests__/migrationLedger.test.ts→ 24 passednpx tsc -p tsconfig.test.json --noEmit→ cleangit diff --staton the fix itself: 1 file, 7 insertions / 3 deletionsIndependent of #389; does not block it.