jedi icon indicating copy to clipboard operation
jedi copied to clipboard

importing jedi might lower your recursion limit

Open wimglenn opened this issue 2 years ago • 3 comments

>>> sys.setrecursionlimit(5000)
>>> sys.getrecursionlimit()
5000
>>> import jedi
>>> sys.getrecursionlimit()
3000

In https://github.com/davidhalter/jedi/blob/v0.18.2/jedi/api/init.py#L46-L48

# Jedi uses lots and lots of recursion. By setting this a little bit higher, we
# can remove some "maximum recursion depth" errors.
sys.setrecursionlimit(3000)

Perhaps it should check if you're not already higher than 3000, so that you're not lowering an already higher bound which user might have set for a good reason.

Actually, I think it's questionable for a library to mess with the global recursion limit in the first place - maybe the parts of jedi which need to use deep recursion can do so with a context-managed recursion limit, or be rewritten to use an explicit stack.

wimglenn avatar Apr 10 '23 02:04 wimglenn

Actually, I think it's questionable for a library to mess with the global recursion limit in the first place - maybe the parts of jedi which need to use deep recursion can do so with a context-managed recursion limit, or be rewritten to use an explicit stack.

Completely agree. This should be a decorator. We discussed this here already https://github.com/dask/dask/pull/9805 and to a lesser degree here: https://github.com/davidhalter/jedi/issues/1902.

Feel free to send a pull request that sets the recusion limit in specific places and resets it afterwards.

davidhalter avatar Apr 10 '23 10:04 davidhalter

See https://github.com/sagemath/sage/issues/35803 for an (admittedly dubious) example of this happening in the wild.

mezzarobba avatar Jul 02 '23 14:07 mezzarobba

There's another side to this: 3000 may be too high in some scenarios, and may cause CPython to crash instead of throwing an exception. I've seen it multiple times now, when jedi was imported implicitly via some other package and then caused e.g. django tests to crash.

mgorny avatar Dec 04 '23 15:12 mgorny