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 a-feld
Recipients a-feld, deekay, pablogsal, tim.peters
Date 2020-04-20.17:55:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1587405313.81.0.911775883739.issue40312@roundup.psfhosted.org>
In-reply-to
Content
I definitely understand the possibility that some code is relying on the current gc behavior of weakref callbacks being invoked after finalizers.

That being said, the behavior is currently inconsistent between gc and reference counted paths. The language doesn't have to define the explicit ordering but the internal inconsistency was definitely unexpected for us.

The result is that behavioral consistency becomes more difficult in application code when using language provided structures such as WeakValueDictionary.

cache = weakref.WeakValueDictionary()

class Bar:
    pass

class Foo:
    def __init__(self):
        self._self = self
        self.value = Bar()
        cache[id(self.value)] = self.value

    def __del__(self):
        # the cache may or may not have self.value at this point
        # even though self.value is strongly referenced!
        print(list(cache.items()))


From the weakref docs:
> Entries in the dictionary will be discarded when no strong reference to the value exists any more.

But doesn't the code above imply that the entry is discarded even though there are strong references to the value?

In any case, I definitely appreciate all the eyes on this Tim + Pablo! At the very least, documentation updates do sound like a good idea if we're moving forward with leaving the behavior of weakrefs as currently specified. In particular, it would be worth pointing out that weakrefs callbacks can run even when the object is referenced.
History
Date User Action Args
2020-04-20 17:55:13a-feldsetrecipients: + a-feld, tim.peters, pablogsal, deekay
2020-04-20 17:55:13a-feldsetmessageid: <1587405313.81.0.911775883739.issue40312@roundup.psfhosted.org>
2020-04-20 17:55:13a-feldlinkissue40312 messages
2020-04-20 17:55:13a-feldcreate