Official Codebase of 3DCodeVerse
Python
proc: run_subprocess ignored its own timeout_s when a descendant escaped the group
gaoypeng6555c8b9775a
diff --git a/harness/codeverse/proc.py b/harness/codeverse/proc.pyindex 6619237d..9092b72b 100644--- a/harness/codeverse/proc.py+++ b/harness/codeverse/proc.py@@ -11,6 +11,7 @@ callers; it will become a re-export of this module.from __future__ import annotations+import contextlibimport jsonimport osimport signal@@ -67,7 +68,7 @@ def run_subprocess(except subprocess.TimeoutExpired:timed_out = Truekill_group(proc)- out, err = proc.communicate()+ out, err = _drain_after_kill(proc)return ProcResult(returncode=proc.returncode if proc.returncode is not None else -1,stdout=out or "",@@ -77,6 +78,35 @@ def run_subprocess()+#: how long to keep draining the pipes after the group kill. Matches+#: ``agents/watchdog.py``'s ``thread.join(timeout=10)``.+DRAIN_TIMEOUT_S = 10.0+_ABANDONED = "(output abandoned: a detached descendant still holds the pipe)"+++def _drain_after_kill(proc: subprocess.Popen[str]) -> tuple[str, str]:+ """Collect whatever the killed child wrote, WITHOUT waiting forever.++ ``kill_group`` killpg's only the child's own session, so a descendant that+ setsid'd or daemonised while inheriting stdout/stderr keeps those pipes open —+ and an unbounded ``communicate()`` then blocks until IT exits, which has no upper+ bound. That silently voided ``timeout_s`` for every build/render path (blender,+ node, cadquery, urdf, gl_render, joints_export, doctor) that funnels through here.+ """+ try:+ return proc.communicate(timeout=DRAIN_TIMEOUT_S)+ except subprocess.TimeoutExpired:+ # The child itself is already SIGKILLed; close our ends of the pipes so the+ # escaped descendant cannot hold us, then reap the child (it is a zombie now).+ for pipe in (proc.stdin, proc.stdout, proc.stderr):+ if pipe is not None:+ with contextlib.suppress(OSError):+ pipe.close()+ with contextlib.suppress(subprocess.TimeoutExpired):+ proc.wait(timeout=DRAIN_TIMEOUT_S)+ return "", _ABANDONED++def kill_group(proc: subprocess.Popen[str]) -> None:"""SIGKILL ``proc``'s whole process group (fall back to just the child)."""try:diff --git a/harness/tests/core/test_proc.py b/harness/tests/core/test_proc.pyindex 52a38c9c..31c965d8 100644--- a/harness/tests/core/test_proc.py+++ b/harness/tests/core/test_proc.py@@ -44,6 +44,35 @@ def test_timeout_kills_the_whole_process_group(tmp_path: Path):raise AssertionError(f"grandchild sleep (pid {grandchild}) survived the group kill")+def test_a_detached_descendant_holding_the_pipes_cannot_extend_the_timeout(tmp_path: Path, monkeypatch):+ """CP-4: after TimeoutExpired the code called a SECOND communicate() with no+ timeout. killpg reaps only the child's own session, so a descendant that+ setsid'd while inheriting stdout keeps the pipe open and that call blocked until+ IT exited — unbounded. A caller passing timeout_s=N was never released at N.+ """+ import codeverse.proc as proc_mod++ # raising=False so this test still RUNS (and fails on the hang) against the+ # pre-fix module, which has no such constant+ monkeypatch.setattr(proc_mod, "DRAIN_TIMEOUT_S", 1.0, raising=False)+ t0 = time.monotonic()++ r = run_subprocess(["bash", "-c", "setsid sleep 20 & echo started; sleep 20"],+ cwd=tmp_path, timeout_s=1.0)++ elapsed = time.monotonic() - t0+ assert r.timed_out and r.returncode != 0+ # timeout_s + one drain window + slack — NOT the 20 s the escaped descendant lives+ assert elapsed < 8.0, f"run_subprocess returned only after {elapsed:.1f}s"+++def test_a_same_group_grandchild_still_has_its_output_collected(tmp_path: Path):+ """The bound must not cost us the normal case: a grandchild inside the group is+ killed by killpg, so the drain completes and the output survives."""+ r = run_subprocess(["bash", "-c", "echo hello; sleep 30 & wait"], cwd=tmp_path, timeout_s=0.4)+ assert r.timed_out and "hello" in r.stdout++def test_preexec_fn_runs_in_the_child(tmp_path: Path):soft = 256