Skip to content

fix: Connection terminated unexpectedly error not caught in on error handler #783

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 1 commit into
base: master
Choose a base branch
from

Conversation

rchandranmani
Copy link

@rchandranmani rchandranmani commented Jul 9, 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.

Unit tests have been added for coverage; rewire (dev dependency) was used to avoid exporting functions solely for testing.

A new lint-fix command has also been introduced to address linting issues.

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

@rchandranmani rchandranmani marked this pull request as ready for review July 9, 2025 17:18
@rchandranmani
Copy link
Author

@aaqilniz Can you take a look now.

@rchandranmani rchandranmani force-pushed the fix-connection-terminated-unexpectedly-error branch from 7aabec6 to 0ba53be Compare July 10, 2025 18:47
Signed-off-by: Ramachandran Mani <ramachandranm@pnp-hcl.com>
@rchandranmani rchandranmani force-pushed the fix-connection-terminated-unexpectedly-error branch from 0ba53be to 8e2863d Compare July 10, 2025 18:48
@rchandranmani
Copy link
Author

@aaqilniz fixed the commit casing

@rchandranmani
Copy link
Author

Hi @dhmlau - Could you please take a look at this PR.

Copy link
Member

@dhmlau dhmlau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rchandranmani, thanks for your contribution. It LGTM. 👍

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