gh-127582: Make object resurrection thread-safe for free threading. (GH-127612)

Objects may be temporarily "resurrected" in destructors when calling
finalizers or watcher callbacks. We previously undid the resurrection
by decrementing the reference count using `Py_SET_REFCNT`. This was not
thread-safe because other threads might be accessing the object
(modifying its reference count) if it was exposed by the finalizer,
watcher callback, or temporarily accessed by a racy dictionary or list
access.

This adds internal-only thread-safe functions for temporary object
resurrection during destructors.
This commit is contained in:
Sam Gross 2024-12-05 21:07:31 +00:00 committed by GitHub
parent 657d0e99aa
commit f4f530804b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 87 additions and 20 deletions

View file

@ -1092,14 +1092,11 @@ static void
func_dealloc(PyObject *self)
{
PyFunctionObject *op = _PyFunction_CAST(self);
assert(Py_REFCNT(op) == 0);
Py_SET_REFCNT(op, 1);
_PyObject_ResurrectStart(self);
handle_func_event(PyFunction_EVENT_DESTROY, op, NULL);
if (Py_REFCNT(op) > 1) {
Py_SET_REFCNT(op, Py_REFCNT(op) - 1);
if (_PyObject_ResurrectEnd(self)) {
return;
}
Py_SET_REFCNT(op, 0);
_PyObject_GC_UNTRACK(op);
if (op->func_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject *) op);