Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Server shutdown with "Error: Can't set headers after they are sent." (close #937) #940

Merged
merged 1 commit into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/request-pipeline/destination-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ export default class DestinationRequest extends EventEmitter {
this._send(requiresResBody(res));
}

_abort () {
this.aborted = true;
this.req.abort();
_fatalError (msg) {
if (!this.aborted) {
this.aborted = true;
this.req.abort();
this.emit('fatalError', getText(msg, this.opts.url));
}
}

_isDNSErr (err) {
Expand All @@ -100,10 +103,8 @@ export default class DestinationRequest extends EventEmitter {
_onTimeout () {
// NOTE: this handler is also called if we get an error response (for example, 404). So, we should check
// for the response presence before raising the timeout error.
if (!this.hasResponse) {
this._abort();
this.emit('fatalError', getText(MESSAGE.destRequestTimeout, this.opts.url));
}
if (!this.hasResponse)
this._fatalError(MESSAGE.destRequestTimeout);
}

_onError (err) {
Expand All @@ -113,7 +114,8 @@ export default class DestinationRequest extends EventEmitter {
}

else if (this._isDNSErr(err))
this.emit('fatalError', getText(MESSAGE.cantResolveUrl, this.opts.url));
this._fatalError(MESSAGE.cantResolveUrl);

else
this.emit('error');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ test('document.baseURI (GH-920)', function () {
}
};

documentMock.toString.toString = function () {
return 'function test() { [native code] }';
};

strictEqual(getProperty(documentMock, 'baseURI'), url);
});

Expand Down
42 changes: 42 additions & 0 deletions test/server/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down Expand Up @@ -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);
});
});
});