http: emit timeout duration overflow warning sync

Emit the `TimeoutOverflowWarning` synchronously, even when
still connecting, to get a better stack trace.

With thanks to Anatoli Papirovski for providing helpful
tips when writing the test.

PR-URL: https://github.com/nodejs/node/pull/18906
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anna Henningsen 2018-02-21 17:52:35 +01:00
parent d83f8303fd
commit 648d668fcc
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 23 additions and 0 deletions

View File

@ -40,6 +40,7 @@ const { urlToOptions, searchParamsSymbol } = require('internal/url');
const { outHeadersKey, ondrain } = require('internal/http');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
const { validateTimerDuration } = require('internal/timers');
const INVALID_PATH_REGEX = /[^\u0021-\u00ff]/;
@ -678,6 +679,7 @@ function _deferToConnect(method, arguments_, cb) {
}
ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
msecs = validateTimerDuration(msecs);
if (callback) this.once('timeout', callback);
const emitTimeout = () => this.emit('timeout');

View File

@ -0,0 +1,21 @@
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
// Checks that the setTimeout duration overflow warning is emitted
// synchronously and therefore contains a meaningful stacktrace.
process.on('warning', common.mustCall((warning) => {
assert(warning.stack.includes(__filename));
}));
const server = http.createServer((req, resp) => resp.end());
server.listen(common.mustCall(() => {
http.request(`http://localhost:${server.address().port}`)
.setTimeout(2 ** 40)
.on('response', common.mustCall(() => {
server.close();
}))
.end();
}));