0

Is there a way to limit the depth of recursion in NodeJS other than the --stack-size option?

I know that NodeJS will let you control the size of you call stack via the --stack-size option. i.e. a program like this

const recurse = (num=0) => {
  context.num++
  recurse(++num)
}
const context = {
  num:0
}
try {
  recurse(context)
} catch(e) {
  console.log('Took ' + context.num + ' trips before bailing')
}

will output something like this by default.

$ node so.js 
Took 12530 trips before bailing

However, you can use --stack-size to tell node how big you want to let the stack get (in kb) before bailing out. Use a bigger stack, you get more recursion.

$ node --stack-size=5000 so.js 
Took 63935 trips before bailing

The --stack-size option is one of Node's V8 options. You can see a list of all these options by running node --v8-options.

What I want to know is whether there's a way in NodeJS to explicitly limit the recursion depth by some specific number.

I don't have a specific end goal in mind here -- I'm just trying to understand what tools NodeJS does and does not have compared to other languages I've works in. It's OK if node doesn't have this -- I'm just learning here :)

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • 4
    For reference, a previous question from the same person on this topic: [Nodjs and CallStack Size Protection](https://stackoverflow.com/questions/61370901/nodejs-and-callstack-size-protection). – jfriend00 Apr 22 '20 at 20:14
  • 1
    Theoretically, you can figure out the sizes of all the stack frames and multiply by how many you want to allow, but all kinds of [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) alarms should be firing. I know you claim you have no goals, but the question still remains: why? – ggorlen Apr 22 '20 at 22:03
  • @ggorlen Good question. I work on development tools and I generally and I like to understand what my runtime is doing. It's definitely not what 95% of people need. These specific series of questions came about because we discovered some cases where we had an unintentional infinite recursion bug, but --stack-size didn't save us. I've found having a feature that says "hey, if this is stack is 1000 calls deep we should probably just crash because something is seriously wrong" to be useful. – Alana Storm Apr 22 '20 at 23:53
  • @ggorlen happy to continue this conversation elsewhere, but when you say "you shouldn't be eating up this much stack memory" that doesn't account for recursive scenarios where the stack doesn't grow. When execution returns to a function after an await, the V8 callstack doesn't contain the previous recursive calls. i.e. the callstack isn't growing, but the recursion will continue. This recursion can cause other problems, and is non-obvious. A theoretical limit on userland recursion depth would solve the problem nicely. – Alana Storm Apr 23 '20 at 14:51
  • Sure, and it looks like you can't just divide by the call size then. I don't have an answer and am probably just taking up space--good luck working this out! – ggorlen Apr 23 '20 at 15:02

1 Answers1

1

Based on the research I've done, this doesn't seem possible. This just opened feature request in the NodeJS repository will probably help get to the bottom of things.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599