Files
alzeo/tests/test_pyspy_forensics.py
Hasan BasbunarandClaude Opus 5.5 4a42092cfe pyspy-scheduler.sh: the LFM2.5 and Inkling lanes too, as their probe and their autoheal
The stacks of a wedged SGLang engine were looked for in the 27B and flash containers alone: on the two other SGLang lanes
the evidence was "no serving container running". The wrapper, the probe and the lane registry are held to one list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 08:58:57 +01:00

84 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""A wedge's forensics save the scheduler's stacks, never the wrapper's complaint.
The py-spy wrapper printed its errors ("py-spy not installed", "no serving container
running") on stdout, and the cockpit took any text back, stderr merged in, for a dump:
the complaint was saved as wedge-*.txt under "scheduler stacks saved" (found in review,
2026-09-24). The wrapper runs here from a copy whose py-spy and docker are stubs."""
import pathlib
import re
import subprocess
import sys
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO))
import lane_registry # noqa: E402
WRAPPER = (REPO / "dashboard" / "pyspy-scheduler.sh").read_text()
COCKPIT = (REPO / "dashboard" / "cockpit.py").read_text()
def run(pyspy_exists, container=""):
d = pathlib.Path(tempfile.mkdtemp(prefix="pyspy-"))
(d / "bin").mkdir()
# no container answers, or the one that serves, with its scheduler's process (comm is 15 characters)
(d / "bin" / "docker").write_text(f'#!/bin/sh\n[ "$1 $2" = "top {container}" ] || exit 1\nprintf "PID COMMAND\\n1 python3\\n2 sglang::schedul\\n"\n')
(d / "bin" / "docker").chmod(0o755)
fake = d / "py-spy"
if pyspy_exists:
fake.write_text("#!/bin/sh\necho stacks\n")
fake.chmod(0o755)
text = WRAPPER.replace("for c in /usr/local/bin/py-spy /usr/bin/py-spy /root/.local/bin/py-spy; do",
f"for c in {fake}; do")
assert text != WRAPPER
(d / "w.sh").write_text(text)
r = subprocess.run(["bash", str(d / "w.sh")], capture_output=True, text=True, timeout=30,
env={"PATH": f"{d}/bin:/usr/bin:/bin"})
return r.returncode, r.stdout, r.stderr
class TheWrapperKeepsStdoutForTheDump(unittest.TestCase):
def test_no_py_spy(self):
rc, out, err = run(False)
self.assertEqual((rc, out), (3, ""))
self.assertIn("py-spy not installed", err)
def test_no_container(self):
rc, out, err = run(True)
self.assertEqual((rc, out), (4, ""))
self.assertIn("no serving container running", err)
class EverySGLangLanesSchedulerIsDumped(unittest.TestCase):
"""The wrapper named two containers: a wedge of the LFM2.5 or the Inkling lane, which the same probe and the same
autoheal watch, left "no serving container running" for evidence (found in an audit, 2026-11-30)."""
CONTAINERS = {f["unit"][:-len(".service")] for f in lane_registry.LANES.values() if f["engine"] == "sglang"}
def test_each_of_them_is_found(self):
for cont in sorted(self.CONTAINERS):
with self.subTest(container=cont):
rc, out, err = run(True, cont)
self.assertEqual((rc, out.splitlines()[-1:]), (0, ["stacks"]), err)
self.assertIn(f"== {cont} scheduler pid 2", out)
def test_the_wrapper_and_the_cockpits_probe_name_the_same(self):
named = set(re.search(r"^for cont in (.+); do$", WRAPPER, re.M).group(1).split())
probed = set(re.findall(r'"(qwen38-[a-z0-9-]+)"', re.search(r"^SGL_CONTAINERS = \((.+?)\)", COCKPIT, re.M).group(1)))
self.assertEqual((named, probed), (self.CONTAINERS, self.CONTAINERS))
class TheCockpitReadsTheStatus(unittest.TestCase):
def test_a_dump_is_saved_only_on_success(self):
i = COCKPIT.index('"/usr/local/bin/qwen38-pyspy-scheduler"]')
block = COCKPIT[i - 300:i + 1400]
self.assertIn("if r.returncode == 0:\n", block)
self.assertLess(block.index("if r.returncode == 0:"), block.index("dump = r.stdout"))
self.assertIn('add_event("forensics", f"no scheduler stacks: {why}")', block)
self.assertNotIn("merge_err=True", block)
if __name__ == "__main__":
unittest.main(verbosity=2)