Skip to content

Commit

Permalink
http: handle errors on idle sockets
Browse files Browse the repository at this point in the history
This change adds a new event handler to the `error` event of the socket
after it has been used by the http_client.

The purpose of this change is to catch errors on *keep alived*
connections from idle sockets that otherwise will cause an uncaugh error
event on the application.

Fix: #3595
PR-URL: #4482
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
jfromaniello authored and Myles Borins committed Mar 2, 2016
1 parent 39b2503 commit ebb9f0d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ function socketErrorListener(err) {
socket.destroy();
}

function freeSocketErrorListener(err) {
var socket = this;
debug('SOCKET ERROR on FREE socket:', err.message, err.stack);
socket.destroy();
socket.emit('agentRemove');
}

function socketOnEnd() {
var socket = this;
var req = this._httpMessage;
Expand Down Expand Up @@ -440,6 +447,7 @@ function responseOnEnd() {
}
socket.removeListener('close', socketCloseListener);
socket.removeListener('error', socketErrorListener);
socket.once('error', freeSocketErrorListener);
// Mark this socket as available, AFTER user-added end
// handlers have a chance to run.
process.nextTick(emitFreeNT, socket);
Expand Down Expand Up @@ -474,6 +482,7 @@ function tickOnSocket(req, socket) {
}

parser.onIncoming = parserOnIncomingClient;
socket.removeListener('error', freeSocketErrorListener);
socket.on('error', socketErrorListener);
socket.on('data', socketOnData);
socket.on('end', socketOnEnd);
Expand Down
55 changes: 55 additions & 0 deletions test/parallel/test-http-agent-error-on-idle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var http = require('http');
var Agent = http.Agent;

var agent = new Agent({
keepAlive: true,
});

var requestParams = {
host: 'localhost',
port: common.PORT,
agent: agent,
path: '/'
};

var socketKey = agent.getName(requestParams);

function get(callback) {
return http.get(requestParams, callback);
}

var server = http.createServer(function(req, res) {
res.end('hello world');
});

server.listen(common.PORT, function() {
get(function(res) {
assert.equal(res.statusCode, 200);
res.resume();
res.on('end', function() {
process.nextTick(function() {
var freeSockets = agent.freeSockets[socketKey];
assert.equal(freeSockets.length, 1,
'expect a free socket on ' + socketKey);

//generate a random error on the free socket
var freeSocket = freeSockets[0];
freeSocket.emit('error', new Error('ECONNRESET: test'));

get(done);
});
});
});
});

function done() {
assert.equal(Object.keys(agent.freeSockets).length, 0,
'expect the freeSockets pool to be empty');

agent.destroy();
server.close();
process.exit(0);
}

0 comments on commit ebb9f0d

Please sign in to comment.