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/99b84915b197ae0ade7face262b8af66 to your computer and use it in GitHub Desktop.

Select an option

Save devdanzin/99b84915b197ae0ade7face262b8af66 to your computer and use it in GitHub Desktop.
OOM-0018: Abort: ownership assert in `set_keys` (`dictobject.c:205`)
# Authoritative gdb backtrace (3.16_ft_debug_asan, commit 15d7406)
# Abort: assert(_Py_IsOwnedByCurrentThread((PyObject *)mp) || IS_DICT_SHARED(mp))
# in set_keys(), reached from PyObject_ClearManagedDict's OOM-recovery branch.
# Free-threaded only. The crash is racy: the offending object is created/owned by
# one thread and its last reference is dropped on ANOTHER thread, so the dealloc
# (and the managed-dict clear) runs on a thread that does not own the dict, and the
# dict was never marked shared. Captured under `gdb -ex run` after several attempts
# (other OOM-driven aborts -- mimalloc realloc, BRC merge -- can win the race first).
python: Objects/dictobject.c:205: void set_keys(PyDictObject *, PyDictKeysObject *): Assertion `_Py_IsOwnedByCurrentThread((PyObject *)mp) || IS_DICT_SHARED(mp)' failed.
Thread 1 "python" received signal SIGABRT, Aborted.
#0 __pthread_kill_implementation (...) at ./nptl/pthread_kill.c:44
#1 __pthread_kill_internal (...) at ./nptl/pthread_kill.c:89
#2 __GI___pthread_kill (...) at ./nptl/pthread_kill.c:100
#3 __GI_raise (sig=...) at ../sysdeps/posix/raise.c:26
#4 __GI_abort () at ./stdlib/abort.c:77
#5 __libc_message_impl (...) at ../sysdeps/posix/libc_fatal.c:138
#6 __libc_message_wrapper (...) at ../include/stdio.h:203
#7 __assert_fail (assertion=..., file=..., line=..., function=...) at ./assert/assert.c:37
#8 set_keys (mp=..., keys=...) at Objects/dictobject.c:205 <- assert _Py_IsOwnedByCurrentThread(mp) || IS_DICT_SHARED(mp)
#9 PyObject_ClearManagedDict (obj=...) at Objects/dictobject.c:7896 <- set_keys(dict, Py_EMPTY_KEYS) in the OOM-recovery branch (detach_dict_from_object failed)
#10 subtype_dealloc (self=...) at Objects/typeobject.c:2847 <- tp_dealloc clears the managed dict
#11 _Py_Dealloc (op=...) at Objects/object.c:3319
#12 _Py_brc_queue_object (ob=...) at Python/brc.c:91 <- biased-refcount: object owned by another thread, freed here
#13 Py_DECREF (...) at ./Include/refcount.h:363 <- last (cross-thread) decref of the managed-dict object
# Original fuzzer vehicle (python-5/wsgiref_util-assertion) shows the same assert with the
# managed-dict object freed on the main eval thread via _Py_HandlePending (BRC merge):
# abort <- __assert_fail <- PyObject_ClearManagedDict+0x8be (set_keys inlined, dictobject.c:7896)
# <- ... <- _Py_HandlePending+0x16a <- _PyEval_EvalFrameDefault
# (set_keys is `static inline`, so it is inlined into PyObject_ClearManagedDict in that build's C stack.)

Abort: ownership assert in set_keys (dictobject.c:205)

PyObject_ClearManagedDict's OOM-recovery branch rewrites ma_keys via set_keys(dict, Py_EMPTY_KEYS) without first calling ensure_shared_on_resize(), so on free-threaded builds set_keys's _Py_IsOwnedByCurrentThread(mp) || IS_DICT_SHARED(mp) assert aborts when clearing a dict that's neither owned nor marked shared.

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

Crash report

On free-threaded builds, PyObject_ClearManagedDict() clears an object whose materialized dict still points at the object's inline values by calling detach_dict_from_object(), which copy_values()-copies the inline values out. Under OOM that copy fails and the function enters a recovery branch that rewrites dict->ma_keys via set_keys(dict, Py_EMPTY_KEYS) (Objects/dictobject.c:7896). Unlike every other path that rewrites ma_keys, this branch never calls ensure_shared_on_resize(), so set_keys's debug assert _Py_IsOwnedByCurrentThread(mp) || IS_DICT_SHARED(mp) (dictobject.c:205) fires whenever the dict being cleared is neither owned by the current thread nor already marked shared — and the interpreter aborts.

Two routes reach this unguarded set_keys, both observed: the cyclic GC clearing such an object under OOM (delete_garbage -> subtype_clear -> PyObject_ClearManagedDict), which is the deterministic manifestation the reproducer and the vehicles actually take (8/8 on a normal run); and a cross-thread dealloc where biased reference counting routes the last decref through _Py_brc_queue_object on a non-owning thread (a rarer race, captured only intermittently under gdb). Both converge on the same defect, and the fix is the same.

Reproducer

Minimal, stdlib-only (shrinkray-reduced from the wsgiref.util vehicle). Deterministic — aborts on every run (30/30); requires a free-threaded debug build (PYTHON_GIL=0):

from unittest.mock import MagicMock
from _testcapi import set_nomemory

set_nomemory(200)              # fail allocations from the 200th onward (lands in shutdown GC)
(MagicMock(), undefined_name)  # build a MagicMock (managed __dict__), then NameError abandons
                               # it into a traceback cycle -> survives to shutdown GC

A MagicMock (which has a managed __dict__) is abandoned into the traceback reference cycle created by the NameError, so it survives to interpreter shutdown. With allocation failure still armed, the shutdown cyclic GC clears it via delete_garbage -> subtype_clear -> PyObject_ClearManagedDict, whose OOM-recovery branch trips the assert. This is exactly the path the full vehicles take on a normal run (8/8 under gdb, see Notes), so it is a faithful reduction, not a drift.

The set_nomemory(N) argument must land the failing allocation inside the shutdown GC: the working window is roughly N in [113, 900] on this build (N=200 is comfortably central) — too small fails during setup, too large (e.g. 999) overshoots shutdown. The window is an allocation-count effect, not tied to the small-int cache (tested N=254..258 straddling the 256 boundary: no change). Minimization complete; the full fuzzer vehicle is preserved as vehicle_source.py.

Backtrace

Deterministic path (reproducer + vehicles, bt under gdb):

#8  set_keys                  Objects/dictobject.c:205          <- assert _Py_IsOwnedByCurrentThread(mp) || IS_DICT_SHARED(mp)
#9  PyObject_ClearManagedDict Objects/dictobject.c:7896         <- set_keys(dict, Py_EMPTY_KEYS) in the OOM-recovery branch
#10 subtype_clear             Objects/typeobject.c:2700
#11 delete_garbage            Python/gc_free_threading.c:1761
#12 gc_collect_internal       Python/gc_free_threading.c:2176
#13 gc_collect_main           Python/gc_free_threading.c:2257   <- reason=_Py_GC_REASON_SHUTDOWN
#14 finalize_modules          Python/pylifecycle.c:1955
#15 _Py_Finalize              Python/pylifecycle.c:2491

Alternate path (cross-thread dealloc) — same assert/branch, reached via biased reference counting. Captured during first triage but did not recur in a 210-run local census (see Notes); likely host-specific timing:

#10 subtype_dealloc           Objects/typeobject.c:2847
#11 _Py_Dealloc               Objects/object.c:3319
#12 _Py_brc_queue_object      Python/brc.c:91                   <- object owned by another thread, freed here
#13 Py_DECREF                 ./Include/refcount.h:363          <- cross-thread last decref

Root cause

Objects/dictobject.c, PyObject_ClearManagedDict() (L7865), the detach_dict_from_object()-failed branch:

    Py_BEGIN_CRITICAL_SECTION(dict);
    err = detach_dict_from_object(dict, obj);   /* L7885: copy_values() fails under OOM -> -1 */
    Py_END_CRITICAL_SECTION();
    if (err) {
        assert(PyErr_Occurred() == PyExc_MemoryError);
        PyErr_FormatUnraisable(...);
        Py_BEGIN_CRITICAL_SECTION(dict);
        PyDictKeysObject *oldkeys = dict->ma_keys;
        set_keys(dict, Py_EMPTY_KEYS);                  /* L7896: rewrites ma_keys WITHOUT marking shared */
        dict->ma_values = NULL;
        dictkeys_decref(oldkeys, IS_DICT_SHARED(dict)); /* L7898: same wrong shared flag */
        STORE_USED(dict, 0);
        clear_inline_values(_PyObject_InlineValues(obj));
        Py_END_CRITICAL_SECTION();
    }

set_keys() (L202) asserts _Py_IsOwnedByCurrentThread((PyObject *)mp) || IS_DICT_SHARED(mp) (L205). Every other site that rewrites ma_keys -- dictresize (L2167), clear (L3098), update (L4144) -- first calls ensure_shared_on_resize(mp) (L1418), which marks a dict that isn't owned by the current thread as DICT_SHARED. This recovery branch, added in commit 6c450f44c283, skips that step. Because the object is freed via biased reference counting on a thread other than its owner, _Py_IsOwnedByCurrentThread(dict) is false and the dict was never marked shared, so the invariant is violated. It is a missing "mark shared before resize", not a use-after-free.

Suggested fix

Mark the dict shared before rewriting ma_keys, mirroring the normal resize paths. Inside the recovery critical section, just before set_keys:

    Py_BEGIN_CRITICAL_SECTION(dict);
    PyDictKeysObject *oldkeys = dict->ma_keys;
    ensure_shared_on_resize(dict);     /* <-- add: marks DICT_SHARED if not owned by current thread */
    set_keys(dict, Py_EMPTY_KEYS);
    dict->ma_values = NULL;
    dictkeys_decref(oldkeys, IS_DICT_SHARED(dict));   /* now sees the correct shared flag */
    ...

This also corrects the IS_DICT_SHARED(dict) argument to dictkeys_decref (L7898), so the old keys are freed with the proper QSBR delay protecting concurrent lock-free readers.

Notes

Found by OOM-injection fuzzing (set_nomemory). Free-threading-specific: set_keys's ownership assert and the _Py_IsOwnedByCurrentThread/IS_DICT_SHARED machinery only exist under Py_GIL_DISABLED (the non-FT set_keys at L270 is a bare mp->ma_keys = keys). Reproduces as an abort only on the FT debug build -- the FT release build defines NDEBUG, so the assert is compiled out, but the same branch then rewrites ma_keys and decrefs the old keys with the wrong IS_DICT_SHARED() value, skipping the QSBR delay (a latent memory-safety hazard for concurrent readers, not just a debug assert). GIL builds (jit, upstream) lack the field and the assert entirely and run the reproducer cleanly. Per the OOM-catalog convention for assert-based aborts, non-debug builds are recorded as n/a.

Minimization complete (2026-06-19): the deterministic manifestation is the cyclic GC clearing a managed-dict object under OOM, not a cross-thread race. A crash-face census of the wsgiref_util vehicle resolved the caller frame above PyObject_ClearManagedDict (via addr2line) on every run: shutdown-GC (subtype_clear) in 160/160 runs without gdb and 50/50 under gdb — 0/210 cross-thread. The original "racy / ~1-in-20 / cross-thread" characterization (and the _Py_brc_queue_object backtrace below) was a real capture during first triage but does not recur on this local build; it is host-specific timing (the fuzzing host runs the older pre-ad1513a263b build, cf. host_only_candidates.md). gdb does not perturb the face here — both modes land on shutdown-GC. shrinkray reduced the vehicle to the 30/30 4-line repro above; the MagicMock()-abandoned-into-a-traceback-cycle that all three vehicles (wsgiref_util, pickletools, sched) build is exactly what survives to shutdown GC. The set_nomemory argument selects when the allocation fails (window [113, 900] on this build); it is not tied to the small-int cache.

Versions

  • main (3.16.0a0, commit 15d7406); aborts on the free-threaded debug+ASan build. Release/JIT/upstream builds: assertion compiled out / not present (n/a). Recovery branch introduced in commit 6c450f44c283.

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

"""OOM-0018 minimal reproducer (stdlib only) — reduced from the wsgiref.util fuzz vehicle with shrinkray.
A free-threaded build's managed-dict teardown has an unsafe OOM-recovery branch:
`PyObject_ClearManagedDict` (Objects/dictobject.c:7896), when `detach_dict_from_object` fails
under memory pressure, calls `set_keys(dict, Py_EMPTY_KEYS)` — but `set_keys` asserts
`_Py_IsOwnedByCurrentThread(mp) || IS_DICT_SHARED(mp)` (dictobject.c:205), a precondition the
recovery path does not re-establish. Under OOM this aborts on debug builds; on release it
clears the dict without the ownership/shared invariant.
This hits that branch deterministically via the cyclic GC: a MagicMock (which has a managed
`__dict__`) is abandoned into a traceback reference cycle, so it survives to interpreter
shutdown; with allocation failure still armed, the shutdown GC's clear path
(`delete_garbage -> subtype_clear -> PyObject_ClearManagedDict`) takes the OOM-recovery branch
and trips the assert. This is the SAME path the full vehicle takes on a normal run (8/8 under
gdb); the cross-thread dealloc path (`_Py_brc_queue_object`) the report also shows is a rare
alternate route to the identical assert/branch.
Requires a free-threaded debug build (PYTHON_GIL=0). Deterministic (30/30). The
`set_nomemory(N)` argument must land the failing allocation inside the shutdown GC: the
working window is roughly N in [113, 900] on this build (N=200 is comfortably central); too
small fails during setup, too large (e.g. 999) overshoots shutdown. NOT tied to the small-int
cache — tested N=254..258 across the 256 boundary, no effect. Full vehicle: vehicle_source.py.
"""
from unittest.mock import MagicMock
from _testcapi import set_nomemory
set_nomemory(200) # fail allocations from the 200th onward (lands in shutdown GC)
(MagicMock(), undefined_name) # build a MagicMock, then NameError abandons it into a traceback
# cycle -> survives to shutdown GC, cleared there under active OOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment