Skip to content

Commit

Permalink
Fix protect error when handlers timeout with onPreResponse. Closes #1885
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Aug 28, 2014
1 parent 99e4f86 commit 12fa386
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ exports.invoke = function (request, event, callback) {
return Hoek.nextTick(callback)();
}

if (event === 'onPreResponse') {
request._protect.reset();
}

request._protect.run('ext:' + event, callback, function (exit) {

Items.serial(exts.nodes, function (ext, next) {
Expand Down
9 changes: 8 additions & 1 deletion lib/protect.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internals.Protect.prototype.run = function (at, next, enter) { // enter

var self = this;

Hoek.assert(!this._error, 'Invalid nested use of protect.run() during: ' + root._at + ' while trying: ' + at);
Hoek.assert(!this._error, 'Invalid nested use of protect.run() during: ' + this._at + ' while trying: ' + at);

var finish = function (/* arguments */) {

Expand All @@ -55,3 +55,10 @@ internals.Protect.prototype.run = function (at, next, enter) { // enter

enter(finish);
};


internals.Protect.prototype.reset = function () {

this._error = null;
this._at = '';
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hapi",
"description": "HTTP Server framework",
"homepage": "http://hapijs.com",
"version": "6.7.0",
"version": "6.7.1",
"repository": {
"type": "git",
"url": "git://github.com/hapijs/hapi"
Expand Down
21 changes: 21 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,27 @@ describe('Server', function () {
});
});

it('handles server handler timeout with onPreResponse ext', function (done) {

var handler = function (request, reply) {

setTimeout(reply, 20);
};

var serverExt = new Hapi.Server({ timeout: { server: 10 } });
serverExt.route({ method: 'GET', path: '/', config: { handler: handler } });
serverExt.ext('onPreResponse', function (request, next) {

next();
});

serverExt.inject('/', function (res) {

expect(res.statusCode).to.equal(503);
done();
});
});

it('does not return an error response when server is slow but faster than timeout', function (done) {

var server = new Hapi.Server({ timeout: { server: 50 } });
Expand Down

0 comments on commit 12fa386

Please sign in to comment.