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

Greg Ewing greg at cosc.canterbury.ac.nz
Mon Nov 17 19:53:20 EST 2003

Tim Peters <tim at zope.com>:

> The other plausible idea is fixing the glitch with the simpler-at-first "do
> tp_clear on trash weakref objects first" idea.  The problem with that is
> that doing tp_clear on a weakref (or proxy) object ends up decref'ing the
> callback, and the callback may *itself* have a weak reference to it, so that
> decref'ing the callback triggers a different callback, and again arbitrary
> Python code starts running in the middle of gc.

If the second weakref is from inside the cycle, it's callback doesn't
need to be called, by the same reasoning that applies to the first

If the second weakref is from outside the cycle, its callback can't
reach anything inside the cycle by strong refs, otherwise the cycle
wouldn't be garbage. So calling its callback can safely be deferred
until after the cycle has been torn down.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+

More information about the Python-Dev mailing list