From 2cfe085adadd20b70471ed1be4cac875a02dd0c8 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:11:54 +0300 Subject: [PATCH 1/2] fix(ci): wait for PostgreSQL over TCP in the release smoke tests The smoke tests of publish-release-images and release-tooling-validation waited for PostgreSQL with pg_isready over the Unix socket. The postgres image runs a temporary server during its own setup that listens on the Unix socket only, so the wait could pass against it and the fail-closed check after it could then land in the restart window and fail the job with "PG not ready" (#501). Both jobs now call pg_isready with -h 127.0.0.1. The temporary server does not listen on TCP, so the check answers only when the real server is up. scripts/test-pg-readiness-race.sh starts one postgres container, asks it both ways, back to back, from before it is up, and keeps asking for fifteen seconds after the TCP check first answered. It asserts one thing: once the TCP check has answered accepting, it answers accepting in every later probe. A check that had answered against a temporary server would stop answering when that server stops. How often the socket form answered accepting while the TCP form did not is reported and not judged, because it depends on timing; a socket "no" beside a TCP "yes" means nothing, since the real server can start between the two probes. The new CI job pg-readiness-check runs the test. Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com> --- .github/workflows/ci.yml | 30 ++++++++++-- scripts/test-pg-readiness-race.sh | 81 +++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 scripts/test-pg-readiness-race.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e249d90b..030a7892 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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. @@ -585,12 +600,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 @@ -863,8 +881,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" \ diff --git a/scripts/test-pg-readiness-race.sh b/scripts/test-pg-readiness-race.sh new file mode 100644 index 00000000..ffbd333d --- /dev/null +++ b/scripts/test-pg-readiness-race.sh @@ -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" From cb54b6754abcb3d293bd4b7ee975f90083ff0a25 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:02:32 +0300 Subject: [PATCH 2/2] fix(ci): the docker-build-app health check also waits for PostgreSQL over TCP Third gate of #501, after #506. The service container of docker-build-app used --health-cmd with pg_isready over the Unix socket, which can pass against the postgres image's temporary setup server. A health check is asked again until it passes, so the service is eventually reported healthy; but a first pass against the temporary server can mark the service healthy while the real server is still coming up, and the job's steps start on that. The health-cmd now uses -h 127.0.0.1, like the wait loop and the fail-closed check of the two smoke jobs since #506. scripts/check-pg-readiness-tcp.sh is a static gate: every pg_isready in the workflow has to use -h 127.0.0.1, in the health-cmd, in the two smoke jobs, and in this job's own step names. It fails on the master workflow and passes here, and the pg-readiness-check job runs it after the race test. Signed-off-by: Alajmah <55954113+Alajmah@users.noreply.github.com> --- .github/workflows/ci.yml | 4 +++- scripts/check-pg-readiness-tcp.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 scripts/check-pg-readiness-tcp.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 030a7892..07a93282 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,6 +82,8 @@ jobs: - uses: actions/checkout@v4 - name: The readiness check over TCP never passes early run: bash scripts/test-pg-readiness-race.sh + - name: No socket-form pg_isready remains in the workflow + run: bash scripts/check-pg-readiness-tcp.sh # ── Package test jobs ───────────────────────────────────────────────────── # Each package gets its own job so failures are attributed clearly. @@ -223,7 +225,7 @@ jobs: ports: - 5432:5432 options: >- - --health-cmd "pg_isready -U gitwire -d gitops_hub" + --health-cmd "pg_isready -h 127.0.0.1 -U gitwire -d gitops_hub" --health-interval 5s --health-timeout 5s --health-retries 10 diff --git a/scripts/check-pg-readiness-tcp.sh b/scripts/check-pg-readiness-tcp.sh new file mode 100644 index 00000000..5a1c9784 --- /dev/null +++ b/scripts/check-pg-readiness-tcp.sh @@ -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"