Skip to content

LeakSanitizer reports leaked code object when running Cython #158963

Open

Description

@ashm-dev

Bug 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

Activity

  1. picnixz commented on Oct 7, 2026

    @picnixz
    Member

    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?

  2. added
    pendingThe issue will be closed if no feedback is provided
    on Oct 7, 2026
  3. ZeroIntensity commented on Oct 7, 2026

    @ZeroIntensity
    Member

    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.

  4. ashm-dev commented on Oct 7, 2026

    @ashm-dev
    ContributorAuthor

    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.

  5. ZeroIntensity commented on Oct 7, 2026

    @ZeroIntensity
    Member

    Do you have a repro without any third party packages?

  6. ashm-dev commented on Oct 9, 2026

    @ashm-dev
    ContributorAuthor

    @ZeroIntensity I was able to find one for 3.14, but unfortunately not yet for main or 3.15—though I'm still looking!

  7. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    and removed
    pendingThe issue will be closed if no feedback is provided
    on Oct 10, 2026
  8. picnixz commented on Oct 10, 2026

    @picnixz
    Member

    Please update the issue title as well and the description properly, at least for 3.14

  9. ashm-dev commented on Oct 10, 2026

    @ashm-dev
    ContributorAuthor

    @picnixz As of 3.14, since I have a separate issue with Segfault, and it’s updated there.

  10. picnixz commented on Oct 10, 2026

    @picnixz
    Member

    Is this issue still relevant then?

  11. added
    pendingThe issue will be closed if no feedback is provided
    on Oct 10, 2026
  12. ashm-dev commented on Oct 10, 2026

    @ashm-dev
    ContributorAuthor

    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.

  13. picnixz commented on Oct 10, 2026

    @picnixz
    Member

    Please first update the description.. Stale descriptions are useless. Is it still about editable build and sqlalchemy??

  14. removed
    pendingThe issue will be closed if no feedback is provided
    3.14bugs and security fixes
    on Oct 10, 2026
  15. 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, 2026
  16. ashm-dev commented on Oct 21, 2026

    @ashm-dev
    ContributorAuthor

    @picnixz Updated the title and description with the Cython reproducer.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions