Skip to content

Commit 25c19eb

Browse files
lpincaaddaleax
authored andcommitted
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. PR-URL: #25488 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b779c07 commit 25c19eb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/_http_client.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,10 @@ function tickOnSocket(req, socket) {
655655
socket.on('end', socketOnEnd);
656656
socket.on('close', socketCloseListener);
657657

658-
if (req.timeout !== undefined) {
658+
if (
659+
req.timeout !== undefined ||
660+
(req.agent && req.agent.options && req.agent.options.timeout)
661+
) {
659662
listenSocketTimeout(req);
660663
}
661664
req.emit('socket', socket);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const { expectsError, mustCall } = require('../common');
4+
const { Agent, get } = require('http');
5+
6+
// Test that the `'timeout'` event is emitted on the `ClientRequest` instance
7+
// when the socket timeout set via the `timeout` option of the `Agent` expires.
8+
9+
const request = get({
10+
agent: new Agent({ timeout: 500 }),
11+
// Non-routable IP address to prevent the connection from being established.
12+
host: '192.0.2.1'
13+
});
14+
15+
request.on('error', expectsError({
16+
type: Error,
17+
code: 'ECONNRESET',
18+
message: 'socket hang up'
19+
}));
20+
21+
request.on('timeout', mustCall(() => {
22+
request.abort();
23+
}));

0 commit comments

Comments
 (0)