Skip to content

mocha leaks event listeners on process #4495

@eps1lon

Description

@eps1lon

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: node.jscommand-line-or-Node.js-specifictype: buga defect, confirmed by a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions