Skip to content

Instantly share code, notes, and snippets.

@devdanzin
Last active June 20, 2026 01:08
Show Gist options
  • Select an option

  • Save devdanzin/c809eb4072c0c787c0c890f54ba1c843 to your computer and use it in GitHub Desktop.

Select an option

Save devdanzin/c809eb4072c0c787c0c890f54ba1c843 to your computer and use it in GitHub Desktop.
OOM-0006: Abort/Segfault: `_PyObject_GC_UNTRACK` assert on untracked iterator in `dictiter_dealloc` (`dictobject.c:5532`)
Program received signal SIGABRT, Aborted.
#0 __pthread_kill_implementation (threadid=..., signo=..., no_tid=...) at ./nptl/pthread_kill.c:44
#1 __pthread_kill_internal (threadid=..., signo=...) at ./nptl/pthread_kill.c:89
#2 __GI___pthread_kill (threadid=..., signo=...) at ./nptl/pthread_kill.c:100
#3 0x00007ffff7c45b7e in __GI_raise (sig=...) at ../sysdeps/posix/raise.c:26
#4 0x00007ffff7c288ec in __GI_abort () at ./stdlib/abort.c:77
#5 0x00005555560a1494 in fatal_error_exit (status=...) at Python/pylifecycle.c:3517
#6 0x00005555560a1160 in fatal_error (fd=..., header=..., prefix=..., msg=..., status=...) at Python/pylifecycle.c:3742
#7 0x000055555609ca40 in _Py_FatalErrorFunc (func=..., msg=...) at Python/pylifecycle.c:3765
#8 0x000055555580641b in _PyObject_AssertFailed (obj=..., expr=..., msg=..., file=..., line=..., function=...) at Objects/object.c:3278
#9 0x0000555555be418d in _PyObject_GC_UNTRACK (lineno=..., op=..., filename=...) at ./Include/internal/pycore_gc.h:254
#10 dictiter_dealloc (self=...) at Objects/dictobject.c:5532
#11 0x0000555555c34cd2 in _Py_Dealloc (op=...) at Objects/object.c:3319
#12 0x0000555555bf99e8 in Py_DECREF (lineno=..., op=..., filename=...) at ./Include/refcount.h:359
#13 dictiter_new (dict=..., itertype=...) at Objects/dictobject.c:5516
#14 0x0000555555a7923d in PyObject_GetIter (o=...) at Objects/abstract.c:2825
#15 0x0000555555ec162d in _PyEval_GetIter (iterable=..., index_or_null=..., yield_from=...) at Python/ceval.c:1142
#16 0x0000555555edce1f in _PyEval_EvalFrameDefault (tstate=..., frame=..., throwflag=...) at Python/generated_cases.c.h:6950
#17 0x0000555555ebd9b4 in _PyEval_EvalFrame (tstate=..., frame=..., throwflag=...) at ./Include/internal/pycore_ceval.h:122

Abort/Segfault: _PyObject_GC_UNTRACK assert on untracked iterator in dictiter_dealloc (dictobject.c:5532)

In dictiter_new(), a dict item-iterator's di_result tuple is allocated before _PyObject_GC_TRACK; under OOM that alloc fails and the Py_DECREF(di) error path runs dictiter_dealloc, which unconditionally untracks the never-tracked object — asserting on debug, corrupting the GC list (later segfault) on release.

AI Disclaimer: this gist was drafted by Claude Code, which also generated the reduced reproducer.

Crash report

Creating a dict item-iterator (iter(d.items()), reversed items) while allocations are failing aborts on a debug build with Objects/dictobject.c:5532: _PyObject_GC_UNTRACK: Assertion "_PyObject_GC_IS_TRACKED(op)" failed: object not tracked by the garbage collector (object type dict_itemiterator). dictiter_new() allocates the iterator with PyObject_GC_New but only calls _PyObject_GC_TRACK(di) at the very end; if the intermediate _PyTuple_FromPairSteal() (for di_result) fails under OOM, the function does Py_DECREF(di) on the still-untracked object, and dictiter_dealloc() unconditionally calls _PyObject_GC_UNTRACK(di). On release builds (NDEBUG) the assert is compiled out and the un-tracking corrupts the GC list, segfaulting later in _Py_Dealloc.

Reproducer

Minimal, stdlib-only (shrinkray-reduced from the vehicle, then cleaned; deterministic, re-verified). Triggers an iter(d.items()) inside _strptime under the set_nomemory sweep, hitting the dictiter_new OOM error path.

import faulthandler, _strptime
faulthandler.enable()
from _testcapi import set_nomemory
for start in range(60):
    set_nomemory(start)
    try:
        _strptime._strptime("", "")
    except BaseException:
        pass
print("done, no crash")

The full fuzzer vehicle is preserved as vehicle_source.py.

Backtrace

#8  _PyObject_AssertFailed                          Objects/object.c:3278
#9  _PyObject_GC_UNTRACK                            Include/internal/pycore_gc.h:254   <- assert: !_PyObject_GC_IS_TRACKED
#10 dictiter_dealloc (self=dict_itemiterator)       Objects/dictobject.c:5532
#11 _Py_Dealloc                                     Objects/object.c:3319
#12 Py_DECREF                                       Include/refcount.h:359
#13 dictiter_new (itertype=PyDictIterItem_Type)     Objects/dictobject.c:5516          <- Py_DECREF(di) after di_result alloc fails
#14 PyObject_GetIter                                Objects/abstract.c:2825
#15 _PyEval_GetIter                                 Python/ceval.c:1142
#16 _PyEval_EvalFrameDefault (GET_ITER)             Python/generated_cases.c.h:6950

Faulting object: a freshly-allocated, never-tracked dict_itemiterator (object refcount : 0, object type name: dict_itemiterator) being deallocated from inside its own constructor's error path.

Root cause

Objects/dictobject.c, dictiter_new() (L5486-5525):

di = PyObject_GC_New(dictiterobject, itertype);   /* L5491: allocated, NOT yet GC-tracked */
if (di == NULL) { return NULL; }
di->di_dict = (PyDictObject*)Py_NewRef(dict);
...
if (itertype == &PyDictIterItem_Type ||
    itertype == &PyDictRevIterItem_Type) {
    di->di_result = _PyTuple_FromPairSteal(Py_None, Py_None);   /* L5514: can fail (OOM) */
    if (di->di_result == NULL) {
        Py_DECREF(di);                                          /* L5516: di is UNTRACKED -> dealloc asserts */
        return NULL;
    }
}
else {
    di->di_result = NULL;
}
_PyObject_GC_TRACK(di);                                         /* L5523: tracking happens only here */
return (PyObject *)di;

dictiter_dealloc() (L5527) unconditionally untracks (correct for the normal lifecycle, since "UnTrack is needed before calling any callbacks", bpo-31095):

static void dictiter_dealloc(PyObject *self) {
    dictiterobject *di = (dictiterobject *)self;
    _PyObject_GC_UNTRACK(di);          /* L5532: asserts the object IS tracked */
    Py_XDECREF(di->di_dict);
    Py_XDECREF(di->di_result);
    PyObject_GC_Del(di);
}

_PyObject_GC_UNTRACK (Include/internal/pycore_gc.h:254) asserts _PyObject_GC_IS_TRACKED(op). On the di_result == NULL error path the iterator is deallocated before it was ever tracked, so the assert fires (debug) or the GC freelist is corrupted (release -> later segfault in _Py_Dealloc).

This only affects item iterators (dict_itemiterator / dict_reverseitemiterator), because they are the only ones whose di_result requires an allocation between PyObject_GC_New and _PyObject_GC_TRACK (matching every observed object type name: dict_itemiterator).

Suggested fix

Track the iterator before the only fallible allocation, or use a manual free that does not assume tracking on the error path. Cleanest fix -- move _PyObject_GC_TRACK(di) before the _PyTuple_FromPairSteal call (initialize di->di_result = NULL first so dictiter_dealloc's Py_XDECREF is safe), so the standard Py_DECREF(di) / dictiter_dealloc path is valid:

    di->di_result = NULL;
    _PyObject_GC_TRACK(di);                     /* track once, before any fallible alloc */
    if (itertype == &PyDictIterItem_Type || itertype == &PyDictRevIterItem_Type) {
        di->di_result = _PyTuple_FromPairSteal(Py_None, Py_None);
        if (di->di_result == NULL) {
            Py_DECREF(di);                       /* now di is tracked: dealloc is valid */
            return NULL;
        }
    }
    return (PyObject *)di;

Alternative (keep tracking last): free the untracked object directly instead of via the GC-untracking dealloc:

    if (di->di_result == NULL) {
        Py_DECREF((PyObject *)dict);   /* matching the Py_NewRef above */
        PyObject_GC_Del(di);           /* untracked: free directly, no UNTRACK assert */
        return NULL;
    }

Notes

  • Found by OOM-injection fuzzing (_testcapi.set_nomemory). The assert fires only on builds with assertions (debug). Release builds strip the assert (NDEBUG) and instead corrupt the GC list, segfaulting later in _Py_Dealloc -- which is why the zipapp vehicle was originally labeled "segmentation_fault" yet trips the identical dictiter_dealloc/_PyObject_GC_UNTRACK assert on the debug build (confirmed).
  • Vehicles (all confirmed on ft_debug_asan, all dict_itemiterator): asyncio_runners-assertion (representative, via weakref.py:240 dict.items()), re-assertion-sigabrt (via re/_compiler.py:775), and zipapp-segmentation_fault (segv on release+ASan upstream; same assert on debug).
  • Minimization: partial. A stdlib-only iter(d.items()) sweep does not reproduce: in the warm steady state neither PyObject_GC_New(di) (pymalloc) nor tuple_alloc(2) (size-2 tuple freelist) consults the hooked allocator, so the failure window is never entered. Forcing fresh-pool/freelist churn (fresh dicts per round) trips a different OOM bug first (_Py_NegativeRefcount assert in PyStackRef_XCLOSE, pycore_stackref.h:726, from GET_ITER error handling). The vehicle remains the reliable reproducer. The C defect itself is build-agnostic and unambiguous.
  • Same family as OOM-0001/OOM-0002: an error path acting on a partially-constructed object whose intermediate allocation/sequencing was left unchecked under OOM.

Versions

  • main (3.16.0a0, commit 15d7406). Reproduces (via the vehicle): ft_debug_asan -> abort (assert), jit -> abort (assert), upstream (release+ASan) -> segv in _Py_Dealloc. ft_release -> no crash in this run (NDEBUG; assert stripped, segv did not manifest on that path). Long-standing code; 3.13-3.15 likely affected (unverified).

Part of python/cpython#151763 — an umbrella tracking 35 OOM-related crash findings.

"""OOM-0006 minimal reproducer (stdlib only) — shrinkray-reduced from the vehicle, then cleaned.
Trigger: _strptime._strptime("", "") under the set_nomemory sweep. Deterministic (re-verified).
"""
import faulthandler, _strptime
faulthandler.enable()
from _testcapi import set_nomemory
for start in range(60):
set_nomemory(start)
try:
_strptime._strptime("", "")
except BaseException:
pass
print("done, no crash")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment