From d6df5c81fded03911c76f1ece2f3e35a1976d4bf Mon Sep 17 00:00:00 2001 From: Marc Bonnici Date: Mon, 20 Aug 2018 11:16:09 +0100 Subject: [PATCH] utils/ssh: Force connection to be closed if logout is unsuccessful If the connection is unavailable when attempting the logout procure it can fail leaving the connection open on the host. Now if something goes wrong ensure that we still close the connection. --- devlib/utils/ssh.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/devlib/utils/ssh.py b/devlib/utils/ssh.py index 5e1eb10..8d4088b 100644 --- a/devlib/utils/ssh.py +++ b/devlib/utils/ssh.py @@ -243,7 +243,11 @@ class SshConnection(object): def close(self): logger.debug('Logging out {}@{}'.format(self.username, self.host)) - self.conn.logout() + try: + self.conn.logout() + except: + logger.debug('Connection lost.') + self.conn.close(force=True) def cancel_running_command(self): # simulate impatiently hitting ^C until command prompt appears