1

I am getting "RangeError: Maximum call stack size exceeded" error on running a particular user code in vm.runInNewContext on each iteration of loop (given by the user).

I have checked this issue on StackOverflow (ref. Node.js - Maximum call stack size exceeded) and from there I found that we should wrap function call into setImmediate or process.nextTick. But, my entire project code is promisified.

Can anyone help here for any possible solution for this?

Thanks!

RATTLESNAKE
  • 3,218
  • 2
  • 21
  • 24
  • Do you have any `self-calling-again-functions` aka. `recursive functions`. You know what I mean? – deEr. May 10 '18 at 06:01
  • Share the piece of code – Rahul May 10 '18 at 06:07
  • No recursive functions in this case. An end user can provide loops (ex. 300 times) and custom code (let say, "return 1+1;") which I am executing with vm.runInNewContext on my server side for each iteration and custom code provided by the user. – RATTLESNAKE May 10 '18 at 06:17
  • Please share your code. – Nah May 10 '18 at 07:03
  • Thanks Guys! Was my bad, the input data passed to the user was getting changed by the user due to which somehow it was recursively calling itself and giving the max. call stack trace error. Now, I clone the input data and passed it to user on each iteration which fixed the issue. – RATTLESNAKE May 10 '18 at 07:49

1 Answers1

0

Maximum call stack size exceeded almost always means you have a problem with recursion in JavaScript code, as there isn't any other way in JavaScript to consume lots of stack. You can localize the issue by setting a breakpoint on RangeError type of exception, and then adjust the code appropriately.

I see you've solved the issue already, nevertheless as the answer you've linked to explains, it's possible to cause infinite recursion in a fully promisified code, too. That can happen if the promises in a chain don't actually perform any asynchronous execution, in which case control never really returns to the event loop, even though the code otherwise appears to be asynchronous. That's when it's useful to wrap the calls in setImmediate or process.nextTick.

rustyx
  • 80,671
  • 25
  • 200
  • 267