Skip to content

Commit dd5e2a9

Browse files
Yhg1smiss-islington
authored andcommitted
pythongh-111777: Fix assertion errors on incorrectly still-tracked GC object destruction (pythonGH-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. (cherry picked from commit ce6a533) Co-authored-by: T. Wouters <[email protected]>
1 parent cd3e2d3 commit dd5e2a9

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Modules/gcmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2347,14 +2347,16 @@ PyObject_GC_Del(void *op)
23472347
size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
23482348
PyGC_Head *g = AS_GC(op);
23492349
if (_PyObject_GC_IS_TRACKED(op)) {
2350+
gc_list_remove(g);
23502351
#ifdef Py_DEBUG
2352+
PyObject *exc = PyErr_GetRaisedException();
23512353
if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
23522354
"gc", NULL, "Object of type %s is not untracked before destruction",
23532355
((PyObject*)op)->ob_type->tp_name)) {
23542356
PyErr_WriteUnraisable(NULL);
23552357
}
2358+
PyErr_SetRaisedException(exc);
23562359
#endif
2357-
gc_list_remove(g);
23582360
}
23592361
GCState *gcstate = get_gc_state();
23602362
if (gcstate->generations[0].count > 0) {

0 commit comments

Comments
 (0)