Skip to content

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

fix(bench): limit replay process cleanup to its own server group (#46)

Afloat3b0011af1e46
diff --git a/bench/rrun.py b/bench/rrun.py
index d79d555..f490381 100644
--- a/bench/rrun.py
+++ b/bench/rrun.py
@@ -137,7 +137,8 @@ def start_server(arm, a, out, tag):
def server_pid(shell_pid):
"""The llama-server process under the shell / serve wrapper (exec'd, so usually the same pid)."""
try:
- out = subprocess.run(["pgrep", "-f", "llama-server"], capture_output=True, text=True).stdout.split()
+ out = subprocess.run(["pgrep", "-g", str(shell_pid), "-x", "llama-server"],
+ capture_output=True, text=True).stdout.split()
return int(out[0]) if out else shell_pid
except Exception:
return shell_pid
@@ -146,13 +147,19 @@ def server_pid(shell_pid):
def stop_server(srv):
try:
os.killpg(srv.pid, signal.SIGTERM)
+ except ProcessLookupError:
+ pass
+ try:
srv.wait(timeout=60)
- except Exception:
+ except subprocess.TimeoutExpired:
+ pass
+ finally:
+ # Reap the leader and stop any remaining children of this run only.
try:
os.killpg(srv.pid, signal.SIGKILL)
- except Exception:
+ except ProcessLookupError:
pass
- subprocess.run("pkill -9 -f llama-server", shell=True)
+ srv.wait()
time.sleep(2)
@@ -210,8 +217,6 @@ def main():
arm = json.load(open(a.arms))[a.arm]
data = json.load(open(a.replay))
name = a.arm + a.tag
- subprocess.run("pkill -9 -f llama-server", shell=True)
- time.sleep(1)
srv, pid, cmd = start_server(arm, a, out, a.tag)
meta = {"arm": a.arm, "tag": a.tag, "cmd": cmd, "env": arm.get("env", {}), "ctx": a.ctx, "gen": a.gen,
"replay": a.replay, "t_start": time.strftime("%FT%TZ", time.gmtime()),
diff --git a/bench/test_replay_processes.py b/bench/test_replay_processes.py
new file mode 100644
index 0000000..42a5f6c
--- /dev/null
+++ b/bench/test_replay_processes.py
@@ -0,0 +1,40 @@
+import signal
+import subprocess
+import unittest
+from unittest.mock import Mock, patch
+
+import rrun
+
+
+class ReplayProcessTest(unittest.TestCase):
+ def test_pid_lookup_is_limited_to_the_started_process_group(self):
+ def pgrep(command, **kwargs):
+ # A global lookup finds another user's server before our own child.
+ owned = "-g" in command and command[command.index("-g") + 1] == "400"
+ return Mock(stdout="401\n" if owned else "99\n401\n")
+
+ with patch.object(rrun.subprocess, "run", side_effect=pgrep):
+ self.assertEqual(rrun.server_pid(400), 401)
+
+ def test_shutdown_only_signals_the_owned_group(self):
+ proc = Mock(pid=400)
+ with patch.object(rrun.os, "killpg") as killpg, \
+ patch.object(rrun.subprocess, "run") as run, patch.object(rrun.time, "sleep"):
+ rrun.stop_server(proc)
+ self.assertFalse(run.called, "cleanup must not run global pkill")
+ self.assertTrue(killpg.called)
+ self.assertTrue(all(call.args[0] == 400 for call in killpg.call_args_list))
+ proc.wait.assert_called()
+
+ def test_forced_shutdown_reaps_the_child(self):
+ proc = Mock(pid=400)
+ proc.wait.side_effect = [subprocess.TimeoutExpired("server", 60), 0]
+ with patch.object(rrun.os, "killpg") as killpg, \
+ patch.object(rrun.subprocess, "run"), patch.object(rrun.time, "sleep"):
+ rrun.stop_server(proc)
+ killpg.assert_any_call(400, signal.SIGKILL)
+ self.assertEqual(proc.wait.call_count, 2)
+
+
+if __name__ == "__main__":
+ unittest.main()