]> 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:05:46 +0000 (14:05 +0100)
committerRichard Oudkerk <shibturn@gmail.com>
Fri, 27 Jul 2012 13:05:46 +0000 (14:05 +0100)
Original patch by J Derek Wilson.

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

index a02793ff26b8ffc90a8c459990dcc42cc7b8cc19..4421ac5cfd7a274279e489d3dd43603e0cfbb897 100644 (file)
@@ -186,6 +186,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()
@@ -251,6 +253,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()
@@ -269,6 +272,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()
@@ -286,6 +290,7 @@ def SocketClient(address):
     '''
     family = address_type(address)
     s = socket.socket( getattr(socket, family) )
+    s.setblocking(True)
     t = _init_timeout()
 
     while 1:
index 3937dc75b972c68de507aa787513214c9ce89552..bc2c048b681d1c603ab453f548f955a12ca704b8 100644 (file)
@@ -2315,8 +2315,42 @@ class TestStdinBadfiledescriptor(unittest.TestCase):
         flike.flush()
         assert sio.getvalue() == 'foo'
 
+#
+# 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]
+                   TestStdinBadfiledescriptor, TestTimeouts]
 
 #
 #
index 75659f03249471754f5be38ea22a322b7f17a618..cdea735e2432116b64bb0ef89c259533633012a8 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -931,6 +931,7 @@ Sue Williams
 Gerald S. Williams
 Frank Willison
 Greg V. Wilson
+J Derek Wilson
 Jody Winston
 Collin Winter
 Dik Winter
index b0f77a0a7f11a7c4eeaa47dc5f4f57b9c8ae35f7..c2594c26424362c1c4f983dd6806dc937e3c4676 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -129,6 +129,9 @@ Library
 - Issue #14653: email.utils.mktime_tz() no longer relies on system
   mktime() when timezone offest is supplied.
 
+- Issue #6056: Make multiprocessing use setblocking(True) on the
+  sockets it uses.  Original patch by J Derek Wilson.
+
 - Issue #15101: Make pool finalizer avoid joining current thread.
 
 - Issue #15054: A bug in tokenize.tokenize that caused string literals