]> granicus.if.org Git - python/commitdiff
Issue #18418: After fork(), reinit all threads states, not only active ones.
authorCharles-François Natali <cf.natali@gmail.com>
Fri, 30 Aug 2013 21:32:53 +0000 (23:32 +0200)
committerCharles-François Natali <cf.natali@gmail.com>
Fri, 30 Aug 2013 21:32:53 +0000 (23:32 +0200)
Patch by A. Jesse Jiryu Davis.

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

index 79967dc0cf62b239899dbf4b67ac07a8497b6d2a..bd019cddd32346c8b350b1368046800de126c805 100644 (file)
@@ -444,6 +444,27 @@ class ThreadTests(BaseTestCase):
         self.assertEqual(out, b'')
         self.assertEqual(err, b'')
 
+    @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
+    def test_is_alive_after_fork(self):
+        # Try hard to trigger #18418: is_alive() could sometimes be True on
+        # threads that vanished after a fork.
+        old_interval = sys.getswitchinterval()
+        self.addCleanup(sys.setswitchinterval, old_interval)
+
+        # Make the bug more likely to manifest.
+        sys.setswitchinterval(1e-6)
+
+        for i in range(20):
+            t = threading.Thread(target=lambda: None)
+            t.start()
+            self.addCleanup(t.join)
+            pid = os.fork()
+            if pid == 0:
+                os._exit(1 if t.is_alive() else 0)
+            else:
+                pid, status = os.waitpid(pid, 0)
+                self.assertEqual(0, status)
+
 
 class ThreadJoinOnShutdown(BaseTestCase):
 
index aaece74e097f025fdb480d6dc66a2fe2fc65990f..c98a0062314ad137976d7ac122a14f8301651208 100644 (file)
@@ -935,7 +935,7 @@ def _after_fork():
     new_active = {}
     current = current_thread()
     with _active_limbo_lock:
-        for thread in _active.values():
+        for thread in _enumerate():
             # Any lock/condition variable may be currently locked or in an
             # invalid state, so we reinitialize them.
             thread._reset_internal_locks()
index 9596db71d225a3a9027271a85f486eaf22604968..20eeafd03035b78e5d29ea42073ed6de41f7400e 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -279,6 +279,7 @@ Ben Darnell
 Kushal Das
 Jonathan Dasteel
 Pierre-Yves David
+A. Jesse Jiryu Davis
 John DeGood
 Ned Deily
 Vincent Delft
index 4572ae6c379f960966c17643681e23424779401c..d859de108012d0ed5bc84734e82fb5d4398c9523 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18418: After fork(), reinit all threads states, not only active ones.
+  Patch by A. Jesse Jiryu Davis.
+
 - Issue #16611: http.cookie now correctly parses the 'secure' and 'httponly'
   cookie flags.