fix(realtime): handle pg.Pool idle-client errors so admin-killed connections do not crash the worker (BIT-32447) #267

Merged
spark merged 1 commits from fix/BIT-32447-pg-pool-error-handler into main 2026-11-18 17:33:20 +00:00
Owner

Root cause

PostgreSQL error 57P01 (terminating connection due to administrator command) is emitted as an error event on the pg.Pool when an idle pooled client is killed (server restart, pg_terminate_backend, or admin command).

Both attachRealtime (API) and createRemoteBroadcaster (ingest worker) created pg.Pool instances without an error listener. With no listener, Node treats the event as an uncaughtException and crashes the process — exactly the uncaught: true report in BIT-32447.

Fix

Add pool.on("error", ...) handlers that forward to onError and skip pool-after-end noise during shutdown, mirroring the existing client.on("error") reconnection logic for the LISTEN client.

Test

New regression test does not crash when Postgres terminates an idle pooled connection (BIT-32447): publishes once to establish the pool connection, terminates the idle backend via pg_terminate_backend (simulating the admin command), then asserts no uncaughtException fires, onError receives the terminating connection error, and a subsequent publish recovers on a fresh connection. Verified the test fails (uncaughtException) without the fix and passes with it.

Paperclip-Issue: BIT-32447
Paperclip-Cluster: 52b3174b839d

## Root cause PostgreSQL error `57P01` (`terminating connection due to administrator command`) is emitted as an `error` event on the `pg.Pool` when an idle pooled client is killed (server restart, `pg_terminate_backend`, or admin command). Both `attachRealtime` (API) and `createRemoteBroadcaster` (ingest worker) created `pg.Pool` instances **without** an `error` listener. With no listener, Node treats the event as an `uncaughtException` and crashes the process — exactly the `uncaught: true` report in BIT-32447. ## Fix Add `pool.on("error", ...)` handlers that forward to `onError` and skip `pool-after-end` noise during shutdown, mirroring the existing `client.on("error")` reconnection logic for the LISTEN client. ## Test New regression test `does not crash when Postgres terminates an idle pooled connection (BIT-32447)`: publishes once to establish the pool connection, terminates the idle backend via `pg_terminate_backend` (simulating the admin command), then asserts no `uncaughtException` fires, `onError` receives the `terminating connection` error, and a subsequent publish recovers on a fresh connection. Verified the test fails (uncaughtException) without the fix and passes with it. Paperclip-Issue: BIT-32447 Paperclip-Cluster: 52b3174b839d
spark added 1 commit 2026-11-18 17:21:44 +00:00
fix(realtime): handle pg.Pool idle-client errors so admin-killed connections do not crash the worker (BIT-32447)
ci / rust (pull_request) Successful in 4m22s
ci / typescript (pull_request) Successful in 10m36s
4a7dba5480
PostgreSQL error 57P01 (terminating connection due to administrator command)
is emitted as an 'error' event on the pg.Pool when an idle pooled client is
killed. Both attachRealtime (API) and createRemoteBroadcaster (ingest worker)
created pools without an 'error' listener, so Node treated it as an
uncaughtException and crashed the process. Add pool.on('error') handlers that
forward to onError (and skip noise during shutdown), mirroring the existing
client.on('error') reconnection logic.

Paperclip-Issue: BIT-32447
Paperclip-Cluster: 52b3174b839d
spark force-pushed fix/BIT-32447-pg-pool-error-handler from 5be8e8aafe to 4a7dba5480 2026-11-18 17:21:44 +00:00 Compare
paperclip-review-bot left a comment
Member

auto-approve: CI success on 4a7dba5480 (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 4a7dba548087 (gitea_auto_approve_open_prs_once.py)
founding-engineer left a comment
Member

auto-approve: CI success on 4a7dba5480 (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 4a7dba548087 (gitea_auto_approve_open_prs_once.py)
devops-engineer left a comment
Member

auto-approve: CI success on 4a7dba5480 (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 4a7dba548087 (gitea_auto_approve_open_prs_once.py)
spark merged commit 76aadb60ae into main 2026-11-18 17:33:20 +00:00
Sign in to join this conversation.