[issue43383] imprecise handling of weakref callbacks

Mark Dickinson report at bugs.python.org
Wed Mar 3 11:39:26 EST 2021


Mark Dickinson <dickinsm at gmail.com> added the comment:

If I understand correctly, the reported bug is that you're seeing a weakref callback being executed after the weakref.ref instance it's attached to is deleted. Is that correct?

Do you have a minimal example we can use to reproduce the effect? Without such an example, there's not much of a realistic path to moving this forward.

Without the code: I can't see a plausible mechanism by which the callback could execute after the weakref.ref has ceased to exist.

However, my suspicion is that that's not what's actually happening here. I suspect that when you say "after the corresponding weak reference has been deleted with del", that the "del" statement you refer to is not actually deleting the last reference to the weakref.ref object, so the weakref still exists after the "del". One easy way that this could happen is if the weakref is part of a reference cycle (and I know from personal experience that it's horribly easy to accidentally *create* reference cycles via weakref callbacks, especially if those callbacks refer to instance methods).

Furthermore, if the weakref exists as part of a cycle and that cycle is being collected by the garbage collector, I could see how the callback could be sometimes executed and sometimes not depending on the exact order in which the cycle is cleaned up.

----------
nosy: +mark.dickinson, pitrou

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


More information about the Python-bugs-list mailing list