Catch pg client connection errors + add synchronous crash logging#397
Merged
Conversation
Prevents the recurring idle-in-transaction crash from killing the process.
When Postgres terminates a checked-out connection (e.g. a transaction held
open past idle_in_transaction_session_timeout while awaiting a slow
Twilio/Teams send), the client emits an 'error' event. Previously nothing
listened for it on a checked-out client (pool.on('error') only covers idle
pool clients), so it became an unhandled 'error' -> uncaughtException ->
process exit(1) and a 2-3 min outage while ECS replaced the task.
- db.js: attach client.on('error') in pool.on('connect') (once per physical
connection) so the termination is logged ("Database client connection
error: ...") and handled instead of crashing the process. The in-flight
request still fails and rolls back, but the process stays up.
- crashLogging.js + index.js: install process-level uncaughtException /
unhandledRejection handlers that write the stack synchronously to stderr
(fs.writeSync), so any future fatal error is captured in CloudWatch even
if Sentry's async delivery is lost on exit.
This is a safety net that stops the outages regardless of which code path
still holds a send inside a transaction; the remaining in-transaction sends
in eventHandlers.js will be moved out separately.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prevents the recurring idle-in-transaction crash from killing the process. When Postgres terminates a checked-out connection (e.g. a transaction held open past idle_in_transaction_session_timeout while awaiting a slow Twilio/Teams send), the client emits an 'error' event. Previously nothing listened for it on a checked-out client (pool.on('error') only covers idle pool clients), so it became an unhandled 'error' -> uncaughtException -> process exit(1) and a 2-3 min outage while ECS replaced the task.
This is a safety net that stops the outages regardless of which code path still holds a send inside a transaction; the remaining in-transaction sends in eventHandlers.js will be moved out separately.