Skip to content

Commit

Permalink
http: make timeout event work with agent timeout
Browse files Browse the repository at this point in the history
The `'timeout'` event is currently not emitted on the `ClientRequest`
instance when the socket timeout expires if only the `timeout` option
of the agent is set. This happens because, under these circumstances,
`listenSocketTimeout()` is not called.

This commit fixes the issue by calling it also when only the agent
`timeout` option is set.

PR-URL: #25488
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
lpinca authored and addaleax committed Feb 1, 2019
1 parent b779c07 commit 25c19eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,10 @@ function tickOnSocket(req, socket) {
socket.on('end', socketOnEnd);
socket.on('close', socketCloseListener);

if (req.timeout !== undefined) {
if (
req.timeout !== undefined ||
(req.agent && req.agent.options && req.agent.options.timeout)
) {
listenSocketTimeout(req);
}
req.emit('socket', socket);
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-http-agent-timeout-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const { expectsError, mustCall } = require('../common');
const { Agent, get } = require('http');

// Test that the `'timeout'` event is emitted on the `ClientRequest` instance
// when the socket timeout set via the `timeout` option of the `Agent` expires.

const request = get({
agent: new Agent({ timeout: 500 }),
// Non-routable IP address to prevent the connection from being established.
host: '192.0.2.1'
});

request.on('error', expectsError({
type: Error,
code: 'ECONNRESET',
message: 'socket hang up'
}));

request.on('timeout', mustCall(() => {
request.abort();
}));

0 comments on commit 25c19eb

Please sign in to comment.