Skip to content

Commit bc55838

Browse files
committed
pythonGH-97592: Anticipate fut_callbacks being cleared by PyObject_RichCompareBool
1 parent 9a11ed8 commit bc55838

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Modules/_asynciomodule.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,11 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn)
10521052
return NULL;
10531053
}
10541054

1055-
for (i = 0; i < PyList_GET_SIZE(self->fut_callbacks); i++) {
1055+
// Beware: PyObject_RichCompareBool below may change fut_callbacks.
1056+
// See GH-97592.
1057+
for (i = 0;
1058+
self->fut_callbacks != NULL && i < PyList_GET_SIZE(self->fut_callbacks);
1059+
i++) {
10561060
int ret;
10571061
PyObject *item = PyList_GET_ITEM(self->fut_callbacks, i);
10581062
Py_INCREF(item);
@@ -1071,7 +1075,8 @@ _asyncio_Future_remove_done_callback(FutureObj *self, PyObject *fn)
10711075
}
10721076
}
10731077

1074-
if (j == 0) {
1078+
// Note: fut_callbacks may have been cleared.
1079+
if (j == 0 || self->fut_callbacks == NULL) {
10751080
Py_CLEAR(self->fut_callbacks);
10761081
Py_DECREF(newlist);
10771082
return PyLong_FromSsize_t(len + cleared_callback0);

0 commit comments

Comments
 (0)