NUSSOC/glide

Fatal error when recursion limit is reached

pakshuang opened this issue · 2 comments

When the max call stack size is exceeded, the console can no longer be used.

Here, I ran a function (now commented) that caused the max call stack size to be exceeded. Then, I defined a new function that did not exceed the max call stack size. When I ran the script, it would not run and the page had to be reloaded.

image

Thanks for catching this!

This issue was raised here pyodide/pyodide#951. Basically, seems like the default recursion limit is too high, such that the browser cannot keep up. There was a patch that resulted in that conversation to fatally terminate Pyodide to prevent code execution on potentially invalid state of the VM, hence the last line of the error message in your screenshot.

The recursion error was actually bubbled into JavaScript's RangeError, and contrary to what we see in IDLE, we don't see Python's native RecursionError. We can catch the RangeError in JavaScript and automatically restart a new instance of Pyodide safely.

Another alternative is to manually set the recursion limit, e.g.,

import sys
sys.setrecursionlimit(500)

If the recursion limit is actually reached, then we will see RecursionError. In my computer, any limit beyond 410 will no longer be caught. Some computers can handle up to 500.

I feel like whether or not this recursion limit is set is up for deciding. The correct patch is to actually catch when Pyodide fatally dies and respawn. This way, we don't risk some computers from potentially not reaching some arbitrary recursion limit (if any).

Fixed as of d1eb634. This patch will be available in the next release.

This is what we will see now:

image

Thanks! Could the message also remind the user to save their work first?