[Python-Dev] More fun with Python shutdown

Tim Peters tim.one at comcast.net
Mon Nov 10 21:48:50 EST 2003


Jim (Fulton) refactored oodles of Zope3 to make heavier use of weak
references.  Now Zope3 dies with a segfault when it's shut down, which makes
its adoption of Python 2.3.2 a bit less attractive <wink>.

The problem isn't really understood.  I hope that once it is, there will be
a simple way to avoid it under 2.3.2.  Jim filed a bug report with a fix to
the symptom here:

    http://www.python.org/sf/839548

It's another case where things go crazy during the second call of
PyGC_Collect in Py_Finalize.  Alas, we haven't found a simpler failing test
case than "Zope3" yet.

For bafflement value, I'll give a cmdline-parameterized snippet here that
displays at least 4 distinct behaviors at shutdown, although a segfault
isn't one of them:

"""
import weakref
import os

class C(object):
    def hi(self, w=os.write):
        w(1, 'hi 1\n')
        print 'hi 2'

def pp(c=C()):
    c.hi()

import sys
exec "import %s as somemodule" % sys.argv[1] in globals()
del sys

somemodule.c1 = C()
somemodule.awr = weakref.ref(somemodule.c1, lambda ignore, pp=pp: pp())

del C, pp
"""

Here are the ways it behaves (on Windows, anyway):

C:\Code\python\PCbuild>python temp4.py tempfile
hi 1
hi 2

C:\Code\python\PCbuild>python temp4.py math # curiously, __main__ the same

C:\Code\python\PCbuild>python temp4.py __builtin__
hi 1

C:\Code\python\PCbuild>python temp4.py sys
hi 1
Exception exceptions.AttributeError: "'NoneType' object has no attribute
    'write'" in <function <lambda> at 0x006B6C70> ignored

C:\Code\python\PCbuild>

The only one I can't make any sense of is __builtin__:  the weakref callback
is certainly invoked then, but its print statement neither produces output
nor raises an exception.

Note that the exception in the "sys" example has nothing to do with the
"os.write" default-arg value.  That's really the print statement,
complaining because sys.stdout is None by the time shutdown gets there.




More information about the Python-Dev mailing list