You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2020/08/04 18:34:26 UTC

[libcloud] 05/06: Also try to close the connection before re-attempting to re-connect.

This is an automated email from the ASF dual-hosted git repository.

tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git

commit 109eeca34f9b113ac5697a37cc79da9ce4119a11
Author: Tomaz Muraus <to...@tomaz.me>
AuthorDate: Tue Aug 4 18:45:45 2020 +0200

    Also try to close the connection before re-attempting to re-connect.
    
    In most cases this won't be needed, but it's better we try to do that.
---
 libcloud/compute/base.py                 | 17 +++++++++++++----
 libcloud/test/compute/test_deployment.py |  3 ++-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/libcloud/compute/base.py b/libcloud/compute/base.py
index 9231ad4..9cc48be 100644
--- a/libcloud/compute/base.py
+++ b/libcloud/compute/base.py
@@ -1883,12 +1883,21 @@ class NodeDriver(BaseDriver):
 
                 if "ssh session not active" in str(e).lower():
                     # Sometimes connection gets closed or disconnected half
-                    # way through.
+                    # way through for wahtever reason.
                     # If this happens, we try to re-connect before
                     # re-attempting to run the step.
-                    timeout = int(ssh_client.timeout) if ssh_client.timeout else 10
-                    ssh_client = self._ssh_client_connect(ssh_client=ssh_client,
-                                                          timeout=timeout)
+                    try:
+                        ssh_client.close()
+                    except Exception:
+                        # Non fatal since connection is most likely already
+                        # closed at this point
+                        pass
+
+                    timeout = (int(ssh_client.timeout) if ssh_client.timeout
+                               else 10)
+                    ssh_client = self._ssh_client_connect(
+                        ssh_client=ssh_client,
+                        timeout=timeout)
 
                 if tries >= max_tries:
                     raise LibcloudError(value='Failed after %d tries: %s'
diff --git a/libcloud/test/compute/test_deployment.py b/libcloud/test/compute/test_deployment.py
index 0d7c53a..184128c 100644
--- a/libcloud/test/compute/test_deployment.py
+++ b/libcloud/test/compute/test_deployment.py
@@ -477,6 +477,7 @@ class DeploymentTests(unittest.TestCase):
         ssh_client.timeout = 20
 
         self.assertEqual(ssh_client.connect.call_count, 0)
+        self.assertEqual(ssh_client.close.call_count, 0)
 
         self.driver._run_deployment_script(task=task,
                                            node=self.node,
@@ -484,7 +485,7 @@ class DeploymentTests(unittest.TestCase):
                                            max_tries=5)
 
         self.assertEqual(ssh_client.connect.call_count, 2)
-
+        self.assertEqual(ssh_client.close.call_count, 2 + 1)
 
     @patch('libcloud.compute.base.SSHClient')
     @patch('libcloud.compute.ssh')