-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
Avoid memory leak in _pop_preserved() #147998
Description
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:
Line 2968 in 7817651
| _PyXI_namespace *xidata = _create_sharedns(session->_preserved); |
Line 2991 in 7817651
| if (xidata != NULL) { |
The xidata seems to be shadowed inside the else, and the error: path does not see it.
Reproduction
Somewhat convoluted (with CC=clang CXX=clang++ ./configure --with-pydebug --with-address-sanitizer), on b4fac15:
2026-04-02T13:09:02.142750423+0000 maurycy@weiss /home/maurycy/cpython (main) % ASAN_OPTIONS=detect_leaks=1 ./python.exe -c "
import _interpreters as _interp
interp_id = _interp.create()
# The callable must SUCCEED but return something non-shareable.
# globals() returns a dict with module refs -- not shareable.
for i in range(200):
try:
_interp.call(interp_id, globals)
except Exception:
pass
_interp.destroy(interp_id)
print('done')
"
done
=================================================================
==40178==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 12736 byte(s) in 199 object(s) allocated from:
#0 0xaaaab7a32010 in calloc (/home/maurycy/cpython/python.exe+0x352010) (BuildId: 852dcd919b2c086f83f861eb443f7a4bfcbf1a00)
#1 0xaaaab7d325f4 in _PyMem_DebugRawAlloc /home/maurycy/cpython/Objects/obmalloc.c:3040:24
#2 0xaaaab7d325f4 in _PyMem_DebugRawCalloc /home/maurycy/cpython/Objects/obmalloc.c:3085:12
#3 0xaaaab7fc82b4 in _sharedns_alloc /home/maurycy/cpython/Python/crossinterp.c:2277:27
#4 0xaaaab7fc82b4 in _create_sharedns /home/maurycy/cpython/Python/crossinterp.c:2341:27
#5 0xaaaab7fcb228 in _pop_preserved /home/maurycy/cpython/Python/crossinterp.c:2968:35
The full output:
https://gist.github.com/maurycy/17e5aae1c8c665118c7980f373494e6e
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS