Skip to content

Avoid memory leak in _pop_preserved() #147998

Closed

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Apr 2, 2026
  2. serhiy-storchaka commented on Apr 2, 2026

    @serhiy-storchaka
    Member

    The same code exists in 3.14. What about 3.13? Does it have such bug in other code?

  3. maurycy commented on Apr 2, 2026

    @maurycy
    ContributorAuthor

    @serhiy-storchaka

    The same code exists in 3.14. What about 3.13?

    This is when this line was added:

    https://github.com/python/cpython/pull/133484/changes#diff-88c171bf6a3ab394012cb7af2d9929543d379573ff46279de7ad38d4e7a12c54R2784

    No backport to 3.13:

    https://github.com/python/cpython/blob/3.13/Python/crossinterp.c

    Does it have such bug in other code?

    Do you mean more shadowings in other places? It reported lots stuff but I didn't immediately agree with it and don't want to be responsible (yet? until I review.)

  4. maurycy commented on May 3, 2026

    @maurycy
    ContributorAuthor

    @serhiy-storchaka Do you want to merge? (I’m going through my backlog.)

  5. added a commit that references this issue on May 4, 2026
  6. serhiy-storchaka commented on May 4, 2026

    @serhiy-storchaka
    Member

    Thank you for your contribution @maurycy.

  7. serhiy-storchaka commented on May 4, 2026

    @serhiy-storchaka
    Member

    Do you mean more shadowings in other places?

    I meant that while the exact form of these lines is new in 3.14, was there other code replaced but these lines and did it suffer from the same error? It could be a new bug introduced in new code, but it could also be an old bug that survived refactoring.

  8. added a commit that references this issue on May 4, 2026
  9. maurycy commented on May 4, 2026

    @maurycy
    ContributorAuthor
  10. serhiy-storchaka commented on May 4, 2026

    @serhiy-storchaka
    Member

    Thank you for confirmation.

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

    3.14bugs and security fixes3.15bugs and security fixesinterpreter-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