Skip to content

Commit

Permalink
fix stalled subprocess close
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mike8 committed Sep 21, 2023
1 parent 5f36a57 commit a5a9218
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tbot/machine/channel/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check warning on line 111 in tbot/machine/channel/subprocess.py

View check run for this annotation

Codecov / codecov/patch

tbot/machine/channel/subprocess.py#L109-L111

Added lines #L109 - L111 were not covered by tests

# 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
Expand Down

0 comments on commit a5a9218

Please sign in to comment.