From 145e2101dc39a701286bc51ddaa0dbd1a4cf022f Mon Sep 17 00:00:00 2001 From: pmikus Date: Fri, 30 Sep 2016 14:29:08 +0100 Subject: [PATCH] CSIT-176 Fix interactive SSH console deadlock Change-Id: Iba291d6781238c0e880534195ff65a7b3c4bede1 Signed-off-by: pmikus --- resources/libraries/python/ssh.py | 41 +++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/resources/libraries/python/ssh.py b/resources/libraries/python/ssh.py index 287ad31d65..8acb54b382 100644 --- a/resources/libraries/python/ssh.py +++ b/resources/libraries/python/ssh.py @@ -21,7 +21,6 @@ import paramiko from paramiko import RSAKey from paramiko.ssh_exception import SSHException from scp import SCPClient -from interruptingcow import timeout as icTimeout from robot.api import logger from robot.utils.asserts import assert_equal @@ -213,19 +212,23 @@ class SSH(object): chan.get_pty() chan.invoke_shell() chan.settimeout(int(time_out)) + chan.set_combine_stderr(True) buf = '' - try: - with icTimeout(time_out, exception=RuntimeError): - while not buf.endswith(':~$ '): - if chan.recv_ready(): - buf = chan.recv(4096) - except RuntimeError: - raise Exception('Open interactive terminal timeout.') + while not buf.endswith(':~$ '): + try: + chunk = chan.recv(self.__MAX_RECV_BUF) + if not chunk: + break + buf += chunk + if chan.exit_status_ready(): + logger.error('Channel exit status ready') + break + except socket.timeout: + raise Exception('Socket timeout: {0}'.format(buf)) return chan - @staticmethod - def interactive_terminal_exec_command(chan, cmd, prompt, + def interactive_terminal_exec_command(self, chan, cmd, prompt, time_out=30): """Execute command on interactive terminal. @@ -247,13 +250,17 @@ class SSH(object): """ chan.sendall('{c}\n'.format(c=cmd)) buf = '' - try: - with icTimeout(time_out, exception=RuntimeError): - while not buf.endswith(prompt): - if chan.recv_ready(): - buf += chan.recv(4096) - except RuntimeError: - raise Exception("Exec '{c}' timeout.".format(c=cmd)) + while not buf.endswith(prompt): + try: + chunk = chan.recv(self.__MAX_RECV_BUF) + if not chunk: + break + buf += chunk + if chan.exit_status_ready(): + logger.error('Channel exit status ready') + break + except socket.timeout: + raise Exception('Socket timeout: {0}'.format(buf)) tmp = buf.replace(cmd.replace('\n', ''), '') return tmp.replace(prompt, '') -- 2.16.6