Skip to content

Commit 631ada0

Browse files
committedApr 29, 2022
Fix hanging on large stack of sync routes
fixes #4899
1 parent 75e0c7a commit 631ada0

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed
 

‎History.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased
2+
==========
3+
4+
* Fix hanging on large stack of sync routes
5+
16
4.18.0 / 2022-04-25
27
===================
38

‎lib/router/index.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,14 @@ proto.handle = function handle(req, res, out) {
279279
// this should be done for the layer
280280
self.process_params(layer, paramcalled, req, res, function (err) {
281281
if (err) {
282-
return next(layerError || err);
282+
next(layerError || err)
283+
} else if (route) {
284+
layer.handle_request(req, res, next)
285+
} else {
286+
trim_prefix(layer, layerError, layerPath, path)
283287
}
284288

285-
if (route) {
286-
return layer.handle_request(req, res, next);
287-
}
288-
289-
trim_prefix(layer, layerError, layerPath, path);
289+
sync = 0
290290
});
291291
}
292292

@@ -327,8 +327,6 @@ proto.handle = function handle(req, res, out) {
327327
} else {
328328
layer.handle_request(req, res, next);
329329
}
330-
331-
sync = 0
332330
}
333331
};
334332

‎test/Router.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,23 @@ describe('Router', function(){
7878
router.handle({ url: '/', method: 'GET' }, { end: done });
7979
});
8080

81-
it('should not stack overflow with a large sync stack', function (done) {
81+
it('should not stack overflow with a large sync route stack', function (done) {
82+
this.timeout(5000) // long-running test
83+
84+
var router = new Router()
85+
86+
for (var i = 0; i < 6000; i++) {
87+
router.get('/foo', function (req, res, next) { next() })
88+
}
89+
90+
router.get('/foo', function (req, res) {
91+
res.end()
92+
})
93+
94+
router.handle({ url: '/foo', method: 'GET' }, { end: done })
95+
})
96+
97+
it('should not stack overflow with a large sync middleware stack', function (done) {
8298
this.timeout(5000) // long-running test
8399

84100
var router = new Router()

0 commit comments

Comments
 (0)
Please sign in to comment.