twister: terminate_process: use contextlib.suppress

While it doesn't cause any issues, it's more consistent and makes future
commits which add handling for more exception types more readable.

Based on: 0df8240b49

Signed-off-by: Michael Zimmermann <sigmaepsilon92@gmail.com>
This commit is contained in:
Michael Zimmermann 2025-02-19 07:43:37 +01:00 committed by Benjamin Cabé
commit 133e1278c2

View file

@ -4,6 +4,7 @@
from __future__ import annotations
import contextlib
import logging
import os
import platform
@ -42,10 +43,8 @@ def terminate_process(proc: subprocess.Popen) -> None:
Try to terminate provided process and all its subprocesses recursively.
"""
for child in psutil.Process(proc.pid).children(recursive=True):
try:
with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess):
os.kill(child.pid, signal.SIGTERM)
except (ProcessLookupError, psutil.NoSuchProcess):
pass
proc.terminate()
# sleep for a while before attempting to kill
time.sleep(0.5)