loopback-connector-postgresql icon indicating copy to clipboard operation
loopback-connector-postgresql copied to clipboard

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

Open rchandranmani opened this issue 5 months ago • 0 comments

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 https://github.com/loopbackio/loopback-connector-postgresql/issues/778 See also https://github.com/loopbackio/loopback-connector-postgresql/issues/746

Checklist

  • [x] 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 avatar Jun 27 '25 19:06 rchandranmani