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: destroy timeout socket by Agent #23752

Closed
wants to merge 1 commit into from
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
5 changes: 5 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,11 @@ removed: v10.0.0
Used when an invalid character is found in an HTTP response status message
(reason phrase).

<a id="ERR_HTTP_SOCKET_TIMEOUT"></a>
### ERR_HTTP_SOCKET_TIMEOUT
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mcollina change to ERR_HTTP_SOCKET_TIMEOUT and add update on request.setTimeout().


A socket timed out, it's triggered by HTTP.

<a id="ERR_INDEX_OUT_OF_RANGE"></a>
### ERR_INDEX_OUT_OF_RANGE
<!-- YAML
Expand Down
3 changes: 3 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,9 @@ added: v0.5.9
Once a socket is assigned to this request and is connected
[`socket.setTimeout()`][] will be called.

Emitted when the underlying socket times out from inactivity.
This only notifies that the socket has been idle. The request must be aborted manually.

### request.socket
<!-- YAML
added: v0.3.0
Expand Down
23 changes: 23 additions & 0 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const util = require('util');
const EventEmitter = require('events');
const debug = util.debuglog('http');
const { async_id_symbol } = require('internal/async_hooks').symbols;
const errors = require('internal/errors');
const {
ERR_HTTP_SOCKET_TIMEOUT,
} = errors.codes;

// New Agent code.

Expand Down Expand Up @@ -268,6 +272,24 @@ function installListeners(agent, s, options) {
}
s.on('close', onClose);

function onTimeout() {
debug('CLIENT socket onTimeout after', s.timeout, 'ms');
const name = agent.getName(options);
// If free socket timeout, must remove it from freeSockets list immediately
// to prevent new requests from being sent through this timeout socket.
if (agent.freeSockets[name] && agent.freeSockets[name].indexOf(s) !== -1) {
s.destroy();
agent.removeSocket(s, options);
debug('CLIENT free socket destroy');
} else if (s.listeners('timeout').length === 1) {
// No req timeout handler, agent must destroy the socket.
ronag marked this conversation as resolved.
Show resolved Hide resolved
s.destroy(new ERR_HTTP_SOCKET_TIMEOUT());
agent.removeSocket(s, options);
debug('CLIENT active socket destroy');
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I think just doing:

s.destroy(new ERR_HTTP_SOCKET_TIMEOUT());

should be enough now in master

s.on('timeout', onTimeout);

function onRemove() {
// We need this function for cases like HTTP 'upgrade'
// (defined by WebSockets) where we need to remove a socket from the
Expand All @@ -276,6 +298,7 @@ function installListeners(agent, s, options) {
agent.removeSocket(s, options);
s.removeListener('close', onClose);
s.removeListener('free', onFree);
s.removeListener('timeout', onTimeout);
s.removeListener('agentRemove', onRemove);
}
s.on('agentRemove', onRemove);
Expand Down
1 change: 1 addition & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ E('ERR_HTTP_HEADERS_SENT',
E('ERR_HTTP_INVALID_HEADER_VALUE',
'Invalid value "%s" for header "%s"', TypeError);
E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
E('ERR_HTTP_SOCKET_TIMEOUT', 'Socket timeout', Error);
E('ERR_HTTP_TRAILER_INVALID',
'Trailers are invalid with this transfer encoding', Error);
E('ERR_INSPECTOR_ALREADY_CONNECTED', '%s is already connected', Error);
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-gc-http-client-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const common = require('../common');
const onGC = require('../common/ongc');
const assert = require('assert');

function serverHandler(req, res) {
setTimeout(function() {
Expand Down Expand Up @@ -38,6 +39,11 @@ function getall() {
req.setTimeout(10, function() {
console.log('timeout (expected)');
});
req.on('error', (err) => {
Copy link
Contributor Author

@fengmk2 fengmk2 Oct 20, 2018

Choose a reason for hiding this comment

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

this is a break change, now timeout request will emit ERR_HTTP_SOCKET_TIMEOUT error if you don't handle the timeout event yourself.

// only allow Socket timeout error
assert.strictEqual(err.code, 'ERR_SOCKET_TIMEOUT');
assert.strictEqual(err.message, 'Socket timeout');
});

count++;
onGC(req, { ongc });
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-http-client-timeout-option-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const options = {
server.listen(0, options.host, common.mustCall(() => {
options.port = server.address().port;
doRequest(common.mustCall((numListeners) => {
assert.strictEqual(numListeners, 1);
// including the default onTimeout on Agent
assert.strictEqual(numListeners, 2);
doRequest(common.mustCall((numListeners) => {
assert.strictEqual(numListeners, 1);
assert.strictEqual(numListeners, 2);
server.close();
agent.destroy();
}));
Expand Down
26 changes: 26 additions & 0 deletions test/sequential/test-http-custom-timeout-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall((req, res) => {
server.close();
// do nothing, wait client socket timeout and close
}));

server.listen(0, common.mustCall(() => {
const agent = new http.Agent({ timeout: 100 });
const req = http.get({
path: '/',
port: server.address().port,
timeout: 50,
agent
}, common.mustNotCall())
.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'socket hang up');
assert.strictEqual(err.code, 'ECONNRESET');
}));
req.on('timeout', common.mustCall(() => {
req.abort();
}));
}));
29 changes: 29 additions & 0 deletions test/sequential/test-http-default-timeout-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall((req, res) => {
server.close();
// do nothing, wait client socket timeout and close
}));

server.listen(0, common.mustCall(() => {
let req;
const timer = setTimeout(() => {
req.abort();
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
assert.fail('should not timeout here');
}, 100);

const agent = new http.Agent({ timeout: 50 });
req = http.get({
path: '/',
port: server.address().port,
agent
}, common.mustNotCall())
.on('error', common.mustCall((err) => {
clearTimeout(timer);
assert.strictEqual(err.message, 'Socket timeout');
assert.strictEqual(err.code, 'ERR_HTTP_SOCKET_TIMEOUT');
}));
}));
52 changes: 52 additions & 0 deletions test/sequential/test-http-request-on-free-socket-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCallAtLeast((req, res) => {
const content = 'Hello Agent';
res.writeHead(200, {
'Content-Length': content.length.toString(),
});
res.write(content);
res.end();
}, 2));

server.listen(0, common.mustCall(() => {
const agent = new http.Agent({ timeout: 100, keepAlive: true });
const req = http.get({
path: '/',
port: server.address().port,
agent
}, common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
res.resume();
res.on('end', common.mustCall());
}));

const timer = setTimeout(() => {
assert.fail('should not timeout here');
req.abort();
}, 1000);

req.on('socket', common.mustCall((socket) => {
// wait free socket become free and timeout
socket.on('timeout', common.mustCall(() => {
// free socket should be destroyed
assert.strictEqual(socket.writable, false);
// send new request will be fails
clearTimeout(timer);
const newReq = http.get({
path: '/',
port: server.address().port,
agent
}, common.mustCall((res) => {
// agent must create a new socket to handle request
assert.notStrictEqual(newReq.socket, socket);
assert.strictEqual(res.statusCode, 200);
res.resume();
res.on('end', common.mustCall(() => server.close()));
}));
}));
}));
}));