Skip to content

fix(readyz): a pool-exhausted relay reports unavailable with a healthy database, and nothing alerts on it #1356

Done

Description

@chappie-daemon

Observed

On 2026-12-03 a self-hosted relay built from this repo served /readyz as HTTP 503 for at least an hour, with {"database":{"ok":false},"redis":{"ok":true}}, while the database was healthy: a fresh client with the relay's own credentials, connecting from a container attached to the same network, ran SELECT 1 in milliseconds. Restarting the relay restored /readyz to ok immediately.

Cause: the readiness ping needs a pool slot it cannot get

pingDatabase() in src/utils/admin-health.ts runs getMasterDbClient().raw('SELECT 1'). The master pool's max defaults to 3 (src/database/client.ts, DB_MAX_POOL_SIZE unset).

pg_stat_activity showed exactly three sessions for the relay's role in state active: three older than five minutes, the oldest over four hours. All three pool slots were held by queries that never completed, so every readiness ping waited for a free connection.

Why the timeout does not save it

pingDatabase races the query against a 3 s timer (DEFAULT_ADMIN_DEPENDENCY_PING_TIMEOUT_MS), but the timeout neither cancels the query nor destroys its connection. acquireConnectionTimeout is 60 s, so the ping gives up 57 s before the pool does, and the pending acquire stays in the queue. The result is not a slow answer but a permanent ok:false against a database that answers instantly.

Why nothing alerted

The deployment's container healthcheck and its uptime check both probe /, which answers 200 with an HTML page, so Docker reported healthy and the monitor stayed green for the whole outage. Only an external check that reads /readyz caught it, which is the point of a dependency probe.

Proposal

  1. Run the readiness probe on a connection the probe owns (a dedicated small client, or statement_timeout on the pooled one), so a saturated application pool cannot make readiness lie and a hung probe is cancelled rather than leaked.
  2. When the probe times out, destroy the connection it used instead of leaving it checked out; today a single stalled socket permanently removes one of three slots.
  3. Deployment side: point the container healthcheck and the uptime monitor at /readyz. A 200 on / says nothing about Postgres or Redis.

The three stuck sessions were cleared by the restart before their query text could be captured; the pool monitor's N used / N free / N pending line every 60 s is the signal to read next time this happens.

Activity

  1. added
    rewrite
    rust
    on Dec 3, 2026
  2. chappie-daemon commented on Dec 3, 2026

    @chappie-daemon
    CollaboratorAuthor

    Second occurrence, same signature — so the pool wedges again within a single uptime, not once.

    • beszel-baseline-check (redis+postgres-via-nostream) notified at ~13:23Z that /readyz fails; three probes answered 503 in 3.00s each with {"status":"unavailable","database":{"ok":false},"redis":{"ok":true}}.
    • docker ps said Up 12 hours (healthy): the container healthcheck still probes /, because the compose change to /readyz needs a recreate and has not had one.
    • docker restart nostream at 13:33Z cleared it; the next probe answered 200 with {"status":"ok","database":{"ok":true},"redis":{"ok":true}}.

    The 3.00 s answers are pingDatabase()'s own timeout, not a slow database — a fresh client gets a slot in milliseconds, the relay's pool (max 3) does not. That is the acquire-timeout-versus-ping-timeout mismatch this issue describes, and the ordering is unchanged since the 01:15Z occurrence.

  3. cameri commented on Dec 3, 2026

    @cameri
    Owner

    @chappie-daemon assigned to you

  4. chappie-daemon commented on Dec 3, 2026

    @chappie-daemon
    CollaboratorAuthor

    Code side is in review as #1362: pingDatabase() no longer takes a slot from the master pool — it opens its own client from the master connection config, bounded by ADMIN_DEPENDENCY_PING_TIMEOUT_MS for connect, query and statement, with an error listener and a close that cannot outlive the same deadline.

    Measured against the relay's Postgres with all three pool slots held by pg_sleep(45):

    {"legacy":"timeout","legacyMs":3000,"databaseOk":true,"snapshotMs":11}
    

    The pooled raw('select 1') gave up at 3.0 s exactly as in both outages; the new probe answered ok in 11 ms.

    Two deployment steps are left, and neither is in the repository's code:

    1. The compose healthcheck already probes /readyz (containers/nostream/compose.yml:66), but a recreate is needed before it takes effect — the running container still probes /, which is why docker ps said healthy through both outages.
    2. The Gatus row still probes http://nostream:8008 (containers/gatus/config/config.yaml:547) and should become /readyz. That file currently carries ~112 uncommitted lines from another session (UniFi/AP rows), so the edit should wait until that lands rather than be committed alongside it.

    Until both land, the only check that reads /readyz is the consumer probe in containers/scripts/beszel-baseline-check, which caught both outages within its interval.

  5. chappie-daemon commented on Dec 3, 2026

    @chappie-daemon
    CollaboratorAuthor

    Deployed 2026-12-03T16:25Z.

    • Container rebuilt from containers/compose.yml (networks cloudflare + gatus), running main — NIP-11 reports version: 3.17.1, which is main's, not rewrite/main's 3.11.0.
    • The Docker healthcheck now runs /readyz and reports healthy, so a wedged database pool surfaces at the container level instead of only through the external probe.
    • Public https://relay.cameri.dev/readyz answers 200 with both dependencies ok.

    One detour worth recording: the first recreate ran from projects/nostream/docker-compose.yml (the repo's own upstream dev stack) and took the nostream name, which is loopback-only with no healthcheck — the relay answered 502 until it was torn down and the right stack came back up. docker inspect <c> --format '{{index .Config.Labels "com.docker.compose.project.config_files"}}' names the file that created a container, which is how that was diagnosed.

    Still open: the Gatus row probes /, not /readyz (containers/gatus/config/config.yaml), so the external check keeps reporting healthy on a wedged pool until that edit lands.

  6. added 2 commits that reference this issue on Dec 3, 2026
    8b73070
    0da5ca8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

rewriterust

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions