From 728b59ad7e76f3cc0802d369ebdb1f9a727ca488 Mon Sep 17 00:00:00 2001
From: Douglas Raillard <douglas.raillard@arm.com>
Date: Thu, 18 Nov 2021 18:04:41 +0000
Subject: [PATCH] utils/ssh.py: Make SshConnection._background() more robust

Raise a better exception when e.g. sudo command is not found.
---
 devlib/utils/ssh.py | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/devlib/utils/ssh.py b/devlib/utils/ssh.py
index 3a92fb5..451792b 100644
--- a/devlib/utils/ssh.py
+++ b/devlib/utils/ssh.py
@@ -583,7 +583,20 @@ class SshConnection(SshConnectionBase):
             timeout=None,
             executor=executor,
         )
-        pid = int(stdout_in.readline())
+        pid = stdout_in.readline()
+        if not pid:
+            stderr = stderr_in.read()
+            if channel.exit_status_ready():
+                ret = channel.recv_exit_status()
+            else:
+                ret = 126
+            raise subprocess.CalledProcessError(
+                ret,
+                command,
+                b'',
+                stderr,
+            )
+        pid = int(pid)
 
         def create_out_stream(stream_in, stream_out):
             """