Skip to content

LeakSanitizer reports leaked code object during SQLAlchemy editable build on 3.15 and main #158963

Description

@ashm-dev

Bug report

I built both CPython checkouts with:

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:

  • 3.15: Python 3.15.0rc3+dev (heads/3.15:874d1122deb, Oct 7 2026, 14:48:22) [Clang 23.1.1 ]
  • main: Python 3.16.0a0 (heads/main:37cc8dd0b9a, Oct 7 2026, 14:51:35) [Clang 23.1.1 ]

From a SQLAlchemy checkout, I ran:

uv sync --all-extras --all-groups --all-packages --python /home/boss/projects/oss/cpython/3.15/python
uv sync --all-extras --all-groups --all-packages --python /home/boss/projects/oss/cpython/main/python

On 3.15, setuptools.build_meta.prepare_metadata_for_build_editable created the metadata, then LeakSanitizer reported 852 bytes leaked in 6 allocations. On main, setuptools.build_meta.build_editable created the wheel, then LeakSanitizer reported 703 bytes leaked in 6 allocations. Both builds exited with status 1, so uv sync failed.

Relevant excerpt from the main run (not the full log):

==254631==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 264 byte(s) in 1 object(s) allocated from:
    #0 0x55eb6fe15b81 in malloc (/home/boss/projects/oss/cpython/main/python+0xf66b81)
    #1 0x55eb703eaa37 in _PyMem_DebugRawAlloc /home/boss/projects/oss/cpython/main/Objects/obmalloc.c:3103:24
    #2 0x55eb70392647 in _PyObject_NewVar /home/boss/projects/oss/cpython/main/Objects/object.c:570:26
    #3 0x55eb701df82c in _PyCode_New /home/boss/projects/oss/cpython/main/Objects/codeobject.c:734:10
    #4 0x55eb70b941d8 in r_object /home/boss/projects/oss/cpython/main/Python/marshal.c:1734:29

SUMMARY: AddressSanitizer: 703 byte(s) leaked in 6 allocation(s).

Expected: the build process exits without a LeakSanitizer error.

CPython versions tested on:

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!

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

    pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions