loopback-connector-postgresql
loopback-connector-postgresql copied to clipboard
Fix: Connection terminated unexpectedly error not caught in on error listener
Our application was crashing when the database connection unexpectedly failed. This happened because:
- We were previously using
removeAllListeners('error'), which unintentionally removed the pg library's error handlers. - This left connection termination errors unhandled, leading to app crashes which uses this connector
We've fixed this by:
- 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. - 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 testpasses 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