Fix Jest force exit from unclosed pg pool event listeners#94
Closed
Copilot wants to merge 3 commits into
Closed
Conversation
Co-authored-by: colleenpridemore <4281084+colleenpridemore@users.noreply.github.com>
Co-authored-by: colleenpridemore <4281084+colleenpridemore@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix Jest force exit issues by cleaning up database connections
Fix Jest force exit by cleaning up DatabaseClient event listeners
Jan 30, 2026
Copilot
AI
changed the title
Fix Jest force exit by cleaning up DatabaseClient event listeners
Fix Jest force exit from unclosed pg pool event listeners
Jan 30, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR attempts to fix Jest force-exit issues caused by unclosed PostgreSQL connection pool event listeners. The root cause was correctly identified: DatabaseClient registered error handlers that weren't removed before calling pool.end(), keeping Node's event loop active.
Changes:
- Added
removeAllListeners()call beforepool.end()in DatabaseClient cleanup - Added
afterAllhooks in unit test files to close DatabaseClient instances - Increased global teardown delay from 2s to 3s for async completion
- Removed
forceExit: truefrom Jest unit config
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
src/database/client.ts |
Added removeAllListeners() before pool cleanup to clear error handlers |
src/database/module.test.ts |
Added afterAll hook to close test DatabaseClient instances |
src/knowledge-base/module.test.ts |
Added afterAll hook to close test DatabaseClient instances |
jest.globalTeardown.ts |
Increased cleanup delay from 2000ms to 3000ms |
jest.unit.config.js |
Removed forceExit: true flag |
|
|
||
| it('should have required methods', () => { | ||
| const db = new DatabaseClient(); | ||
| testClient = db; |
There was a problem hiding this comment.
This assignment will overwrite the testClient reference from line 40, causing that DatabaseClient pool to leak. Each subsequent assignment in this test file compounds the problem.
Contributor
|
Superseded by #100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Jest force-exited after all tests passed due to lingering event listeners on PostgreSQL connection pools, causing CI failures despite successful test execution.
Root Cause
DatabaseClientregistered error handlers on the pgPoolbut never removed them before callingpool.end(). Node's event loop remained active with registered listeners.Changes
Pool cleanup (
src/database/client.ts)removeAllListeners()beforepool.end()to clear error handlersTest lifecycle (
src/database/module.test.ts,src/knowledge-base/module.test.ts)afterAllhooks to close DatabaseClient instances created in unit testsTiming (
jest.globalTeardown.ts)Config (
jest.unit.config.js)forceExit: true(no longer required)Tests now exit cleanly with code 0.
Original prompt
This pull request was created from Copilot chat.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.