diff --git a/labgrid/driver/sshdriver.py b/labgrid/driver/sshdriver.py index 3ad6fafa5..c6cda9942 100644 --- a/labgrid/driver/sshdriver.py +++ b/labgrid/driver/sshdriver.py @@ -507,16 +507,35 @@ def _check_keepalive(self): return self._keepalive.poll() is None def _stop_keepalive(self): + def log_output(name, output): + if not output: + return + + s = output.decode("utf-8", errors="ignore") + for line in s.splitlines(): + self.logger.warning("Keepalive %s %s: %s", self.networkservice.address, name, line) + assert self._keepalive is not None self.logger.debug('Stopping keepalive for %s', self.networkservice.address) + stdout = None + stderr = None + try: - self._keepalive.communicate(timeout=60) + stdout, stderr = self._keepalive.communicate(timeout=60) except subprocess.TimeoutExpired: self._keepalive.kill() - - try: - self._keepalive.wait(timeout=60) + try: + # Try again to get output + stdout, stderr = self._keepalive.communicate(timeout=60) + except subprocess.TimeoutExpired: + try: + self._keepalive.wait(timeout=60) + except subprocess.TimeoutExpired: + self.logger.warning("ssh keepalive for %s timed out during termination", self.networkservice.address) finally: self._keepalive = None + + log_output("stdout", stdout) + log_output("stderr", stderr)