Skip to content

fix(db-init): the fail-closed check reads the list without a pipeline - #391

Merged
IvanJS17 merged 1 commit into
mainfrom
fix/adopt-ledger-fail-closed-swallowed-by-sigpipe
Nov 28, 2026
Merged

IvanJS17 merged 1 commit into
mainfrom
fix/adopt-ledger-fail-closed-swallowed-by-sigpipe

Conversation

@IvanJS17

@IvanJS17 IvanJS17 commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

What

docker/db-init/adopt-ledger.sh:40 had the same defect class PR #389 fixed in backend/scripts/migrate.sh: 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 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 case match against the newline-delimited list — no pipe, so the SIGPIPE race cannot occur. docker/db-init/adopt-ledger.sh:25 (the single ~20-byte-line grep -q memory_files check, measured 0/6000) is untouched, as is everything else in the file.

Test

Added backend/src/__tests__/adoptLedgerFailClosedSigpipe.test.ts: a stub printf that writes the matching line, sleeps, then the rest, turns the real ~0.05% race into a deterministic repro. It shows the old printf|grep -q form swallows the match every run (25/25) and the new case form never does (0/25), plus a regression check that the file no longer pipes into grep at that line.

Scope

Local docker-compose.yml only — adopt-ledger.sh is mounted there and nowhere else. Production (deploy/prod/compose.yaml) runs only migrate.sh up and is untouched. No migration is touched.

Verification

  • bash -n docker/db-init/adopt-ledger.sh → 0
  • npx vitest run src/__tests__/adoptLedgerFailClosedSigpipe.test.ts src/__tests__/migrationLedger.test.ts → 24 passed
  • npx tsc -p tsconfig.test.json --noEmit → clean
  • git diff --stat on the fix itself: 1 file, 7 insertions / 3 deletions

Independent of #389; does not block it.

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.
@IvanJS17
IvanJS17 merged commit f98fc87 into main Nov 28, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant