Skip to content

Commit fa7e93a

Browse files
committed
gh-149816: #96 Fix race condition in invoke_gc_callback with free threading
1 parent 5775aa8 commit fa7e93a

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix race conditions in ``invoke_gc_callback`` iterating ``gc.callbacks``
2+
in free-threading mode.

Python/gc_free_threading.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY()
1010
#include "pycore_interp.h" // PyInterpreterState.gc
1111
#include "pycore_interpframe.h" // _PyFrame_GetLocalsArray()
12+
#include "pycore_list.h" // _PyList_GetItemRef()
1213
#include "pycore_object_alloc.h" // _PyObject_MallocWithType()
1314
#include "pycore_pystate.h" // _PyThreadState_GET()
1415
#include "pycore_tstate.h" // _PyThreadStateImpl
@@ -1940,33 +1941,33 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
19401941

19411942
/* The local variable cannot be rebound, check it for sanity */
19421943
assert(PyList_CheckExact(gcstate->callbacks));
1943-
PyObject *info = NULL;
1944-
if (PyList_GET_SIZE(gcstate->callbacks) != 0) {
1945-
info = Py_BuildValue("{sisnsnsnsd}",
1946-
"generation", generation,
1947-
"collected", collected,
1948-
"uncollectable", uncollectable,
1949-
"candidates", candidates,
1950-
"duration", duration);
1951-
if (info == NULL) {
1952-
PyErr_FormatUnraisable("Exception ignored while "
1953-
"invoking gc callbacks");
1954-
return;
1955-
}
1944+
1945+
PyObject *info = Py_BuildValue("{sisnsnsnsd}",
1946+
"generation", generation,
1947+
"collected", collected,
1948+
"uncollectable", uncollectable,
1949+
"candidates", candidates,
1950+
"duration", duration);
1951+
if (info == NULL) {
1952+
PyErr_FormatUnraisable("Exception ignored while "
1953+
"invoking gc callbacks");
1954+
return;
19561955
}
19571956

19581957
PyObject *phase_obj = PyUnicode_FromString(phase);
19591958
if (phase_obj == NULL) {
1960-
Py_XDECREF(info);
1959+
Py_DECREF(info);
19611960
PyErr_FormatUnraisable("Exception ignored while "
19621961
"invoking gc callbacks");
19631962
return;
19641963
}
19651964

19661965
PyObject *stack[] = {phase_obj, info};
19671966
for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
1968-
PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
1969-
Py_INCREF(cb); /* make sure cb doesn't go away */
1967+
PyObject *r, *cb = _PyList_GetItemRef((PyListObject *)gcstate->callbacks, i);
1968+
if (cb == NULL) {
1969+
break;
1970+
}
19701971
r = PyObject_Vectorcall(cb, stack, 2, NULL);
19711972
if (r == NULL) {
19721973
PyErr_FormatUnraisable("Exception ignored while "
@@ -1978,7 +1979,7 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
19781979
Py_DECREF(cb);
19791980
}
19801981
Py_DECREF(phase_obj);
1981-
Py_XDECREF(info);
1982+
Py_DECREF(info);
19821983
assert(!_PyErr_Occurred(tstate));
19831984
}
19841985

0 commit comments

Comments
 (0)