Skip to content

Avoid memory leak in _pop_preserved() #147998

@maurycy

Description

@maurycy

Bug report

Bug description:

Inspired by gh-147960, I asked Claude Code (full disclosure), using @devdanzin's https://github.com/devdanzin/code-review-toolkit, to find more similar blunders. I think that this kind of mechanical omissions are ideal matches for LLM.

While I could not find exact missing PyMem_RawFree() and I do not agree with other (tortured) findings, there was a real one:

_PyXI_namespace *xidata = _create_sharedns(session->_preserved);

if (xidata != NULL) {

The xidata seems to be shadowed inside the else, and the error: path does not see it.

Reproduction

[130] 2026-04-02T19:53:24.286652120+0000 maurycy@weiss /home/maurycy/cpython (main) % ./python.exe -m test --single-process test_interpreters -m 'test_callable_requires_frame'
Using random seed: 2805989155
0:00:00 load avg: 1.02 Run 1 test sequentially in a single process
0:00:00 load avg: 1.02 [1/1] test_interpreters
0:00:00 load avg: 1.02 [1/1] test_interpreters passed

== Tests result: SUCCESS ==

1 test OK.

Total duration: 167 ms
Total tests: run=1 (filtered)
Total test files: run=1/1 (filtered)
Result: SUCCESS

=================================================================
==99950==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 192 byte(s) in 3 object(s) allocated from:
    #0 0xaaaab6763810 in calloc (/home/maurycy/cpython/python.exe+0x343810) (BuildId: 1fbad198f97060c46a5d9d2231aca43704eee88e)
    #1 0xaaaab6a3f4a0 in _PyMem_DebugRawAlloc /home/maurycy/cpython/Objects/obmalloc.c:3040:24
    #2 0xaaaab6a3f4a0 in _PyMem_DebugRawCalloc /home/maurycy/cpython/Objects/obmalloc.c:3085:12
    #3 0xaaaab6ccb5e0 in _sharedns_alloc /home/maurycy/cpython/Python/crossinterp.c:2277:27
    #4 0xaaaab6ccb5e0 in _create_sharedns /home/maurycy/cpython/Python/crossinterp.c:2341:27

on CC=clang CXX=clang++ ./configure --with-pydebug --with-address-sanitizer --without-pymalloc -without-mimalloc build.

Another (Claude Code's) reproduction:

https://gist.github.com/maurycy/17e5aae1c8c665118c7980f373494e6e

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions