Skip to content

Commit

Permalink
gh-115103: Update refleak checker to trigger _PyMem_ProcessDelayed
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Mar 2, 2024
1 parent 5dc8c84 commit c28c79f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Include/internal/pycore_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ def gc_collect():
gc.collect()
gc.collect()
gc.collect()
gc._collect_delayed_objects()

@contextlib.contextmanager
def disable_gc():
Expand Down
20 changes: 19 additions & 1 deletion Modules/clinic/gcmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ gc_collect_impl(PyObject *module, int generation)
return _PyGC_Collect(tstate, generation, _Py_GC_REASON_MANUAL);
}

/*[clinic input]
gc._collect_delayed_objects
Process delayed free requests by force
[clinic start generated code]*/

static PyObject *
gc__collect_delayed_objects_impl(PyObject *module)
/*[clinic end generated code: output=a016a10f967d4229 input=1064c31903cd9fac]*/
{
#ifdef Py_GIL_DISABLED
PyInterpreterState *interp = _PyInterpreterState_GET();
_PyGC_Clear_DelayedObjects(interp);
#endif
Py_RETURN_NONE;
}

/*[clinic input]
gc.set_debug
Expand Down Expand Up @@ -508,6 +526,7 @@ static PyMethodDef GcMethods[] = {
GC_FREEZE_METHODDEF
GC_UNFREEZE_METHODDEF
GC_GET_FREEZE_COUNT_METHODDEF
GC__COLLECT_DELAYED_OBJECTS_METHODDEF
{NULL, NULL} /* Sentinel */
};

Expand Down
12 changes: 12 additions & 0 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -1758,4 +1758,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

0 comments on commit c28c79f

Please sign in to comment.