python / python/cpython

sys.setrecursionlimit no longer affecting pickle.dump

Open
#128,590 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.12 3.13 3.14 extension-modules type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

This is a follow up of https://github.com/python/cpython/issues/112282 (updating doc).
It's also a different case of https://github.com/python/cpython/issues/128544 (import chains impacted).

In particular, on top of the doc no longer being correct for sys.setrecursionlimit, the regression needs to be fixed.

Note: this works in python 3.11 or below.

import pickle
import sys

nested_object = current = {}
for i in range(10000):
    current['nested'] = {}
    current = current['nested']

sys.setrecursionlimit(3750000)
x = pickle.dumps(nested_object)

Result of the run:

▶▶ python3.11 test-issue.py
# runs ok

▶▶ python3.12 test-issue.py
Traceback (most recent call last):
  File "~/test-issue.py", line 10, in <module>
    x = pickle.dumps(nested_object)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded while pickling an object

Impact: all programs trying to pickle data that is too nested cannot be ported to python 3.12 or 3.13 so far

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Linux, macOS, Windows

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided nested-dictionary reproducer under Python 3.11 and 3.12, then trace how pickle.dumps handles recursion after sys.setrecursionlimit is raised. The fix is complete when the example succeeds on the affected versions without the RecursionError, while preserving the documented recursion-limit behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.