[π Bug]: Possible EventEmitter Memory Leak Detected
[π Bug]: Possible EventEmitter memory leak detected
As a user of WebdriverIO, a popular open-source test automation framework, you may have encountered the issue of a possible EventEmitter memory leak detected. This issue can cause your test results to be unstable and may lead to performance problems. In this article, we will explore the possible causes of this issue and provide a step-by-step guide on how to reproduce and fix it.
When executing a test suite that runs the execute
command multiple times, you may see the following message displayed multiple times:
[0-0] (node:49036) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 result listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
This message indicates that there is a possible EventEmitter memory leak detected, which can cause your test results to be unstable.
The relevant log output is as follows:
> wdio run ./wdio.conf.ts
Execution of 1 workers started at 2025-03-10T05:45:52.194Z
[0-0] RUNNING in chrome - file:///test/api.spec.ts
[0-0] (node:49036) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 result listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
[0-0] at genericNodeError (node:internal/errors:984:15)
[0-0] at wrappedFn (node:internal/errors:538:14)
[0-0] at _addListener (node:events:593:17)
[0-0] at EventEmitter.addListener (node:events:611:10)
[0-0] at Browser.WebDriver.prototype.<computed> (/work/wdio-electron-service/node_modules/.pnpm/@wdio+utils@9.11.0/node_modules/@wdio/utils/build/index.js:462:29)
[0-0] at Browser.manageSessionAbortions (file:///work/wdio-electron-service/node_modules/.pnpm/webdriver@9.11.0/node_modules/webdriver/build/node.js:180:8)
[0-0] at Browser.protocolCommand (file:///work/wdio-electron-service/node_modules/.pnpm/webdriver@9.11.0/node_modules/webdriver/build/node.js:100:72)
[0-0] at Browser.wrapCommandFn (/work/wdio-electron-service/node_modules/.pnpm/@wdio+utils@9.11.0/node_modules/@wdio/utils/build/index.js:902:32)
[0-0] at Browser.<anonymous> (/work/wdio-electron-service/node_modules/.pnpm/@wdio+utils@9.11.0/node_modules/@wdio/utils/build/index.js:1017:20)
[0-0] at Browser.execute (/work/wdio-electron-service/node_modules/.pnpm/webdriverio@9.11.0_puppeteer-core@22.15.0/node_modules/webdriverio/build/node.js:3576:15)
[0-0] at Browser.wrapCommandFn (/work/wdio-electron-service/node_modules/.pnpm/@wdio+utils@9.11.0/node_modules/@wdio/utils/build/index.js:902:32)
[0-0] at Browser.<anonymous> (/work/wdio-electron-service/node_modules/.pnpm/@wdio+utils@9.11.0/node_modules/@wdio/utils/build/index.js:1017:20)
[0-0] at ElectronWorkerService.execute (/work/wdio-electron-service/packages/src/commands/execute.ts:26:37)
[0-0] at Object.execute (/work/wdio-electron-service/packages/src/service.ts:68:77)
[0-0] at Function.mock.update (/work/wdio-electron-service/packages/src/mock.ts:290:2)
[0-0] at <anonymous> (/work/wdio-electron-service/packages/src/service.ts:196:73)
The possible causes of this issue are:
- EventEmitter memory leak: The EventEmitter memory leak occurs when an event listener is added to an EventEmitter without removing it. This can cause the EventEmitter to retain a reference to the event listener, leading to a memory leak.
- MaxListenersExceededWarning: The MaxListenersExceededWarning is triggered when the number of event listeners added to an EventEmitter exceeds the maximum allowed limit. This can cause the EventEmitter to throw an error and prevent it from functioning correctly.
To reproduce the bug, follow these steps:
- Create a test suite: Create a test suite that runs the
execute
command multiple times. - Run the test suite: Run the test suite using the
wdio
command. - Observe the log output: Observe the log output and look for the
MaxListenersExceededWarning
message.
To fix the bug, follow these steps:
- Increase the maximum number of event listeners: Increase the maximum number of event listeners allowed for the EventEmitter by setting the
maxListeners
property to a higher value. - Remove event listeners: Remove event listeners from the EventEmitter after they are no longer needed to prevent memory leaks.
- Use a different event emitter: Consider using a different event emitter that does not have the same memory leak issues.
In conclusion, the possible EventEmitter memory leak detected issue can cause your test results to be unstable and may lead to performance problems. By following the steps outlined in this article, you can reproduce and fix the bug. Remember to increase the maximum number of event listeners, remove event listeners, and consider using a different event emitter to prevent memory leaks.
- WebdriverIO Version: 9.11.0
- Node.js Version: v20.18.0
- Mode: WDIO Testrunner
- Capabilities:
browserName
:electron
,wdio:electronServiceOptions
:{ appBinaryPath, appArgs: ['foo', 'bar=baz'] }
- #14246: [π Bug]: Possible EventEmitter memory leak detected
- I agree to follow this project's Code of Conduct.
- I have searched the existing issues.
Q&A: Possible EventEmitter memory leak detected
In our previous article, we discussed the possible EventEmitter memory leak detected issue in WebdriverIO. In this article, we will provide a Q&A section to help you better understand the issue and how to fix it.
A: An EventEmitter memory leak occurs when an event listener is added to an EventEmitter without removing it. This can cause the EventEmitter to retain a reference to the event listener, leading to a memory leak.
A: The MaxListenersExceededWarning is triggered when the number of event listeners added to an EventEmitter exceeds the maximum allowed limit. This can cause the EventEmitter to throw an error and prevent it from functioning correctly.
A: To reproduce the bug, follow these steps:
- Create a test suite: Create a test suite that runs the
execute
command multiple times. - Run the test suite: Run the test suite using the
wdio
command. - Observe the log output: Observe the log output and look for the
MaxListenersExceededWarning
message.
A: To fix the bug, follow these steps:
- Increase the maximum number of event listeners: Increase the maximum number of event listeners allowed for the EventEmitter by setting the
maxListeners
property to a higher value. - Remove event listeners: Remove event listeners from the EventEmitter after they are no longer needed to prevent memory leaks.
- Use a different event emitter: Consider using a different event emitter that does not have the same memory leak issues.
A: The possible causes of the bug are:
- EventEmitter memory leak: The EventEmitter memory leak occurs when an event listener is added to an EventEmitter without removing it. This can cause the EventEmitter to retain a reference to the event listener, leading to a memory leak.
- MaxListenersExceededWarning: The MaxListenersExceededWarning is triggered when the number of event listeners added to an EventEmitter exceeds the maximum allowed limit. This can cause the EventEmitter to throw an error and prevent it from functioning correctly.
A: To prevent the bug from occurring, follow these best practices:
- Remove event listeners: Remove event listeners from the EventEmitter after they are no longer needed to prevent memory leaks.
- Use a different event emitter: Consider using a different event emitter that does not have the same memory leak issues.
- Increase the maximum number of event listeners: Increase the maximum number of event listeners allowed for the EventEmitter by setting the
maxListeners
property to a higher value.
A: The benefits of fixing the bug are:
- Improved performance: Fixing the bug can improve the performance of your test suite by preventing memory leaks and reducing the number of event listeners.
- Reduced errors: Fixing the bug can reduce the number of errors that occur during test execution, making it easier to debug and maintain your test suite.
- Increased reliability: Fixing the bug can increase the reliability of your test suite by preventing it from crashing or throwing errors due to memory leaks.
In conclusion, the possible EventEmitter memory leak detected issue can cause your test results to be unstable and may lead to performance problems. By following the steps outlined in this article, you can reproduce and fix the bug. Remember to increase the maximum number of event listeners, remove event listeners, and consider using a different event emitter to prevent memory leaks.
- WebdriverIO Version: 9.11.0
- Node.js Version: v20.18.0
- Mode: WDIO Testrunner
- Capabilities:
browserName
:electron
,wdio:electronServiceOptions
:{ appBinaryPath, appArgs: ['foo', 'bar=baz'] }
- #14246: [π Bug]: Possible EventEmitter memory leak detected
- I agree to follow this project's Code of Conduct.
- I have searched the existing issues.