[Python-Dev] Making weakref callbacks safe in cyclic gc

Neil Schemenauer nas-python at python.ca
Mon Nov 17 18:35:02 EST 2003

On Mon, Nov 17, 2003 at 05:57:06PM -0500, Tim Peters wrote:
> That's the primary reason I was loathe to run __del__ methods in
> an arbitrary order:  horrid order-dependent bugs can easily escape
> non-exhaustive testing

Very good point.  I had forgotten about that issue.

> For that reason, I'm growing increasingly fond of the idea of clearing the
> trash weakrefs first.  If no callbacks get invoked, the order they're not
> invoked in probably doesn't matter <wink>.  The technical hangup with that
> one right now is that clearing a weakref decrefs the callback, which can
> make the callback object die, and the callback object can itself have a
> weakref (with a different callback) pointing to *it*.  In that case,
> arbitrary Python code gets executed during gc, and in an arbitrary order
> again.  There must be a hack to worm around that.

A hack you say?  Create a list the references itself (i.e. append
itself).  Append all the unreachable callbacks to it and remove them
from the weakrefs.  Put the list in the youngest generation.  The
next gc should clean it up.


More information about the Python-Dev mailing list