python / python/cpython

Reference leak in code object with reference loop

Open
#148,722 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.13 3.14 3.15 interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

If the code object creates a reference loop, this causes a reference leak. For example (see :

def f():
    return 1234.5

code = f.__code__
a = []
code = code.replace(co_consts=code.co_consts + (a,))
a.append(code)

(See https://github.com/python/cpython/blob/d61fcf834d197f0113a6a507fdbecc1545d9d483/Lib/test/test_marshal.py#L354-L360 added in #148698)

This is because in the GIL-enabled build the code object does not support GC and does not have tp_traverse.

Linked PRs
  • gh-151155

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

Start with the reproducer and the referenced regression test in Lib/test/test_marshal.py around lines 354-360. Inspect how GIL-enabled code objects handle garbage collection and traversal, focusing on the missing support described in the issue. Done means the reference loop no longer leaks and the marshal test continues to pass.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.