Repository navigation
meta: probe databases over TCP in container healthchecks - #18291
Conversation
`yarn start-postgres-{oldest,latest}` fails intermittently in CI, roughly one
job in twenty-four, with exit code 7 — the exit code Node produces when
`dev/check-connection.ts` dies on `ECONNREFUSED`.
The healthcheck was the cause. `pg_isready -U sequelize_test` has no `-h`, so
it probes the unix socket. During `initdb` the postgres entrypoint runs a
temporary bootstrap server that listens on the socket only, with an empty
`listen_addresses`, so the probe passes and the container is reported healthy
while nothing is listening on TCP yet. `wait-until-healthy.sh` then returns and
`check-connection.ts` connects to the published port and is refused. CI volumes
are always empty, so every job runs `initdb` and every job races.
Measured at the instant docker reported healthy, 4 runs out of 4:
init-complete=NO | TCP: 127.0.0.1:5432 - no response
| socket: /var/run/postgresql:5432 - accepting connections
Passing `-h 127.0.0.1` probes TCP instead, which the bootstrap server does not
answer. Throttling the container to 0.15 CPU makes the flake deterministic —
3 failures out of 3 before the change, 0 out of 3 after, for both oldest and
latest.
mysql and mariadb have the same defect: their entrypoints start the temporary
server on the same socket path with `--skip-networking`, and `sequelize_test`
is created while it is running. The window is much narrower — polling both
probes across a full startup catches `socket=ok tcp=fail` for only 2 samples
out of ~2200 — but it is real, so they get `--protocol=TCP` too.
Also raise the probe timeout to 5s and add a `start_period`, so a slow runner
does not mark a container unhealthy while it is legitimately still starting.
While here, `wait-until-healthy.sh`:
- `return_code=$?` captured the exit status of the preceding `echo`, so it was
always 0 and the check was dead code.
- The timeout message claimed 20s; the loop actually ran for about 170s.
- It waited out the full timeout even when the container had already exited,
and printed nothing on failure — no logs, no probe output — which is why the
CI failures were undiagnosable from the job log alone.
It now fails fast on an exited or missing container, reports the real timeout,
and dumps `.State.Health` plus the last 50 log lines when it gives up.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughDatabase Compose healthchecks now use explicit TCP probes with expanded startup timing. The health-waiting script adds strict validation, deadline-based polling, container-state handling, deduplicated output, and failure diagnostics. ChangesContainer health readiness
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant wait_until_healthy.sh
participant Docker
participant DatabaseContainer
participant Healthcheck
wait_until_healthy.sh->>Docker: inspect container state and health status
Docker->>DatabaseContainer: execute configured healthcheck
DatabaseContainer->>Healthcheck: run TCP readiness probe
Healthcheck-->>Docker: return probe status
Docker-->>wait_until_healthy.sh: return health status
wait_until_healthy.sh->>Docker: request probe details and logs on failure
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@dev/wait-until-healthy.sh`:
- Around line 16-26: Handle failure of the health-status docker inspect in the
wait loop after the initial state check. Update the inspection assigning health
so a missing container immediately records an appropriate failure and exits or
breaks consistently with the existing failure handling, instead of continuing
with an empty status. Preserve normal health polling when the inspection
succeeds.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: bac5aeb7-2d03-4829-b202-b6cdb5aee249
📒 Files selected for processing (7)
dev/mariadb/latest/docker-compose.ymldev/mariadb/oldest/docker-compose.ymldev/mysql/latest/docker-compose.ymldev/mysql/oldest/docker-compose.ymldev/postgres/latest/docker-compose.ymldev/postgres/oldest/docker-compose.ymldev/wait-until-healthy.sh
The health inspection did not silence stderr the way the state inspection above it does, so a container removed between the two calls would leak a raw docker error and one "waiting for ... (currently: )" line before the next iteration exited. Handle it the same way as the state inspection instead. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Problem
yarn start-postgres-{oldest,latest}fails intermittently in CI — roughly one job in twenty-four, always exactly one job of the postgres matrix. Recent examples: 30449391928, 30446403587, 30446392962, 30165843220.All four failed with exit code 7, which is what Node produces when
dev/check-connection.tsdies onECONNREFUSED. The failing steps ran 24–36s against 16–22s for successful ones, so it is not thewait-until-healthy.shtimeout (that would be ~170s): the healthcheck passes, and then the connection is refused.Cause
pg_isready -U sequelize_testhas no-h, so it probes the unix socket. Duringinitdbthe postgres entrypoint runs a temporary bootstrap server that listens on the socket only, with an emptylisten_addresses. The probe passes, the container is marked healthy, and nothing is listening on TCP yet.Measured at the instant docker reported
healthy, 4 runs out of 4:CI volumes always start empty, so every job runs
initdband every job races. Whether the first probe lands inside that ~1–2s window is pure timing.Fix
Probe over TCP, which the bootstrap server genuinely does not answer. Throttling the container to 0.15 CPU — what a loaded runner effectively looks like — makes the flake deterministic:
mysql and mariadb have the same defect: their entrypoints start the temporary server on the same socket path with
--skip-networking, andsequelize_testis created while it is running. The window is far narrower — polling both probes across a full startup catchessocket=ok tcp=failfor only 2 samples out of ~2200 — but it is real, so they get--protocol=TCPtoo. All four containers were confirmed to still reach healthy and connect.The probe timeout also goes to 5s and gains a
start_period, so a slow runner cannot mark a container unhealthy while it is legitimately still starting.wait-until-healthy.shThree separate problems, all of which made these failures harder to diagnose than they should have been:
return_code=$?captured the exit status of the precedingecho, so it was always0and the check was dead code.It now fails fast on an exited or missing container, reports the real timeout, and dumps
.State.Healthplus the last 50 log lines when it gives up. Output is also one line per state change rather than a\rredraw every second, which renders badly in Actions logs.Reproducing locally
The volume must be removed between runs, otherwise
initdbis skipped and it always passes.🤖 Generated with Claude Code
Summary by CodeRabbit