From c565d66e390f613f776146665bebbe63a163b0e2 Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Mon, 8 Mar 2004 09:47:47 +0000 Subject: [PATCH] [project @ Arch-1:robey@lag.net--2003-public%secsh--dev--1.0--patch-28] fix lingering thread bug this bug has been in there forever and i could never figure out a workaround till now. when the python interpreter exits, it doesn't necessarily destroy the remaining objects or call __del__ on anything, and it will lock up until all threads finish running. how the threads are supposed to notice the exiting interpreter has always been sort of a mystery to me. tonight i figured out how to use the 'atexit' module to register a handler that runs when the interpreter exits. now we keep a list of active threads and ask them all to exit on shutdown. no more going to another shell to kill -9 python! yeah!! --- paramiko/transport.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/paramiko/transport.py b/paramiko/transport.py index 69669aa..ea62322 100644 --- a/paramiko/transport.py +++ b/paramiko/transport.py @@ -76,6 +76,15 @@ except: randpool.randomize() +# for thread cleanup +_active_threads = [] +def _join_lingering_threads(): + for thr in _active_threads: + thr.active = False +import atexit +atexit.register(_join_lingering_threads) + + class BaseTransport (threading.Thread): """ Handles protocol negotiation, key exchange, encryption, and the creation @@ -788,6 +797,7 @@ class BaseTransport (threading.Thread): def _run(self): self.active = True + _active_threads.append(self) try: # SSH-1.99-OpenSSH_2.9p2 self._write_all(self.local_version + '\r\n') @@ -838,6 +848,7 @@ class BaseTransport (threading.Thread): self._log(DEBUG, 'Unknown exception: ' + str(e)) self._log(DEBUG, tb_strings()) self.saved_exception = e + _active_threads.remove(self) if self.active: self.active = False if self.completion_event != None: