mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
[3.13] gh-127582: Make object resurrection thread-safe for free threading. (GH-127612) (GH-127659)
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.
(cherry picked from commit f4f530804b
)
This commit is contained in:
parent
304111e967
commit
69bb1c6c41
6 changed files with 87 additions and 20 deletions
|
@ -1845,14 +1845,11 @@ free_monitoring_data(_PyCoMonitoringData *data)
|
|||
static void
|
||||
code_dealloc(PyCodeObject *co)
|
||||
{
|
||||
assert(Py_REFCNT(co) == 0);
|
||||
Py_SET_REFCNT(co, 1);
|
||||
_PyObject_ResurrectStart((PyObject *)co);
|
||||
notify_code_watchers(PY_CODE_EVENT_DESTROY, co);
|
||||
if (Py_REFCNT(co) > 1) {
|
||||
Py_SET_REFCNT(co, Py_REFCNT(co) - 1);
|
||||
if (_PyObject_ResurrectEnd((PyObject *)co)) {
|
||||
return;
|
||||
}
|
||||
Py_SET_REFCNT(co, 0);
|
||||
|
||||
#ifdef Py_GIL_DISABLED
|
||||
PyObject_GC_UnTrack(co);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue