diff --git a/dashboard/cockpit.py b/dashboard/cockpit.py index 59dc9bd2..50f015a1 100644 --- a/dashboard/cockpit.py +++ b/dashboard/cockpit.py @@ -562,11 +562,14 @@ def _iso_epoch(line): return None -def crash_cause(unit, invocation, now=None): +def crash_cause(unit, invocation, now=None, ended=None): """The known cause of the run `invocation` of `unit`, read once per run: its last 400 lines - and the kernel's lines of the 15 minutes before (the same exact sudo argv collect_kernel - uses). The matched line is shown with the API key masked, as the diagnostics bundle masks it.""" + and the kernel's lines of the 15 minutes around the minute it entered failed (the same exact + sudo argv collect_kernel uses, the hour it can reach). `ended` is that minute: a cockpit + that looks late still reads the minutes of the crash, not the minutes of the looking. The + matched line is shown with the API key masked, as the diagnostics bundle masks it.""" now = time.time() if now is None else now + anchor = now if ended is None else ended seen = CRASH_CAUSE.get(unit) if seen and seen.get("invocation") == invocation: return seen @@ -580,8 +583,10 @@ def crash_cause(unit, invocation, now=None): return seen # a journal that did not answer is read again next tick lines = raw.splitlines() kern = run(["sudo", "-n", "/usr/bin/journalctl", "-k", "--since", "-1h", "--no-pager", "-o", "short-iso"], timeout=8) + if not answered(kern): + return seen # and so is a kernel log that did not answer kernel = [ln for ln in kern.splitlines() - if (t := _iso_epoch(ln)) is not None and now - 900 <= t <= now + 60] + if (t := _iso_epoch(ln)) is not None and anchor - 900 <= t <= anchor + 60] cause = mod.crash_cause(lines, kernel, data) key = api_key() if cause and cause.get("line") and key: @@ -1630,7 +1635,9 @@ def collect_lifecycle(): elif st["state"] != "failed": CRASHED_SERVING.pop(unit, None) if st["state"] == "failed" and not is_image and not is_video: - crash_cause(unit, d.get("InvocationID", "")) + chg_us = int(d.get("StateChangeTimestampMonotonic") or 0) + crash_cause(unit, d.get("InvocationID", ""), + ended=time.time() - (monotonic_now() - chg_us / 1e6) if chg_us else None) crash = CRASH_CAUSE.get(unit) crash_age = time.time() - crash["at"] if crash else None # what the start guard said, when it is why the lane is not up (held, refused) diff --git a/dashboard/tests/test_cockpit_crash_cause.py b/dashboard/tests/test_cockpit_crash_cause.py index b0ce1679..5bd47fe3 100644 --- a/dashboard/tests/test_cockpit_crash_cause.py +++ b/dashboard/tests/test_cockpit_crash_cause.py @@ -1,9 +1,9 @@ """A text lane that stopped is told what is known about why (doctor.py's crash signatures). Once per run: the run's own journal (its systemd invocation) and the kernel's lines of the 15 -minutes before, through the exact sudo argv collect_kernel uses. A journal that did not answer -is read again; the API key never reaches the page; a doctor.py that does not load costs the -cockpit nothing but the causes.""" +minutes around the minute it failed, through the exact sudo argv collect_kernel uses. A journal +or a kernel log that did not answer is read again; the API key never reaches the page; a +doctor.py that does not load costs the cockpit nothing but the causes.""" import importlib.util import os import shutil @@ -59,6 +59,7 @@ class TheCrashCause(unittest.TestCase): f"{iso(self.now - 5)} gx10 kernel: NVRM: Xid (PCI:000f:01:00): 31, pid=4242, name=python3", "not a dated line"] self.journal_ok = True + self.kernel_ok = True def fake(argv, timeout=5.0, merge_err=False): self.calls.append(list(argv)) @@ -66,7 +67,9 @@ class TheCrashCause(unittest.TestCase): r = self.cp.Ran("\n".join(self.journal) + "\n" if self.journal_ok else "") r.ok = self.journal_ok return r - return self.cp.Ran("\n".join(self.kernel) + "\n") + r = self.cp.Ran("\n".join(self.kernel) + "\n") + r.ok = self.kernel_ok + return r self.cp.run = fake def test_a_known_cause_with_the_xid_of_the_same_minutes(self): @@ -107,6 +110,23 @@ class TheCrashCause(unittest.TestCase): self.assertEqual(self.cp.crash_cause("qwen38-flash.service", "inv-4", now=self.now)["cause"]["id"], "cublas-internal-error") + def test_a_kernel_log_that_did_not_answer_is_read_again(self): + self.kernel_ok = False + self.assertIsNone(self.cp.crash_cause("qwen38-flash.service", "inv-8", now=self.now)) + self.assertEqual(self.events, [], "no cause is claimed while the kernel log has not answered") + self.kernel_ok = True + rec = self.cp.crash_cause("qwen38-flash.service", "inv-8", now=self.now) + self.assertEqual(rec["cause"]["id"], "cublas-internal-error") + self.assertEqual(len(self.events), 1, "said once, when the kernel log answered") + + def test_the_kernel_window_follows_the_crash_not_the_reading(self): + self.kernel = [f"{iso(self.now - 1800)} gx10 kernel: NVRM: Xid (PCI:000f:01:00): 31, pid=4242, name=python3"] + rec = self.cp.crash_cause("qwen38-sglang.service", "inv-9", now=self.now, ended=self.now - 1800) + self.assertEqual(rec["cause"]["xids"], [31], "a late look finds the Xid of the crash minutes") + self.cp.CRASH_CAUSE.clear() + rec = self.cp.crash_cause("qwen38-sglang.service", "inv-10", now=self.now) + self.assertEqual(rec["cause"]["xids"], [], "without a crash minute, the half-hour before the look is the rule") + def test_a_doctor_that_does_not_load_costs_only_the_causes(self): saved = list(self.cp._DOCTOR) self.cp._DOCTOR.clear()