From ecce9caaa074b98955c8f459e9f2c97e38cee2cf Mon Sep 17 00:00:00 2001 From: Danny Date: Mon, 16 Nov 2026 09:55:08 -0800 Subject: [PATCH] StarPort: Record Wi-Fi firmware restarts in stall captures --- .../starpilot/system/starport/supervisor.py | 3 ++- .../starpilot/system/starport/system_slice.py | 26 ++++++++++++++++--- .../starport/tests/test_system_slice.py | 19 ++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/openpilot/starpilot/system/starport/supervisor.py b/openpilot/starpilot/system/starport/supervisor.py index f0b042b569..96cd9f4901 100644 --- a/openpilot/starpilot/system/starport/supervisor.py +++ b/openpilot/starpilot/system/starport/supervisor.py @@ -420,7 +420,8 @@ class EventLog: """Save the last seconds of device logs now; the session's own capture keeps only its newest lines.""" if self.path is not None: now = datetime.now(UTC).timestamp() - system_slice.write_sidecar_async(self.path, now - STALL_CAPTURE_S, now, tag=".stall") + system_slice.write_sidecar_async(self.path, now - STALL_CAPTURE_S, now, tag=".stall", + on_firmware=lambda lines, reason: self("wifi_firmware_restart", lines=lines, reason=reason)) def _line(self, record: dict) -> str: return json.dumps(record, default=str) + "\n" diff --git a/openpilot/starpilot/system/starport/system_slice.py b/openpilot/starpilot/system/starport/system_slice.py index 115d223d07..1f4433aa19 100644 --- a/openpilot/starpilot/system/starport/system_slice.py +++ b/openpilot/starpilot/system/starport/system_slice.py @@ -19,6 +19,8 @@ MAX_LINE = 300 SIDECAR_SUFFIX = ".system.json" QUOTED = re.compile(r"'[^']*'|\"[^\"]*\"") SSID = re.compile(r"(?i)\b(e?ssid)\b[=: ]+\S+") +FIRMWARE = re.compile(r"subsys-restart|failure reason|Root PD|icnss: (PD service down|QMI Service Disconnected)") +MAX_FIRMWARE_LINES = 40 def _timestamp(value: float) -> str: @@ -53,6 +55,12 @@ def _lines(data: bytes) -> list[str]: return [line for line in text.splitlines() if line.strip()][-MAX_LINES:] +def firmware_lines(data: bytes) -> list[str]: + """Wi-Fi and modem firmware restart lines from the whole kernel window, kept apart from the newest-lines cap.""" + text = system_snapshot.redact(data).decode("utf-8", errors="replace") + return [line[:MAX_LINE] for line in text.splitlines() if FIRMWARE.search(line)][:MAX_FIRMWARE_LINES] + + def collect(start: float, end: float, *, collector=system_snapshot.collect) -> dict: """The slice for [start, end] (epoch seconds), each source as {"lines": [...]} or {"error": ...}.""" files = collector(start, DEADLINE_S, command_set=commands(start, end)) @@ -72,6 +80,9 @@ def collect(start: float, end: float, *, collector=system_snapshot.collect) -> d proc = files.get("proc.txt") if proc: result["sources"]["proc"] = {"lines": _lines(proc)} + firmware = firmware_lines(files.get("kernel", b"")) + if firmware: + result["sources"]["firmware"] = {"lines": firmware} return result @@ -80,11 +91,17 @@ def sidecar_path(log_path: Path, tag: str = "") -> Path: return log_path.with_name(f"session-{match[1] if match else log_path.stem}{tag}{SIDECAR_SUFFIX}") -def write_sidecar(log_path: Path, start: float, end: float, *, collector=system_snapshot.collect, tag: str = "") -> None: +def write_sidecar(log_path: Path, start: float, end: float, *, collector=system_snapshot.collect, tag: str = "", + on_firmware=None) -> None: """Capture the window and save it beside the session log; never raises.""" try: path = sidecar_path(log_path, tag) - text = json.dumps(collect(start, end, collector=collector)) + captured = collect(start, end, collector=collector) + firmware = captured["sources"].get("firmware", {}).get("lines", []) + if firmware and on_firmware is not None: + reason = next((line.split("failure reason", 1)[1].strip(" :") for line in firmware if "failure reason" in line), "") + on_firmware(len(firmware), reason[:200]) + text = json.dumps(captured) temporary = path.with_name(path.name + ".tmp") fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) with os.fdopen(fd, "w") as handle: @@ -94,8 +111,9 @@ def write_sidecar(log_path: Path, start: float, end: float, *, collector=system_ pass -def write_sidecar_async(log_path: Path, start: float, end: float, tag: str = "") -> threading.Thread: - thread = threading.Thread(target=write_sidecar, args=(log_path, start, end), kwargs={"tag": tag}, name="aa_system_slice", daemon=True) +def write_sidecar_async(log_path: Path, start: float, end: float, tag: str = "", on_firmware=None) -> threading.Thread: + thread = threading.Thread(target=write_sidecar, args=(log_path, start, end), kwargs={"tag": tag, "on_firmware": on_firmware}, + name="aa_system_slice", daemon=True) thread.start() return thread diff --git a/openpilot/starpilot/system/starport/tests/test_system_slice.py b/openpilot/starpilot/system/starport/tests/test_system_slice.py index 85e6245fe0..2123c27b10 100644 --- a/openpilot/starpilot/system/starport/tests/test_system_slice.py +++ b/openpilot/starpilot/system/starport/tests/test_system_slice.py @@ -67,3 +67,22 @@ def test_a_collector_that_raises_leaves_no_sidecar(tmp_path): system_slice.write_sidecar(log, 0.0, 1.0, collector=broken) assert system_slice.read_sidecar(log) == {} + + +def test_firmware_restart_lines_survive_the_cap_and_are_reported(tmp_path): + def crashed(since, deadline, *, command_set): + files = fake_collector(since, deadline, command_set=command_set) + manifest = json.loads(files["manifest.json"]) + del manifest["commands"]["kernel"]["error"] + files["manifest.json"] = json.dumps(manifest).encode() + files["kernel"] = (b"modem subsystem failure reason: wlan_fw.c:12: assert\nsubsys-restart: Shutting down modem\n" + + b"noise\n" * (system_slice.MAX_LINES + 5)) + return files + + log = tmp_path / "session-000009-20261116-120000.jsonl" + log.write_text("") + seen = [] + system_slice.write_sidecar(log, 1_000_000.0, 1_000_060.0, collector=crashed, tag=".stall", on_firmware=lambda *a: seen.append(a)) + saved = json.loads((tmp_path / "session-000009.stall.system.json").read_text())["sources"] + assert len(saved["firmware"]["lines"]) == 2 and "failure reason" not in "".join(saved["kernel"]["lines"]) + assert seen == [(2, "wlan_fw.c:12: assert")]