From a5a92189a2715e2adf8cbb83b9407a169dfd4fb1 Mon Sep 17 00:00:00 2001 From: Michael Speck Date: Thu, 21 Sep 2023 08:47:44 +0200 Subject: [PATCH] fix stalled subprocess close It happens from time to time that subprocess close() stalls forever. I guess the reason is a raise conditions when terminating the process and closing the piped streams. A note in Python's subprocess documentation[1] suggests to use following code snipped to properly terminate/kill a subprocess: proc = subprocess.Popen(...) try: outs, errs = proc.communicate(timeout=15) except TimeoutExpired: proc.kill() outs, errs = proc.communicate() Reference: [1]: https://docs.python.org/3/library/subprocess.html --- tbot/machine/channel/subprocess.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tbot/machine/channel/subprocess.py b/tbot/machine/channel/subprocess.py index 8052ab09..df2f0e05 100644 --- a/tbot/machine/channel/subprocess.py +++ b/tbot/machine/channel/subprocess.py @@ -104,7 +104,11 @@ def close(self) -> None: self.p.terminate() os.close(self.pty_slave) os.close(self.pty_master) - self.p.wait() + try: + outs, errs = self.p.communicate(timeout=10) + except subprocess.TimeoutExpired: + self.p.kill() + outs, errs = self.p.communicate() # Wait for all processes in the session to end. Most of the time # this will return immediately, but in some cases (eg. a serial session