I ran into a timing issue where an HTTPS request followed by an HTTP request (the fact it is/not TLS might not matter) produces the following exception:
TypeError: Cannot call method 'emit' of undefined
at CleartextStream.<anonymous> (http.js:1199:9)
at CleartextStream.emit (events.js:64:17)
at Socket.onerror (tls.js:874:17)
at Socket.emit (events.js:64:17)
at Array.<anonymous> (net.js:825:27)
at EventEmitter._tickCallback (node.js:170:26)
This is 100% reproducible on one server but not on another.
The offending code in http.js:
Agent.prototype._establishNewConnection = function() {
<snip/>
socket.on('error', function(err) {
debug('AGENT SOCKET ERROR: ' + err.message);
var req;
if (socket._httpMessage) {
req = socket._httpMessage;
} else if (self.queue.length) {
req = self.queue.shift();
assert(req._queue === self.queue);
req._queue = null;
} else {
// No requests on queue? Where is the request
assert(0);
}
req.emit('error', err); // <-------------------------------------------------
req._hadError = true; // hacky
// clean up so that agent can handle new requests
parser.finish();
socket.destroy();
self._removeSocket(socket);
self._cycle();
});
The issue is that no req is found (the 'No requests on queue? Where is the request' comment). I'm happy to help debug it if someone can provide some assistance or insight.
EHL