Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

possible EventEmitter memory leak detected

I'd like to point out here that that warning is there for a reason and there's a good chance the right fix is not increasing the limit but figuring out why you're adding so many listeners to the same event. Only increase the limit if you know why so many listeners are being added and are confident it's what you really want.

I found this page because I got this warning and in my case there was a bug in some code I was using that was turning the global object into an EventEmitter! I'd certainly advise against increasing the limit globally because you don't want these things to go unnoticed.


This is explained in the node eventEmitter documentation

What version of Node is this? What other code do you have? That isn't normal behavior.

In short, its: process.setMaxListeners(0);

Also see: node.js - request - How to “emitter.setMaxListeners()”?


The accepted answer provides the semantics on how to increase the limit, but as @voltrevo pointed out that warning is there for a reason and your code probably has a bug.

Consider the following buggy code:

//Assume Logger is a module that emits errors
var Logger = require('./Logger.js');

for (var i = 0; i < 11; i++) {
    //BUG: This will cause the warning
    //As the event listener is added in a loop
    Logger.on('error', function (err) {
        console.log('error writing log: ' + err)
    });

    Logger.writeLog('Hello');
}

Now observe the correct way of adding the listener:

//Good: event listener is not in a loop
Logger.on('error', function (err) {
    console.log('error writing log: ' + err)
});

for (var i = 0; i < 11; i++) {
    Logger.writeLog('Hello');
}

Search for similar issues in your code before changing the maxListeners (which is explained in other answers)


By default, a maximum of 10 listeners can be registered for any single event.

If it's your code, you can specify maxListeners via:

const emitter = new EventEmitter()
emitter.setMaxListeners(100)
// or 0 to turn off the limit
emitter.setMaxListeners(0)

But if it's not your code you can use the trick to increase the default limit globally:

require('events').EventEmitter.prototype._maxListeners = 100;

Of course you can turn off the limits but be careful:

// turn off limits by default (BE CAREFUL)
require('events').EventEmitter.prototype._maxListeners = 0;

BTW. The code should be at the very beginning of the app.

ADD: Since node 0.11 this code also works to change the default limit:

require('events').EventEmitter.defaultMaxListeners = 0

Replace .on() with once(). Using once() removes event listeners when the event is handled by the same function.

If this doesn't fix it, then reinstall restler with this in your package.json "restler": "git://github.com/danwrong/restler.git#9d455ff14c57ddbe263dbbcd0289d76413bfe07d"

This has to do with restler 0.10 misbehaving with node. you can see the issue closed on git here: https://github.com/danwrong/restler/issues/112 However, npm has yet to update this, so that is why you have to refer to the git head.


Node Version : v11.10.1

Warning message from stack trace :

process.on('warning', e => console.warn(e.stack));
(node:17905) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 wakeup listeners added. Use emitter.setMaxListeners() to increase limit
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 wakeup listeners added. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:255:17)
    at Connection.addListener (events.js:271:10)
    at Connection.Readable.on (_stream_readable.js:826:35)
    at Connection.once (events.js:300:8)
    at Connection._send (/var/www/html/fleet-node-api/node_modules/http2/lib/protocol/connection.js:355:10)
    at processImmediate (timers.js:637:19)
    at process.topLevelDomainCallback (domain.js:126:23)

After searching for github issues, documentation and creating similar event emitter memory leaks, this issue was observed due to node-apn module used for iOS push notification.

This resolved it :

You should only create one Provider per-process for each certificate/key pair you have. You do not need to create a new Provider for each notification. If you are only sending notifications to one app then there is no need for more than one Provider.

If you are constantly creating Provider instances in your app, make sure to call Provider.shutdown() when you are done with each provider to release its resources and memory.

I was creating provider object each time the notification was sent and expected the gc to clear it.


I am getting this warning too when install aglio on my mac osx.

I use cmd fix it.

sudo npm install -g npm@next

https://github.com/npm/npm/issues/13806