Skip to content

Commit

Permalink
http: make ClientRequest#setTimeout() noop at end
Browse files Browse the repository at this point in the history
Originally discovered and resolved by @szmarczak.

PR-URL: #25536
Fixes: #25499
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
timdp authored and addaleax committed Jan 23, 2019
1 parent 65478fa commit d6b50c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,10 @@ function _deferToConnect(method, arguments_, cb) {
}

ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
if (this._ended) {
return this;
}

listenSocketTimeout(this);
msecs = validateTimerDuration(msecs);
if (callback) this.once('timeout', callback);
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-http-client-set-timeout-after-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

// Test https://github.com/nodejs/node/issues/25499 fix.

const { mustCall } = require('../common');

const { Agent, createServer, get } = require('http');
const { strictEqual } = require('assert');

const server = createServer(mustCall((req, res) => {
res.end();
}));

server.listen(0, () => {
const agent = new Agent({ keepAlive: true, maxSockets: 1 });
const port = server.address().port;

let socket;

const req = get({ agent, port }, (res) => {
res.on('end', () => {
strictEqual(req.setTimeout(0), req);
strictEqual(socket.listenerCount('timeout'), 0);
agent.destroy();
server.close();
});
res.resume();
});

req.on('socket', (sock) => {
socket = sock;
});
});

0 comments on commit d6b50c6

Please sign in to comment.