fix(pg-cache): add error handler to prevent unhandled pool errors #456
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.
Summary
Adds an error handler to pg pools to prevent unhandled 'error' events from crashing Node.js when database connections are terminated during cleanup.
When
pgpm test-packagesrunsdropDatabaseto clean up test databases, it terminates all connections viapg_terminate_backend(). This causes idle pool connections to receive a 57P01 (admin_shutdown) error. Without an error handler, Node.js crashes and dumps the entire pg Client object to the console - producing hundreds of lines of useless output instead of a meaningful error message.The fix:
Why this is safe (does NOT swallow real errors):
The handler only catches errors emitted on IDLE pooled connections via the EventEmitter pattern. Query errors (
pool.query(),client.query()) are returned via Promise rejection and bubble up through async/await as normal exceptions - they are NOT affected by this handler.Updates since last revision
Added extensive inline documentation explaining:
Review & Testing Checklist for Human
pg.tsto verify the explanation of pg-pool's two error paths (Promise rejection for queries vs EventEmitter for idle connections) is accuratepgpm test-packagesin constructive-db to confirm the fix produces clean outputTest plan: After publishing the new pgpm version, re-run CI on constructive-db PR #108 and verify all modules pass without the pg Client object dump.
Notes
This is a companion fix for constructive-db PR #108 which replaces the custom
test-all-packages.jsscript withpgpm test-packages.Local testing confirmed: ran
pgpm test-packagesagainst constructive-db using the locally built pgpm with this fix - all 37 modules passed successfully.Link to Devin run: https://app.devin.ai/sessions/ef6ccaafd0f44b358f6b529e50306865
Requested by: Dan Lynch ([email protected]) / @pyramation