You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
3.12 setrecursionlimit is ignored in connection with @functools.cache #112215
changed the title [-]3.12 setrecursionlimit is ignored, probably in connection with @cache[/-][+]3.12 setrecursionlimit is ignored in connection with @cache[/+]on Nov 18, 2023
changed the title [-]3.12 setrecursionlimit is ignored in connection with @cache[/-][+]3.12 setrecursionlimit is ignored in connection with `@functools.cache`[/+]on Nov 18, 2023
The bug is triggered by the use of the C implementation of _lru_cache_wrapper, in Modules/_functoolsmodule.c.
To see this, disable it (then Python code will be used) by applying
For the particular case of cache() and lru_cache(), we may need to provide an easy way to get to the pure python version that works as expected. Perhaps something like from functools import py_lru_cache as lru_cache. Without such an option, users would need to code their own caching decorator to be able to run dynamic programming like that found in the StackOverflow question. That is what the user had to do in #127346 . I'm pretty sure that is not what we want.
I think the solution to this is to implement the wrapper function in Python, but implement the cache management in C.
That will fix the recursion issue, with little or no performance impact.
The wrapper function would look something like this:
Activity
dimpase commentedon Nov 17, 2023
also tested on Linux (with the call to
sys.getwindowsversion()removed).As well, there is no discrepancy with 3.11 vs 3.12 if
@cacheis removed. So the working hypothesis is that@cacheis to blame for the regression.The current master is bad too.
Bisecting currently, last year things were still OK.
In my testing I modified the lines
I removed the 1st one (it's very recent), and only left
dimpase commentedon Nov 17, 2023
I found the 1st bad commit, 7644935, by bisecting:
dimpase commentedon Nov 17, 2023
@markshannon - greetings from Oxford, by the way :-)
sunmy2019 commentedon Nov 18, 2023
Yes. I was suggesting exposing an API for the C recursion limit, but not receiving active feedback.
See the discussion here: #107263
dimpase commentedon Nov 18, 2023
@PeterLuschny - basic Fibonacci
shows the bug just as well
dimpase commentedon Nov 18, 2023
it's not "probably in connection with
@cache", it's definitely[-]3.12 setrecursionlimit is ignored, probably in connection with @cache[/-][+]3.12 setrecursionlimit is ignored in connection with @cache[/+][-]3.12 setrecursionlimit is ignored in connection with @cache[/-][+]3.12 setrecursionlimit is ignored in connection with `@functools.cache`[/+]dimpase commentedon Nov 18, 2023
The bug is triggered by the use of the C implementation of
_lru_cache_wrapper, inModules/_functoolsmodule.c.To see this, disable it (then Python code will be used) by applying
and see
./python <fib.pycomputing the answer, instead of erroring out.In a way, it's to be expected, as C code isn't governed by
setrecursionlimitany more in 3.12+.sunmy2019 commentedon Nov 20, 2023
@arhadthedev This is not related to performance. It's a new hard-coded limit not adjustable.
43 remaining items
Add test_long_kernel
Add test_long_kernel
pythonGH-112215: Increase C recursion limit for non debug builds (pyt…
Eclips4 commentedon Nov 27, 2024
What's the plan for 3.13? It looks like it still doesn't behave as expectedI cannot reproduce error from #127346 on my macos, so it's probably a Windows issue?
gpshead commentedon Nov 27, 2024
beware that C stack size issues are typically local platform & build config dependent as to what numbers you need to use to bump into a given issue.
rhettinger commentedon Jan 12, 2025
Another user reported this as a bug: #127346
Also there is the question on StackOverflow: https://stackoverflow.com/questions/78389885/why-lru-cache-doesnt-work-on-desktops-but-only-works-in-the-online-python-int
For the particular case of
cache()andlru_cache(), we may need to provide an easy way to get to the pure python version that works as expected. Perhaps something likefrom functools import py_lru_cache as lru_cache. Without such an option, users would need to code their own caching decorator to be able to run dynamic programming like that found in the StackOverflow question. That is what the user had to do in #127346 . I'm pretty sure that is not what we want.markshannon commentedon Feb 12, 2025
I think the solution to this is to implement the wrapper function in Python, but implement the cache management in C.
That will fix the recursion issue, with little or no performance impact.
The wrapper function would look something like this:
markshannon commentedon May 6, 2025
This is fixed in 3.14.
encukou commentedon Nov 20, 2025
The fix is backported to 3.13 & 3.12.