fix(db): attach an error handler to the pg pool#3172
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughIn ChangesPool and Client Error Handling
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~2 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches✨ Simplify code
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
A Postgres connection can drop at any time — e.g. a serverless Postgres such as Neon terminating the connection (error code 57P01). node-postgres surfaces this as an 'error' event; without a listener node re-throws it as an unhandled 'error' and crashes the process. Idle clients emit on the pool, but a client that is connecting or checked out emits on the client itself, so we listen on both the pool and each client. The pool then discards the dead client and opens a fresh one on the next query, so the server survives transient/idle disconnects. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1994b0e to
1244e6f
Compare
What
Add an
'error'listener to the sharedpgPoolinpackages/db/src/client.ts.Why
node-postgresemits anerrorevent on the pool when an idle pooled client loses its connection. Per the node-postgres docs, if there is no listener, the error is re-emitted as an unhandled'error'event on anEventEmitter, which crashes the Node process. The pool is currently created with no listener, so a single idle-connection drop takes down the whole server.I hit this in a self-hosted setup backed by a serverless Postgres: the database terminated an idle connection (
FATAL 57P01 admin_shutdown) and the API process exited with:Fix
Log the pool error instead of letting it go unhandled. The pool then discards the dead client and opens a fresh one on the next query, so the server survives transient/idle disconnects.
Impact
Testing
pnpm --filter @reactive-resume/db typecheck✓pnpm --filter @reactive-resume/db test✓ (34 passed)pnpm exec biome check packages/db/src/client.ts✓Summary by CodeRabbit