Skip to content

Commit ce6a533

Browse files
authored
gh-111777: Fix assertion errors on incorrectly still-tracked GC object destruction (#111778)
In PyObject_GC_Del, in Py_DEBUG mode, when warning about GC objects that were not properly untracked before starting destruction, take care to untrack the object _before_ warning, to avoid triggering a GC run and causing the problem the code tries to warn about. Also make sure to save and restore any pending exceptions, which the warning would otherwise clobber or trigger an assertion error on.
1 parent 21615f7 commit ce6a533

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Modules/gcmodule.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -2396,14 +2396,16 @@ PyObject_GC_Del(void *op)
23962396
size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
23972397
PyGC_Head *g = AS_GC(op);
23982398
if (_PyObject_GC_IS_TRACKED(op)) {
2399+
gc_list_remove(g);
23992400
#ifdef Py_DEBUG
2401+
PyObject *exc = PyErr_GetRaisedException();
24002402
if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
24012403
"gc", NULL, "Object of type %s is not untracked before destruction",
24022404
((PyObject*)op)->ob_type->tp_name)) {
24032405
PyErr_WriteUnraisable(NULL);
24042406
}
2407+
PyErr_SetRaisedException(exc);
24052408
#endif
2406-
gc_list_remove(g);
24072409
}
24082410
GCState *gcstate = get_gc_state();
24092411
if (gcstate->generations[0].count > 0) {

0 commit comments

Comments
 (0)