Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions mypyc/lib-rt/misc_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions mypyc/test-data/run-threading.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading