]> granicus.if.org Git - python/commitdiff
Issue #17555: Fix ForkAwareThreadLock so that size of after fork
authorRichard Oudkerk <shibturn@gmail.com>
Wed, 17 Apr 2013 19:58:00 +0000 (20:58 +0100)
committerRichard Oudkerk <shibturn@gmail.com>
Wed, 17 Apr 2013 19:58:00 +0000 (20:58 +0100)
registry does not grow exponentially with generation of process.

Lib/multiprocessing/util.py
Lib/test/test_multiprocessing.py
Misc/NEWS

index 72385a8fa3b01f1180417eaebe0ac0a88c924bba..f5862b49a4082f511e51a8a6676ac32e57c009f8 100644 (file)
@@ -322,10 +322,13 @@ atexit.register(_exit_function)
 
 class ForkAwareThreadLock(object):
     def __init__(self):
+        self._reset()
+        register_after_fork(self, ForkAwareThreadLock._reset)
+
+    def _reset(self):
         self._lock = threading.Lock()
         self.acquire = self._lock.acquire
         self.release = self._lock.release
-        register_after_fork(self, ForkAwareThreadLock.__init__)
 
 class ForkAwareLocal(threading.local):
     def __init__(self):
index 14ec61c39323bc4459a8caa850f9d4e1eb6e1faf..9b3e180e4c3e077f5f10e4e0f5ec3d61acfa0b95 100644 (file)
@@ -3431,13 +3431,43 @@ class TestNoForkBomb(unittest.TestCase):
             self.assertEqual('123', out.decode('ascii').rstrip())
             self.assertEqual('', err.decode('ascii'))
 
+#
+# Issue #17555: ForkAwareThreadLock
+#
+
+class TestForkAwareThreadLock(unittest.TestCase):
+    # We recurisvely start processes.  Issue #17555 meant that the
+    # after fork registry would get duplicate entries for the same
+    # lock.  The size of the registry at generation n was ~2**n.
+
+    @classmethod
+    def child(cls, n, conn):
+        if n > 1:
+            p = multiprocessing.Process(target=cls.child, args=(n-1, conn))
+            p.start()
+            p.join()
+        else:
+            conn.send(len(util._afterfork_registry))
+        conn.close()
+
+    def test_lock(self):
+        r, w = multiprocessing.Pipe(False)
+        l = util.ForkAwareThreadLock()
+        old_size = len(util._afterfork_registry)
+        p = multiprocessing.Process(target=self.child, args=(5, w))
+        p.start()
+        new_size = r.recv()
+        p.join()
+        self.assertLessEqual(new_size, old_size)
+
 #
 #
 #
 
 testcases_other = [OtherTest, TestInvalidHandle, TestInitializers,
                    TestStdinBadfiledescriptor, TestWait, TestInvalidFamily,
-                   TestFlags, TestTimeouts, TestNoForkBomb]
+                   TestFlags, TestTimeouts, TestNoForkBomb,
+                   TestForkAwareThreadLock]
 
 #
 #
index be59be013c43cdb101e45ccf0f200c007b7dd36e..5a8c0b9c2b40da55d3d8eca64744db6fa25087ca 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17555: Fix ForkAwareThreadLock so that size of after fork
+  registry does not grow exponentially with generation of process.
+
 - Issue #17707: multiprocessing.Queue's get() method does not block for short
   timeouts.