jurassic icon indicating copy to clipboard operation
jurassic copied to clipboard

StackOverflowException with large scripts due to recursion

Open cabal95 opened this issue 5 years ago • 1 comments

Two things brought me to this error.

First, I discovered that when running in an IIS process, the default stack size is 512KB instead of 4MB. This isn't an issue for you to solve, but an annoying fact for people to be aware of.

Second, what I am using Jurassic for is to transpile TypeScript files into JavaScript by using the typescript.js file used by node. This does work, but I discovered that this causes a TON of recursive method calls. I was crashing at around 45 or so recursion calls. Who knows how many actual recursion calls it takes to actually do this. I worked around this by running the Jurassic engine in a background thread and overriding the thread stack size with the Thread constructor. 1MB still failed, 2MB worked.

So the actual issue, is that when you hit a true StackOverflowException, you cannot catch that and it will blow up the program. There is currently a manual check for a maximum number of recursion depth allowed, if set by the user. However, I think this should be augmented with an automatic check to ensure there is enough stack space.

System.Runtime.CompilerServices.RuntimeHelpers.EnsureSufficientExecutionStack will verify that there is enough stack space for. If there is not, an exception is thrown that is catchable by the user-code.

As to performance hit, I did a test by calling this method 10,000 times inside a loop and timed with Stopwatch. It completed in less than 1ms, so I don't believe anybody will notice this addition but can drastically save people large amounts of time trying to figure out why their app is crashing without warning and try/catch handlers are not working.

cabal95 avatar Apr 02 '19 17:04 cabal95

The problem still doesn't seem to be fixed. If an overflow exception occurs in Jurassic, there is no way for the programmer to catch it. I think this problem is very serious...

NeuroWhAI avatar Oct 08 '19 09:10 NeuroWhAI