improve terminating processes on error

This commit is contained in:
Jörg Thalheim
2024-10-10 17:24:25 +02:00
parent d97bda9c0d
commit 71e7ecd49c
3 changed files with 45 additions and 19 deletions

View File

@@ -18,6 +18,7 @@ from shlex import quote
from threading import Thread
from typing import IO, Any, Generic, TypeVar
from clan_cli.cmd import terminate_process_group
from clan_cli.errors import ClanError
# https://no-color.org
@@ -294,7 +295,7 @@ class Host:
elapsed = now - start
if now - last_output > NO_OUTPUT_TIMEOUT:
elapsed_msg = time.strftime("%H:%M:%S", time.gmtime(elapsed))
cmdlog.warn(
cmdlog.warning(
f"still waiting for '{displayed_cmd}' to finish... ({elapsed_msg} elapsed)",
extra={"command_prefix": self.command_prefix},
)
@@ -366,7 +367,9 @@ class Host:
stderr=stderr_write,
env=env,
cwd=cwd,
start_new_session=True,
) as p:
stack.enter_context(terminate_process_group(p))
if write_std_fd is not None:
write_std_fd.close()
if write_err_fd is not None:
@@ -387,11 +390,7 @@ class Host:
stderr_read,
timeout,
)
try:
ret = p.wait(timeout=max(0, timeout - (time.time() - start)))
except subprocess.TimeoutExpired:
p.kill()
raise
ret = p.wait(timeout=max(0, timeout - (time.time() - start)))
if ret != 0:
if check:
raise subprocess.CalledProcessError(