This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author pablogsal
Recipients a-feld, deekay, pablogsal, tim.peters
Date 2020-04-20.16:06:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1587398762.52.0.711281838248.issue40312@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2020-04-20 16:06:02pablogsalsetrecipients: + pablogsal, tim.peters, a-feld, deekay
2020-04-20 16:06:02pablogsalsetmessageid: <1587398762.52.0.711281838248.issue40312@roundup.psfhosted.org>
2020-04-20 16:06:02pablogsallinkissue40312 messages
2020-04-20 16:06:02pablogsalcreate