]> 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:30:50 +0000 (23:30 +0200)
committerCharles-François Natali <cf.natali@gmail.com>
Fri, 30 Aug 2013 21:30:50 +0000 (23:30 +0200)
Patch by A. Jesse Jiryu Davis.

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

index ef04cd360a4f5fcb7638a8b7b3edf1e705d540db..5a765f32083702f468f1b7c7591d1dbc64cc4401 100644 (file)
@@ -443,6 +443,29 @@ class ThreadTests(BaseTestCase):
         self.assertEqual(out, '')
         self.assertEqual(err, '')
 
+    @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.getcheckinterval()
+
+        # Make the bug more likely to manifest.
+        sys.setcheckinterval(10)
+
+        try:
+            for i in range(20):
+                t = threading.Thread(target=lambda: None)
+                t.start()
+                pid = os.fork()
+                if pid == 0:
+                    os._exit(1 if t.is_alive() else 0)
+                else:
+                    t.join()
+                    pid, status = os.waitpid(pid, 0)
+                    self.assertEqual(0, status)
+        finally:
+            sys.setcheckinterval(old_interval)
+
 
 class ThreadJoinOnShutdown(BaseTestCase):
 
index 225448bf561fda8f4ff081af48ec5964fb38e039..72c83197fd1cf4b901ba9163f18f910507a5a304 100644 (file)
@@ -1220,7 +1220,7 @@ def _after_fork():
     new_active = {}
     current = current_thread()
     with _active_limbo_lock:
-        for thread in _active.itervalues():
+        for thread in _enumerate():
             # Any lock/condition variable may be currently locked or in an
             # invalid state, so we reinitialize them.
             if hasattr(thread, '_reset_internal_locks'):
index 6a063966313518fc269400df783f95f9d993b615..8977c02fa28a985c92b48bd92b8bdc6dc040924d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -230,6 +230,7 @@ Scott David Daniels
 Ben Darnell
 Kushal Das
 Jonathan Dasteel
+A. Jesse Jiryu Davis
 John DeGood
 Ned Deily
 Vincent Delft
index f7e9372316540aaaee2ff6d7e19648ac6ad67bb3..9544b3b913563e0ae0fcf5bf5dc5fa2fa87d25a2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #18418: After fork(), reinit all threads states, not only active ones.
+  Patch by A. Jesse Jiryu Davis.
+
 - Issue #11973: Fix a problem in kevent. The flags and fflags fields are now
   properly handled as unsigned.