Index: Lib/threading.py =================================================================== --- Lib/threading.py (Revision 52904) +++ Lib/threading.py (Arbeitskopie) @@ -636,13 +636,22 @@ _active_limbo_lock.acquire() _active[_get_ident()] = self _active_limbo_lock.release() - import atexit - atexit.register(self.__exitfunc) + import atexit, sys + # Don't register ourselves with atexit: + # __exitfunc *must* run before any atexit + # handlers, because we aren't really exiting + # until all threads have terminated. + # Instead, install a sys.exitfunc; this is + # likely the one that the atexit module + # installed + self.next_exit = sys.exitfunc + sys.exitfunc = self.__exitfunc def _set_daemon(self): return False def __exitfunc(self): + next_exit = self.next_exit self._Thread__stop() t = _pickSomeNonDaemonThread() if t: @@ -654,6 +663,8 @@ if __debug__: self._note("%s: exiting", self) self._Thread__delete() + # Invoke atexit handling + next_exit() def _pickSomeNonDaemonThread(): for t in enumerate():