From 7c5ff160ae4d5d573a2ee5c82023298f62ed6a10 Mon Sep 17 00:00:00 2001 From: Kurt Godwin Date: Thu, 16 Jun 2022 09:24:47 -0400 Subject: [PATCH] Cleanup socket on connection error/timeout --- pssh/clients/base/single.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pssh/clients/base/single.py b/pssh/clients/base/single.py index 56bc2c16..e7856e16 100644 --- a/pssh/clients/base/single.py +++ b/pssh/clients/base/single.py @@ -302,8 +302,12 @@ def _connect_socket(self, family, _type, proto, sock_addr, host, port, retries): try: self.sock.connect(sock_addr) except ConnectionRefusedError: + self.sock.shutdown(socket.SHUT_RDWR) + self.sock.close() raise except sock_error as ex: + self.sock.shutdown(socket.SHUT_RDWR) + self.sock.close() logger.error("Error connecting to host '%s:%s' - retry %s/%s", host, port, retries, self.num_retries) while retries < self.num_retries: