Repository navigation
fix(ci): the docker-build-app health check also waits for PostgreSQL over TCP (#501) #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alajmah
wants to merge
2
commits into
master
Choose a base branch
from
fix/pg-readiness-over-tcp-v5
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/usr/bin/env bash | ||
| # scripts/check-pg-readiness-tcp.sh | ||
| # | ||
| # Static gate for #501: every PostgreSQL readiness check in the CI workflow | ||
| # goes over TCP. The postgres image's temporary setup server listens on the | ||
| # Unix socket only, so a socket check can pass against it and then fail in the | ||
| # restart window. Three places matter, and all three are checked: | ||
| # 1. the service health-cmd of docker-build-app, | ||
| # 2. the wait loop and fail-closed check of publish-release-images, | ||
| # 3. the wait loop and fail-closed check of release-tooling-validation. | ||
| # Exit 0 when no socket-form pg_isready remains, 1 otherwise. | ||
| set -u | ||
| WF="${1:-.github/workflows/ci.yml}" | ||
| LINES=$(grep -n "pg_isready" "$WF" || true) | ||
| if [ -z "$LINES" ]; then | ||
| echo "FAIL: no pg_isready found in $WF" | ||
| exit 1 | ||
| fi | ||
| BAD=$(printf '%s\n' "$LINES" | grep -v -- "-h 127.0.0.1" | grep -vE "^[0-9]+:[[:space:]]*#" | grep -vE "^[0-9]+:[[:space:]]*(- )?(name|uses):" || true) | ||
| if [ -n "$BAD" ]; then | ||
| echo "FAIL: PostgreSQL readiness checks that do not go over TCP:" | ||
| printf '%s\n' "$BAD" | cut -c1-140 | ||
| exit 1 | ||
| fi | ||
| printf '%s\n' "$LINES" | sed 's/^/ /' | ||
| echo "PASS: every pg_isready in $WF uses -h 127.0.0.1" | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #!/usr/bin/env bash | ||
| # scripts/test-pg-readiness-race.sh | ||
| # | ||
| # Regression test for #501. The postgres image starts a temporary server during | ||
| # its own setup, on the Unix socket only, and replaces it with the real one. | ||
| # A readiness check over the Unix socket can pass against that temporary | ||
| # server and fail a moment later, in the restart window. A check over TCP | ||
| # cannot: the temporary server does not listen on TCP. | ||
| # | ||
| # This starts one postgres container and asks it both ways, back to back, many | ||
| # times a second from before it is up, and keeps asking for a while after the | ||
| # TCP check first answered. | ||
| # | ||
| # What it asserts: once the TCP check has answered accepting, it answers | ||
| # accepting in every later probe. A check that had answered against the | ||
| # temporary server would stop answering when that server stops, and that is | ||
| # what fails the test. | ||
| # | ||
| # What it only reports: how many probes had the socket check answering | ||
| # accepting and the TCP check, run right after it, not answering. That is the | ||
| # race window of the old form. It depends on timing, so its count is | ||
| # information and not a verdict. A socket "no" next to a TCP "yes" is not | ||
| # counted as anything: the real server can start between the two probes. | ||
| # | ||
| # Needs docker. | ||
| # Exit 0 when the assertion holds, 1 when it does not, 2 when the test could | ||
| # not run. | ||
| set -u | ||
| NAME="pg-readiness-race-$$" | ||
| IMAGE="${PG_IMAGE:-postgres:16-alpine}" | ||
| WATCH="${WATCH_SECONDS:-15}" # seconds to keep probing after the first TCP accept | ||
| cleanup() { docker rm -f "$NAME" >/dev/null 2>&1 || true; } | ||
| trap cleanup EXIT | ||
| cleanup | ||
|
|
||
| docker run -d --name "$NAME" -e POSTGRES_USER=gitwire -e POSTGRES_PASSWORD=test -e POSTGRES_DB=gitops_hub "$IMAGE" >/dev/null || { echo "FAIL: could not start $IMAGE"; exit 2; } | ||
|
|
||
| socket_sees=0 | ||
| tcp_sees=0 | ||
| race_moments=0 # socket accepting, TCP right after it not accepting | ||
| socket_flickers=0 # the socket check stopped answering after it had answered | ||
| tcp_flickers=0 # the TCP check stopped answering after it had answered | ||
| probes=0 | ||
| prev_s=0 | ||
| prev_t=0 | ||
| first_tcp_s="" | ||
| first_tcp_at="" | ||
| start=$(date +%s) | ||
| while :; do | ||
| probes=$((probes + 1)) | ||
| s=0; docker exec "$NAME" pg_isready -U gitwire -d gitops_hub >/dev/null 2>&1 && s=1 | ||
| t=0; docker exec "$NAME" pg_isready -h 127.0.0.1 -U gitwire -d gitops_hub >/dev/null 2>&1 && t=1 | ||
| [ "$s" = 1 ] && socket_sees=$((socket_sees + 1)) | ||
| [ "$t" = 1 ] && tcp_sees=$((tcp_sees + 1)) | ||
| [ "$s" = 1 ] && [ "$t" = 0 ] && race_moments=$((race_moments + 1)) | ||
| [ "$prev_s" = 1 ] && [ "$s" = 0 ] && socket_flickers=$((socket_flickers + 1)) | ||
| [ "$prev_t" = 1 ] && [ "$t" = 0 ] && tcp_flickers=$((tcp_flickers + 1)) | ||
| prev_s=$s | ||
| prev_t=$t | ||
| now=$(date +%s) | ||
| if [ "$t" = 1 ] && [ -z "$first_tcp_at" ]; then first_tcp_at=$now; first_tcp_s=$((now - start)); fi | ||
| if [ -n "$first_tcp_at" ] && [ $((now - first_tcp_at)) -ge "$WATCH" ]; then break; fi | ||
| if [ $((now - start)) -gt 120 ]; then echo "FAIL: $IMAGE did not answer over TCP within 120 s"; exit 2; fi | ||
| done | ||
|
|
||
| echo "image: $IMAGE" | ||
| echo "probes, each one socket check followed by one TCP check: $probes" | ||
| echo "socket check answered accepting: $socket_sees | TCP check answered accepting: $tcp_sees" | ||
| echo "first TCP accept after about ${first_tcp_s}s; probed for ${WATCH}s after it" | ||
| echo "race window of the socket form, socket yes and TCP no in one probe: $race_moments probe(s)" | ||
| echo "the socket check stopped answering after it had answered: $socket_flickers time(s)" | ||
| echo "the TCP check stopped answering after it had answered: $tcp_flickers time(s)" | ||
| if [ "$tcp_flickers" -ne 0 ]; then | ||
| echo "FAIL: the TCP check did not keep answering once it had answered" | ||
| exit 1 | ||
| fi | ||
| if [ "$tcp_sees" -lt 2 ]; then | ||
| echo "FAIL: the TCP check never reached the real server" | ||
| exit 1 | ||
| fi | ||
| echo "PASS: once the TCP check answered accepting, it kept answering" |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[LOW] Static gate passes silently if all real pg_isready invocations are removed
check-pg-readiness-tcp.sh only fails when a pg_isready line is a socket-form invocation. The "no pg_isready found" guard on line 14-18 only triggers when there are zero mentions of any kind (including comments). So if a future edit deletes every actual readiness check but leaves any comment or a step name that mentions pg_isready (e.g. the comment "The smoke tests of the release jobs wait for PostgreSQL with pg_isready."), LINES is non-empty, BAD is empty after the comment/name/uses filters, and the gate prints PASS with no real check present. The protection the gate exists to enforce can therefore be removed without the gate noticing. Suggested fix: after computing BAD, also assert that at least one mention survives the comment/name/uses exclusions (i.e., count non-excluded pg_isready lines) and fail with a distinct message when that count is zero, so both "socket-form remnant" and "no checks at all" are failures.