GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #158963

New issue
Copy link
New issue
Copy link
Open
Open
LeakSanitizer reports leaked code object when running Cython#158963
Copy link
Labels
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

Description

@ashm-dev
ashm-dev
opened on Oct 7, 2026
Issue body actions

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. ashm-dev
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 7, 2026
  2. picnixz commented on Oct 7, 2026

    @picnixz
    picnixz
    on Oct 7, 2026
    Member
    More actions

    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?

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

    @ZeroIntensity
    ZeroIntensity
    on Oct 7, 2026
    Member
    More actions

    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.

  5. ashm-dev commented on Oct 7, 2026

    @ashm-dev
    ashm-dev
    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.

  6. ZeroIntensity commented on Oct 7, 2026

    @ZeroIntensity
    ZeroIntensity
    on Oct 7, 2026
    Member
    More actions

    Do you have a repro without any third party packages?

  7. ashm-dev commented on Oct 9, 2026

    @ashm-dev
    ashm-dev
    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!

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

    @picnixz
    picnixz
    on Oct 10, 2026
    Member
    More actions

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

  10. picnixz
    added
    3.14bugs and security fixesbugs and security fixes
    on Oct 10, 2026
  11. ashm-dev commented on Oct 10, 2026

    @ashm-dev
    ashm-dev
    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.

  12. picnixz commented on Oct 10, 2026

    @picnixz
    picnixz
    on Oct 10, 2026
    Member
    More actions

    Is this issue still relevant then?

  13. picnixz
    added
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Oct 10, 2026
  14. ashm-dev commented on Oct 10, 2026

    @ashm-dev
    ashm-dev
    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.

  15. picnixz commented on Oct 10, 2026

    @picnixz
    picnixz
    on Oct 10, 2026
    Member
    More actions

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

  16. picnixz
    removed
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    3.14bugs and security fixesbugs and security fixes
    on Oct 10, 2026
  17. kevinprimeraquecreo-stack
    added this to Project_VKon Oct 18, 2026
  18. github-project-automation
    moved this to Todo in Project_VKon Oct 18, 2026
  19. kevinprimeraquecreo-stack
    added this to Project_VKon Oct 18, 2026
  20. ashm-dev
    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
  21. ashm-dev commented on Oct 21, 2026

    @ashm-dev
    ashm-dev
    on Oct 21, 2026
    ContributorAuthor
    More actions

    @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)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.