fix(agent): kill timed out ansible process groups

This commit is contained in:
ogt
2026-07-14 18:34:44 +08:00
parent baac2ed4e8
commit cf644be09d
2 changed files with 75 additions and 4 deletions

View File

@@ -12,6 +12,7 @@ import json
import os
import re
import shutil
import signal
import time
from collections.abc import Mapping
from dataclasses import dataclass, replace
@@ -632,10 +633,22 @@ async def _run_ansible_command(spec: AnsibleCommandSpec, *, timeout_seconds: int
*spec.command,
cwd=str(spec.cwd),
env=spec.env,
start_new_session=True,
stdin=asyncio.subprocess.DEVNULL,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
def kill_process_group() -> None:
if process.returncode is not None:
return
try:
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
return
except PermissionError:
process.kill()
timed_out = False
try:
stdout_bytes, stderr_bytes = await asyncio.wait_for(
@@ -644,10 +657,10 @@ async def _run_ansible_command(spec: AnsibleCommandSpec, *, timeout_seconds: int
)
except TimeoutError:
timed_out = True
process.kill()
kill_process_group()
stdout_bytes, stderr_bytes = await process.communicate()
except asyncio.CancelledError:
process.kill()
kill_process_group()
await process.communicate()
raise
duration_ms = int((time.monotonic() - started) * 1000)