Skip to content
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
http: make timeout event work with agent timeout
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.
  • Loading branch information
lpinca committed Jan 21, 2019
commit 772c28be85e9b96bdfa724b21a4f3bf4fe04168d
5 changes: 4 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,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();
}));