twister: Fix failure on MacOS

On MacOS, ps utils raises a `NoSuchProcess` error rather than a
`ProcessLookupError` when a pid no longer exists.

Signed-off-by: Wilfried Chauveau <wilfried.chauveau@arm.com>
This commit is contained in:
Wilfried Chauveau 2023-10-25 04:54:52 +01:00 committed by Carles Cufí
commit 2deea4eeee
2 changed files with 6 additions and 4 deletions

View file

@ -44,7 +44,7 @@ def terminate_process(proc: subprocess.Popen) -> None:
for child in psutil.Process(proc.pid).children(recursive=True): for child in psutil.Process(proc.pid).children(recursive=True):
try: try:
os.kill(child.pid, signal.SIGTERM) os.kill(child.pid, signal.SIGTERM)
except ProcessLookupError: except (ProcessLookupError, psutil.NoSuchProcess):
pass pass
proc.terminate() proc.terminate()
# sleep for a while before attempting to kill # sleep for a while before attempting to kill

View file

@ -56,7 +56,7 @@ def terminate_process(proc):
for child in psutil.Process(proc.pid).children(recursive=True): for child in psutil.Process(proc.pid).children(recursive=True):
try: try:
os.kill(child.pid, signal.SIGTERM) os.kill(child.pid, signal.SIGTERM)
except ProcessLookupError: except (ProcessLookupError, psutil.NoSuchProcess):
pass pass
proc.terminate() proc.terminate()
# sleep for a while before attempting to kill # sleep for a while before attempting to kill
@ -182,7 +182,7 @@ class BinaryHandler(Handler):
self.pid_fn = None # clear so we don't try to kill the binary twice self.pid_fn = None # clear so we don't try to kill the binary twice
try: try:
os.kill(pid, signal.SIGKILL) os.kill(pid, signal.SIGKILL)
except ProcessLookupError: except (ProcessLookupError, psutil.NoSuchProcess):
pass pass
def _output_reader(self, proc): def _output_reader(self, proc):
@ -805,7 +805,7 @@ class QEMUHandler(Handler):
try: try:
if pid: if pid:
os.kill(pid, signal.SIGTERM) os.kill(pid, signal.SIGTERM)
except ProcessLookupError: except (ProcessLookupError, psutil.NoSuchProcess):
# Oh well, as long as it's dead! User probably sent Ctrl-C # Oh well, as long as it's dead! User probably sent Ctrl-C
pass pass
@ -864,6 +864,8 @@ class QEMUHandler(Handler):
if cpu_time < timeout and not out_state: if cpu_time < timeout and not out_state:
timeout_time = time.time() + (timeout - cpu_time) timeout_time = time.time() + (timeout - cpu_time)
continue continue
except psutil.NoSuchProcess:
pass
except ProcessLookupError: except ProcessLookupError:
out_state = "failed" out_state = "failed"
break break