[issue40312] Weakref callbacks running before finalizers in GC collection

Pablo Galindo Salgado report at bugs.python.org
Mon Apr 20 12:06:02 EDT 2020


Pablo Galindo Salgado <pablogsal at gmail.com> added the comment:

One thing we could do is call the weakref callbacks *after* we call `finalize_garbage` and only on the "final_unreachable" set (the objects that do not resurrect). Notice that doing this still has one difference: the callback will be executed AFTER the finalizer while in the normal refcount path is executed BEFORE the finalizer.

I have not given enough thought to the correctness of doing this but my gut feeling tells me something can go wrong if we do that. What do you think about this path, Tim? 

Here is the diff I am refering to for clarity:

diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 5727820f09..498ff927ab 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -1252,9 +1252,6 @@ collect(PyThreadState *tstate, int generation,
         }
     }

-    /* Clear weakrefs and invoke callbacks as necessary. */
-    m += handle_weakrefs(&unreachable, old);
-
     validate_list(old, collecting_clear_unreachable_clear);
     validate_list(&unreachable, collecting_set_unreachable_clear);

@@ -1267,6 +1264,9 @@ collect(PyThreadState *tstate, int generation,
     PyGC_Head final_unreachable;
     handle_resurrected_objects(&unreachable, &final_unreachable, old);

+    /* Clear weakrefs and invoke callbacks as necessary. */
+    m += handle_weakrefs(&final_unreachable, old);
+
     /* Call tp_clear on objects in the final_unreachable set.  This will cause
     * the reference cycles to be broken.  It may also cause some objects
     * in finalizers to be freed.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40312>
_______________________________________


More information about the Python-bugs-list mailing list