Categories
eventemitter memory-leaks node.js

possible EventEmitter memory leak detected

327

I am getting following warning:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace: 
    at EventEmitter.<anonymous> (events.js:139:15)
    at EventEmitter.<anonymous> (node.js:385:29)
    at Server.<anonymous> (server.js:20:17)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1514:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1410:22)
    at TCP.onread (net.js:354:27)

I wrote code like this in server.js:

http.createServer(
    function (req, res) { ... }).listen(3013);

How to fix this ?

1

  • 71

    Use process.on('warning', e => console.warn(e.stack)); to debug the warning. Don’t use process.setMaxListeners(0); as the warning is there for some reason.

    Jan 8, 2018 at 9:09

118

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()”?

4

  • 5

    I am using process.on('uncaughtException', callback);

    – Riz

    Mar 19, 2012 at 10:51


  • 36

    I wouldn’t remove the max listener limit. You won’t get warnings, but you will get memory leaks.

    – user9016207

    Apr 6, 2018 at 19:45

  • 3

    Using process.setMaxListeners(0) you might be unknowingly leaking memory.

    May 14, 2021 at 5:37


  • The eventEmitter URL returns a 500 error. This is the one I found for the latest version: nodejs.org/docs/latest/api/events.html

    – hernvnc

    Apr 18 at 20:55

118

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()”?

4

  • 5

    I am using process.on('uncaughtException', callback);

    – Riz

    Mar 19, 2012 at 10:51


  • 36

    I wouldn’t remove the max listener limit. You won’t get warnings, but you will get memory leaks.

    – user9016207

    Apr 6, 2018 at 19:45

  • 3

    Using process.setMaxListeners(0) you might be unknowingly leaking memory.

    May 14, 2021 at 5:37


  • The eventEmitter URL returns a 500 error. This is the one I found for the latest version: nodejs.org/docs/latest/api/events.html

    – hernvnc

    Apr 18 at 20:55

107

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)

1

  • 2

    It may be legitimate code, for example several async functions run in parallel

    Dec 11, 2021 at 21:42