]> granicus.if.org Git - python/commitdiff
Issue #6056: Make multiprocessing use setblocking(True) on the sockets it uses.
authorRichard Oudkerk <shibturn@gmail.com>
Fri, 27 Jul 2012 13:06:11 +0000 (14:06 +0100)
committerRichard Oudkerk <shibturn@gmail.com>
Fri, 27 Jul 2012 13:06:11 +0000 (14:06 +0100)
Original patch by J Derek Wilson.

Lib/multiprocessing/connection.py
Lib/test/test_multiprocessing.py
Misc/ACKS
Misc/NEWS

index f537a36ca43957d628b391e140608f3e448fa982..4fa6f70bf3f8193b11856e6fb658306badc7463f 100644 (file)
@@ -199,6 +199,8 @@ if sys.platform != 'win32':
         '''
         if duplex:
             s1, s2 = socket.socketpair()
+            s1.setblocking(True)
+            s2.setblocking(True)
             c1 = _multiprocessing.Connection(os.dup(s1.fileno()))
             c2 = _multiprocessing.Connection(os.dup(s2.fileno()))
             s1.close()
@@ -264,6 +266,7 @@ class SocketListener(object):
         self._socket = socket.socket(getattr(socket, family))
         try:
             self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+            self._socket.setblocking(True)
             self._socket.bind(address)
             self._socket.listen(backlog)
             self._address = self._socket.getsockname()
@@ -282,6 +285,7 @@ class SocketListener(object):
 
     def accept(self):
         s, self._last_accepted = self._socket.accept()
+        s.setblocking(True)
         fd = duplicate(s.fileno())
         conn = _multiprocessing.Connection(fd)
         s.close()
@@ -299,6 +303,7 @@ def SocketClient(address):
     '''
     family = address_type(address)
     with socket.socket( getattr(socket, family) ) as s:
+        s.setblocking(True)
         t = _init_timeout()
 
         while 1:
index b812e4840b830d0d36b13f904b4b3199f687ce40..847deb41b2a26d348d75ff1bf345cfb4ea7e7e65 100644 (file)
@@ -2395,9 +2395,43 @@ class TestInvalidFamily(unittest.TestCase):
         with self.assertRaises(ValueError):
             multiprocessing.connection.Listener('/var/test.pipe')
 
+#
+# Test interaction with socket timeouts - see Issue #6056
+#
+
+class TestTimeouts(unittest.TestCase):
+    @classmethod
+    def _test_timeout(cls, child, address):
+        time.sleep(1)
+        child.send(123)
+        child.close()
+        conn = multiprocessing.connection.Client(address)
+        conn.send(456)
+        conn.close()
+
+    def test_timeout(self):
+        old_timeout = socket.getdefaulttimeout()
+        try:
+            socket.setdefaulttimeout(0.1)
+            parent, child = multiprocessing.Pipe(duplex=True)
+            l = multiprocessing.connection.Listener(family='AF_INET')
+            p = multiprocessing.Process(target=self._test_timeout,
+                                        args=(child, l.address))
+            p.start()
+            child.close()
+            self.assertEqual(parent.recv(), 123)
+            parent.close()
+            conn = l.accept()
+            self.assertEqual(conn.recv(), 456)
+            conn.close()
+            l.close()
+            p.join(10)
+        finally:
+            socket.setdefaulttimeout(old_timeout)
 
 testcases_other = [OtherTest, TestInvalidHandle, TestInitializers,
-                   TestStdinBadfiledescriptor, TestInvalidFamily]
+                   TestStdinBadfiledescriptor, TestInvalidFamily,
+                   TestTimeouts]
 
 #
 #
index 51b5996e7734c592f8e7c50e1eca1d6ffaad6885..e04677ede1aa295bb78923fd50527ab39e0ce760 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1021,6 +1021,7 @@ Sue Williams
 Gerald S. Williams
 Frank Willison
 Greg V. Wilson
+J Derek Wilson
 Jody Winston
 Collin Winter
 Dik Winter
index eac19b528e8f4f9fdc728fbe38dcac1305caef2b..ab877cf21fd695109eace57bb549ba0330229f77 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #6056: Make multiprocessing use setblocking(True) on the
+  sockets it uses.  Original patch by J Derek Wilson.
+
 - Issue #15041: update "see also" list in tkinter documentation.
 
 - Issue #15402: An issue in the struct module that caused sys.getsizeof to