summaryrefslogtreecommitdiffstats
path: root/Lib/multiprocessing
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-01-03 15:47:59 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-01-03 15:47:59 (GMT)
commit2b69506c7997881fa7bad2bfd334db747f8cb820 (patch)
treec0d1999851e59b2502927f750930791b5260ddde /Lib/multiprocessing
parent90efac7f3794760b3a4d07171b90c86eeb4f2f81 (diff)
downloadcpython-2b69506c7997881fa7bad2bfd334db747f8cb820.zip
cpython-2b69506c7997881fa7bad2bfd334db747f8cb820.tar.gz
cpython-2b69506c7997881fa7bad2bfd334db747f8cb820.tar.bz2
Issue #10816: multiprocessing.SocketClient() closes the socket on error
Use a context manager to close immediatly the socket on error.
Diffstat (limited to 'Lib/multiprocessing')
-rw-r--r--Lib/multiprocessing/connection.py29
1 files changed, 14 insertions, 15 deletions
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
index 846d396..d6c23fb 100644
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -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
#