From da7c0c3df820ed9505fb3f6af7ba925d0cb7c6f7 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sun, 15 Nov 2026 03:34:25 +0500 Subject: [PATCH] [mypyc] Fix race in the __weakref__ getter on free-threaded builds The __weakref__ getter of native classes read the head of the weak reference list and increfed it without synchronization. On a free-threaded build, another thread could free the head in between. CPython fixed the same race in subtype_getweakref by holding its internal weak reference list lock, which extensions can't take. On free-threaded builds, call CPython's own getter instead, which we get from the __weakref__ descriptor of a class created with type(). --- mypyc/lib-rt/misc_ops.c | 48 ++++++++++++++++++++++++++++++ mypyc/test-data/run-threading.test | 41 +++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/mypyc/lib-rt/misc_ops.c b/mypyc/lib-rt/misc_ops.c index d0111e861db91..9f1a519e7a447 100644 --- a/mypyc/lib-rt/misc_ops.c +++ b/mypyc/lib-rt/misc_ops.c @@ -1038,6 +1038,42 @@ PyObject *CPySingledispatch_RegisterFunction(PyObject *singledispatch_func, } +#ifdef Py_GIL_DISABLED +// The '__weakref__' descriptor of a class defined in Python, which wraps CPython's +// own getter (subtype_getweakref in typeobject.c). Created on first use. +static PyObject *CPy_SubtypeWeakrefDescr = NULL; + +static PyGetSetDef *CPy_GetSubtypeWeakrefGetSet(void) { + PyObject *descr = _Py_atomic_load_ptr_acquire(&CPy_SubtypeWeakrefDescr); + if (descr == NULL) { + // Don't hold a lock here, since creating the class can run arbitrary code + // (through the GC) that may reenter this function. + PyObject *cls = PyObject_CallFunction( + (PyObject *)&PyType_Type, "s(){}", "_CPyWeakrefGetter"); + if (cls == NULL) { + return NULL; + } + descr = PyObject_GetAttrString(cls, "__weakref__"); + Py_DECREF(cls); // The descriptor keeps the class alive + if (descr == NULL) { + return NULL; + } + if (!Py_IS_TYPE(descr, &PyGetSetDescr_Type)) { + Py_DECREF(descr); + PyErr_SetString(PyExc_SystemError, "unexpected __weakref__ descriptor type"); + return NULL; + } + PyObject *expected = NULL; + if (!_Py_atomic_compare_exchange_ptr(&CPy_SubtypeWeakrefDescr, &expected, descr)) { + // Another thread got there first + Py_DECREF(descr); + descr = expected; + } + } + return ((PyGetSetDescrObject *)descr)->d_getset; +} +#endif + // Implementation of the '__weakref__' getter of native classes that support weak // references. Adapted from subtype_getweakref in CPython's typeobject.c. PyObject *CPy_GetWeakrefs(PyObject *obj, void *closure) @@ -1047,11 +1083,23 @@ PyObject *CPy_GetWeakrefs(PyObject *obj, void *closure) PyErr_SetString(PyExc_AttributeError, "This object has no __weakref__"); return NULL; } +#ifdef Py_GIL_DISABLED + // Another thread may remove the head of the weak reference list and free it at + // any time, so the list may only be read while holding CPython's internal weak + // reference lock. Only CPython's own getter can take the lock, so use it. It + // works for any type, since it only uses tp_weaklistoffset. + PyGetSetDef *getset = CPy_GetSubtypeWeakrefGetSet(); + if (getset == NULL) { + return NULL; + } + return getset->get(obj, getset->closure); +#else PyObject **weaklistptr = (PyObject **)((char *)obj + offset); if (*weaklistptr == NULL) { return Py_NewRef(Py_None); } return Py_NewRef(*weaklistptr); +#endif } // Adapted from ceval.c GET_AITER diff --git a/mypyc/test-data/run-threading.test b/mypyc/test-data/run-threading.test index 7ae693ca40b7e..5b07d672dbdc4 100644 --- a/mypyc/test-data/run-threading.test +++ b/mypyc/test-data/run-threading.test @@ -613,3 +613,44 @@ for thread in threads: for thread in threads: thread.join() assert not errors, errors + +[case testConcurrentWeakrefGetter] +import threading +import weakref +from typing import Any +from mypy_extensions import mypyc_attr + +@mypyc_attr(weakrefs=True) +class C: + pass + +def churn(o: C, count: int) -> None: + # Each new weak reference becomes the head of the weak reference list, and it's + # freed right away + for _ in range(count): + weakref.ref(o, None) + weakref.ref(o, lambda r: None) + +def read(o: Any, count: int) -> None: + for _ in range(count): + # Note: mypy doesn't know about '__weakref__', so use getattr(). + r = getattr(o, "__weakref__") + assert r is None or isinstance(r, weakref.ref) + +def test_concurrent_weakref_getter() -> None: + # On a free-threaded build, reading __weakref__ races with other threads that + # free the head of the list + o = C() + count = 100_000 + threads = [ + threading.Thread(target=churn, args=(o, count)), + threading.Thread(target=churn, args=(o, count)), + threading.Thread(target=read, args=(o, count)), + threading.Thread(target=read, args=(o, count)), + ] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + r = weakref.ref(o) + assert r() is o