Skip to content

sys.setrecursionlimit docs are incorrect in 3.12 and 3.13 #112282

Closed as duplicate

Description

@dimpase

Since 3.12, sys.setrecursionlimit does not control the recursion limit of the C stack (it's hardcoded in Py_C_RECURSION_LIMIT, due to #91079), while the 3.13 docs say

Set the maximum depth of the Python interpreter stack to limit. 
This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.

The fallout can be seen in #112215 (basically, any code that uses lru_cache in combination with sys.setrecursionlimit is potentially, or actually, broken in 3.12, while still working in 3.11) or in #107263 (which was papered over with an increase of Py_C_RECURSION_LIMIT, not really fixed).

This is an obvious change of behaviour, as code working in 3.11 can easily be broken in 3.12, as found in #112215

import sys
sys.setrecursionlimit(1001)
from functools import cache
@cache
def fib(n):
    if n<1: return 0
    if n==1: return 1
    return fib(n-1) + fib(n-2)

print(fib(500))
$ python3.11 <fib.py 
139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125
$ python3.12 <fib.py 
Traceback (most recent call last):
  File "<stdin>", line 15, in <module>
  File "<stdin>", line 13, in fib
  File "<stdin>", line 13, in fib
  File "<stdin>", line 13, in fib
  [Previous line repeated 496 more times]
RecursionError: maximum recursion depth exceeded

Such changes should carry a warning in the documentation.

Potentially, any recursive Python code combining C recursion (in a module, e.g. lru_cache, via @lru_cache decorator) with increased (beyond the hardcoded Py_C_RECURSION_LIMIT) sys.setrecursionlimit is bound to be broken in 3.12.

I believe the whole hardcoding of Py_C_RECURSION_LIMIT has to be fixed (perhaps with a separate API provided to change it), as it's a regression and an incompatible change of code behaviour, but meanwhile the docs have to reflect the reality.

Activity

  1. Jiehong commented on Oct 9, 2024

    @Jiehong

    I confirm this breaks some code out there.

    In particular, we are unable to move from python 3.11 to anything higher, since we can no longer pickle.dump our objects, even if sys.setrecursionlimit is called beforehand for example.

  2. seddonym commented on Jan 7, 2025

    @seddonym

    +1 to updating the docs. The text from What's New in Python 3.12 might be a good starting point.

    I think it would also be worth documenting that the recursion limit can also be triggered by long import chains, as described in this issue. Do people agree, and if so does it make sense to fold that issue into this one?

  3. B1oomEe commented on Jan 31, 2025

    @B1oomEe

    same issue

  4. LCcodder commented on Jan 31, 2025

    @LCcodder

    I have same issue with my 3.12 python

    import sys
    
    sys.setrecursionlimit(1000000)
    from functools import cache
    
    
    @cache
    def f(n):
        if n >= 2025:
            return 1
        return n - f(n + 2) - f(n + 4)
    
    
    print(f(20) + f(25))

    [Previous line repeated 496 more times] RecursionError: maximum recursion depth exceeded

    Tried with python 3.11 and 3.8 and everything works

  5. added 2 commits that reference this issue on Apr 28, 2025
  6. encukou commented on Nov 20, 2025

    @encukou
    Member

    This is a duplicate of #112215.

  7. kbulygin commented on Oct 1, 2026

    @kbulygin

    Is it really a duplicate? #112215 is not about docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docsDocumentation in the Doc dir

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions