Skip to content

Commit

Permalink
sshdriver: Prevent timeout from deadlock
Browse files Browse the repository at this point in the history
Using Popen.wait() on a process that has output sent to a pipe can
potentially deadlock if the process produces enough output to fill the
pipe, since it will stall and never terminate waiting for the pipe to
have more space.

Instead, use Popen.communicate() with the timeout parameter. This will
consume all output until EOF (preventing the process from stalling due
to a full pipe), and then check the return code. In the event of a
timeout error, Popen.communicate() doesn't loose any data, so it's safe
to call it again after the Popen.kill() in the exception handler.

This likely was done this way because the timeout parameter was new in
Python 3.3, but this shouldn't be a concern anymore

Signed-off-by: Joshua Watt <[email protected]>
  • Loading branch information
JoshuaWatt committed Sep 7, 2023
1 parent f9ca024 commit f2247f2
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions labgrid/driver/sshdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ def _start_own_master_once(self, timeout):

try:
subprocess_timeout = timeout + 5
return_value = self.process.wait(timeout=subprocess_timeout)
if return_value != 0:
stdout, _ = self.process.communicate(timeout=subprocess_timeout)
stdout, _ = self.process.communicate(timeout=subprocess_timeout)
if self.process.returncode != 0:
stdout = stdout.split(b"\n")
for line in stdout:
self.logger.warning("ssh: %s", line.rstrip().decode(encoding="utf-8", errors="replace"))
Expand All @@ -159,8 +158,10 @@ def _start_own_master_once(self, timeout):
stdout=stdout,
)
except subprocess.TimeoutExpired:
self.process.kill()
stdout, _ = self.process.communicate()
raise ExecutionError(
f"Subprocess timed out [{subprocess_timeout}s] while executing {args}",
f"Subprocess timed out [{subprocess_timeout}s] while executing {args}: {stdout}",
)
finally:
if self.networkservice.password and os.path.exists(pass_file):
Expand Down

0 comments on commit f2247f2

Please sign in to comment.