Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocha leaks event listeners on process #4495

Closed
4 tasks done
eps1lon opened this issue Nov 2, 2020 · 2 comments
Closed
4 tasks done

mocha leaks event listeners on process #4495

eps1lon opened this issue Nov 2, 2020 · 2 comments
Labels
area: node.js command-line-or-Node.js-specific type: bug a defect, confirmed by a maintainer

Comments

@eps1lon
Copy link

eps1lon commented Nov 2, 2020

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Runner does not clean up event listeners on global objects.

Steps to Reproduce

https://github.com/eps1lon/mocha-event-leak

Expected behavior: [What you expect to happen]

Removes all event listeners when destroyed.

Actual behavior: [What actually happens]

Event listeners on process leak:

ℹ [mocha] waiting for changes...
(node:18921) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 uncaughtException listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18921) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 unhandledRejection listeners added to [process]. Use emitter.setMaxListeners() to increase limit

Runner.prototype.dispose is never called in watchmode.

Reproduces how often: [What percentage of the time does it reproduce?]

Every time after the ~10th run

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version:
  • The output of node --version: 14.4.0
  • Your operating system
    • name and version: Ubuntu 18.04
    • architecture (32 or 64-bit): 64 bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): fish
  • Your browser and version (if running browser tests): N/A
  • Any third-party Mocha-related modules (and their versions): no
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): no

Additional Information

The event handlers are attempted to be removed in prepare:

mocha/lib/runner.js

Lines 1063 to 1064 in 7e490aa

this._removeEventListener(process, 'uncaughtException', this.uncaught);
this._removeEventListener(process, 'unhandledRejection', this.uncaught);

That doesn't have any effect after instantiation since this.uncaught is different between each runner instance. Whatever constructs the Runner instance also needs to call dispose. Might be better if the Runner took care of it e.g. with this.once('end', () => this.dispose()) in the constructor.

@boneskull
Copy link
Member

thanks. plausible, since we've had problems with this before.

@boneskull boneskull added type: bug a defect, confirmed by a maintainer area: node.js command-line-or-Node.js-specific and removed unconfirmed-bug labels Nov 2, 2020
boneskull added a commit that referenced this issue Nov 2, 2020
Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
@boneskull
Copy link
Member

See #4496

boneskull added a commit that referenced this issue Nov 2, 2020
Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: node.js command-line-or-Node.js-specific type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

No branches or pull requests

2 participants