atexit + threads = bug?

David Rushby DavidRushby at gmail.com
Thu Jan 12 13:48:12 EST 2006


Consider the following program (underscores are used to force
indentation):
------------------------------------------------
import atexit, threading, time

def atExitFunc():
____print 'atExitFunc called.'

atexit.register(atExitFunc)

class T(threading.Thread):
____def run(self):
________assert not self.isDaemon()
________print 'T before sleep.'
________time.sleep(1.0)
________print 'T after sleep.'

T().start()
print 'Main thread finished.'
------------------------------------------------

I would expect the program to print 'atExitFunc called.' after 'T after
sleep.', but instead, it prints (on Windows XP with Python 2.3.5 or
2.4.2):
------------------------------------------------
T before sleep.
Main thread finished.
atExitFunc called.
T after sleep.
------------------------------------------------

atExitFunc is called when the main thread terminates, rather than when
the process exits.  The atexit documentation contains several warnings,
but nothing about this.  Is this a bug?




More information about the Python-list mailing list