Skip to content

Commit 5757642

Browse files
committed
node: allow nextTick infinite recursion
Removing the depth counter while processing the nextTickQueue made it possible to run out of memory if in an infinite recursive loop using nextTick(). There was also an edge case where too many callbacks were pushed onto the nextTickQueue, while not actually being recursive. This is being done to prevent possible cryptic FATAL ERROR messages from popping up, and issues being posted about them.
1 parent 1a7d3e2 commit 5757642

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/node.js

+2
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@
603603
}
604604
if (hasQueue)
605605
_unloadAsyncQueue(tock);
606+
if (1e4 < tickInfo[kIndex])
607+
tickDone();
606608
}
607609

608610
tickDone();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
var complete = 0;
26+
27+
// This will fail with:
28+
// FATAL ERROR: JS Allocation failed - process out of memory
29+
// if the depth counter doesn't clear the nextTickQueue properly.
30+
(function runner() {
31+
if (1e8 > ++complete)
32+
process.nextTick(runner);
33+
}());
34+
35+
setImmediate(function() {
36+
console.log('ok');
37+
});

0 commit comments

Comments
 (0)