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

Tim Peters tim.one at comcast.net
Tue Nov 18 01:28:16 EST 2003

There's a new version of the patch at:


trying to force the "do tp_clear() on trash weakrefs first" idea to work.

All the tests we've discussed here survive it fine (including the ones that
broke the first patch, and there are corresponding unittests for all these
cases in the new patch), but there are several combinations of extreme
endcase complication that haven't yet been tested (except in my head).

I haven't yet been able to convince myself that the following does or does
not have a slow memory leak after the patch (this can be hard to tell on
Win9x!  the system allocator is so strange):

import gc, weakref

def boom(ignore):
    print 'boom'

while 1:
    class C(object):
        def callback(self, ignore):

    class D(C):

    class E(object):
        def __del__(self):
            print 'del',

    c1, c2 = C(), D()
    c1.wr = weakref.ref(c2, c1.callback)
    c2.wr = weakref.ref(c1, c2.callback)
    c1.c = c2
    c2.c = c1
    C.objs = [c1, c2]
    C.wr = weakref.ref(D, boom)
    D.wr = weakref.ref(E, boom)
    C.E = E()
    print '.',
    assert gc.garbage == []

Try that under 2.3.2 instead, and it will eventually segfault -- but not as
soon as you expect!  It typically goes thru about 8 rounds of gc on my box
before it blows up -- it may be a memory corruption bug there.

More information about the Python-Dev mailing list