[issue40312] Weakref callbacks running before finalizers in GC collection

Tim Peters report at bugs.python.org
Mon Apr 20 17:54:30 EDT 2020


Tim Peters <tim at python.org> added the comment:

A simple (finalizer-only) example of what an SCC-based DAG topsort ordering would accomplish:

    import gc

    class C:
        def __init__(self, val):
            self.val = val
        def __del__(self):
            print("finalizing", self.val)

    c, b, a = map(C, "cba")
    a.next = b
    b.next = c

    #a.loop = a
    del c, b, a
    gc.collect()

That finalizes in the order a, b, c.  Refcount semantics force that.  But, uncomment the "a.loop = a" line, and the order changes to c, b, a.  They all look exactly the same to gc, so it runs finalizers in the order they happen to appear in the list gc is crawling over.  A DAG topsort ordering would force a, b, c order again.

----------

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


More information about the Python-bugs-list mailing list