8000 [3.12] gh-111777: Fix assertion errors on incorrectly still-tracked G… · python/cpython@13ca1f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 13ca1f0

Browse files
[3.12] gh-111777: Fix assertion errors on incorrectly still-tracked GC object destruction (GH-111778) (#111989)
gh-111777: Fix assertion errors on incorrectly still-tracked GC object destruction (GH-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 <thomas@python.org>
1 parent 1afc4dc commit 13ca1f0

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
@@ -2381,14 +2381,16 @@ PyObject_GC_Del(void *op)
23812381
size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
23822382
PyGC_Head *g = AS_GC(op);
23832383
if (_PyObject_GC_IS_TRACKED(op)) {
2384+
gc_list_remove(g);
23842385
#ifdef Py_DEBUG
2386+
PyObject *exc = PyErr_GetRaisedException();
23852387
if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
23862388
"gc", NULL, "Object of type %s is not untracked before destruction",
23872389
((PyObject*)op)->ob_type->tp_name)) {
23882390
PyErr_WriteUnraisable(NULL);
23892391
}
2392+
PyErr_SetRaisedException(exc);
23902393
#endif
2391-
gc_list_remove(g);
23922394
}
23932395
GCState *gcstate = get_gc_state();
23942396
if (gcstate->generations[0].count > 0) {

0 commit comments

Comments
 (0)
0