diff --git a/Include/internal/pycore_gc.h b/Include/internal/pycore_gc.h index 40414a868518bb..dfce27a9c4c6fa 100644 --- a/Include/internal/pycore_gc.h +++ b/Include/internal/pycore_gc.h @@ -288,6 +288,9 @@ extern PyObject *_PyGC_GetReferrers(PyInterpreterState *interp, PyObject *objs); extern void _PyGC_ClearAllFreeLists(PyInterpreterState *interp); extern void _Py_ScheduleGC(PyThreadState *tstate); extern void _Py_RunGC(PyThreadState *tstate); +#ifdef Py_GIL_DISABLED +extern void _PyGC_Clear_DelayedObjects(PyInterpreterState *interp); +#endif #ifdef __cplusplus } diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index d4fb50106093ee..f0df1fb542a30d 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -1154,6 +1154,7 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason) } } + _PyGC_Clear_DelayedObjects(interp); /* Update stats */ struct gc_generation_stats *stats = &gcstate->generation_stats[generation]; stats->collections++; @@ -1758,4 +1759,16 @@ _PyGC_ClearAllFreeLists(PyInterpreterState *interp) HEAD_UNLOCK(&_PyRuntime); } +void +_PyGC_Clear_DelayedObjects(PyInterpreterState *interp) +{ + HEAD_LOCK(&_PyRuntime); + PyThreadState *tstate = interp->threads.head; + while (tstate != NULL) { + _PyMem_ProcessDelayed(tstate); + tstate = (PyThreadState *)tstate->next; + } + HEAD_UNLOCK(&_PyRuntime); +} + #endif // Py_GIL_DISABLED