Skip to content

Commit 76b24fa

Browse files
M1chakartben
authored andcommitted
twister: terminate_process: fix NoSuchProcess error
NOTE: Even though previous commits indicate, that this can only happen on MacOS, that's actually not true. It happens on Linux as well. The constructor of `psutil.Process` can throw an exception as well, so we need to wrap the whole loop in another try, unfortunately. Signed-off-by: Michael Zimmermann <[email protected]>
1 parent 133e127 commit 76b24fa

File tree

2 files changed

+8
-6
lines changed
  • scripts/pylib

2 files changed

+8
-6
lines changed

scripts/pylib/pytest-twister-harness/src/twister_harness/device/utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ def terminate_process(proc: subprocess.Popen) -> None:
4242
"""
4343
Try to terminate provided process and all its subprocesses recursively.
4444
"""
45-
for child in psutil.Process(proc.pid).children(recursive=True):
46-
with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess):
47-
os.kill(child.pid, signal.SIGTERM)
45+
with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess):
46+
for child in psutil.Process(proc.pid).children(recursive=True):
47+
with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess):
48+
os.kill(child.pid, signal.SIGTERM)
4849
proc.terminate()
4950
# sleep for a while before attempting to kill
5051
time.sleep(0.5)

scripts/pylib/twister/twisterlib/handlers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ def terminate_process(proc):
5959
so we need to use try_kill_process_by_pid.
6060
"""
6161

62-
for child in psutil.Process(proc.pid).children(recursive=True):
63-
with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess):
64-
os.kill(child.pid, signal.SIGTERM)
62+
with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess):
63+
for child in psutil.Process(proc.pid).children(recursive=True):
64+
with contextlib.suppress(ProcessLookupError, psutil.NoSuchProcess):
65+
os.kill(child.pid, signal.SIGTERM)
6566
proc.terminate()
6667
# sleep for a while before attempting to kill
6768
time.sleep(0.5)

0 commit comments

Comments
 (0)