Run the test PostgreSQL behind an init and let pg_isready give up before Docker (refs #98)
The server log of a failing job shows every restart as 'untracked child process exited with exit code 2'. A health check that outlives its 3-second timeout leaves pg_isready to PostgreSQL, which is PID 1 in its container; when pg_isready exits the postmaster takes it for a crashed backend and restarts every connection. --init reaps such orphans, and pg_isready -t 5 under a 15-second timeout is never cut off. Same change in docker-compose.yml. ADR 0015. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
72f4cbe0a5
commit
6ea303e1dc
4 files changed
+58
-5
No files matched your search
@@ -24,10 +24,13 @@ jobs:
|
|||||||
POSTGRES_DB: anubis
|
POSTGRES_DB: anubis
|
||||||
POSTGRES_USER: anubis
|
POSTGRES_USER: anubis
|
||||||
POSTGRES_PASSWORD: anubis-ci
|
POSTGRES_PASSWORD: anubis-ci
|
||||||
|
# --init and a pg_isready that gives up before Docker does (ADR 0015, issue #98). Without them a health
|
||||||
|
# check that outlives its timeout leaves pg_isready to PostgreSQL as PID 1, and its exit restarts the server.
|
||||||
options: >-
|
options: >-
|
||||||
--health-cmd "pg_isready -U anubis -d anubis"
|
--init
|
||||||
|
--health-cmd "pg_isready -U anubis -d anubis -t 5"
|
||||||
--health-interval 3s
|
--health-interval 3s
|
||||||
--health-timeout 3s
|
--health-timeout 15s
|
||||||
--health-retries 20
|
--health-retries 20
|
||||||
env:
|
env:
|
||||||
ANUBIS_TEST_DB: ${{ matrix.db }}
|
ANUBIS_TEST_DB: ${{ matrix.db }}
|
||||||
|
|||||||
+5
-2
@@ -31,6 +31,9 @@ services:
|
|||||||
|
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:18
|
image: postgres:18
|
||||||
|
# An init as PID 1 and a pg_isready that gives up before Docker does (ADR 0015): otherwise a slow health check
|
||||||
|
# leaves pg_isready to PostgreSQL, and its exit makes the server restart every connection.
|
||||||
|
init: true
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: anubis
|
POSTGRES_DB: anubis
|
||||||
POSTGRES_USER: anubis
|
POSTGRES_USER: anubis
|
||||||
@@ -38,9 +41,9 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:55432:5432"
|
- "127.0.0.1:55432:5432"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U anubis -d anubis"]
|
test: ["CMD-SHELL", "pg_isready -U anubis -d anubis -t 5"]
|
||||||
interval: 3s
|
interval: 3s
|
||||||
timeout: 3s
|
timeout: 15s
|
||||||
retries: 20
|
retries: 20
|
||||||
volumes:
|
volumes:
|
||||||
- postgres-data:/var/lib/postgresql
|
- postgres-data:/var/lib/postgresql
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
# 0015 — PostgreSQL nei container con un processo init e un controllo di salute che non si interrompe a metà
|
||||||
|
|
||||||
|
Data: 2026-11-15 · Stato: accettata · Precisa la CI (SPEC 15.5, ADR 0007) e il `docker-compose.yml` di sviluppo, issue #98, senza componenti esterni nuovi
|
||||||
|
|
||||||
|
## Contesto
|
||||||
|
|
||||||
|
Dal 14 novembre 2026 il lavoro `test (postgres)` della CI falliva spesso, ma non sempre. Dopo uno-cinque minuti di test, il PostgreSQL di servizio chiudeva tutte le connessioni. I test rimasti fallivano con `57P03: the database system is in recovery mode` o con `EndOfStreamException`. Rilanciando il lavoro passava. In locale la suite passava sempre.
|
||||||
|
|
||||||
|
Il registro del lavoro non mostrava quello del server. Per vederlo, la CI ora stampa a fine lavoro registro, stato e memoria del contenitore PostgreSQL (`tasks ci-service-log`). Li legge dal socket di Docker che l'esecutore monta nel lavoro, senza il client `docker` né altro da installare.
|
||||||
|
|
||||||
|
In una prova con la suite ripetuta tre volte sullo stesso server, il server si è riavviato quattro volte. Ogni volta il registro dice la stessa cosa:
|
||||||
|
|
||||||
|
```
|
||||||
|
LOG: untracked child process (PID 248) exited with exit code 2
|
||||||
|
LOG: terminating any other active server processes
|
||||||
|
LOG: all server processes terminated; reinitializing
|
||||||
|
```
|
||||||
|
|
||||||
|
Nessun processo ucciso per memoria (`OOMKilled false`, nessun evento `oom` di Docker) e nessun segnale. Il processo "sconosciuto" che esce con codice 2 è `pg_isready`. Il codice 2 vuol dire "il server non ha risposto".
|
||||||
|
|
||||||
|
Che cosa succede:
|
||||||
|
1. Docker esegue il controllo di salute dentro il contenitore come `/bin/sh -c "pg_isready …"`.
|
||||||
|
2. Sull'esecutore di Unraid il disco è lento. Ogni `DROP DATABASE` dei test forza un checkpoint immediato, con fino a 21 secondi di scrittura sul disco. In quei momenti il server può metterci più di 3 secondi a rispondere.
|
||||||
|
3. Passato il tempo massimo del controllo (`--health-timeout 3s`), Docker chiude la shell. `pg_isready` resta senza genitore e il sistema lo affida al processo 1 del contenitore. L'immagine `postgres` non ha un processo init, quindi il processo 1 è PostgreSQL stesso.
|
||||||
|
4. Quando `pg_isready` si arrende ed esce, PostgreSQL vede morire un figlio che non conosce. Per prudenza lo tratta come un suo processo andato in crash: chiude tutte le connessioni e rifà il recupero dal registro delle scritture.
|
||||||
|
|
||||||
|
Spiega anche gli altri indizi:
|
||||||
|
- capita solo a volte, perché serve un server lento proprio mentre scade il controllo;
|
||||||
|
- si vede da quando la suite è cresciuta con il commit della raccolta dei dati (issue #95), cioè più database creati e cancellati;
|
||||||
|
- in locale non capita, perché il disco è veloce.
|
||||||
|
|
||||||
|
Non c'entrano la memoria, `/dev/shm` (64 MiB, il server ne usa poco), `pg_database_size` del campionatore e il `CREATE`/`DROP DATABASE` dei test in sé: quelli rendono il server lento, non lo fanno ripartire.
|
||||||
|
|
||||||
|
## Decisione
|
||||||
|
|
||||||
|
1. **Un processo init davanti a PostgreSQL.** Il servizio `postgres` della CI parte con `--init`, e quello di `docker-compose.yml` con `init: true`. Il processo 1 diventa il piccolo init di Docker: raccoglie lui i processi rimasti senza genitore, e PostgreSQL non li vede più.
|
||||||
|
2. **`pg_isready` si arrende prima di Docker.** Il controllo è `pg_isready -U anubis -d anubis -t 5` con `--health-timeout 15s`. Così Docker non chiude mai la shell mentre `pg_isready` sta ancora aspettando. L'intervallo resta di 3 secondi, quindi l'avvio non rallenta.
|
||||||
|
3. **Il registro del server resta nella CI.** Il passo `PostgreSQL log` gira sempre a fine lavoro `test (postgres)`. Mostra anche se l'init è attivo e com'è il controllo di salute. Il prossimo problema del server si legge lì, senza rilanciare alla cieca.
|
||||||
|
|
||||||
|
Le due misure si coprono a vicenda: ognuna da sola basta a togliere la causa.
|
||||||
|
|
||||||
|
## Conseguenze
|
||||||
|
|
||||||
|
- Il lavoro `test (postgres)` non perde più il server a metà. Rilanciarlo per farlo passare nascondeva la causa, e ora non serve più.
|
||||||
|
- Nessun test e nessuna parte di Anubis cambia: il difetto era nel modo in cui gira il contenitore, non nel bot.
|
||||||
|
- **Per l'installazione su Unraid:** se il PostgreSQL che usa Anubis gira in un container con un controllo di salute basato su `pg_isready` e senza init, ha lo stesso rischio sotto carico. Il rimedio è lo stesso: l'opzione `--init` (in Unraid, "Extra Parameters") oppure un controllo con `-t` più breve del suo tempo massimo. Il template `deploy/unraid/anubis.xml` non installa PostgreSQL, quindi lì non cambia niente.
|
||||||
@@ -74,7 +74,8 @@ internal static class ServiceLog
|
|||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine($"== {image} {id[..12]}");
|
Console.WriteLine($"== {image} {id[..12]}");
|
||||||
Console.WriteLine($"Stato: {state?["Status"]}, avviato {state?["StartedAt"]}, riavvii {inspect["RestartCount"]}, OOMKilled {state?["OOMKilled"]}, uscita {state?["ExitCode"]}");
|
Console.WriteLine($"Stato: {state?["Status"]}, avviato {state?["StartedAt"]}, riavvii {inspect["RestartCount"]}, OOMKilled {state?["OOMKilled"]}, uscita {state?["ExitCode"]}");
|
||||||
Console.WriteLine($"Limiti: memoria {Mib(host?["Memory"])}, /dev/shm {Mib(host?["ShmSize"])}");
|
Console.WriteLine($"Limiti: memoria {Mib(host?["Memory"])}, /dev/shm {Mib(host?["ShmSize"])}, init {host?["Init"]?.ToString() ?? "no"}");
|
||||||
|
Console.WriteLine($"Controllo di salute: {inspect["Config"]?["Healthcheck"]?["Test"]?.ToJsonString()}, timeout {(inspect["Config"]?["Healthcheck"]?["Timeout"]?.GetValue<long>() ?? 0) / 1_000_000_000} s");
|
||||||
|
|
||||||
if (state?["Running"]?.GetValue<bool>() == true)
|
if (state?["Running"]?.GetValue<bool>() == true)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user