Repository navigation
fix: handle Postgres pool errors instead of crashing - #19
Conversation
An idle client disconnected by a database restart or failover makes pg.Pool emit `error`; with no listener, Node throws and the API and worker processes exit. Register a listener that logs via backgroundFailure (error name only, no connection details). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No unresolved issues were identified, and regression coverage is included.
Review effort: Lite
Findings: None
What changed in this PR
Prevents PostgreSQL pool errors from crashing API and worker processes by logging them safely.
Changes:
- Adds centralized PostgreSQL pool error handling.
- Routes store creation through the new pool factory.
- Adds regression coverage for emitted pool errors.
| File | Summary |
|---|---|
tests/persistence.test.ts |
Verifies pool errors are logged without throwing. |
apps/server/src/db.ts |
Adds safe pool creation and error handling. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Hey @ZJKung, Thanks for this, good catch. The fix looks right, and the test does its job: if you remove the listener, it fails. One idea, not a blocker. Postgres server errors from I'll get this in front of the team to merge. Thanks again! |
|
Security review — no issues found (reviewed head
CI hasn't run on this PR yet; merging once it's green. |
What changed
pg.Poolhad noerrorlistener. When the database restarts or fails over, an idle client disconnects and the pool emitserror. With no listener, Node throws and the API and task worker processes crash.createStorenow builds its pool with a newcreatePool(), which registers a listener that logs the failure throughbackgroundFailure. Only the error's name is logged; connection details are never logged. The pool discards the broken client and opens a fresh connection on the next query.Verification
tests/persistence.test.tsemits a poolerrorwithout connecting to a database. It checks that nothing throws and that the error is logged once. The test failed before the fix and passes after it.pnpm typecheckpasses.pnpm testgives 157 passes and 1 failure. The failure iscomputer-runner.test.ts, which is unrelated to this change: its fakedockerscript depends on a shebang line, and Windows ignores that line, so the script cannot run there.biome checkis clean on the changed files.