Repository navigation
Description
Crash report
The C implementation of decimal.localcontext() takes the current context as a borrowed reference and keeps using it across context_setattrs(). context_setattrs() calls PyObject_IsTrue() on the values of the traps and flags mappings, which runs arbitrary Python code. If that code replaces the current context (decimal.setcontext(...)), the old context loses its last reference and is freed. Back in localcontext(), self->global = Py_NewRef(global) then increments the refcount of a freed object, and the manager object keeps the dangling pointer.
Reproducer
import decimal
class Replace:
def __bool__(self):
# Called from context_setattrs() while localcontext() still only
# borrows the current context. Drop the last reference to it.
decimal.setcontext(decimal.Context())
return True
traps = dict(decimal.getcontext().traps)
traps[decimal.Inexact] = Replace()
manager = decimal.localcontext(traps=traps) # uses the freed context here
with manager:
pass
print("no crash")Output
3.15.2, built from the official tarball with --with-address-sanitizer and the bundled libmpdecimal:
ERROR: AddressSanitizer: heap-use-after-free
READ of size 4 at 0x50c00002e0d0 thread T0
#0 Py_INCREF Include/refcount.h:286:31
#1 _Py_NewRef Include/refcount.h:549:5
#2 _decimal_localcontext_impl Modules/_decimal/_decimal.c:2116:20
#3 _decimal_localcontext Modules/_decimal/clinic/_decimal.c.h:586:20
...
0x50c00002e0d0 is located 16 bytes inside of 120-byte region [0x50c00002e0c0,0x50c00002e138)
freed by thread T0 here:
#1 context_dealloc Modules/_decimal/_decimal.c:1527:5
#2 _Py_Dealloc Objects/object.c:3324:5
#3 Py_DECREF Include/refcount.h:427:9
#4 token_tp_clear Python/context.c:1228:5
...
The same report comes from 3.13.18, 3.14.10, 3.16.0a2 and main (4b66b87), 3 runs each; on main the increment is at Modules/_decimal/_decimal.c:2170. 3.11.18 and 3.12.16 are not affected: the script finishes normally there under ASan (3 runs each), and also on an uninstrumented 3.12.3 (20 of 20 runs).
Without a sanitizer the script segfaulted in 10 of 10 runs on 3.13.5 and in 16 of 20 runs on 3.16.0a2.
Analysis
Modules/_decimal/_decimal.c, _decimal_localcontext_impl() (called ctxmanager_new() in 3.13 and 3.14), main:
decimal_state *state = get_module_state(module);
CURRENT_CONTEXT(state, global); /* borrowed: the macro drops the new reference */
...
PyObject *local_copy = context_copy(state, local);
...
int ret = context_setattrs(local_copy, prec, rounding,
Emin, Emax, capitals, clamp, flags, traps);
... /* PyObject_IsTrue() on the values can free `global` */
self->local = local_copy;
self->global = Py_NewRef(global); /* use-after-free */Up to 3.12 the manager took the reference before context_setattrs() ran, so the context stayed alive. 3.13 moved the PyDecContextManagerObject allocation after context_setattrs() and introduced the problem.
Possible fix
Hold a strong reference from the start, instead of the borrowed one:
@@ -2079,11 +2079,18 @@
PyObject *global;
decimal_state *state = get_module_state(module);
- CURRENT_CONTEXT(state, global);
+ /* Keep a strong reference: context_setattrs() below may run arbitrary
+ Python code (__bool__ on a traps/flags value) that replaces the
+ current context and drops the last reference to it. */
+ global = current_context(state);
+ if (global == NULL) {
+ return NULL;
+ }
if (local == Py_None) {
local = global;
}
else if (!PyDecContext_Check(state, local)) {
+ Py_DECREF(global);
PyErr_SetString(PyExc_TypeError,
"optional argument must be a context");
return NULL;
@@ -2091,6 +2098,7 @@
PyObject *local_copy = context_copy(state, local);
if (local_copy == NULL) {
+ Py_DECREF(global);
return NULL;
}
@@ -2100,6 +2108,7 @@
clamp, flags, traps
);
if (ret < 0) {
+ Py_DECREF(global);
Py_DECREF(local_copy);
return NULL;
}
@@ -2108,12 +2117,13 @@
self = PyObject_GC_New(PyDecContextManagerObject,
state->PyDecContextManager_Type);
if (self == NULL) {
+ Py_DECREF(global);
Py_DECREF(local_copy);
return NULL;
}
self->local = local_copy;
- self->global = Py_NewRef(global);
+ self->global = global; /* reference transferred */
PyObject_GC_Track(self);
return (PyObject *)self;I tested this on 3.15.2: the reproducer finishes normally (5 of 5 runs, no ASan report) and test_decimal passes.
CPython versions tested on:
3.11, 3.12, 3.13, 3.14, 3.15, 3.16, CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.15.2 (main, Nov 25 2026, 20:13:23) [Clang 18.1.3 (1ubuntu1)]
Activity
skirpichev commentedon Nov 26, 2026
Alternative solution, probably, is essentially return pre-fb0d9b9ac1e: allocate PyDecContextManagerObject before context_setattrs() call:
Would you like to make a PR?
xuziqiang98 commentedon Nov 26, 2026
yes, i will submit a PR