make docker-postgres fails on first run: "the database system is shutting down"

Aperta Adatta ai principianti
#1,242 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
bash, docker, postgresql
Ambito
devops

Direzione di ricerca

La race si verifica nel target docker-postgres del Makefile, intorno alla riga 62, dove pg_isready -U cre -d cre viene eseguito dentro il contenitore cre-postgres. Leggi il comportamento di docker-entrypoint.sh citato nell'issue, poi riproduci il problema avviando un contenitore nuovo e interrogandone la disponibilità. È completato quando si aggiunge -h 127.0.0.1 a quel controllo, così che superi solo quando il vero server TCP è attivo, e le esecuzioni ripetute con contenitori nuovi non falliscono più nel passaggio psql.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

I cloned the repo and ran make install for the first time (staging branch, commit d9514e52).
It failed at the Postgres step:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  the database system is shutting down
make[1]: *** [Makefile:36: docker-postgres] Error 2
[admin-local] ERROR: make docker-postgres failed
make: *** [Makefile:184: install] Error 1

When I ran make install again, it worked.

Cause

When the Postgres container starts for the first time, it does this:

  1. Starts a temporary server (only on the Unix socket, not TCP)
  2. Creates the user and database
  3. Stops the temporary server
  4. Starts the real server

The docker-postgres target checks with pg_isready -U cre -d cre. This checks the Unix socket, so it can say "ready" during step 1.
Then psql runs during step 3 and fails.

From docker-entrypoint.sh in the pgvector/pgvector:pg16 image:

# does not listen on external TCP/IP and waits until start finishes
set -- "$@" -c listen_addresses='' -p "${PGPORT:-5432}"

Steps to reproduce

I used a separate test container (cre-pg-test) so my real database was not affected.
Each run starts a fresh container and does the same check + psql command as the Makefile.

Check Runs Failed
pg_isready -U cre -d cre (current) 20 1
pg_isready -h 127.0.0.1 -U cre -d cre 20 0

The failed run gave the same error as above. It depends on timing, so it does not fail every time.

Test script
#!/bin/bash
# Usage: ./pg_race_test.sh old|fix [runs]
MODE="$1"; RUNS="${2:-5}"; fails=0
for run in $(seq 1 "$RUNS"); do
  docker rm -f cre-pg-test >/dev/null 2>&1
  docker run -d --name cre-pg-test -e POSTGRES_PASSWORD=password -e POSTGRES_USER=cre -e POSTGRES_DB=cre pgvector/pgvector:pg16 >/dev/null
  for i in $(seq 1 30); do
    if [ "$MODE" = "fix" ]; then
      docker exec cre-pg-test pg_isready -h 127.0.0.1 -U cre -d cre >/dev/null 2>&1 && break
    else
      docker exec cre-pg-test pg_isready -U cre -d cre >/dev/null 2>&1 && break
    fi
    sleep 1
  done
  if out=$(docker exec cre-pg-test psql -U cre -d cre -c "CREATE EXTENSION IF NOT EXISTS vector;" 2>&1); then
    echo "run $run: OK"
  else
    echo "run $run: FAILED -> $(echo "$out" | tail -1)"; fails=$((fails+1))
  fi
done
docker rm -f cre-pg-test >/dev/null 2>&1
echo "== mode=$MODE : $fails / $RUNS failed =="

Suggested fix

Add -h 127.0.0.1 to the pg_isready check (Makefile line 62, same on main and staging):

-		if docker exec cre-postgres pg_isready -U cre -d cre >/dev/null 2>&1; then ready=1; break; fi; \
+		if docker exec cre-postgres pg_isready -h 127.0.0.1 -U cre -d cre >/dev/null 2>&1; then ready=1; break; fi; \

The temporary server does not listen on TCP, so this check only passes when the real server is up.

Environment

  • Windows 11 + WSL2 (Ubuntu 24.04)
  • Docker Desktop 28.4.0
  • pgvector/pgvector:pg16

I can open a PR for this. Should it target main or staging?

Lingua principale
Python
Stelle
216
Fork
173
Merge medio
1g 21h
PR unite (30g)
21

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OWASP/OpenCRE

Tutte le issue di OWASP/OpenCRE

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.