Skip to content

Commit 199c8aa

Browse files
authored
Adding parseInt to config.timeout (#3781)
* Adding parseInt to config.timeout * Fixing test message
1 parent 94fc4ea commit 199c8aa

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

lib/adapters/http.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,29 @@ module.exports = function httpAdapter(config) {
279279

280280
// Handle request timeout
281281
if (config.timeout) {
282+
// This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
283+
var timeout = parseInt(config.timeout, 10);
284+
285+
if (isNaN(timeout)) {
286+
reject(createError(
287+
'error trying to parse `config.timeout` to int',
288+
config,
289+
'ERR_PARSE_TIMEOUT',
290+
req
291+
));
292+
293+
return;
294+
}
295+
282296
// Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
283297
// And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
284298
// At this time, if we have a large number of request, nodejs will hang up some socket on background. and the number will up and up.
285299
// And then these socket which be hang up will devoring CPU little by little.
286300
// ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
287-
req.setTimeout(config.timeout, function handleRequestTimeout() {
301+
req.setTimeout(timeout, function handleRequestTimeout() {
288302
req.abort();
289303
reject(createError(
290-
'timeout of ' + config.timeout + 'ms exceeded',
304+
'timeout of ' + timeout + 'ms exceeded',
291305
config,
292306
config.transitional && config.transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
293307
req

test/unit/adapters/http.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,64 @@ describe('supports http with nodejs', function () {
2929
}
3030
});
3131

32+
it('should throw an error if the timeout property is not parsable as a number', function (done) {
33+
34+
server = http.createServer(function (req, res) {
35+
setTimeout(function () {
36+
res.end();
37+
}, 1000);
38+
}).listen(4444, function () {
39+
var success = false, failure = false;
40+
var error;
41+
42+
axios.get('http://localhost:4444/', {
43+
timeout: { strangeTimeout: 250 }
44+
}).then(function (res) {
45+
success = true;
46+
}).catch(function (err) {
47+
error = err;
48+
failure = true;
49+
});
50+
51+
setTimeout(function () {
52+
assert.equal(success, false, 'request should not succeed');
53+
assert.equal(failure, true, 'request should fail');
54+
assert.equal(error.code, 'ERR_PARSE_TIMEOUT');
55+
assert.equal(error.message, 'error trying to parse `config.timeout` to int');
56+
done();
57+
}, 300);
58+
});
59+
});
60+
61+
it('should parse the timeout property', function (done) {
62+
63+
server = http.createServer(function (req, res) {
64+
setTimeout(function () {
65+
res.end();
66+
}, 1000);
67+
}).listen(4444, function () {
68+
var success = false, failure = false;
69+
var error;
70+
71+
axios.get('http://localhost:4444/', {
72+
timeout: '250'
73+
}).then(function (res) {
74+
success = true;
75+
}).catch(function (err) {
76+
error = err;
77+
failure = true;
78+
});
79+
80+
setTimeout(function () {
81+
assert.equal(success, false, 'request should not succeed');
82+
assert.equal(failure, true, 'request should fail');
83+
assert.equal(error.code, 'ECONNABORTED');
84+
assert.equal(error.message, 'timeout of 250ms exceeded');
85+
done();
86+
}, 300);
87+
});
88+
});
89+
3290
it('should respect the timeout property', function (done) {
3391

3492
server = http.createServer(function (req, res) {

0 commit comments

Comments
 (0)