diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index edf74de1cc7..fe076b134f2 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -219,6 +219,25 @@ class ThreadTests(unittest.TestCase): t.join() # else the thread is still running, and we have no way to kill it + def test_limbo_cleanup(self): + # Issue 7481: Failure to start thread should cleanup the limbo map. + def fail_new_thread(*args): + raise thread.error() + _start_new_thread = threading._start_new_thread + threading._start_new_thread = fail_new_thread + try: + t = threading.Thread(target=lambda: None) + try: + t.start() + assert False + except thread.error: + self.assertFalse( + t in threading._limbo, + "Failed to cleanup _limbo map on failure of Thread.start()." + ) + finally: + threading._start_new_thread = _start_new_thread + def test_finalize_runnning_thread(self): # Issue 1402: the PyGILState_Ensure / _Release functions may be called # very late on python exit: on deallocation of a running thread for diff --git a/Lib/threading.py b/Lib/threading.py index 15c5f293155..ee70cd86334 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -470,7 +470,12 @@ class Thread(_Verbose): _active_limbo_lock.acquire() _limbo[self] = self _active_limbo_lock.release() - _start_new_thread(self.__bootstrap, ()) + try: + _start_new_thread(self.__bootstrap, ()) + except Exception: + with _active_limbo_lock: + del _limbo[self] + raise self.__started.wait() def run(self): diff --git a/Misc/NEWS b/Misc/NEWS index e68d1ce83e3..c2a12473949 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -232,6 +232,9 @@ Library - Issue #5037: Proxy the __unicode__ special method to __unicode__ instead of __str__. +- Issue #7481: When a threading.Thread failed to start it would leave the + instance stuck in initial state and present in threading.enumerate(). + Extension Modules -----------------