From 9202cba05ac4c4debca1125d7615d57a5e70fb0a Mon Sep 17 00:00:00 2001 From: LavrovArtem Date: Tue, 15 Nov 2016 12:56:46 +0300 Subject: [PATCH] fix `Server shutdown with "Error: Can't set headers after they are sent." ` (close #937) --- .../destination-request/index.js | 5 ++- .../code-instrumentation/getters-test.js | 4 ++ test/server/proxy-test.js | 42 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/request-pipeline/destination-request/index.js b/src/request-pipeline/destination-request/index.js index 8e13d1efb5..4ade4cd554 100644 --- a/src/request-pipeline/destination-request/index.js +++ b/src/request-pipeline/destination-request/index.js @@ -112,8 +112,11 @@ export default class DestinationRequest extends EventEmitter { this._send(); } - else if (this._isDNSErr(err)) + else if (this._isDNSErr(err)) { + this._abort(); this.emit('fatalError', getText(MESSAGE.cantResolveUrl, this.opts.url)); + } + else this.emit('error'); } diff --git a/test/client/fixtures/sandbox/code-instrumentation/getters-test.js b/test/client/fixtures/sandbox/code-instrumentation/getters-test.js index dd3ef88bf3..9b3aa38388 100644 --- a/test/client/fixtures/sandbox/code-instrumentation/getters-test.js +++ b/test/client/fixtures/sandbox/code-instrumentation/getters-test.js @@ -169,6 +169,10 @@ test('document.baseURI (GH-920)', function () { } }; + documentMock.toString.toString = function () { + return 'function test() { [native code] }'; + }; + strictEqual(getProperty(documentMock, 'baseURI'), url); }); diff --git a/test/server/proxy-test.js b/test/server/proxy-test.js index 307843371d..a5f845810b 100644 --- a/test/server/proxy-test.js +++ b/test/server/proxy-test.js @@ -229,6 +229,12 @@ describe('Proxy', function () { res.end(); }); + app.get('/wait/:ms', function (req, res) { + setTimeout(function () { + res.end('text'); + }, req.params.ms); + }); + destServer = app.listen(2000); @@ -1415,5 +1421,41 @@ describe('Proxy', function () { }); }); + it('Should abort destination request after fatal error (GH-937)', function (done) { + var savedReqTimeout = DestinationRequest.TIMEOUT; + var fatalErrorEventCount = 0; + + DestinationRequest.TIMEOUT = 100; + + var destReq = new DestinationRequest({ + url: 'http://127.0.0.1:2000/wait/150', + protocol: 'http:', + hostname: '127.0.0.1', + host: '127.0.0.1:2000', + port: 2000, + path: '/wait/150', + method: 'GET', + body: new Buffer([]), + isXhr: false, + headers: [] + }); + + destReq.on('error', function () { + }); + destReq.on('fatalError', function () { + fatalErrorEventCount++; + }); + + setTimeout(function () { + destReq._onError({ message: 'ECONNREFUSED' }); + }, 50); + + setTimeout(function () { + DestinationRequest.TIMEOUT = savedReqTimeout; + + expect(fatalErrorEventCount).eql(1); + done(); + }, 150); + }); }); });