Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http(s) Agent: handle errors on idle sockets #4482

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,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 @@ -448,6 +455,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After push free socket to freeSockets https://github.com/nodejs/node/blob/master/lib/_http_agent.js#L77 , then add the freeSocketErrorListener to handle the errors.

// lib/_http_agent.js#L77

socket._httpMessage = null;
self.removeSocket(socket, options);
freeSockets.push(socket);
+++ socket.once('error', freeSocketErrorListener);

Expand Down Expand Up @@ -483,6 +491,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
56 changes: 56 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,56 @@
'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 destroy_queue = {};
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);
}