Skip to content

Commit f9ca024

Browse files
committed
ssh: Prevent timeout from deadlock
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]>
1 parent 2dcc631 commit f9ca024

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

labgrid/util/ssh.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ def _start_own_master(self):
453453
)
454454

455455
try:
456-
if self._master.wait(timeout=connect_timeout) != 0:
457-
stdout, stderr = self._master.communicate()
456+
stdout, stderr = self._master.communicate(timeout=connect_timeout)
457+
if self._master.returncode != 0:
458458
raise ExecutionError(
459459
f"failed to connect to {self.host} with args {args}, returncode={self._master.returncode} {stdout},{stderr}" # pylint: disable=line-too-long
460460
)

0 commit comments

Comments
 (0)