diff --git a/tes/sshwrapper/__init__.py b/tes/sshwrapper/__init__.py
index a38aa1fb5989715cb791a76eea53d1af60b927ab..45c3339e3d0bf8bb742af67a9a3090adc25374f1 100644
--- a/tes/sshwrapper/__init__.py
+++ b/tes/sshwrapper/__init__.py
@@ -113,8 +113,8 @@ class Ssh:
         import logging
         logger = logging.getLogger()
         logger.debug("enter get_ctrl_master_socket")
-        sess.lock.acquire()
         ctrlsocket = "/tmp/cm-{}-{}-{}".format(user,host,sess.sshsessid)
+        sess.lock.acquire()
         if Ssh.check_ctrlsocket(sess,ctrlsocket):
             sess.lock.release()
             return ctrlsocket
@@ -126,25 +126,29 @@ class Ssh:
         except FileNotFoundError as e:
             pass
 
-        sshcmd = Ssh.SSHCMDWITHOPTS.copy()
-        sshcmd.extend([
-                  "-S", ctrlsocket,
-                  "-M", '-o', 'ControlPersist=60s',
-                  '-p', sshport, '-N','-l', user, host])
-        env = os.environ.copy()
-        if sess.socket is None:
+        try:
+            sshcmd = Ssh.SSHCMDWITHOPTS.copy()
+            sshcmd.extend([
+                    "-S", ctrlsocket,
+                    "-M", '-o', 'ControlPersist=60s',
+                    '-p', sshport, '-N','-l', user, host])
+            env = os.environ.copy()
+            if sess.socket is None:
+                sess.lock.release()
+                logger.debug("Exception get_ctrl_master_socket")
+                raise SshAgentException("No ssh-agent yet")
+            env['SSH_AUTH_SOCK'] = sess.socket
+            logger.debug("creating master socket")
+            logger.debug("ssh_auth_sock {}".format(sess.socket))
+            logger.debug(" ".join(sshcmd))
+            ctrl_p = subprocess.Popen(sshcmd,
+                                    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+                                    stdin=None,
+                                    env=env)
+            (stderr,stdout) = ctrl_p.communicate() # The process should exit, while the mux persists
+        except Exception as e:
             sess.lock.release()
-            logger.debug("Exception get_ctrl_master_socket")
-            raise SshAgentException("No ssh-agent yet")
-        env['SSH_AUTH_SOCK'] = sess.socket
-        logger.debug("creating master socket")
-        logger.debug("ssh_auth_sock {}".format(sess.socket))
-        logger.debug(" ".join(sshcmd))
-        ctrl_p = subprocess.Popen(sshcmd,
-                                  stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                                  stdin=None,
-                                  env=env)
-        (stderr,stdout) = ctrl_p.communicate() # The process should exit, while the mux persists
+            raise e
         if ctrl_p.returncode == 0:
             sess.lock.release()
             return ctrlsocket
diff --git a/tes/tunnelstat/__init__.py b/tes/tunnelstat/__init__.py
index d8bdf106e26f228a37752882be9e36bec7dc030a..bddc41b55fc8839266bce9f6ab2602350fd770a2 100644
--- a/tes/tunnelstat/__init__.py
+++ b/tes/tunnelstat/__init__.py
@@ -64,8 +64,12 @@ class SSHSession:
         import logging
         logger = logging.getLogger()
         self.lock.acquire()
-        if self.socket is None:
-            self.start_agent()
+        try:
+            if self.socket is None:
+                self.start_agent()
+        except Exception as e:
+            self.lock.release()
+            raise e
         self.lock.release()
         keyf = tempfile.NamedTemporaryFile(mode='w',delete=False)
         keyname = keyf.name
@@ -206,8 +210,12 @@ class SSHSession:
         logger=logging.getLogger()
         env = os.environ.copy()
         sess.lock.acquire()
-        if sess.socket is None:
-            sess.start_agent()
+        try:
+            if sess.socket is None:
+                sess.start_agent()
+        except Exception as e:  
+            sess.lock.release()
+            raise e
         sess.lock.release()
         env['SSH_AUTH_SOCK'] = sess.socket
         cmd = [sess.sshadd,'-l']