Programmer
Programmer

Reputation: 8707

NodeJS : warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit

I have the below code:

var schild = spawn('script.sh', ["process1", "process2"]);
        schild.stderr.on('data', function (data) {
                        logger.info('stderr: ' + data);
        });

        schild.on('exit', function (code) {
          logger.info('child process exited with code ' + code);
        });

        schild.stdout.on('data', function (data) {
           logger.info('Data ' + data);
        });

when I run the code I get below error:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at EventEmitter.addListener (events.js:175:15)
    at EventEmitter.once (events.js:196:8)
    at Transport.logException (/x/home/prakash/src/node_modules/winston/lib/winston/transports/transport.js:118:8)
    at logAndWait (/x/home/prakash/src/node_modules/winston/lib/winston/logger.js:613:15)
    at async.forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:86:13)
    at Array.forEach (native)
    at _forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:26:24)
    at Object.async.forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:85:9)
    at Logger._uncaughtException (/x/home/prakash/src/node_modules/winston/lib/winston/logger.js:636:9)
    at process.EventEmitter.emit (events.js:126:20)
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at EventEmitter.addListener (events.js:175:15)
    at EventEmitter.once (events.js:196:8)
    at Transport.logException (/x/home/prakash/src/node_modules/winston/lib/winston/transports/transport.js:117:8)
    at logAndWait (/x/home/prakash/src/node_modules/winston/lib/winston/logger.js:613:15)
    at async.forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:86:13)
    at Array.forEach (native)
    at _forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:26:24)
    at Object.async.forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:85:9)
    at Logger._uncaughtException (/x/home/prakash/src/node_modules/winston/lib/winston/logger.js:636:9)
    at process.EventEmitter.emit (events.js:126:20)

Upvotes: 9

Views: 21245

Answers (1)

LuisCien
LuisCien

Reputation: 6432

I believe the problem is that you're not removing the listeners when you don't need them anymore. You need to use 'schild.removeListener('exit', function)' or 'schild.removeAllListeners('exit')' when you're done with them.

See: http://nodejs.org/api/events.html#events_emitter_removelistener_event_listener

Of course there are scenarios where you need to have more than 10 listeners in which case you should use 'schild.setMaxListeners(0)' (0 means unlimited)

See: http://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n

Hope it helps!

Upvotes: 5

Related Questions