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

Select an option

Save devdanzin/f7e483080647c7b76fbda79bfeb07e9c to your computer and use it in GitHub Desktop.
OOM-0032: Abort: pending-exception assert from `warn_explicit` normalization (`_warnings.c:799/806`)
OOM-0032 — stale exception before warning message-normalization under OOM
==========================================================================
PRIMARY (fleet vehicle, plain-string message -> category constructor branch),
ft_debug_asan, SIGABRT:
python: Objects/typeobject.c:2441: PyObject *type_call(PyObject *, PyObject *, PyObject *): Assertion `!_PyErr_Occurred(tstate)' failed.
#8 type_call Objects/typeobject.c:2441 # assert: entered with an exception already pending
#9 _PyObject_MakeTpCall Objects/call.c:242
#10 PyObject_CallOneArg Objects/call.c:395
#11 warn_explicit Python/_warnings.c:806 # message = PyObject_CallOneArg(category, message)
#12 do_warn Python/_warnings.c:1136
#13 warnings_warn_impl Python/_warnings.c:1184
SIBLING (minimal stdlib repro, Warning-instance message -> PyObject_Str branch),
ft_debug_asan, SIGABRT — same root cause, adjacent line in the same function. (The
assert text is omitted here on purpose so the catalog assert key stays file-scoped to
typeobject.c rather than the over-broad Objects/object.c:!_PyErr_Occurred(tstate).)
PyObject_Str fails the same `!_PyErr_Occurred(tstate)` entry assert at object.c:818.
#8 PyObject_Str Objects/object.c:818 # same invariant, message-Str branch
#9 warn_explicit Python/_warnings.c:799 # text = PyObject_Str(message)
RELEASE NOTE: the assert is `#ifdef Py_DEBUG`, so only the release builds ft_release /
upstream compile it out (jit is also Py_DEBUG and aborts here too). On the release builds
the same fleet vehicle does not stop here — the lost MemoryError lets
execution run on and it instead SIGSEGVs in do_warn (Python/_warnings.c:1139), which is the
separately-cataloged OOM-0001. So OOM-0032's distinctive crash (the !_PyErr_Occurred assert)
is observable only on a debug build; on release it is a latent invariant violation.

Abort: pending-exception assert from warn_explicit normalization (_warnings.c:799/806)

warn_explicit normalizes a warning message without checking for a pending exception; under OOM a MemoryError is already set, so PyObject_Str (object.c:818) and the type_call category-constructor branch (typeobject.c:2441) trip their debug !_PyErr_Occurred(tstate) assert.

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

Crash report

When a warning is emitted while an allocation failure is active, CPython's warnings C path (do_warn → warn_explicit) reaches message normalization with a MemoryError already pending. warn_explicit then normalizes the message without first checking for a pending exception:

  • a plain-string message takes the category-constructor branch message = PyObject_CallOneArg(category, message) (_warnings.c:806) → type_call, which asserts !_PyErr_Occurred(tstate) at its entry (typeobject.c:2441);
  • a Warning-instance message takes text = PyObject_Str(message) (_warnings.c:799) → PyObject_Str, which asserts the same invariant (object.c:818).

Both abort the interpreter on a debug build. The assert is #ifdef Py_DEBUG, so on release builds it is compiled out and the pending exception is silently lost — a latent invariant violation (type_call/PyObject_Str may clear the caller's exception, per the assert's own comment).

Reproducer

Minimal, stdlib-only — emit a warning under the set_nomemory sweep. Deterministic (3/3) on the free-threaded debug+ASan build; trips the PyObject_Str sibling site:

import warnings, faulthandler
faulthandler.enable()
from _testcapi import set_nomemory, remove_mem_hooks
warnings.simplefilter("always")
for start in range(0, 600):
    try:
        set_nomemory(start, 0)
        warnings.warn(UserWarning("oom-%d" % start))   # Warning instance -> PyObject_Str branch
    except MemoryError:
        pass
    except BaseException:
        pass
    finally:
        try: remove_mem_hooks()
        except Exception: pass
print("done, no assert")

The fleet vehicle (vehicle_source.py, fuzzing zoneinfo._tzpath) instead hits the type_call branch (plain-string message). A plain-string minimal sweep segfaults elsewhere in the warnings path before reaching the exact type_call:2441 window, so that specific site is vehicle-confirmed; the root cause is the same and is reproduced deterministically by the PyObject_Str variant above.

Backtrace

Fleet vehicle (debug, SIGABRT) — type_call branch:

python: Objects/typeobject.c:2441: type_call: Assertion `!_PyErr_Occurred(tstate)' failed.
#8  type_call            Objects/typeobject.c:2441     # assert: an exception is already pending
#9  _PyObject_MakeTpCall  Objects/call.c:242
#10 PyObject_CallOneArg   Objects/call.c:395
#11 warn_explicit        Python/_warnings.c:806        # PyObject_CallOneArg(category, message)
#12 do_warn              Python/_warnings.c:1136

Minimal repro (debug, SIGABRT) — PyObject_Str branch:

python: Objects/object.c:818: PyObject_Str: Assertion `!_PyErr_Occurred(tstate)' failed.
#8  PyObject_Str         Objects/object.c:818
#9  warn_explicit        Python/_warnings.c:799        # text = PyObject_Str(message)

Root cause

Python/_warnings.c, warn_explicit (L796–809):

/* Normalize message. */
Py_INCREF(message);  /* DECREF'ed in cleanup. */
if (PyObject_TypeCheck(message, (PyTypeObject *)PyExc_Warning)) {
    text = PyObject_Str(message);            /* L799: asserts !_PyErr_Occurred */
    if (text == NULL)
        goto cleanup;
    category = (PyObject*)Py_TYPE(message);
}
else {
    text = message;
    message = PyObject_CallOneArg(category, message);  /* L806 -> type_call asserts !_PyErr_Occurred */
    if (message == NULL)
        goto cleanup;
}

At the crashing invocation a MemoryError is already set on entry to warn_explicit (nothing between the function entry and these calls sets one), so the defect is a missing pending-exception check on the OOM path before message normalization — either do_warn / warn_explicit should bail (or assert) when entered with an exception set, or an earlier caller is emitting a warning without clearing a failed allocation's MemoryError. Both type_call and PyObject_Str explicitly document that they must not be called with an exception set because they can clear it, losing the caller's exception.

Suggested fix

Guard the warnings emission path against a pre-existing exception under allocation failure: check _PyErr_Occurred(tstate) at the top of warn_explicit (or do_warn) and short-circuit / propagate rather than normalizing the message, and audit the callers that invoke the warnings machinery so they do not do so with a pending MemoryError. This removes both the type_call:2441 and PyObject_Str:818 aborts (same fix) and the corresponding release-build exception loss.

Notes

Found via OOM-injection fuzzing (_testcapi.set_nomemory) by the local systemd fleet; flagged oomNEW and surfaced by the native-backtrace ingest sweep over the 31-bug catalog. Member of the "stale/missing exception under OOM" assert family (cf. OOM-0008/0010/0011/0015), distinct site. Not OOM-0001 (do_warn:1139 Py_DECREF-of-freed segv) — though the same fleet vehicle is build-dependent: it aborts here (OOM-0032) on both debug builds (ft_debug_asan and jit, which is also Py_DEBUG) and segfaults in do_warn (OOM-0001) on the release builds (ft_release/upstream), a textbook one-vehicle-multiple-bugs case. Verified distinct: OOM-0001's own reducer SIGSEGVs at do_warn:1139 (Py_DECREF(filename), the setup_context over-decref) on the debug build too — same site on debug and release, never tripping this warn_explicit assert. Different function, different defect.

Versions

  • main (3.16.0a0), commit 15d7406. SIGABRT on both Py_DEBUG builds (ft_debug_asan and jit); the assert is compiled out on the release builds ft_release / upstream (latent — that vehicle segfaults via OOM-0001 there).

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

"""OOM-0032 minimal reproducer (stdlib only).
A warning emitted while an allocation failure (set_nomemory) is active drives the
warnings C path (do_warn -> warn_explicit) into message-normalization with a
MemoryError already pending. warn_explicit then normalizes the message with an
exception set, tripping the debug-only invariant assert `!_PyErr_Occurred(tstate)`:
- Warning-instance message -> PyObject_Str (Objects/object.c:818) [this repro]
- plain-string message -> type_call (Objects/typeobject.c:2441) [fleet vehicle]
Both are the same bug. Debug builds abort; on release the lost exception is a latent
invariant violation (the same vehicle then segfaults in do_warn, = OOM-0001).
"""
import warnings, faulthandler
faulthandler.enable()
from _testcapi import set_nomemory, remove_mem_hooks
warnings.simplefilter("always")
for start in range(0, 600):
try:
set_nomemory(start, 0)
warnings.warn(UserWarning("oom-%d" % start)) # Warning instance -> PyObject_Str branch
except MemoryError:
pass
except BaseException:
pass
finally:
try: remove_mem_hooks()
except Exception: pass
print("done, no assert")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment