[SUPERSEDED — duplicate of python/cpython#151818] eval-loop stackref over-decref under OOM (_PyFrame_ClearLocals, frame.c:101)
Update (2026-06-25). This was originally filed as a distinct eval-loop stackref over-decref ("OOM-0005"). Reverse-execution under
rrlater showed that both of its reproducers are in fact thelist.append()-under-MemoryErrordouble-free tracked in python/cpython#151818 (_CALL_LIST_APPENDsteals the appended item and_PyList_AppendTakeRefdecrefs it on the failed resize, butERROR_NO_POPleaves the dead stackref on the value stack to be closed a second time during the unwind)._PyFrame_ClearLocals/PyStackRef_XCLOSE(frame.c:101/pycore_stackref.h:726) is the detector, not a separate defect. There is no distinct bug here — see #151818.The reproducer below is retained as an additional pure-stdlib reproducer of #151818.
AI disclaimer: this reproducer and write-up were produced with Claude Code; reviewed and reproduced.
A str argument is threaded through pkgutil.get_importer(s) → os.fsdecode(s) → os.fspath(s)
while a second reference to it is kept in a dict. Under an allocation-failure sweep, the argument
is appended to a list whose grow fails; the failed _CALL_LIST_APPEND double-frees it (the
item has a live second reference), and the second close lands during the frame unwind at
_PyFrame_ClearLocals → PyStackRef_XCLOSE. Because the dict still references the freed string, a
later access is a use-after-free.
Deterministic (≥5/5) on debug+ASan builds (free-threaded and GIL); needs a debug build exposing
_testcapi.set_nomemory:
import pkgutil
import faulthandler
faulthandler.enable()
from _testcapi import set_nomemory
d = {"s": str}
d["arg"] = d["s"](0) # a heap str ("0"), also kept alive by this dict
def sweep(thunk):
for start in range(60):
set_nomemory(start) # fail every allocation from #start onward
try:
thunk()
except BaseException:
pass
def call_get_importer():
pkgutil.get_importer(d["arg"])
sweep(call_get_importer)- debug GIL + ASan: ASan
heap-use-after-free; freed-by =PyStackRef_XCLOSE@pycore_stackref.h:726←_PyFrame_ClearLocals@frame.c:101(exit_unwind); read = a laterPy_INCREFvia a dict lookup; allocated-by = the victimstr. - debug free-threaded + ASan: the freed local is read by
PyOS_FSPath→PyType_HasFeatureon freed memory → SIGSEGV. - plain debug build: caught earlier as
_Py_NegativeRefcountat thePyStackRef_XCLOSEsite.
rr reverse-execution from the crash back through the victim's refcount history shows the freeing
chain runs through _PyList_AppendTakeRefListResize (Objects/listobject.c:531) ←
_CALL_LIST_APPEND — i.e. #151818. See OOM-0005-backtrace.txt for the captured stacks.
CPython main (3.16.0a0), commit 1b9fe5c. Same fix as #151818.