Skip to content

The agent that grows with you

fix: the update's Windows child runner and the probe runner share one kill-and-drain

Tekniumb7179ad67fb2
diff --git a/hermes_cli/_subprocess_compat.py b/hermes_cli/_subprocess_compat.py
index 57341530ce3..2896ed29b07 100644
--- a/hermes_cli/_subprocess_compat.py
+++ b/hermes_cli/_subprocess_compat.py
@@ -839,17 +839,26 @@ def bounded_probe_run(
# Timeout OR any other communicate() failure (torn-down pipe, decode error): tree-kill and
# drain bounded — leaving it running would leak the suspended-descendant class this guards.
_close_job(job)
- kill_process_tree(proc)
- try:
- proc.communicate(timeout=1)
- except Exception:
- pass
+ kill_and_drain(proc, 1)
return None
# The probe exited on its own; anything it left behind (`&` jobs) goes with the job.
_close_job(job)
return subprocess.CompletedProcess(list(argv), proc.returncode, stdout, stderr)
+def kill_and_drain(proc: "subprocess.Popen", seconds: float) -> "tuple | None":
+ """Tree-kill *proc* (:func:`kill_process_tree`), then read what its pipes still hold for at
+ most *seconds*: ``(stdout, stderr)``, or ``None`` when a descendant the kill missed still
+ holds them. Those pipes are left to ``communicate()``'s reader threads, never closed
+ (closing a pipe a thread is reading blocks; a ``with Popen`` exit would close them)."""
+ kill_process_tree(proc)
+ try:
+ return proc.communicate(timeout=seconds)
+ except (OSError, ValueError, subprocess.SubprocessError):
+ proc.stdin = proc.stdout = proc.stderr = None
+ return None
+
+
def _close_job(job) -> None:
if job is None:
return
diff --git a/hermes_cli/update_custody.py b/hermes_cli/update_custody.py
index 5ff9c75d995..d34d17c7535 100644
--- a/hermes_cli/update_custody.py
+++ b/hermes_cli/update_custody.py
@@ -267,14 +267,13 @@ def run(argv: Sequence[str], *, inherit_lock: bool = False, **kwargs) -> subproc
try:
stdout, stderr = proc.communicate(input, timeout=timeout)
except subprocess.TimeoutExpired as exc:
- _kill_tree(proc)
- proc.kill()
- try:
- exc.stdout, exc.stderr = proc.communicate(timeout=_DRAIN_SECONDS)
- except subprocess.TimeoutExpired:
- # A descendant the tree kill missed still holds the pipes: leave them to the
- # reader threads (closing a pipe a thread is reading blocks) and give up on them.
- proc.stdin = proc.stdout = proc.stderr = None
+ # git.exe's git-remote-https inherits the pipes: kill the tree, not git.exe alone (and
+ # never the job: it also holds the update's other children).
+ from hermes_cli._subprocess_compat import kill_and_drain
+
+ drained = kill_and_drain(proc, _DRAIN_SECONDS)
+ if drained is not None:
+ exc.stdout, exc.stderr = drained
raise
except BaseException:
proc.kill()
@@ -319,16 +318,6 @@ def popen_post_commit(argv: Sequence[str], *, label: str, **kwargs) -> subproces
_DRAIN_SECONDS = 5
-def _kill_tree(proc: subprocess.Popen) -> None:
- """Windows: kill the child AND what it started (git.exe's git-remote-https inherits the
- stdout/stderr pipes; killing git.exe alone leaves communicate() waiting on the helper). Not
- TerminateJobObject: the job also holds the update's other children."""
- with contextlib.suppress(OSError, subprocess.SubprocessError):
- subprocess.run(["taskkill", "/T", "/F", "/PID", str(proc.pid)], stdin=subprocess.DEVNULL,
- stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=10,
- creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0))
-
-
def run_git(git_cmd: Sequence[str], args: Sequence[str], **kwargs) -> subprocess.CompletedProcess:
"""THE updater git runner: custody config in argv, the lock fd only into local mutators,
Windows job binding inside an update. ``kwargs`` are ``subprocess.run``'s."""