summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryoav <yoav@excelero.com>2021-03-04 10:44:17 +0200
committerJeff Forcier <jeff@bitprophet.org>2022-06-03 19:23:45 -0400
commit7b8bb927e75c07caad6a00473ad25521486ed4d3 (patch)
tree8b52c81ddd51169df782603c3cf49f4d141709e8
parentdc2547d62b7dfe7e5eb917585e0436f2f3a65a46 (diff)
downloadparamiko-7b8bb927e75c07caad6a00473ad25521486ed4d3.tar.gz
Close socket on failure
As mentioned in socket docs: "Sockets are automatically closed when they are garbage-collected, but it is recommended to close() them explicitly, or to use a with statement around them." Resolve #1126
-rw-r--r--paramiko/client.py3
1 files changed, 3 insertions, 0 deletions
diff --git a/paramiko/client.py b/paramiko/client.py
index 80c956cd..ae70ac0b 100644
--- a/paramiko/client.py
+++ b/paramiko/client.py
@@ -350,6 +350,9 @@ class SSHClient(ClosingContextManager):
# Break out of the loop on success
break
except socket.error as e:
+ # As mentioned in socket docs - it is better to close sockets explicitly
+ if sock:
+ sock.close()
# Raise anything that isn't a straight up connection error
# (such as a resolution error)
if e.errno not in (ECONNREFUSED, EHOSTUNREACH):