Skip to content

Commit bd0ce2b

Browse files
committed
sshdriver: 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 f9ca024 commit bd0ce2b

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

labgrid/driver/sshdriver.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ def _start_own_master_once(self, timeout):
136136

137137
try:
138138
subprocess_timeout = timeout + 5
139-
return_value = self.process.wait(timeout=subprocess_timeout)
140-
if return_value != 0:
141-
stdout, _ = self.process.communicate(timeout=subprocess_timeout)
139+
stdout, _ = self.process.communicate(timeout=subprocess_timeout)
140+
if self.process.returncode != 0:
142141
stdout = stdout.split(b"\n")
143142
for line in stdout:
144143
self.logger.warning("ssh: %s", line.rstrip().decode(encoding="utf-8", errors="replace"))
@@ -155,12 +154,14 @@ def _start_own_master_once(self, timeout):
155154
pass
156155

157156
raise ExecutionError(
158-
f"Failed to connect to {self.networkservice.address} with {' '.join(args)}: return code {return_value}", # pylint: disable=line-too-long
157+
f"Failed to connect to {self.networkservice.address} with {' '.join(args)}: return code {self.process.returncode}", # pylint: disable=line-too-long
159158
stdout=stdout,
160159
)
161160
except subprocess.TimeoutExpired:
161+
self.process.kill()
162+
stdout, _ = self.process.communicate()
162163
raise ExecutionError(
163-
f"Subprocess timed out [{subprocess_timeout}s] while executing {args}",
164+
f"Subprocess timed out [{subprocess_timeout}s] while executing {args}: {stdout}",
164165
)
165166
finally:
166167
if self.networkservice.password and os.path.exists(pass_file):

0 commit comments

Comments
 (0)