Skip to content

Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.

fix(bench): reap timed-out evaluation process groups (#50)

Afloatd597c7624435
diff --git a/bench/evaluate.py b/bench/evaluate.py
index e58b90a..bdb98c5 100644
--- a/bench/evaluate.py
+++ b/bench/evaluate.py
@@ -13,6 +13,7 @@ import json
import os
import re
import resource
+import signal
import subprocess
import sys
import tempfile
@@ -65,15 +66,29 @@ def run(src: str) -> tuple[bool, str]:
with tempfile.TemporaryDirectory() as d:
f = os.path.join(d, "prog.py")
open(f, "w").write(src)
- try:
- r = subprocess.run([sys.executable, "-I", f], cwd=d, capture_output=True, timeout=10, preexec_fn=limits,
- stdin=subprocess.DEVNULL, text=True)
- except subprocess.TimeoutExpired:
- return False, "timeout"
- if r.returncode == 0:
- return True, ""
- err = (r.stderr.strip().splitlines() or ["rc %d" % r.returncode])[-1]
- return False, err[:200]
+ with subprocess.Popen([sys.executable, "-I", f], cwd=d, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ preexec_fn=limits, stdin=subprocess.DEVNULL, text=True) as proc:
+ try:
+ try:
+ _, stderr = proc.communicate(timeout=10)
+ except subprocess.TimeoutExpired:
+ try:
+ os.killpg(proc.pid, signal.SIGKILL)
+ except ProcessLookupError:
+ pass
+ proc.communicate()
+ return False, "timeout"
+ if proc.returncode == 0:
+ return True, ""
+ err = (stderr.strip().splitlines() or ["rc %d" % proc.returncode])[-1]
+ return False, err[:200]
+ finally:
+ # limits() creates a session; children belong to this evaluation too.
+ try:
+ os.killpg(proc.pid, signal.SIGKILL)
+ except ProcessLookupError:
+ pass
+ proc.wait()
def main():
diff --git a/bench/test_evaluate_processes.py b/bench/test_evaluate_processes.py
new file mode 100644
index 0000000..eb1193a
--- /dev/null
+++ b/bench/test_evaluate_processes.py
@@ -0,0 +1,53 @@
+import os
+from pathlib import Path
+import signal
+import tempfile
+import time
+import unittest
+
+from evaluate import run
+
+
+class EvaluationProcessTest(unittest.TestCase):
+ def test_timeout_stops_child_processes(self):
+ with tempfile.TemporaryDirectory() as directory:
+ pidfile = Path(directory) / "child.pid"
+ source = f'''
+import subprocess, sys, time
+child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"],
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+with open({str(pidfile)!r}, "w") as f:
+ f.write(str(child.pid))
+time.sleep(60)
+'''
+ pid = None
+ try:
+ self.assertEqual(run(source), (False, "timeout"))
+ pid = int(pidfile.read_text())
+ for _ in range(100):
+ try:
+ state = Path(f"/proc/{pid}/stat").read_text().rsplit(")", 1)[1].split()[0]
+ except FileNotFoundError:
+ state = "gone"
+ if state in ("gone", "Z"):
+ break
+ time.sleep(0.01)
+ self.assertIn(state, ("gone", "Z"), "timed-out evaluation left a running child")
+ finally:
+ if pid is None and pidfile.exists():
+ pid = int(pidfile.read_text())
+ if pid is not None:
+ try:
+ os.kill(pid, signal.SIGKILL)
+ except ProcessLookupError:
+ pass
+
+ def test_successful_evaluation_still_passes(self):
+ self.assertEqual(run("assert 2 + 2 == 4"), (True, ""))
+
+ def test_failure_keeps_the_last_error_line(self):
+ self.assertEqual(run("raise ValueError('example')"), (False, "ValueError: example"))
+
+
+if __name__ == "__main__":
+ unittest.main()