Create a gist now

Instantly share code, notes, and snippets.

@othiym23 /sample.js Secret forked from dizzyd/Errors
Created Dec 8, 2012

What would you like to do?
bottom is output of the top run using NODE_DEBUG=net,event node sample.js
'use strict';
var k = 1;
var events = require('events');
var emit = events.EventEmitter.prototype.emit;
events.EventEmitter.prototype.emit = function () {
var type = arguments[0];
if (type !== 'close') return emit.apply(this, arguments);
if (!this._events) return false;
var closeListeners = this._events[type];
if (closeListeners &&
Array.isArray(closeListeners) &&
closeListeners.length > 10) {
var sliced =;
console.log("%d: class - %s", k,;
console.log("%d: type - %s", k, type);
console.log("%d: closeListeners length - %s", k, sliced.length);
console.log("%d: max closeListeners value - %s", k, sliced[sliced.length - 1]);
// debugger;
return emit.apply(this, arguments);
var request = require('request');
function hambulance() {
var req = request.head({
url : '',
headers : {
'User-Agent': 'AppleWebKit/525.13 (KHTML, like Gecko) Safari/525.13.',
'Connection': 'close'
followRedirect : false
}, function (err, res, body) {
var json = JSON.stringify(res);
if (err) console.log("%d: Err: %j", k, err);
k += 1;
NET: connect: find host
NET: afterConnect
NET: Drain the connect queue
NET: destroy
NET: close
NET: connect: find host
NET: afterConnect
NET: Drain the connect queue
NET: destroy
NET: close
NET: connect: find host
NET: afterConnect
NET: Drain the connect queue
NET: destroy
NET: close
4: class - Socket
4: type - close
4: closeListeners length - 10383
4: max closeListeners value - undefined
{ domain: null,
{ free: [Function],
[ [Function],
[Function: socketCloseListener],
... 10,000+ lines deleted...
[10382]: undefined ],
writable: true,
_last: true,
chunkedEncoding: false,
shouldKeepAlive: false,
useChunkedEncodingByDefault: false,
sendDate: false,
_hasBody: true,
_trailer: '',
finished: true,
{ domain: null,
_events: [Object],
_maxListeners: 10,
options: {},
requests: {},
sockets: [Object],
maxSockets: 5,
createConnection: [Function] },
socketPath: undefined,
method: 'HEAD',
path: '/97k9',
{ 'user-agent': 'AppleWebKit/525.13 (KHTML, like Gecko) Safari/525.13.',
connection: 'close',
host: '',
cookie: 'fc=fcVal=6232838655089761726',
'content-length': 0 },
{ 'user-agent': 'User-Agent',
connection: 'Connection',
host: 'host',
cookie: 'cookie',
'content-length': 'content-length' },
_header: 'HEAD /97k9 HTTP/1.1\r\nUser-Agent: AppleWebKit/525.13 (KHTML, like Gecko) Safari/525.13.\r\nConnection: close\r\nhost:\r\ncookie: fc=fcVal=6232838655089761726\r\ncontent-length: 0\r\n\r\n',
_headerSent: true,
socket: [Circular],
connection: [Circular],
parser: null,
{ domain: null,
_events: [Object],
_maxListeners: 10,
socket: [Circular],
connection: [Circular],
httpVersion: '1.1',
complete: true,
headers: [Object],
trailers: {},
readable: false,
_paused: false,
_pendings: [Object],
_endEmitted: true,
url: '',
method: null,
statusCode: 200,
client: [Circular],
httpVersionMajor: 1,
httpVersionMinor: 1,
upgrade: false,
request: [Object],
toJSON: [Function: toJSON],
req: [Circular] } },
ondata: null,
onend: null,
_connectQueue: null,
readable: false,
_idleNext: null,
_idlePrev: null,
_idleTimeout: -1 }
listeners[i].apply(this, args);
TypeError: Cannot call method 'apply' of undefined
at Socket.EventEmitter.emit (events.js:126:20)
at (/Users/forrest/Documents/projects/newrelic/nodejs-agent-git/sample.js:26:15)
at Socket._destroy.destroyed (net.js:357:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment