Skip to content

Commit d8f3503

Browse files
GH-115874: Fix segfault in FutureIter_dealloc (GH-117741)
1 parent 07525c9 commit d8f3503

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a possible segfault during garbage collection of ``_asyncio.FutureIter`` objects

Modules/_asynciomodule.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,11 +1601,25 @@ static void
16011601
FutureIter_dealloc(futureiterobject *it)
16021602
{
16031603
PyTypeObject *tp = Py_TYPE(it);
1604-
asyncio_state *state = get_asyncio_state_by_def((PyObject *)it);
1604+
1605+
// FutureIter is a heap type so any subclass must also be a heap type.
1606+
assert(_PyType_HasFeature(tp, Py_TPFLAGS_HEAPTYPE));
1607+
1608+
PyObject *module = ((PyHeapTypeObject*)tp)->ht_module;
1609+
asyncio_state *state = NULL;
1610+
16051611
PyObject_GC_UnTrack(it);
16061612
tp->tp_clear((PyObject *)it);
16071613

1608-
if (state->fi_freelist_len < FI_FREELIST_MAXLEN) {
1614+
// GH-115874: We can't use PyType_GetModuleByDef here as the type might have
1615+
// already been cleared, which is also why we must check if ht_module != NULL.
1616+
// Due to this restriction, subclasses that belong to a different module
1617+
// will not be able to use the free list.
1618+
if (module && _PyModule_GetDef(module) == &_asynciomodule) {
1619+
state = get_asyncio_state(module);
1620+
}
1621+
1622+
if (state && state->fi_freelist_len < FI_FREELIST_MAXLEN) {
16091623
state->fi_freelist_len++;
16101624
it->future = (FutureObj*) state->fi_freelist;
16111625
state->fi_freelist = it;

0 commit comments

Comments
 (0)