]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-111777: Fix assertion errors on incorrectly still-tracked GC object destruc...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 12 Nov 2023 00:44:24 +0000 (01:44 +0100)
committerGitHub <noreply@github.com>
Sun, 12 Nov 2023 00:44:24 +0000 (00:44 +0000)
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 ce6a533c4bf1afa3775dfcaee5fc7d5c15a4af8c)

Co-authored-by: T. Wouters <thomas@python.org>
Modules/gcmodule.c

index 26ddcdd538a4d4a3efbf9b899cc19127b319a850..149a6a022d08cea9bbfca4c910f005f5a3d75c17 100644 (file)
@@ -2381,14 +2381,16 @@ PyObject_GC_Del(void *op)
     size_t presize = _PyType_PreHeaderSize(((PyObject *)op)->ob_type);
     PyGC_Head *g = AS_GC(op);
     if (_PyObject_GC_IS_TRACKED(op)) {
+        gc_list_remove(g);
 #ifdef Py_DEBUG
+        PyObject *exc = PyErr_GetRaisedException();
         if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0,
                                      "gc", NULL, "Object of type %s is not untracked before destruction",
                                      ((PyObject*)op)->ob_type->tp_name)) {
             PyErr_WriteUnraisable(NULL);
         }
+        PyErr_SetRaisedException(exc);
 #endif
-        gc_list_remove(g);
     }
     GCState *gcstate = get_gc_state();
     if (gcstate->generations[0].count > 0) {