Skip to content

Instantly share code, notes, and snippets.

@prabhakhar
Created June 21, 2012 01:13
Show Gist options
  • Save prabhakhar/2963290 to your computer and use it in GitHub Desktop.
Save prabhakhar/2963290 to your computer and use it in GitHub Desktop.
Possible EventEmitter memory leak detected.
var http = require('http');
var options = {
host : 'www.nodejs.org',
port : 80,
method : 'HEAD',
path : '/api/http.html'
};
for( var i = 0 ; i < 10000 ; ++i ) {
var req = http.request(options, function(res) {
});
req.setTimeout(600, function() {
console.log('timed out');
});
req.end();
}
@ronkorving
Copy link

I guess the fix is just a matter of setTimeout removing the event listener when it fires?

@s3u
Copy link

s3u commented Jun 21, 2012

@ronkorving - not sure if that helps as listeners accumulate with each call to setTimeout. It does not matter whether any request times out or not.

@s3u
Copy link

s3u commented Jun 21, 2012

Link to the issue: nodejs/node-v0.x-archive#3505

@s3u
Copy link

s3u commented Jun 21, 2012

Actually the above test does not demonstrate connection reuse!

@s3u
Copy link

s3u commented Jun 22, 2012

I retract my first two comments. http.request returns a new ClientRequest each time - ClientRequest is an EventEmitter - and hence timeout listeners don't pile up when a connection is reused.

@s3u
Copy link

s3u commented Jun 22, 2012

See https://gist.github.com/2973367 for a variant of the test. The leak warning comes up when a new request is created using a socket that has outstanding requests. You can verify this by setting the agent to false in which case the socket has no outstanding requests. This explains why this warning comes up under load with the default agent.

See https://gist.github.com/joyent/node/blob/v0.6.19-release/lib/net.js#L143.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment