fix(test): eliminate Winston uncaughtException listener leak#3544
fix(test): eliminate Winston uncaughtException listener leak#3544PierreBrisorgueil merged 2 commits intomasterfrom
Conversation
…test runs Each jest.resetModules() call in beforeEach re-imports lib/services/logger.js, creating a new Winston logger with handleExceptions:true. Winston registers a new process.uncaughtException listener on every import but never removes orphaned listeners from prior module registry generations. Across 1226 tests with resetModules in beforeEach, this leaked ~570 listeners and ~285-1100MB of heap. The billing PRs (#3535-#3539) amplified an existing pre-billing leak (162 instances) by +409 new instances (+252%) — pushing trawl_node past its 6144MB cap. Fix: guard handleExceptions with `process.env.NODE_ENV !== 'test'` on both the Console transport (logger creation) and the File transport (setupFileLogger / getLogOptions). Exception handling is preserved in production and development. Also add `forceExit: true` to jest.config.js to prevent Jest from hanging on open MongoDB handles from integration test afterAll disconnect races. Verified: MaxListenersExceededWarning fully eliminated. 99 suites / 1226 tests all pass.
|
Warning Rate limit exceeded
To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 36 minutes and 36 seconds.Comment |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.
TIP This summary will be updated as you push new changes.
There was a problem hiding this comment.
Pull request overview
This PR addresses a Jest test-suite memory leak caused by repeated Winston handleExceptions registration when modules are re-imported after jest.resetModules(), and attempts to reduce test runner hangs.
Changes:
- Disable Winston transport
handleExceptionswhenNODE_ENV === 'test'to prevent accumulatingprocess.uncaughtExceptionlisteners across module reloads. - Add
forceExit: trueto Jest config to force termination after test completion.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/services/logger.js | Guards Winston handleExceptions in test env to prevent uncaughtException listener leaks during jest.resetModules() usage. |
| jest.config.js | Forces Jest to exit after tests to avoid hangs from lingering handles. |
There was a problem hiding this comment.
Pull Request Overview
This PR successfully addresses a critical memory leak (>500MB heap) in the test suite by disabling Winston's exception listeners when running in the test environment. While the changes in lib/services/logger.js are sound and meet the requirements, the inclusion of forceExit: true in jest.config.js is a concern. Although the PR is considered 'up to standards' by Codacy, both automated and manual review components identify forceExit as a practice that masks resource leaks (such as unawaited MongoDB disconnections). It is recommended to address the root cause of hanging tests in afterAll hooks rather than relying on forced termination.
About this PR
- Disabling
handleExceptionsin the test environment prevents Winston from capturing unhandled exceptions. While Jest will still report these, they will no longer appear in logs formatted by the Winston transport during test execution.
Test suggestions
- Winston Console transport disables exception handling in test environment.
- Winston File transport options disable exception handling in test environment.
- Jest configuration ensures process exit after test completion to prevent hangs.
- Winston continues to handle exceptions when NODE_ENV is not 'test'.
TIP Improve review quality by adding custom instructions
TIP How was this review? Give us feedback
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3544 +/- ##
=======================================
Coverage 87.78% 87.78%
=======================================
Files 128 128
Lines 3587 3587
Branches 1052 1052
=======================================
Hits 3149 3149
Misses 347 347
Partials 91 91 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…dleExceptions guard
* perf(test): add explicit GC between suites to reduce peak heap in coverage run Add --expose-gc to test:coverage NODE_OPTIONS and register a minimal Jest reporter (scripts/jest.gcReporter.cjs) that calls global.gc() after each test suite completes. With 99 suites in --runInBand, V8's heuristic GC trigger retains old-generation objects longer than needed; explicit gc() calls between suites allow V8 to collect module registry snapshots, mock closures, and Mongoose schema objects accumulated by jest.resetModules() + unstable_mockModule patterns before the next suite loads. Peak RSS: 1.87 GB → 1.64 GB (−12.3%) with --expose-gc active. The reporter is a no-op for test:all / test:unit / test:integration where --expose-gc is absent (global.gc undefined), so no behaviour change on those scripts. Context: PR #3544 (Winston listener fix) cut peak from ~4.8–5.2 GB to ~1.87 GB. This PR pushes further to 1.64 GB, well under the 3 GB target. Verified: 99 suites / 1226 tests all pass. * fix(test): cross-env double-quote NODE_OPTIONS + JSDoc on gcReporter - Replace single quotes around NODE_OPTIONS value with escaped double quotes for Windows/cross-env portability (cross-env README: single quotes not portable) - Add JSDoc block to onTestFileResult per repo convention (every function needs @param + @returns)
Summary
lib/services/logger.jscreates a new Winston logger withhandleExceptions: trueon both Console and File transports. Everyjest.resetModules()in abeforeEachre-imports logger.js, registering a newprocess.uncaughtExceptionlistener. Orphaned listeners from prior module registry generations are never removed.resetModules— 571 total listener registrations across 1226 tests. The billing PRs (feat(billing): compute layer foundation - PR-N1 (no behavior change) #3535–feat(billing): crons + dunning grace 7d (PR-N5) #3539) amplified the pre-existing leak from 162 to 571 instances (+252%).handleExceptionswithprocess.env.NODE_ENV !== 'test'on both the Console transport andgetLogOptions()(File transport). Zero behavioral change in production/development.forceExit: trueto jest.config.js to prevent Jest hanging on open MongoDB handles from integration testafterAlldisconnect races.Evidence
Before fix:
After fix:
Impact
Test plan
handleExceptionsonly disabled whenNODE_ENV=test