oracle / graalpython

A Python 3 implementation built on GraalVM
Other
1.17k stars 101 forks source link

Set the python recursion limit from java. #362

Closed andrewstein closed 7 months ago

andrewstein commented 9 months ago

I have created a python context inside Java using Context context = Context.create("python"); and using that context to run some python code. The code errors out with "RecursionError: maximum recursion depth exceeded".

Similar code seems to run just fine from the graalpy command line.

On the command line I have:

$ graalpy
Python 3.10.8 (Fri Jul 21 01:52:21 PDT 2023)
[Graal, GraalVM CE, Java 17.0.8] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.getrecursionlimit())
8000

Going back to the java Context with this code I get a value of 1000.

If I explicitly call sys.setrecursionlimit(20000) in the Java Context, then sys.getrecursionlimit() will return 20000, but it seems that this does not effect the original code that I was trying to run.

I could not find documentation on how to set the recursion limit when creating the Context.

Bottom line: either allow sys.setrecursionlimit to have effect when called from a Java Context, or allow one to set the recursion limit when creating the context.

msimacek commented 9 months ago

Hi @andrewstein, unfortunately, we don't support setrecursionlimit, it has no effect. The way to change the maximum recursion depth is to set the stack size for the underlying JVM/SubstrateVM. You can do it with --vm.Xss8M (sets the stack size to 8 MB).

If you're hitting the RecursionError in code that is not deeply recursive, you're probably hitting a bug that should be resolved in GraalPy 23.1, so I would strongly recommend upgrading.

kai-zhu-sonatype commented 9 months ago

Thanks @msimacek set JVM option -Xss8M on 21-graalce and 17.0.8-graalce both worked

msimacek commented 7 months ago

Closing. You can set stack size via the JVM options as mentioned. It's not possible for us to support sys.setrecursionlimit on the JVM.