Skip to content

Correctly invoke async router callback asynchronously #4891

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

Closed
wants to merge 3 commits into from
Closed
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
12 changes: 11 additions & 1 deletion lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ proto.handle = function handle(req, res, out) {
var protohost = getProtohost(req.url) || ''
var removed = '';
var slashAdded = false;
var sync = 0;
var paramcalled = {};

// store options for OPTIONS request
Expand Down Expand Up @@ -203,6 +204,12 @@ proto.handle = function handle(req, res, out) {
return;
}

// max sync stack
if (++sync > 100) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without looking at the current v8 stack frames, there is no way to know if you still have 100 frames left to use. Also params will add/remove to how many frames are used in each iteration, I wouldn't think. How did you come up with the number 100 for this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setImmediate(next, err);
return;
}

// get pathname of request
var path = getPathname(req);

Expand Down Expand Up @@ -256,7 +263,8 @@ proto.handle = function handle(req, res, out) {

// no match
if (match !== true) {
return done(layerError);
setImmediate(done, layerError);
return;
}

// store route for dispatch on change
Expand Down Expand Up @@ -321,6 +329,8 @@ proto.handle = function handle(req, res, out) {
} else {
layer.handle_request(req, res, next);
}

sync = 0;
}
};

Expand Down
10 changes: 10 additions & 0 deletions lib/router/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Route.prototype._options = function _options() {
Route.prototype.dispatch = function dispatch(req, res, done) {
var idx = 0;
var stack = this.stack;
var sync = 0;

if (stack.length === 0) {
return done();
}
Expand Down Expand Up @@ -127,6 +129,12 @@ Route.prototype.dispatch = function dispatch(req, res, done) {
return done(err);
}

// max sync stack
if (++sync > 100) {
setImmediate(next, err);
return;
}

if (layer.method && layer.method !== method) {
return next(err);
}
Expand All @@ -136,6 +144,8 @@ Route.prototype.dispatch = function dispatch(req, res, done) {
} else {
layer.handle_request(req, res, next);
}

sync = 0;
}
};

Expand Down
11 changes: 11 additions & 0 deletions test/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ describe('Route', function(){
done();
});
})

it('should not stack overflow with a large sync stack', function (done) {
var req = { method: 'GET', url: '/' };
var route = new Route('');

for (var i = 0; i < 6000; i++) {
route.all(function (req, res, next) { next() });
}

route.dispatch(req, {}, done);
})
})

describe('.VERB', function(){
Expand Down
24 changes: 24 additions & 0 deletions test/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ describe('Router', function(){
router.handle({ url: '/', method: 'GET' }, { end: done });
});

it('should not stack overflow with many registered sub routes', function(done){
var handler = function(req, res, next){ next() };
var router = new Router();

var subRouter;
for (var i = 0; i < 6000; i++) {
subRouter = new Router();
subRouter.get('/thing' + i, handler)
router.use(subRouter);
}

router.handle({ url: '/', method: 'GET' }, {}, done);
});

it('should not stack overflow with a large sync stack', function (done) {
var router = new Router();

for (var i = 0; i < 6000; i++) {
router.use(function (req, res, next) { next() });
}

router.handle({ url: '/', method: 'GET' }, {}, done);
});

describe('.handle', function(){
it('should dispatch', function(done){
var router = new Router();
Expand Down