[issue40312] Weakref callbacks running before finalizers in GC collection

Tim Peters report at bugs.python.org
Mon Apr 20 12:32:36 EDT 2020


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

Allan, we don't (at least not knowingly) write tests that rely on order of end-of-life actions, because the _language_ defines nothing about the order.  So you can permute the order and it's unlikely any standard tests would fail.

The only reason your example "works" outside of cyclic gc is because, as already noted, CPython (the implementation you're using, not Python the language) primarily relies on reference counting.  That guarantees __del__ is called first, but that's not a language guarantee, it's a consequence of reference counting invoking __del__ IMMEDIATELY upon the refcount falling to 0.  The _language_ not only doesn't guarantee that, it doesn't even guarantee that __del__ will ever be called.

For CPython (this implementation), it's important that we don't introduce gratuitous breakage of programs that are "working" because of implementation details.  How do you know that no program currently relies on the implementation detail that cyclic gc happens to run callbacks before finalizers now?  CPython has done that for many years, and I personally wouldn't risk breaking "working" programs by flipping that order now, not without truly compelling reason (example: stopping a segfault would be compelling).

Pablo, as above, I'm inclined to leave things alone unless we can "prove" no current code could possibly be relying (even by accident) on that gc currently runs callbacks before finalizers.  Which may be the case!  I don't know ;-)

----------

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


More information about the Python-bugs-list mailing list