[Python-checkins] r87686 - python/branches/py3k/Lib/multiprocessing/connection.py

victor.stinner python-checkins at python.org
Mon Jan 3 16:47:59 CET 2011


Author: victor.stinner
Date: Mon Jan  3 16:47:59 2011
New Revision: 87686

Log:
Issue #10816: multiprocessing.SocketClient() closes the socket on error

Use a context manager to close immediatly the socket on error.

Modified:
   python/branches/py3k/Lib/multiprocessing/connection.py

Modified: python/branches/py3k/Lib/multiprocessing/connection.py
==============================================================================
--- python/branches/py3k/Lib/multiprocessing/connection.py	(original)
+++ python/branches/py3k/Lib/multiprocessing/connection.py	Mon Jan  3 16:47:59 2011
@@ -281,25 +281,24 @@
     Return a connection object connected to the socket given by `address`
     '''
     family = address_type(address)
-    s = socket.socket( getattr(socket, family) )
-    t = _init_timeout()
+    with socket.socket( getattr(socket, family) ) as s:
+        t = _init_timeout()
 
-    while 1:
-        try:
-            s.connect(address)
-        except socket.error as e:
-            if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
-                debug('failed to connect to address %s', address)
-                raise
-            time.sleep(0.01)
+        while 1:
+            try:
+                s.connect(address)
+            except socket.error as e:
+                if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
+                    debug('failed to connect to address %s', address)
+                    raise
+                time.sleep(0.01)
+            else:
+                break
         else:
-            break
-    else:
-        raise
+            raise
 
-    fd = duplicate(s.fileno())
+        fd = duplicate(s.fileno())
     conn = _multiprocessing.Connection(fd)
-    s.close()
     return conn
 
 #


More information about the Python-checkins mailing list