GeezerVison is showing github.com — LeakSanitizer reports leaked code object when running Cython · Issue #158963 · python/cpython
LeakSanitizer reports leaked code object when running Cython #158963Labels interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DescriptionBug report
Cython reproducer for CPython 3.14, 3.15, and 3.16 (main).
Install Cython (pip install Cython) and save this as bug.py:
import tempfile
from pathlib import Path
from Cython.Compiler.Main import CompilationOptions, compile_single
with tempfile.TemporaryDirectory() as d:
src = Path(d) / "mre.py"
src.write_text("from cython.cimports.cpython import PyTuple_New\n")
compile_single(
str(src),
CompilationOptions(output_file=f"{d}/mre.c", language_level="3"),
"mre",
)
Run:
./python -X showrefcount bug.py
Output excerpt from 3.16 (main):
[92387 refs, 0 blocks]
ERROR: LeakSanitizer: detected memory leaks
Direct leak of 264 byte(s) in 1 object(s) allocated from:
#0 malloc
#1 _PyMem_DebugRawAlloc Objects/obmalloc.c:3103:24
#2 _PyObject_NewVar Objects/object.c:570:26
#3 _PyCode_New Objects/codeobject.c:734:10
#4 r_object Python/marshal.c:1734:29
...
SUMMARY: AddressSanitizer: 703 byte(s) leaked in 6 allocation(s).
Expected: the process exits without a LeakSanitizer error.
Build configuration:
CC=clang CXX=clang++ LDFLAGS='-fuse-ld=lld' ./configure --with-tail-call-interp --enable-experimental-jit=yes --with-pydebug --with-address-sanitizer --with-undefined-behavior-sanitizer
./python -VV:
Python 3.16.0a0 (heads/main:37cc8dd0b9a, Oct 7 2026, 14:51:35) [Clang 23.1.1 ]
CPython versions tested on:
3.14, 3.15, 3.16 (CPython main branch)
Operating systems tested on:
Linux Activitypicnixz commented on Oct 7, 2026 Honestly I think it's too hard to investigate this from scratch. Can you first trim down the analysis and see if you can at least bisect the bad commit? ZeroIntensity commented on Oct 7, 2026 What does running with -X showrefcount output? I've seen very similar leak traces in the past (with valgrind), but I can't remember if they were just false positives.
If it is a real leak (meaning -X showrefcount displays something other than [0 refs, 0 blocks]), we'll also need a pure-CPython repro; SQLAlchemy (or one of its dependencies) might simply have a memory leak. ashm-dev commented on Oct 7, 2026 ContributorAuthor More actions Here is a trimmed-down reproducer without SQLAlchemy, setuptools, or C compilation. It only requires pip install Cython:
# bug.py
import tempfile
from pathlib import Path
from Cython.Compiler.Main import CompilationOptions, compile_single
with tempfile.TemporaryDirectory() as d:
src = Path(d) / "mre.py"
src.write_text("from cython.cimports.cpython import PyTuple_New\n")
compile_single(
str(src),
CompilationOptions(output_file=f"{d}/mre.c", language_level="3"),
"mre",
)
Running ./python -X showrefcount bug.py outputs:
[92387 refs, 0 blocks]
==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 264 byte(s) in 1 object(s) allocated from:
#0 malloc
#1 _PyMem_DebugRawAlloc Objects/obmalloc.c:3103:24
#2 _PyObject_NewVar Objects/object.c:570:26
#3 _PyCode_New Objects/codeobject.c:734:10
#4 r_object Python/marshal.c:1734:29
...
SUMMARY: AddressSanitizer: 703 byte(s) leaked in 6 allocation(s).
Note: Cython here is running as pure Python bytecode on 3.16 (no compiled .so C extensions of Cython are loaded), so the leaked PyCodeObject is retained inside CPython's own runtime/import path. ZeroIntensity commented on Oct 7, 2026 Do you have a repro without any third party packages? ashm-dev commented on Oct 9, 2026 ContributorAuthor More actions @ZeroIntensity I was able to find one for 3.14, but unfortunately not yet for main or 3.15—though I'm still looking! picnixz commented on Oct 10, 2026 Please update the issue title as well and the description properly, at least for 3.14 ashm-dev commented on Oct 10, 2026 ContributorAuthor More actions @picnixz As of 3.14, since I have a separate issue with Segfault, and it’s updated there. picnixz commented on Oct 10, 2026 Is this issue still relevant then? ashm-dev commented on Oct 10, 2026 ContributorAuthor More actions Yes, this issue is still relevant. #158963 is about the LeakSanitizer report on 3.15 and main (3.16). The 3.14 JIT segfault, with its standard-library-only reproducer, is a different bug tracked in #158961. For #158963, my reduced reproducer still requires Cython; I have not found one without third-party packages yet. Please remove the 3.14 label from this issue—it does not apply to #158963. picnixz commented on Oct 10, 2026 Please first update the description.. Stale descriptions are useless. Is it still about editable build and sqlalchemy?? changed the title [-]LeakSanitizer reports leaked code object during SQLAlchemy editable build on 3.15 and main[/-] [+]LeakSanitizer reports leaked code object when running Cython[/+] on Oct 21, 2026ashm-dev commented on Oct 21, 2026 ContributorAuthor More actions @picnixz Updated the title and description with the Cython reproducer.
MetadataMetadatainterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error DevelopmentNo branches or pull requestsIssue actions
|