Reference leak in code object with reference loop
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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