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: make response.setTimeout() work #34913

Closed
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
20 changes: 15 additions & 5 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,8 @@ function socketOnData(d) {
socket.removeListener('end', socketOnEnd);
socket.removeListener('drain', ondrain);

if (req.timeoutCb)
socket.removeListener('timeout', req.timeoutCb);
if (req.timeoutCb) socket.removeListener('timeout', req.timeoutCb);
socket.removeListener('timeout', responseOnTimeout);

parser.finish();
freeParser(parser, req, socket);
Expand Down Expand Up @@ -644,6 +644,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
// Add our listener first, so that we guarantee socket cleanup
res.on('end', responseOnEnd);
req.on('prefinish', requestOnPrefinish);
socket.on('timeout', responseOnTimeout);

// If the user did not listen for the 'response' event, then they
// can't possibly read the data, so we ._dump() it into the void
Expand Down Expand Up @@ -692,15 +693,16 @@ function responseKeepAlive(req) {

function responseOnEnd() {
const req = this.req;
const socket = req.socket;

if (req.socket && req.timeoutCb) {
req.socket.removeListener('timeout', emitRequestTimeout);
if (socket) {
if (req.timeoutCb) socket.removeListener('timeout', emitRequestTimeout);
socket.removeListener('timeout', responseOnTimeout);
}

req._ended = true;

if (!req.shouldKeepAlive) {
const socket = req.socket;
if (socket.writable) {
debug('AGENT socket.destroySoon()');
if (typeof socket.destroySoon === 'function')
Expand All @@ -719,6 +721,14 @@ function responseOnEnd() {
}
}

function responseOnTimeout() {
const req = this._httpMessage;
if (!req) return;
const res = req.res;
if (!res) return;
res.emit('timeout');
}

function requestOnPrefinish() {
const req = this;

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-http-client-response-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const common = require('../common');
const http = require('http');

const server = http.createServer((req, res) => res.flushHeaders());

server.listen(common.mustCall(() => {
const req =
http.get({ port: server.address().port }, common.mustCall((res) => {
res.on('timeout', common.mustCall(() => req.destroy()));
res.setTimeout(1);
server.close();
}));
}));
4 changes: 2 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,9 @@ const options = {
server.listen(0, options.host, common.mustCall(() => {
options.port = server.address().port;
doRequest(common.mustCall((numListeners) => {
assert.strictEqual(numListeners, 2);
assert.strictEqual(numListeners, 3);
doRequest(common.mustCall((numListeners) => {
assert.strictEqual(numListeners, 2);
assert.strictEqual(numListeners, 3);
server.close();
agent.destroy();
}));
Expand Down