3

Does NodeJS offer the ability to view or set any limits (at run time or via CLI flags?) for the size of the call stack?

If I create a program that looks like this an run it

const recurse = (num=0) => {
  console.log('level ' + num);
  recurse(++num)
}
recurse()

Node will eventually bail out of the program with this error

//...
level 12021
_stream_readable.js:872
Readable.prototype.removeListener = function(ev, fn) {
                                            ^

RangeError: Maximum call stack size exceeded
    at WriteStream.Readable.removeListener (_stream_readable.js:872:45)
    at write (console.js:172:12)
    at Console.log (console.js:200:3)
    at recurse (/path/to/so.js:2:11)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
    at recurse (/path/to/so.js:3:3)
C02V30M6HTDG:test-express-app astorm$ cat so.js 
const recurse = (num=0) => {
  console.log('level ' + num);
  recurse(++num)
}
recurse()

The level/depth at which it bails out varies (usually in the 10,000 - 13,000 range). This error message tells me the call stack size (not the depth) is the problem, but it's unclear what size is here (I presume some internal to node data structure size).

What I'd like to know is -- does node allow me to

  1. View/Set the size at which this call stack protection kicks in
  2. View/set a depth limit for the call stack.

I don't have any particular problem in mind to solve, I'm just trying to understand how much control NodeJS gives me vs. other languages I've worked with.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • 2
    You should treat a call stack size exceeded error as a fatal error. There's no reliable way to recover from one. Encountering one indicates your code has a bug that needs to be fixed, not a scenario that needs to be worked around with a `try...catch` (or by adjusting command-line parameters) – Patrick Roberts Apr 22 '20 at 17:07
  • 1
    Thank you @PatrickRoberts, that's useful information and good advice. However, the information I'm looking for today is knowing whether or not NodeJS has any knobs that allow you to control this. I've used similar features in other languages and they're useful when tracking down bugs, and it's useful to set a very _low_ callstack size to limit possible recursion when developing. – Alana Storm Apr 22 '20 at 17:10
  • 3
    Do these answer your questions? https://stackoverflow.com/q/20748061/1541563 https://stackoverflow.com/q/11332422/1541563 – Patrick Roberts Apr 22 '20 at 17:13
  • Also, remember that local variables and calling arguments are not on the stack like they might be in C++ because they are garbage collected as they can exist after the function returns due to closures (e.g. asynchronous callbacks inside the function that can still reference local variables after the function has returned) so the callstack is likely only the return execution flow. – jfriend00 Apr 22 '20 at 17:23
  • Thank you @PatrickRoberts those previous questions answered some of my questions, and have allowed me to create a more focused refined questions elsewhere. +1 – Alana Storm Apr 22 '20 at 20:12

0 Answers0