]> granicus.if.org Git - python/commitdiff
Issue #10816: multiprocessing.SocketClient() closes the socket on error
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 3 Jan 2011 15:47:59 +0000 (15:47 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 3 Jan 2011 15:47:59 +0000 (15:47 +0000)
Use a context manager to close immediatly the socket on error.

Lib/multiprocessing/connection.py

index 846d3964d653b7b07a545a3ddc3a77d6c6c11c4e..d6c23fb0ecdfe4da0c9b85afd0781696bf13dd28 100644 (file)
@@ -281,25 +281,24 @@ def SocketClient(address):
     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
 
 #