[Python-Dev] Re: More fun with Python shutdown

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Nov 11 19:13:53 EST 2003


Tim Peters <tim at zope.com>:

> If the destruction of a class instance then happened to trigger a
> weakref callback which in turn tried to access an attribute of the
> class, and the class had already been through its tp_clear, then a
> NULL-pointer dereference (due to the cleared tp_mro slot) would be
> unavoidable.

The crux of this seems to be that, now that we have weak references,
__del__ methods are not the only thing that can trigger execution of
arbitrary Python code when an object becomes unreferenced.

Maybe the GC should also refuse to collect cycles in which any member
is referenced by a weak reference with an associated callback?

The alternative is to accept that arbitrary Python code can be called
while the GC is in the midst of breaking a cycle.  In that case, it's
unacceptable for any object's tp_clear to set a Python pointer to
NULL, or do anything else that would render the object no longer a
valid Python object.

That would be enough to stop segfaults, but it still wouldn't entirely
solve the problem at hand, because the fact is there's no way to break
the self-cycle in a class's MRO without rendering it unusable as a
class object for at least some purposes.

Which makes me think that the only safe thing to do is treat a
weak-ref-with-callback as tantamount to a __del__ method for GC
purposes.

> But if that's what's happening, then tricks like the one on the table
> may not be enough to stop segfaults: replacing tp_mro with an empty
> tuple only "works" so long as the class object hasn't also been thru
> its tp_dealloc routine.

But that can't happen until the object's refcount has dropped to zero,
in which case it can't be touched any longer by Python code. I don't
think there's any worry with this.

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