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

Select an option

Save devdanzin/d40d39e6abfb746bd9d96de261205190 to your computer and use it in GitHub Desktop.
OOM-0015: Abort: stale exception in `cfunction_check_kwargs` (`methodobject.c:409`)
# Authoritative backtrace — free-threaded debug+ASan build
# (~/projects/3.16_ft_debug_asan_cpython/python), commit 15d7406.
# Representative vehicle: ~/crashers/python-4/sys-assertion-sigabrt/source.py
#
# Assertion message (from stdout):
# python: Objects/methodobject.c:409: int cfunction_check_kwargs(
# PyThreadState *, PyObject *, PyObject *):
# Assertion `!_PyErr_Occurred(tstate)' failed.
# Fatal Python error: Aborted
# (object type name: MemoryError — a MemoryError is live on the thread state)
#0 __pthread_kill_implementation ./nptl/pthread_kill.c:44
#1 __pthread_kill_internal ./nptl/pthread_kill.c:89
#2 __GI___pthread_kill ./nptl/pthread_kill.c:100
#3 __GI_raise ../sysdeps/posix/raise.c:26
#4 __GI_abort ./stdlib/abort.c:77
#5 __libc_message_impl ../sysdeps/posix/libc_fatal.c:138
#6 __libc_message_wrapper ../include/stdio.h:203
#7 __assert_fail ./assert/assert.c:37
#8 cfunction_check_kwargs Objects/methodobject.c:409 <- assert(!_PyErr_Occurred(tstate))
#9 cfunction_vectorcall_NOARGS Objects/methodobject.c:491
#10 _PyObject_VectorcallTstate Include/internal/pycore_call.h:144
#11 _Py_VectorCall_StackRefSteal Python/ceval.c:726
#12 _PyEval_EvalFrameDefault Python/generated_cases.c.h:4559 (CALL_NON_PY_GENERAL)
#13 _PyEval_EvalFrame Include/internal/pycore_ceval.h:122
#14 _PyEval_Vector Python/ceval.c:2141
# faulthandler Python stack (representative vehicle):
# File ".../source.py", line 402 in oom_call (func(*args, **kwargs))
# File ".../source.py", line 457 in <module>
# The fuzzed call at the crash is a NOARGS sys builtin; the python-7 vehicles
# pin it to sys._baserepl (the function that leaked the pending exception).
# ----------------------------------------------------------------------------
# Minimal stdlib repro hits the SAME root cause one frame earlier: the bad
# result is caught on _baserepl's own return by the debug result-checker
# (cfunction_vectorcall_NOARGS does not run _Py_CheckFunctionResult, but the
# enclosing _PyObject_VectorcallTstate does):
#
# Fatal Python error: _Py_CheckFunctionResult:
# a function returned a result with an exception set
# object type name: MemoryError
# Stack (most recent call first):
# File "repro.py", line 15 in <module> (sys._baserepl())
#
# Both assertions are the same defect: sys._baserepl returns None with a
# MemoryError still pending on the thread state.

Abort: stale exception in cfunction_check_kwargs (methodobject.c:409)

Under OOM, sys._baserepl() ignores PyRun_AnyFileExFlags's failure and returns None with a MemoryError still pending; the next NOARGS sys C-call trips assert(!_PyErr_Occurred(tstate)) at its entry.

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

Crash report

sys._baserepl() (sys__baserepl_impl, Python/sysmodule.c:2620) calls PyRun_AnyFileExFlags(stdin, ...), ignores its return value, and unconditionally does Py_RETURN_NONE. Under OOM, an allocation deep inside the interactive REPL/parse machinery fails and raises MemoryError, but the interactive loop's error handling does not guarantee a clean thread state on every OOM path. _baserepl then returns a valid None object while a MemoryError is still live on the thread state. The very next C-call boundary trips a debug-only "exception pending" assertion: in the fuzzer vehicles, assert(!_PyErr_Occurred(tstate)) in cfunction_check_kwargs (Objects/methodobject.c:409) at the entry of the following NOARGS sys builtin.

Reproducer

import sys
import _testcapi

_testcapi.set_nomemory(1, 0)   # fail every allocation from #1 onward
try:
    sys._baserepl()            # returns None with MemoryError still pending
    sys._clear_type_cache()    # next C call observes the leaked exception
finally:
    _testcapi.remove_mem_hooks()

Run with stdin at EOF: python repro.py < /dev/null. Deterministic at start=1 on the free-threaded debug+ASan build (and on the JIT build, which also has assertions enabled). The minimal repro aborts one frame earlier than the vehicles — the bad result is caught on _baserepl's own return by _Py_CheckFunctionResult (Fatal Python error: ... a function returned a result with an exception set, object type name: MemoryError). It is the identical defect; in the dense fuzzer sweep the leaked exception instead surfaces at the entry of the next NOARGS sys call and hits methodobject.c:409.

Backtrace

#8  cfunction_check_kwargs        Objects/methodobject.c:409   <- assert(!_PyErr_Occurred(tstate))
#9  cfunction_vectorcall_NOARGS   Objects/methodobject.c:491
#10 _PyObject_VectorcallTstate    Include/internal/pycore_call.h:144
#11 _Py_VectorCall_StackRefSteal  Python/ceval.c:726
#12 _PyEval_EvalFrameDefault      Python/generated_cases.c.h:4559   (CALL_NON_PY_GENERAL)

object type name: MemoryError in the stdout dump confirms a live MemoryError is pending when the NOARGS call enters. The faulting condition is a leaked exception, not a NULL/freed pointer.

Root cause

Python/sysmodule.c, sys__baserepl_impl() (L2620):

static PyObject *
sys__baserepl_impl(PyObject *module)
{
    PyCompilerFlags cf = _PyCompilerFlags_INIT;
    PyRun_AnyFileExFlags(stdin, "<stdin>", 0, &cf);   /* L2624: return value IGNORED */
    Py_RETURN_NONE;                                   /* L2625: always returns None */
}

PyRun_AnyFileExFlags -> _PyRun_AnyFileObject -> _PyRun_InteractiveLoopObject -> PyRun_InteractiveOneObjectEx -> pyrun_one_parse_ast / run_mod (Python/pythonrun.c) all allocate. Under OOM one of those allocations fails and sets MemoryError. The interactive loop (pythonrun.c:152-178) only sometimes clears the error (PyErr_Print() / PyErr_Clear()), and on EOF/OOM interleavings can return with the exception still set. Because _baserepl discards both the int return code and the pending exception and returns None, the interpreter is left in the illegal state "non-NULL result + exception set". The next C call asserts !_PyErr_Occurred(tstate) and aborts.

Suggested fix

Propagate the failure in sys__baserepl_impl instead of swallowing it:

    PyCompilerFlags cf = _PyCompilerFlags_INIT;
    if (PyRun_AnyFileExFlags(stdin, "<stdin>", 0, &cf) != 0) {
        /* An exception is (or should be) set; surface it to the caller. */
        if (!PyErr_Occurred()) {
            PyErr_SetNone(PyExc_RuntimeError);   /* defensive */
        }
        return NULL;
    }
    Py_RETURN_NONE;

Additionally harden _PyRun_InteractiveLoopObject (Python/pythonrun.c) so it never returns with a stale exception on the thread state (clear or print on every exit path, including the EOF/OOM paths). Either change alone closes the assertion; doing both is safest.

Notes

Found by OOM-injection fuzzing (set_nomemory). Not free-threading-specific: this is a generic exception-discard bug in sys._baserepl. It aborts on every assertions-enabled build — both the free-threaded debug+ASan build and the JIT build abort; the FT-release and upstream builds compile the assert out (-DNDEBUG) and instead let a normal MemoryError propagate cleanly (exit 1). Per the OOM-catalog convention for assert-based aborts, the non-assert builds are recorded as n/a.

Four fuzzer vehicles abort at the identical Objects/methodobject.c:409 assertion; the two python-7/sys-* vehicles pin the leak to sys._baserepl, and the asyncio_format_helpers vehicle shows the same leaked-exception state surfacing during a later traceback.walk_stack. The minimal stdlib repro hits the same defect one frame earlier (_Py_CheckFunctionResult on _baserepl's own return), which more directly identifies the culprit function. Minimization is therefore partial: the minimal repro is deterministic and stdlib-only but lands on the sibling assertion; the exact methodobject.c:409 frame is reproduced by the vehicle.

Versions

  • main (3.16.0a0, commit 15d7406); aborts on the free-threaded debug+ASan build and the JIT build. FT-release / upstream builds: assert compiled out, clean MemoryError (n/a).

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

"""
Minimal reproducer: abort under OOM because sys._baserepl() returns None
while leaving a MemoryError pending on the thread state.
Affected: CPython 3.16.0a0 (main), commit 15d7406.
Crash: SIGABRT on assertions-enabled builds.
- ft_debug_asan / jit: Fatal Python error: _Py_CheckFunctionResult
("a function returned a result with an exception set"),
object type name: MemoryError.
- fuzzer vehicles reach the equivalent assertion one frame later:
Objects/methodobject.c:409
Assertion `!_PyErr_Occurred(tstate)' failed.
in cfunction_check_kwargs(), on the *next* NOARGS sys call.
Requires: a debug / assertions build exposing _testcapi.set_nomemory.
Root cause (Python/sysmodule.c):
sys._baserepl -> sys__baserepl_impl() (sysmodule.c:2620):
PyCompilerFlags cf = _PyCompilerFlags_INIT;
PyRun_AnyFileExFlags(stdin, "<stdin>", 0, &cf); // L2624: result IGNORED
Py_RETURN_NONE; // L2625: always None
PyRun_AnyFileExFlags -> _PyRun_InteractiveLoopObject -> ... ->
pyrun_one_parse_ast / run_mod allocate freely. Under OOM one of those
allocations fails and a MemoryError is raised. The interactive loop's
error handling (PyErr_Print / PyErr_Clear, pythonrun.c:152-178) does not
guarantee a clean thread-state on every OOM path, and _baserepl discards
both the int return code AND the pending exception, returning None.
On a debug build the bad result is caught the moment _baserepl returns
(_Py_CheckFunctionResult, pycore_call.h:145). In the dense fuzzer sweep
the leak instead surfaces at the entry of the following NOARGS builtin,
tripping assert(!_PyErr_Occurred(tstate)) in cfunction_check_kwargs
(Objects/methodobject.c:409). Same defect, two adjacent assertions.
Likely fix: in sys__baserepl_impl, propagate failure:
if (PyRun_AnyFileExFlags(stdin, "<stdin>", 0, &cf) != 0) {
return NULL; // an exception is (or should be) set
}
Py_RETURN_NONE;
and/or ensure _PyRun_InteractiveLoopObject never returns with a stale
exception still set on the thread state.
The OOM sweep is needed so initialization succeeds (start small) while an
allocation deep inside the REPL/parse machinery fails. Deterministic at
start=1 on the FT debug+ASan build with stdin at EOF.
Self-sweeping: `python repro.py` runs the trigger under set_nomemory(N, 0) for N in a
sweep, each in a FRESH subprocess (a fresh process avoids cache warm-up shifting the OOM
window), and stops at the first N that crashes. Needs a debug build (the check is compiled
out under NDEBUG). The trigger needs stdin at EOF, so each child is fed /dev/null. Bare
trigger (fixed N=1):
import sys, _testcapi
_testcapi.set_nomemory(1, 0)
try:
sys._baserepl() # returns None with MemoryError still pending
sys._clear_type_cache() # next C call observes the leaked exception
finally:
_testcapi.remove_mem_hooks()
# run with stdin at EOF: python repro.py < /dev/null
"""
import os
import sys
import subprocess
TRIGGER = r"""
import sys
import _testcapi
_testcapi.set_nomemory({n}, 0) # fail every allocation from #{n} onward
try:
sys._baserepl() # returns None with MemoryError still pending
sys._clear_type_cache() # next C call observes the leaked exception
finally:
_testcapi.remove_mem_hooks()
"""
SIGNATURE = "a function returned a result with an exception set"
def main():
env = {**os.environ, "ASAN_OPTIONS": "detect_leaks=0:abort_on_error=0"}
# This bug is NOT free-threading-only; it aborts on every assertions build.
for n in range(80):
out = subprocess.run([sys.executable, "-c", TRIGGER.format(n=n)],
capture_output=True, text=True, env=env,
stdin=subprocess.DEVNULL)
if SIGNATURE in out.stdout + out.stderr:
print("reproduced at set_nomemory(%d, 0):" % n)
sys.stdout.write(out.stderr or out.stdout)
return 1
print("no crash in range(80); widen it for your build")
return 0
if __name__ == "__main__":
raise SystemExit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment