Debugging EventEmitter Memory Leaks in Production



I wanted to share a tip for debugging EventEmitter memory leaks when running Meteor in production.

You have one of these when you see a console message like this:

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

Nice to know, but quite unhelpful when it comes to figuring out where it originated.

The solution is to use the following snippet of code to override the addListener() and on() methods of EventEmitter. It will record the type of event and generate an Error exception with a stack trace once the number of added listeners is greater than 10.

The above snippet should be inserted at the beginning of bundle/main.js before the following lines:

process.argv.splice(2, 0, 'program.json');
process.chdir(require('path').join(__dirname, 'programs', 'server'));

This ensures that the EventEmitter addListener() and on() methods are overridden by this debugging code before any of the Meteor project code has a chance to call them.

This technique finally allowed us to determine the cause of random lockup in our application caused by a bug in a third-party http2 library used by node-apn.


Thx for sharing @vlasky

I get one of those warnings in dev also. Where would you insert that snippet if I may ask?