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

Do not display MaxListenersExceededWarning "Warning: Possible EventEmitter memory leak detected." warning message during cypress run #1305

Closed
YevheniiaMazur opened this issue Feb 13, 2018 · 96 comments · Fixed by #15723
Labels
browser: electron type: performance 🏃‍♀️ Performance related type: unexpected behavior User expected result, but got another

Comments

@YevheniiaMazur
Copy link

YevheniiaMazur commented Feb 13, 2018

Current behavior:

After creating 8 spec files warning about EventEmitter memory leak appears (even if specs contain empty describe()). After ignoring this warning and continue creating more specs, last added specs don't run, and appears FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Desired behavior:

Number of spec files do not affect test run

How to reproduce:

  • Add cypress to project
  • At cypress/integration folder create 8 spec files with any content inside them it could be only describe('', () => {});
  • Run created tests using command: cypress run

Additional Info (images, stack traces, etc)

After adding 8th spec next warning appeared:
memoryleakmessage

Even if there are no tests inside them
zerotestwithmemoryleak

After ignoring this warning and creating more specs got this error:
fatalerror

  • Operating System: Linux, MacOS
  • Cypress Version: both 1.4.2 and 1.4.1
  • Node version: 8.8.1
@brian-mann
Copy link
Member

The event emitter warnings are likely nothing important - these are node event bindings which don't affect running tests in the browser. We've had memory issues in the past but it always been related to the browser - the node process itself never uses up that much memory.

This is something we likely need to look at and increase the number of listeners.

Regardless - you are definitely getting OOM's so something has to be wrong. The thing is though - Cypress runs literally thousands of projects, many of which have many more specs than that - sometimes hundreds of spec files. Cypress also records millions of test runs on a regular basis. I'm saying that because this issue could not possibly be as simple as adding 8 spec files with no content. If that were the case, the thousands of internal tests we run on Cypress would all crash immediately.

There has to be something unique to your system that's causing this. How much memory does your system actually have? You say that your OS is both linux and mac - have you seen this exact crash on two independent systems? Your last screenshot looks different as if it's from a CI provider.

@brian-mann brian-mann added the stage: needs information Not enough info to reproduce the issue label Feb 13, 2018
@YevheniiaMazur
Copy link
Author

@brian-mann , I reproduced this issue both at MacOS and at Linux (at separate devices). Screenshot with JavaScript heap out of memory error was from Travis CI log.
If it will be helpful, I can give you link to PR with this error at github, or try reproduce this issue at empty Angular project, and than give you link to it. Which option is better for you?

@brian-mann
Copy link
Member

PR is fine since we can look at the code and should be able to repro

@YevheniiaMazur
Copy link
Author

This is pr with JavaScript heap out of memory error: valor-software/ngx-bootstrap#3701

And here pr, where I reproduced, that adding 8th spec (even if previous specs were all empty) started throwing warning about memory leak valor-software/ngx-bootstrap#3742

@YevheniiaMazur
Copy link
Author

YevheniiaMazur commented Feb 13, 2018

For first PR to run e2e tests locally you have to follow next steps:

  • Open terminal
  • install all dependencies using command npm I
  • run it locally using command npm run demo.serve
  • After opening demo window, in separate tab run command npm run cy:run

For second PR, you have to use next commands:

  • npm i
  • npm run build && npm run link
  • ng serve
  • After appearing message about finishing compiling, in separate tab enter command npm run cy:run

@YevheniiaMazur
Copy link
Author

@brian-mann I've create empty angular project, install there cypress, and this issue appeared there too. link to project: https://github.com/YevheniiaMazur/emptyProjSypress
How to run empty specs:

  • in terminal run commands npm install and then - ng serve
  • after compiling in separate tab run command ./node_modules/.bin/cypress run

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Feb 13, 2018

This warning does display for me in the Angular empty specs run:

Warning: Possible EventEmitter memory leak detected. 11 close listeners added. 
Use emitter.setMaxListeners() to increase limit

There is no warning in Angular empty specs run for me about JavaScript heap out of memory

@brian-mann
Copy link
Member

@YevheniiaMazur @jennifer-shehane I expect to see the node event emitter warning - we can take a look at that and get it fixed.

I do not expect to see OOM crashes - so anything that reproduces this is helpful. @YevheniiaMazur you mentioned that you were seeing this in Travis - do you mind setting up the empty specs project there and giving a link to a run where this has happened?

We'll try to look at the event emitter problem asap just to see where it's coming from and make sure it's not the root cause - preliminarily I don't think it is.

@brian-mann
Copy link
Member

brian-mann commented Feb 13, 2018

@YevheniiaMazur after looking at this more - I think you're really describing two separate issues here.

Issue 1.
When Cypress loads in more than 8 spec files you see an event emitter warning in node. This should be fixed by us - but its incidental and not related to the OOM at all. This warning happens even if there is no content in the spec file.

Issue 2.
This is the real OOM error - but it's only happening when you run all of your tests in the browser. Essentially this error is real and correct - the browser is exhausting all of the available memory in Travis and dying. Why this is happening is another story. This could very well be indicative of a memory leak in Cypress (in the browser).

Issue 2 likely has nothing to do with issue 1.

With that said - there are several existing issues already open in Cypress that have mentioned similar situations. Not OOM'ing - but Cypress slowing down over a period of time. We're aware of these and are about to land a version of Cypress that changes the way we run specs. Instead of bundling them all together - the default way will be to chunk them by spec file and in between that killing the renderer process (the browser). That will purge primed memory and help prevent OOM. This is landing soon (as in landing in Feb) and there's already an open branch with this work.

This is part of a much larger issue here which goes into all the glorious intricate details.

#681

If your issue is related to this one then it'll be fixed relatively soon. For now you can manually run your specs in isolation with this comment. This will come close to working the same way as when we make this the default in a newer version.

#416 (comment)

@joelparkerhenderson
Copy link

joelparkerhenderson commented Jul 26, 2018

Can the Cypress team please update this issue?

I am new to Cypress and encountering a similar error on current Cypress, current macOS, 16G machine, with just a dozen or so tests running:

(node:40489) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exitEarlyWithErr listeners added. Use emitter.setMaxListeners() to increase limit

Thank you.

@drewbrend
Copy link

I've also just run into this issue. It seems to be related to the number of specs for us as well. We have 12 specs in this project currently, they run well and that error is not seen in the console log.

I am working on a ticket now which adds 3 more specs to the run, we now consistently see this error on the 10th and 11th spec ran, then the build hangs and times out.

@subpx
Copy link

subpx commented Aug 30, 2018

Having same issue. Start getting error after running 10 spec files.

@drewbrend
Copy link

Just wanted to add, I only see this in electron browser. Running in chrome does not have this issue. We've switched to chrome for now.

@stclairdaniel
Copy link

I'm having the same issue - it shows the error after the 10th spec file finishes.

@fijimunkii

This comment has been minimized.

@haugsrud

This comment has been minimized.

@surfjedi
Copy link

We are also seeing this on the 11th test..

@Junaid-Aslam1
Copy link

I am also having the same issue and numTestsKeptInMemory=1 also does not work.

@JefferE
Copy link

JefferE commented Nov 30, 2018

We are also seeing this on the 11th test..

@benmardana
Copy link

We're also seeing the same error on 11th test

@nikhilesh009
Copy link

nikhilesh009 commented Dec 6, 2018

We're also getting the same error, it's weird. is any temporary fix ? i am blocked ;(
I ran the test and generated video, normal test (in test runner )run 1.30 second but when i ran on CI ,it hanged up
screenshot 2018-12-07 at 08 36 50
it keep loading until i stopped the test.

@nrso
Copy link

nrso commented Jan 3, 2019

We are also having it after the 10th test

(node:4948) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exitEarlyWithErr listeners added. Use emitter.setMaxListeners() to increase limit

@jennifer-shehane
Copy link
Member

We are aware of the warnings being displayed for Possible EventEmitter memory leak detected. and believe this to not be affecting the behavior or Cypress.

If you are having a situation where they browser crashes, does not load, or in general effects your testing suite run (besides just printing the error!), please open a new issue detailing how to reproduce the error locally on our machines.

This issue will be left open for Cypress to address the EventEmitter memory leak and it's warning message only.

@jennifer-shehane jennifer-shehane added stage: ready for work The issue is reproducible and in scope type: unexpected behavior User expected result, but got another and removed stage: needs information Not enough info to reproduce the issue labels Jan 14, 2019
@rnovosad
Copy link

@dil-bpecor try Cypress.setMaxListeners(20) seems like it helped for me

@NPC
Copy link

NPC commented Feb 23, 2021

@rnovosad interesting! Is Cypress.setMaxListeners documented anywhere? And at what point would you call it, in commands.js?

@rnovosad
Copy link

@NPC sorry, forgot to mention. support/index.js

@joelgrimberg
Copy link

did not work for me; I still get:
(node:98231) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 preprocessor:close listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit

and at the end of my run:

(node:98198) Warning: a promise was created in a handler at internal/timers.js:456:21 but was not returned from it, see http://goo.gl/rRqMUw at Function.Promise.cast (/Users/joelgrimberg/Library/Caches/Cypress/6.4.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/promise.js:225:13)

@k-rokicki
Copy link

Same problem, Cypress.setMaxListeners also did not work

@dil-bpecor
Copy link

dil-bpecor commented Feb 23, 2021

@rnovosad still no luck, maybe I have not set it right?

cypress/support/index.js

import "./commands";

import "@cypress/code-coverage/support";

require("cypress-plugin-tab");

Cypress.setMaxListeners(40);

Cypress.on("window:before:load", function (win) {
	const original = win.EventTarget.prototype.addEventListener;
	win.EventTarget.prototype.addEventListener = function () {
		if (arguments && arguments[0] === "beforeunload") {
			return;
		}
		return original.apply(this, arguments);
	};
	Object.defineProperty(win, "onbeforeunload", {
		get: function () {},
		set: function () {},
	});
});

Update - Removed the below form the cypress/support/index.js no change:

Cypress.on("window:before:load", function (win) {
	const original = win.EventTarget.prototype.addEventListener;
	win.EventTarget.prototype.addEventListener = function () {
		if (arguments && arguments[0] === "beforeunload") {
			return;
		}
		return original.apply(this, arguments);
	};
	Object.defineProperty(win, "onbeforeunload", {
		get: function () {},
		set: function () {},
	});
});
Running:  goals\goal.spec.js                                                            (11 of 33)
(node:31516) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 will-download listeners added to [Session]. Use emitter.setMaxListeners() to increase limit

@NPC
Copy link

NPC commented Feb 23, 2021

Same here, despite “setting” max listeners to 20 (I'm not confident that command works), I still get warning on the 11th one, as before.

@dil-bpecor
Copy link

dil-bpecor commented Feb 24, 2021

For others fighting this issue, if you set cypress back to 4.12.1 do you still get the MaxListenersExceededWarning issue?
Then upgrade to any release after (5.0.0+), does it shows up again?

It clears up for me, but the tests take 3x as long to run, so not really a good option.

Update:

Switching from Electron to Chrome same issue although shows up on 10 of 33 rather then 11 or 33

npx cypress run --browser chrome --config defaultCommandTimeout=10000 screenshotOnRunFailure=true

Running: appHeader\appHeader.spec.js (10 of 33)
##error MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 preprocessor:close listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit


Testing headless chrome (Most test just fail and I get the MaxListenersExceededWarning error):

npx cypress run --headless --browser chrome --config defaultCommandTimeout=10000 screenshotOnRunFailure=true

Running: appHeader\appHeader.spec.js (10 of 33)
(node:9528) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 preprocessor:close listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit

@dil-bpecor
Copy link

dil-bpecor commented Feb 25, 2021

Added Ten test files (maxtest.spec1.js - maxtest.spec10.js) with:

describe('Emitter Test', () => {
it('Does not do much!', () => {
expect(true).to.equal(true)
})
})

Running: aaMaxEmitterTest\maxtest.spec10.js (10 of 45)
(node:47340) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 preprocessor:close listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit

Cypress 6.5.0
Node v12.2.0

@Jacek-fstack
Copy link

We're facing the same issue. I've noticed it's exactly 11 GET requests before it throws the error. And it happens only if these GET requestes are fired before the page loads. But I have no idea what can I do with this information, as changing # of listeners does nothing :)

@spitofland
Copy link

spitofland commented Feb 26, 2021

I'm getting the same error with Cypress 6.3.0, but I don't think it's caused simply by the number of spec files.

I started getting the error when I added some tests to my ninth spec file.
Before the error, the spec file contained a test for login and logout.
The error started when I then added some tests verifying snapshots of some client styling (with cypress-visual-regression).

Now I can successfully run the login test OR the snapshot tests, but if both are enabled, I get the error.
I can successfully run both in the same spec file if I disable all of the other spec files.

I'd like to try working around this issue. Does anyone have a good explanation for which Cypress commands setup event listeners? I'm guessing it's a lot of them.

Update:
My tests just passed when I removed this line from the login test:
cy.wait('@Login').then(xhr => {expect(xhr.status === 303)})

Further Update:
The test is failing again (with this error) on subsequent runs.

(node:14792) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 preprocessor:close listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit

Oops...we found an error preparing this test file:



The error was:

Error: Webpack Compilation Error
multi .
Module not found: Error: Can't resolve 'C:\dev\xxx' in 'C:\dev\xxx'
Looked for and couldn't find the file at the following paths:
[C:\dev\xxx]
[C:\dev\xxx.js]
[C:\dev\xxx.json]
[C:\dev\xxx.jsx]
[C:\dev\xxx.mjs]
[C:\dev\xxx.coffee]
[C:\dev\xxx\index]
[C:\dev\xxx\index.js]
[C:\dev\xxx\index.json]
[C:\dev\xxx\index.jsx]
[C:\dev\xxx\index.mjs]
[C:\dev\xxx\index.coffee]

This occurred while Cypress was compiling and bundling your test code. This is usually caused by:

- A missing file or dependency
- A syntax error in the file or one of its dependencies

Fix the error in your code and re-run your tests.

(xxx is the directory I'm running Cypress from.)

@Kanwar27
Copy link

Kanwar27 commented Feb 27, 2021

Cypress 6.5.0 and running the last test on 10th spec file produced aforementioned error. Browser used (chrome)

Running: **.test.ts (10 of 10)
(node:21792) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 preprocessor:close listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit

@NPC
Copy link

NPC commented Mar 15, 2021

Version 6.7.0, the issue is still happening.

@TomBeckett
Copy link

@jennifer-shehane is this something the Cypress team aware of?

@resumerise
Copy link

Still existing in 6.8.0.

@mmonteiroc
Copy link

mmonteiroc commented Mar 23, 2021

Same behavior here. is there any news of this issue ?
13 tests in my test suite, but had this warning since the 3rd tests I created

Using mocha.
v 6.8.0

@jcui-wa
Copy link

jcui-wa commented Mar 23, 2021

run into same issue:
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 preprocessor:close listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase l

any update for this ?

@Phillipe-Bojorquez
Copy link

Would this be caused by the number of browser instances being created during the run of the tests? Is there a way to output what the listeners are listening to? With this information it seems like it would be easier to track down the issue. I see the maxlisteners warning for the tests but seeing the warning is no guarantee that the tests will fail. Though it does confuse the debugging of the tests because you see a very real warning being appended to your tests. Running the tests with verbose output in Jenkins helped to target in on memory management issues for each test which is nice

https://docs.cypress.io/guides/references/troubleshooting#Print-DEBUG-logs

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 1, 2021

The code for this is done in cypress-io/cypress#15723, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Apr 5, 2021

Released in 7.0.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v7.0.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Apr 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
browser: electron type: performance 🏃‍♀️ Performance related type: unexpected behavior User expected result, but got another
Projects
None yet
Development

Successfully merging a pull request may close this issue.