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

Select an option

Save devdanzin/2dfeabe7508f8e98f27f6df7e381f1cf to your computer and use it in GitHub Desktop.
OOM-0002: Segfault: `Py_DECREF(NULL)` in `PyContextVar_Set` (`context.c:367`)
# Authoritative gdb backtrace (3.16_ft_debug_asan, commit 15d7406)
# Faulting object: tok == NULL (op=0x0)
Program received signal SIGSEGV, Segmentation fault.
#0 _Py_atomic_load_uint32_relaxed (obj=0xc) at ./Include/cpython/pyatomic_gcc.h:367
#1 Py_DECREF (lineno=367, op=0x0, filename=...) at ./Include/refcount.h:345
#2 PyContextVar_Set (ovar=..., val=...) at Python/context.c:367
#3 _PyEval_EvalFrameDefault (tstate=..., frame=..., throwflag=...) at Python/generated_cases.c.h:4453

Segfault: Py_DECREF(NULL) in PyContextVar_Set (context.c:367)

PyContextVar_Set doesn't NULL-check token_new(); under OOM the contextvar_set() error path then Py_DECREFs the NULL token.

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

Crash report

PyContextVar_Set does not NULL-check the result of token_new(). Under memory pressure token_new() returns NULL; if the following contextvar_set() also fails, the error path runs Py_DECREF(tok) on the NULL token and segfaults.

Reproducer

import contextvars
from _testcapi import set_nomemory, remove_mem_hooks
cv = contextvars.ContextVar("x")
for start in range(16):            # crashes at start=2
    set_nomemory(start, 0)
    try:
        try:
            cv.set(object())
        finally:
            remove_mem_hooks()
    except MemoryError:
        pass

A single set_nomemory(0, 0); cv.set(object()) does not crash — PyContextVar_Set's own early context_get() fails first and returns cleanly. The crash needs a budget where the early allocations succeed but token_new() and contextvar_set() then fail, hence the short sweep.

Backtrace

#0 _Py_atomic_load_uint32_relaxed
#1 Py_DECREF              Include/refcount.h:345    (op == 0x0)
#2 PyContextVar_Set       Python/context.c:367      (Py_DECREF(tok), tok == NULL)

Root cause

Python/context.c, PyContextVar_Set (L346):

    PyContextToken *tok = token_new(ctx, var, old_val);  /* L363: may return NULL, UNCHECKED */
    Py_XDECREF(old_val);
    if (contextvar_set(var, val)) {                      /* L366: fails under OOM (_PyHamt_Assoc -> NULL) */
        Py_DECREF(tok);                                  /* L367: tok == NULL -> SIGSEGV */
        return NULL;
    }
    return (PyObject *)tok;

token_new() allocates (PyObject_GC_New) and can return NULL on failure, but tok is not checked before the Py_DECREF(tok) on the contextvar_set() failure path. (On the success path the function also returns tok unchecked — harmless NULL-with-exception, but the modification still happened.)

Suggested fix

    PyContextToken *tok = token_new(ctx, var, old_val);
    Py_XDECREF(old_val);
    if (tok == NULL) {
        return NULL;
    }

(or Py_XDECREF(tok) on the error path).

Notes

Found by OOM-injection fuzzing (set_nomemory). Same pattern as gh-146080 (Py_DECREF(NULL) in an _ssl.c error label). Not free-threading-specific: reproduces on free-threaded and default (GIL) builds, debug and release. (A _py_warnings/ContextVar vehicle reaches this only when context_aware_warnings is enabled — the default on free-threaded builds — but the C bug is build-agnostic.)

Versions

  • main (3.16.0a0); reproduced on free-threaded debug+ASan, free-threaded release, and GIL release builds.

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

"""
Minimal reproducer: Py_DECREF(NULL) segfault in PyContextVar_Set under OOM.
Affected: CPython 3.16.0a0 (main). Build-agnostic: reproduces on free-threaded
and default (GIL) builds, debug and release.
Crash: SIGSEGV in Py_DECREF(tok) with tok == NULL, Python/context.c:367.
Requires: a build exposing _testcapi.set_nomemory (debug/test builds).
Run:
python repro.py
# exits via SIGSEGV (rc 139, or rc 1 under ASan)
Backtrace (gdb):
#0 _Py_atomic_load_uint32_relaxed
#1 Py_DECREF Include/refcount.h:345 (op == 0x0)
#2 PyContextVar_Set Python/context.c:367 (Py_DECREF(tok), tok == NULL)
Root cause (Python/context.c, PyContextVar_Set, ~L346):
PyContextToken *tok = token_new(ctx, var, old_val); /* L363: may return NULL (OOM), UNCHECKED */
Py_XDECREF(old_val);
if (contextvar_set(var, val)) { /* L366: also fails under OOM */
Py_DECREF(tok); /* L367: tok == NULL -> SIGSEGV */
return NULL;
}
return (PyObject *)tok;
token_new() can return NULL under allocation failure, but its result is not
NULL-checked. The reproducer sweeps the OOM budget so that the earlier
context_get()/_PyHamt_Find succeed (start=0 alone exits cleanly), then token_new
returns NULL *and* contextvar_set() fails (its _PyHamt_Assoc returns NULL),
reaching Py_DECREF(NULL). Observed crash at start=2.
Likely fix: NULL-check tok after token_new (`if (tok == NULL) return NULL;`), or
use Py_XDECREF(tok) on the error path.
"""
import contextvars
from _testcapi import set_nomemory, remove_mem_hooks
cv = contextvars.ContextVar("x")
for start in range(16): # observed crash at start=2
set_nomemory(start, 0) # fail every allocation from #start onward
try:
try:
cv.set(object()) # token_new()==NULL (unchecked) + contextvar_set fail -> Py_DECREF(NULL)
finally:
remove_mem_hooks()
except MemoryError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment