Skip to content

Fix: Connection terminated unexpectedly error not caught in on error listener #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

rchandranmani
Copy link

@rchandranmani rchandranmani commented Jun 27, 2025

Our application was crashing when the database connection unexpectedly failed. This happened because:

  1. We were previously using removeAllListeners('error'), which unintentionally removed the pg library's error handlers.
  2. This left connection termination errors unhandled, leading to app crashes which uses this connector

We've fixed this by:

  1. Completely removing the removeAllListeners('error') call within the connector. This ensures that the pg library's built-in error handling for connection issues remains active.
  2. We now only add our connector's specific error listener if the pg client doesn't already have any error listeners attached (!pg.listenerCount('error')). This prevents us from creating too many listeners (addressing past MaxEventListeners warnings) while ensuring all critical connection errors are still properly managed.

Fixes #778
See also #746

Checklist

  • DCO (Developer Certificate of Origin) signed in all commits
  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • Commit messages are following our guidelines

renovate bot and others added 4 commits June 27, 2025 11:44
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Ramachandran Mani <[email protected]>
Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Signed-off-by: Ramachandran Mani <[email protected]>
@rchandranmani rchandranmani marked this pull request as ready for review June 27, 2025 19:13
@aaqilniz
Copy link
Contributor

Hi, @rchandranmani. Can you please fix the linting issues and the commit message?

You'd also need to add some tests around the changes you've made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connection terminated unexpectedly error not caught in on error listener
2 participants