Skip to content

fix(test): eliminate Winston uncaughtException listener leak#3544

Merged
PierreBrisorgueil merged 2 commits intomasterfrom
fix/test-winston-listener-leak
Apr 30, 2026
Merged

fix(test): eliminate Winston uncaughtException listener leak#3544
PierreBrisorgueil merged 2 commits intomasterfrom
fix/test-winston-listener-leak

Conversation

@PierreBrisorgueil
Copy link
Copy Markdown
Contributor

Summary

  • Root cause identified: lib/services/logger.js creates a new Winston logger with handleExceptions: true on both Console and File transports. Every jest.resetModules() in a beforeEach re-imports logger.js, registering a new process.uncaughtException listener. Orphaned listeners from prior module registry generations are never removed.
  • Scope: 50 test files use resetModules — 571 total listener registrations across 1226 tests. The billing PRs (feat(billing): compute layer foundation - PR-N1 (no behavior change) #3535feat(billing): crons + dunning grace 7d (PR-N5) #3539) amplified the pre-existing leak from 162 to 571 instances (+252%).
  • Fix: Guard handleExceptions with process.env.NODE_ENV !== 'test' on both the Console transport and getLogOptions() (File transport). Zero behavioral change in production/development.
  • Bonus: Added forceExit: true to jest.config.js to prevent Jest hanging on open MongoDB handles from integration test afterAll disconnect races.

Evidence

Before fix:

MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
11 uncaughtException listeners added to [process]. MaxListeners is 10.
→ at ExceptionHandler.handle (winston/exception-handler.js:51)
→ at lib/services/logger.js:98 (File transport via setupFileLogger)
→ at lib/services/logger.js:146 (module-level setupFileLogger() call)

After fix:

Test Suites: 99 passed, 99 total
Tests:       1226 passed, 1226 total
Time: ~49s
(no MaxListenersExceededWarning)

Impact

  • Estimated heap savings: 285–1100 MB (conservative ~500KB per listener × 571 instances)
  • Directly unblocks trawl_node OOM at 6144MB after pulling billing PRs
  • Verdict: MIXED — STACK_LEAK (root cause here in devkit) amplified by billing PR test volume

Test plan

  • Full suite: 99 suites / 1226 tests — all pass, no MaxListeners warning
  • Production behavior unchanged: handleExceptions only disabled when NODE_ENV=test
  • CI green expected

…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.
Copilot AI review requested due to automatic review settings April 30, 2026 10:05
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 30, 2026

Warning

Rate limit exceeded

@PierreBrisorgueil has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 36 minutes and 36 seconds before requesting another review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 7db3cb47-2f75-4530-aeed-10d148ea4265

📥 Commits

Reviewing files that changed from the base of the PR and between 9d39136 and cad417a.

📒 Files selected for processing (2)
  • jest.config.js
  • lib/services/logger.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/test-winston-listener-leak

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.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 36 minutes and 36 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented Apr 30, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 handleExceptions when NODE_ENV === 'test' to prevent accumulating process.uncaughtException listeners across module reloads.
  • Add forceExit: true to 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.

Comment thread lib/services/logger.js
Copy link
Copy Markdown

@codacy-production codacy-production Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 handleExceptions in 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

Comment thread jest.config.js
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.78%. Comparing base (9d39136) to head (cad417a).
⚠️ Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@PierreBrisorgueil PierreBrisorgueil merged commit 229ecf1 into master Apr 30, 2026
5 checks passed
@PierreBrisorgueil PierreBrisorgueil deleted the fix/test-winston-listener-leak branch April 30, 2026 10:19
PierreBrisorgueil added a commit that referenced this pull request Apr 30, 2026
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants