diff --git a/lib/http/request.js b/lib/http/request.js index 7998cf69..daf05995 100644 --- a/lib/http/request.js +++ b/lib/http/request.js @@ -292,21 +292,20 @@ Request.prototype.cleanup = function cleanup() { this.response.removeListener('data', this.onData); this.response.removeListener('error', this.onEnd); this.response.removeListener('end', this.onEnd); - if (this.response.socket) + if (this.response.socket) { + this.response.socket.removeListener('error', this.onEnd); this.response.socket.removeListener('end', this.onEnd); + } } }; Request.prototype.close = function close() { - this.cleanup(); - if (this.request) { try { this.request.abort(); } catch (e) { ; } - this.request = null; } if (this.response) { @@ -322,8 +321,12 @@ Request.prototype.close = function close() { ; } } - this.response = null; } + + this.cleanup(); + + this.request = null; + this.response = null; }; Request.prototype.destroy = function destroy() { @@ -441,10 +444,12 @@ Request.prototype._onResponse = function _onResponse(response) { this.response.on('error', this.onEnd); this.response.on('end', this.onEnd); - // An agent socket's `end` sometimes - // won't be emitted on the response. - if (this.response.socket) + if (this.response.socket) { + this.response.socket.on('error', this.onEnd); + // An agent socket's `end` sometimes + // won't be emitted on the response. this.response.socket.on('end', this.onEnd); + } }; Request.prototype._onData = function _onData(data) {