From 8c8fb247791eb906d27c06cbbea4786850e96f8f Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 10 Oct 2026 09:57:38 +0200 Subject: [PATCH] battery: a timeout or interrupt kills rugnux's whole process group subprocess.run's timeout killed only the direct child. Under --gpulock that is flock, under `stress --sanitizer` compute-sanitizer, and in both cases rugnux went on running on the card, orphaned, beside whatever took the GPU lock next - found when a cut-short memcheck stress run left its rugnux behind. Each run now gets a process group of its own, killed whole on a timeout, Ctrl-C or SIGTERM. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi --- tools/battery/battery.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tools/battery/battery.py b/tools/battery/battery.py index 9d6fb6a5c..d4ad079ef 100644 --- a/tools/battery/battery.py +++ b/tools/battery/battery.py @@ -319,6 +319,23 @@ def command(binary, e, opts, model=None): return cmd +def run_logged(cmd, wd, timeout, env=None): + """Run cmd in wd with its output in wd/run.log; its exit code, or None on a timeout. It runs in a + process group of its own, and a timeout or an interrupt kills the whole group: killing only the + direct child - the GPU lock wrapper, or compute-sanitizer - leaves rugnux running on the card.""" + with open(os.path.join(wd, "run.log"), "w") as log: + proc = subprocess.Popen(cmd, cwd=wd, stdout=log, stderr=subprocess.STDOUT, env=env, + start_new_session=True) + try: + return proc.wait(timeout=timeout) + except subprocess.TimeoutExpired: + return None + finally: + if proc.poll() is None: + os.killpg(proc.pid, signal.SIGKILL) + proc.wait() + + def failure_note(log): """rugnux's own last error message, without the spdlog timestamp and severity prefix. Read from the log file rugnux writes beside its outputs (p_log.txt), whose lines are whole; the terminal @@ -375,12 +392,9 @@ def run_one(run_dir, binary, e, opts): row["cmd"] = " ".join(cmd) t0 = time.time() note = "" - with open(os.path.join(wd, "run.log"), "w") as log: - try: - rc = subprocess.run(cmd, cwd=wd, stdout=log, stderr=subprocess.STDOUT, - timeout=opts["timeout"]).returncode - except subprocess.TimeoutExpired: - rc, note = None, "timeout" + rc = run_logged(cmd, wd, opts["timeout"]) + if rc is None: + note = "timeout" row["elapsed_s"] = round(time.time() - t0, 1) row["exit_code"] = rc crash = score.crash_note(rc, wd) @@ -618,12 +632,7 @@ def stress_one(stress_dir, binary, e, i, opts): cmd = ["compute-sanitizer", "--tool", opts["sanitizer"], "--report-api-errors", "no"] + cmd env = dict(os.environ, CUDA_LAUNCH_BLOCKING="1") if opts["launch_blocking"] else None t0 = time.time() - with open(os.path.join(wd, "run.log"), "w") as log: - try: - rc = subprocess.run(cmd, cwd=wd, stdout=log, stderr=subprocess.STDOUT, env=env, - timeout=opts["timeout"]).returncode - except subprocess.TimeoutExpired: - rc = None + rc = run_logged(cmd, wd, opts["timeout"], env) hkl = os.path.join(wd, "p.hkl") return {"set": e["id"], "arm": e["arm"], "rep": i, "cmd": " ".join(cmd), "exit_code": rc, "elapsed_s": round(time.time() - t0, 1), "timeout": rc is None,