Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ jobs:
- name: Check build output
run: test -f docs/.vitepress/dist/index.html

# ── PostgreSQL readiness check ───────────────────────────────────────────
# The smoke tests of the release jobs wait for PostgreSQL with pg_isready.
# Over the Unix socket that check can pass against the temporary server the
# postgres image runs during its own setup, and then fail in the restart
# window (#501). This job shows, against a real postgres container, that the
# TCP form of the check does not answer before the real server is up.
pg-readiness-check:
runs-on: ubuntu-latest
needs: syntax-check
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- name: The readiness check over TCP never passes early
run: bash scripts/test-pg-readiness-race.sh

# ── Package test jobs ─────────────────────────────────────────────────────
# Each package gets its own job so failures are attributed clearly.
# No --passWithNoTests: a suite with zero tests is a CI failure.
Expand Down Expand Up @@ -565,12 +580,15 @@ jobs:
docker run -d --name "$REDIS_CONTAINER" --network "$SMOKE_NET" \
redis:7-alpine

# Fail-closed PostgreSQL readiness
# Fail-closed PostgreSQL readiness. The check goes over TCP: the
# postgres image starts a temporary server during its own setup that
# listens on the Unix socket only, and a socket check can pass
# against it and then fail in the restart window (#501).
for i in $(seq 1 30); do
docker exec "$PG_CONTAINER" pg_isready -U gitwire -d gitops_hub 2>/dev/null && break
docker exec "$PG_CONTAINER" pg_isready -h 127.0.0.1 -U gitwire -d gitops_hub 2>/dev/null && break
sleep 1
done
if ! docker exec "$PG_CONTAINER" pg_isready -U gitwire -d gitops_hub 2>/dev/null; then
if ! docker exec "$PG_CONTAINER" pg_isready -h 127.0.0.1 -U gitwire -d gitops_hub 2>/dev/null; then
echo "::error::PostgreSQL did not become ready"
exit 1
fi
Expand Down Expand Up @@ -843,8 +861,10 @@ jobs:
docker run -d --name "$PG" --network "$SMOKE_NET" \
-e POSTGRES_USER=gitwire -e POSTGRES_PASSWORD=test -e POSTGRES_DB=gitops_hub postgres:16-alpine
docker run -d --name "$REDIS" --network "$SMOKE_NET" redis:7-alpine
for i in $(seq 1 30); do docker exec "$PG" pg_isready -U gitwire -d gitops_hub 2>/dev/null && break; sleep 1; done
docker exec "$PG" pg_isready -U gitwire -d gitops_hub 2>/dev/null || { echo "::error::PG not ready"; exit 1; }
# PostgreSQL readiness over TCP, for the reason given in the
# publish job above (#501).
for i in $(seq 1 30); do docker exec "$PG" pg_isready -h 127.0.0.1 -U gitwire -d gitops_hub 2>/dev/null && break; sleep 1; done
docker exec "$PG" pg_isready -h 127.0.0.1 -U gitwire -d gitops_hub 2>/dev/null || { echo "::error::PG not ready"; exit 1; }
CI_KEY="$(node -e "console.log(require('crypto').randomBytes(32).toString('hex'))")"
docker run -d --name "$APP" --network "$SMOKE_NET" \
-e DATABASE_URL="postgresql://gitwire:test@${PG}:5432/gitops_hub" \
Expand Down
59 changes: 59 additions & 0 deletions scripts/test-pg-readiness-race.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/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 until the real server answers. It fails
# when the socket check answers "accepting" while the TCP check, run
# immediately after, does not. It also fails when the TCP check ever answers
# before the socket check.
#
# Needs docker. Exit 0 when no such moment is seen, 1 when one is.
set -u
NAME="pg-readiness-race-$$"
IMAGE="${PG_IMAGE:-postgres:16-alpine}"
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
tcp_first=0
probes=0
first_tcp_s=""
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))
if [ "$s" = 1 ] && [ "$t" = 0 ]; then race_moments=$((race_moments + 1)); fi
if [ "$t" = 1 ] && [ "$s" = 0 ]; then tcp_first=$((tcp_first + 1)); fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recheck the socket before treating TCP success as premature

If PostgreSQL finishes starting between the socket probe on line 35 and the TCP probe on line 36, the first probe legitimately fails and the second succeeds. This condition nevertheless increments tcp_first, causing the script to exit with failure even after twenty successful TCP checks. Consequently, the new pg-readiness-check job in .github/workflows/ci.yml can intermittently fail healthy CI runs. A simulated normal startup with only the first probe failing reproduces this outcome. Recheck the socket after TCP succeeds, or use an independent startup-state assertion, rather than interpreting sequential observations as simultaneous.

Useful? React with 👍 / 👎.

if [ "$t" = 1 ] && [ -z "$first_tcp_s" ]; then first_tcp_s=$(( $(date +%s) - start )); fi
# Stop once the real server has answered over TCP twenty times in a row.
if [ "$t" = 1 ]; then
n=$(( ${n:-0} + 1 )); [ "$n" -ge 20 ] && break
else
n=0
fi
[ $(( $(date +%s) - start )) -gt 120 ] && { echo "FAIL: $IMAGE did not answer over TCP within 120 s"; exit 2; }
done

echo "image: $IMAGE"
echo "probes (each is 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"
echo "moments with socket=yes and TCP=no (the race window): $race_moments"
echo "moments with TCP=yes and socket=no: $tcp_first"
if [ "$tcp_first" -ne 0 ]; then echo "FAIL: the TCP check passed where the socket check did not"; exit 1; fi

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] Regression test fails spuriously when the real server starts between its socket and TCP probes

Each loop iteration runs the socket check first and the TCP check immediately after, then fails the whole run if any single iteration recorded t=1 && s=0 (tcp_first, enforced at the if [ "$tcp_first" -ne 0 ] ... exit 1 line). That condition also occurs at the normal, expected startup transition: if the real server becomes accepting in the gap between the socket probe and the TCP probe of the same iteration, s=0 and t=1 are recorded and the script exits 1, failing the new pg-readiness-check job even though the fix is working exactly as intended. With two docker exec invocations per iteration (~50-100 ms each), the socket-to-TCP gap covers a sizable fraction of each cycle, so the per-run flake probability is plausibly tens of percent - i.e., the patch introduces a new flaky CI job in a PR whose whole purpose is removing CI flakiness. The assertion is also structurally unable to detect the regression it targets: if the temporary server ever listened on TCP it would listen on the socket too (s=1, t=1), so tcp_first would stay 0; the only realistic producer of t=1 && s=0 is benign timing skew. Suggested fix: re-probe before declaring failure. E.g. on t=1 && s=0, sleep 0.2 and re-run the socket check; count tcp_first only if the re-check also fails. A genuine TCP-only temporary server would still fail the socket re-check, while the normal transition would not, eliminating the spurious exit.

if [ "$tcp_sees" -lt 20 ]; then echo "FAIL: the TCP check never reached the real server"; exit 1; fi
echo "PASS: the TCP check never answered before the real server was up"
Loading