Skip to content

Commit 95cd5c0

Browse files
committed
- Fix Issue #1703448: A joined thread could show up in the
threading.enumerate() list after the join() for a brief period until it actually exited.
1 parent 64c5677 commit 95cd5c0

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

Lib/test/test_threading.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ def waitingThread():
236236
"""])
237237
self.assertEqual(rc, 42)
238238

239+
def test_enumerate_after_join(self):
240+
# Try hard to trigger #1703448: a thread is still returned in
241+
# threading.enumerate() after it has been join()ed.
242+
enum = threading.enumerate
243+
old_interval = sys.getcheckinterval()
244+
sys.setcheckinterval(1)
245+
try:
246+
for i in xrange(1, 1000):
247+
t = threading.Thread(target=lambda: None)
248+
t.start()
249+
t.join()
250+
l = enum()
251+
self.assertFalse(t in l,
252+
"#1703448 triggered after %d trials: %s" % (i, l))
253+
finally:
254+
sys.setcheckinterval(old_interval)
255+
256+
239257
class ThreadingExceptionTests(unittest.TestCase):
240258
# A RuntimeError should be raised if Thread.start() is called
241259
# multiple times.

Lib/threading.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,14 @@ def __bootstrap_inner(self):
515515
if __debug__:
516516
self._note("%s.__bootstrap(): normal return", self)
517517
finally:
518-
self.__stop()
519-
try:
520-
self.__delete()
521-
except:
522-
pass
518+
with _active_limbo_lock:
519+
self.__stop()
520+
try:
521+
# We don't call self.__delete() because it also
522+
# grabs _active_limbo_lock.
523+
del _active[_get_ident()]
524+
except:
525+
pass
523526

524527
def __stop(self):
525528
with self.__block:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ Core and builtins
371371
- Issue #1537: Changed GeneratorExit's base class from Exception to
372372
BaseException.
373373

374+
- Fix Issue #1703448: A joined thread could show up in the
375+
threading.enumerate() list after the join() for a brief period until
376+
it actually exited.
377+
374378
Library
375379
-------
376380

0 commit comments

Comments
 (0)