Skip to content

Commit 6742445

Browse files
[3.11] gh-111777: Fix assertion errors on incorrectly still-tracked GC object destruction (GH-111778) (GH-111990)
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 99a7bdc commit 6742445

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Modules/gcmodule.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -2347,14 +2347,18 @@ 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, *exc_value, *exc_tb;
2353+
PyErr_Fetch(&exc, &exc_value, &exc_tb);
23512354
if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
23522355
"gc", NULL, "Object of type %s is not untracked before destruction",
23532356
((PyObject*)op)->ob_type->tp_name)) {
23542357
PyErr_WriteUnraisable(NULL);
23552358
}
2359+
if (exc != NULL)
2360+
PyErr_Restore(exc, exc_value, exc_tb);
23562361
#endif
2357-
gc_list_remove(g);
23582362
}
23592363
GCState *gcstate = get_gc_state();
23602364
if (gcstate->generations[0].count > 0) {

0 commit comments

Comments
 (0)