bkg() waits a fixed 20s for its command on exit and, on timeout, raises with the child still running, outliving the devices and namespaces the test tears down. Add a timeout argument for that wait, and kill the child when it expires before raising TimeoutExpired as before. Signed-off-by: Rishikesh Jethwani --- tools/testing/selftests/net/lib/py/utils.py | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/net/lib/py/utils.py b/tools/testing/selftests/net/lib/py/utils.py index 1727bc418073..7a3fc39db334 100644 --- a/tools/testing/selftests/net/lib/py/utils.py +++ b/tools/testing/selftests/net/lib/py/utils.py @@ -104,13 +104,32 @@ class cmd: def _process_terminate(self, terminate, timeout): if terminate: self.proc.terminate() - stdout, stderr = self.proc.communicate(timeout=timeout) + timed_out = None + try: + stdout, stderr = self.proc.communicate(timeout=timeout) + except subprocess.TimeoutExpired as exc: + # terminate() is only a request, and bkg() with exit_wait or + # ksft_wait never sends one, so kill rather than leave the child + # running. That does not stop a command run over ssh on the + # remote; it needs a bound of its own. + self.proc.kill() + try: + stdout, stderr = self.proc.communicate(timeout=1) + except subprocess.TimeoutExpired: + # A grandchild (shell=True, ssh) still holds the pipes. + self.proc.wait() + stdout, stderr = b"", b"" + timed_out = exc + self.stdout = stdout.decode("utf-8") self.stderr = stderr.decode("utf-8") self.proc.stdout.close() self.proc.stderr.close() self.ret = self.proc.returncode + if timed_out: + raise timed_out + return stdout, stderr def process(self, terminate=True, fail=None, expect_fail=False, timeout=20): @@ -179,11 +198,12 @@ class bkg(cmd): """ def __init__(self, comm, shell=None, fail=None, expect_fail=None, ns=None, host=None, exit_wait=False, ksft_ready=None, - ksft_wait=None): + ksft_wait=None, timeout=20): super().__init__(comm, background=True, shell=shell, fail=fail, expect_fail=expect_fail, ns=ns, host=host, ksft_ready=ksft_ready, - ksft_wait=ksft_wait) + ksft_wait=ksft_wait, timeout=timeout) + self.timeout = timeout self.terminate = not exit_wait and not ksft_wait self._exit_wait = exit_wait self.check_fail = fail @@ -203,7 +223,7 @@ class bkg(cmd): if self.proc.poll() is None: terminate = terminate or (self._exit_wait and ex_type is not None) return self.process(terminate=terminate, fail=self.check_fail, - expect_fail=self.expect_fail) + expect_fail=self.expect_fail, timeout=self.timeout) GLOBAL_DEFER_QUEUE = [] -- 2.50.1