diff --git a/request.js b/request.js index 0162f4455..198b76093 100644 --- a/request.js +++ b/request.js @@ -828,8 +828,7 @@ Request.prototype.start = function () { if (isConnecting) { var onReqSockConnect = function () { socket.removeListener('connect', onReqSockConnect) - clearTimeout(self.timeoutTimer) - self.timeoutTimer = null + self.clearTimeout() setReqTimeout() } @@ -874,10 +873,7 @@ Request.prototype.onRequestError = function (error) { self.req.end() return } - if (self.timeout && self.timeoutTimer) { - clearTimeout(self.timeoutTimer) - self.timeoutTimer = null - } + self.clearTimeout() self.emit('error', error) } @@ -964,10 +960,7 @@ Request.prototype.onRequestResponse = function (response) { if (self.setHost) { self.removeHeader('host') } - if (self.timeout && self.timeoutTimer) { - clearTimeout(self.timeoutTimer) - self.timeoutTimer = null - } + self.clearTimeout() var targetCookieJar = (self._jar && self._jar.setCookie) ? self._jar : globalCookieJar var addCookie = function (cookie) { @@ -1172,6 +1165,7 @@ Request.prototype.abort = function () { self.response.destroy() } + self.clearTimeout() self.emit('abort') } @@ -1532,6 +1526,7 @@ Request.prototype.resume = function () { } Request.prototype.destroy = function () { var self = this + this.clearTimeout() if (!self._ended) { self.end() } else if (self.response) { @@ -1539,6 +1534,13 @@ Request.prototype.destroy = function () { } } +Request.prototype.clearTimeout = function () { + if (this.timeoutTimer) { + clearTimeout(this.timeoutTimer) + this.timeoutTimer = null + } +} + Request.defaultProxyHeaderWhiteList = Tunnel.defaultProxyHeaderWhiteList.slice() diff --git a/tests/test-timeout.js b/tests/test-timeout.js index 2ab0639c4..c87775d3c 100644 --- a/tests/test-timeout.js +++ b/tests/test-timeout.js @@ -244,6 +244,15 @@ tape('request timeout with keep-alive connection', function (t) { }) }) +tape('calling abort clears the timeout', function (t) { + const req = request({ url: s.url + '/timeout', timeout: 2500 }) + setTimeout(function () { + req.abort() + t.equal(req.timeoutTimer, null) + t.end() + }, 5) +}) + tape('cleanup', function (t) { s.close(function () { t.end()