]> granicus.if.org Git - python/commitdiff
- Fix Issue #1703448: A joined thread could show up in the
authorGregory P. Smith <greg@mad-scientist.com>
Tue, 22 Jan 2008 01:20:42 +0000 (01:20 +0000)
committerGregory P. Smith <greg@mad-scientist.com>
Tue, 22 Jan 2008 01:20:42 +0000 (01:20 +0000)
  threading.enumerate() list after the join() for a brief period until
  it actually exited.

Lib/test/test_threading.py
Lib/threading.py
Misc/NEWS

index 9e2653604c26e186b5186710fb6e826a329a21a9..4f49d7f1cbae124c1ffa3b98a5a41392ba56cb53 100644 (file)
@@ -236,6 +236,24 @@ class ThreadTests(unittest.TestCase):
             """])
         self.assertEqual(rc, 42)
 
+    def test_enumerate_after_join(self):
+        # Try hard to trigger #1703448: a thread is still returned in
+        # threading.enumerate() after it has been join()ed.
+        enum = threading.enumerate
+        old_interval = sys.getcheckinterval()
+        sys.setcheckinterval(1)
+        try:
+            for i in xrange(1, 1000):
+                t = threading.Thread(target=lambda: None)
+                t.start()
+                t.join()
+                l = enum()
+                self.assertFalse(t in l,
+                    "#1703448 triggered after %d trials: %s" % (i, l))
+        finally:
+            sys.setcheckinterval(old_interval)
+
+
 class ThreadingExceptionTests(unittest.TestCase):
     # A RuntimeError should be raised if Thread.start() is called
     # multiple times.
index 98d15b240a9b2e27e0449d36522417d7b47b1da6..50cbb06fff9932ab1d4b91c79ce60db5896233a6 100644 (file)
@@ -515,11 +515,14 @@ class Thread(_Verbose):
                 if __debug__:
                     self._note("%s.__bootstrap(): normal return", self)
         finally:
-            self.__stop()
-            try:
-                self.__delete()
-            except:
-                pass
+            with _active_limbo_lock:
+                self.__stop()
+                try:
+                    # We don't call self.__delete() because it also
+                    # grabs _active_limbo_lock.
+                    del _active[_get_ident()]
+                except:
+                    pass
 
     def __stop(self):
         with self.__block:
index 2aabfbc9e34f5ea91550a5ff4f47ebe809c121b5..3b55d743c5bfe1066cf942863064c188b59554c1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -371,6 +371,10 @@ Core and builtins
 - Issue #1537: Changed GeneratorExit's base class from Exception to
   BaseException.
 
+- 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.
+
 Library
 -------