https://github.com/python/cpython/commit/67424458d23428d524e7c08e1556d7687297dbaa
commit: 67424458d23428d524e7c08e1556d7687297dbaa
branch: 3.11
author: Miss Islington (bot) <[email protected]>
committer: ambv <[email protected]>
date: 2024-01-17T15:10:12+01:00
summary:

[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 ce6a533c4bf1afa3775dfcaee5fc7d5c15a4af8c)

Co-authored-by: T. Wouters <[email protected]>

files:
M Modules/gcmodule.c

diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index dcd46feff0cc48..347ded80b7c18f 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -2347,14 +2347,18 @@ 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, *exc_value, *exc_tb;
+        PyErr_Fetch(&exc, &exc_value, &exc_tb);
         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);
         }
+        if (exc != NULL)
+            PyErr_Restore(exc, exc_value, exc_tb);
 #endif
-        gc_list_remove(g);
     }
     GCState *gcstate = get_gc_state();
     if (gcstate->generations[0].count > 0) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to