Issue 1240614: release lock on exception in threading.Thread.join() (original) (raw)

If an exception happens in threading.Thread.join(), the lock self.__block never gets released. The next time Thread.join() is called, it blocks forever. The only exception I can think of right now that triggers this error is KeyboardInterrupt, but there might be more.

The patch puts everything between self.__block.acquire() and self.__block.release() into a try ... finally block, and releases the lock in the finally clause.

A transcript of the error follows:

import threading def f(): ... while True: ... pass ... t = threading.Thread(target = f) t.setDaemon(True) def j(t): ... while True: ... print "trying to join" ... t.join(1) ... t.start() j(t) trying to join trying to join trying to join

<pressed ctrl+c here> Traceback (most recent call last): File "", line 1, in ? File "", line 4, in j File "/usr/lib/python2.4/threading.py", line 550, in join self.__block.wait(delay) File "/usr/lib/python2.4/threading.py", line 222, in wait _sleep(delay) KeyboardInterrupt

j(t) trying to join