[Python-checkins] cpython (3.2): Issue #6056: Make multiprocessing use setblocking(True) on the sockets it uses.

richard.oudkerk python-checkins at python.org
Fri Jul 27 15:57:34 CEST 2012


http://hg.python.org/cpython/rev/290f04722be3
changeset:   78297:290f04722be3
branch:      3.2
parent:      78293:e18f5301f406
user:        Richard Oudkerk <shibturn at gmail.com>
date:        Fri Jul 27 14:06:11 2012 +0100
summary:
  Issue #6056: Make multiprocessing use setblocking(True) on the sockets it uses.

Original patch by J Derek Wilson.

files:
  Lib/multiprocessing/connection.py |   5 ++
  Lib/test/test_multiprocessing.py  |  36 ++++++++++++++++++-
  Misc/ACKS                         |   1 +
  Misc/NEWS                         |   3 +
  4 files changed, 44 insertions(+), 1 deletions(-)


diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -199,6 +199,8 @@
         '''
         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 @@
         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 @@
 
     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 @@
     '''
     family = address_type(address)
     with socket.socket( getattr(socket, family) ) as s:
+        s.setblocking(True)
         t = _init_timeout()
 
         while 1:
diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -2395,9 +2395,43 @@
         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]
 
 #
 #
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1021,6 +1021,7 @@
 Gerald S. Williams
 Frank Willison
 Greg V. Wilson
+J Derek Wilson
 Jody Winston
 Collin Winter
 Dik Winter
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,9 @@
 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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list