diff --git a/openpilot/starpilot/system/starport/adapter_syslog.py b/openpilot/starpilot/system/starport/adapter_syslog.py index ac28d0c1e5..482fa0da2f 100644 --- a/openpilot/starpilot/system/starport/adapter_syslog.py +++ b/openpilot/starpilot/system/starport/adapter_syslog.py @@ -3,6 +3,7 @@ from __future__ import annotations import http.client +import threading import time import zlib from collections.abc import Callable @@ -20,6 +21,7 @@ DEADLINE_S = 6.0 # whole download, so a trickling adapter cannot hold session c MAX_DOWNLOAD_BYTES = 8 << 20 MAX_TEXT_BYTES = 2 << 20 # newest part kept; the bundle takes the last 1 MiB of it MIN_INTERVAL_S = 30.0 +FIRST_COPY_S = 3.0 # while connected: a first copy soon after video starts, then one every MIN_INTERVAL_S def _text(body: bytes) -> bytes: @@ -57,10 +59,28 @@ class AdapterSyslog: self.log, self.download, self.clock = log, download, clock self.path = (log_dir or identity_store.LOG_DIR) / FILE_NAME self.last = -MIN_INTERVAL_S + self.busy = threading.Lock() - def fetch(self, host: str, local_ip: str = "") -> bool: - if not host or self.clock() - self.last < MIN_INTERVAL_S: + def fetch(self, host: str, local_ip: str = "", force: bool = False) -> bool: + if not host or (not force and self.clock() - self.last < MIN_INTERVAL_S) or not self.busy.acquire(blocking=False): return False + try: + return self._fetch(host, local_ip) + finally: + self.busy.release() + + def watch(self, host: str, local_ip: str, done: threading.Event) -> threading.Thread: + """Copies the syslog while connected, so a crash that takes the adapter's Wi-Fi down still leaves a recent copy.""" + def run(): + wait = FIRST_COPY_S + while not done.wait(wait): + self.fetch(host, local_ip) + wait = MIN_INTERVAL_S + thread = threading.Thread(target=run, name="adapter_syslog", daemon=True) + thread.start() + return thread + + def _fetch(self, host: str, local_ip: str) -> bool: self.last = self.clock() started = self.clock() try: diff --git a/openpilot/starpilot/system/starport/supervisor.py b/openpilot/starpilot/system/starport/supervisor.py index ca4a813677..612c0220b6 100644 --- a/openpilot/starpilot/system/starport/supervisor.py +++ b/openpilot/starpilot/system/starport/supervisor.py @@ -2019,12 +2019,17 @@ class Supervisor: self.log("adapter_address_unavailable", kind=type(error).__name__, error=str(error)[:120]) return "" - def _save_adapter_syslog(self, result, lease) -> None: + def _save_adapter_syslog(self, result, lease, done: threading.Event | None = None) -> None: + """With the AAWireless profile: copy the adapter's syslog every so often while connected (done given), or once now.""" try: endpoint = getattr(result, "endpoint", None) if endpoint is None or self._stop.is_set() or self.car_type() != "adapter" or car_profiles.resolve(self.config)[0] != "aawireless": return - self._adapter_syslog.fetch(str(endpoint.ip), getattr(lease, "local_ip", "") or "") + host, local_ip = str(endpoint.ip), getattr(lease, "local_ip", "") or "" + if done is not None: + self._adapter_syslog.watch(host, local_ip, done) + else: + self._adapter_syslog.fetch(host, local_ip, force=True) except Exception as error: # diagnostics only; never block cleanup self.log("adapter_syslog", ok=False, error=str(error)[:160], kind=type(error).__name__) @@ -2109,12 +2114,15 @@ class Supervisor: except Exception as error: # turn-by-turn is optional; projection must never depend on it self.log("nav_status_unavailable", error=str(error)) car_ended = False + syslog_done = threading.Event() + self._save_adapter_syslog(result, lease, syslog_done) try: self._stream(session, encoder, source, watch, interval) except PeerRequestedStop: car_ended = True raise finally: + syslog_done.set() self._note_streamed(session) self._record_fix_outcome(session, car_ended) if session.audio is not None: diff --git a/openpilot/starpilot/system/starport/tests/test_adapter_syslog.py b/openpilot/starpilot/system/starport/tests/test_adapter_syslog.py index f6ae475f17..d0ba308f0f 100644 --- a/openpilot/starpilot/system/starport/tests/test_adapter_syslog.py +++ b/openpilot/starpilot/system/starport/tests/test_adapter_syslog.py @@ -106,7 +106,9 @@ class FakeSupervisor: if stopped: self._stop.set() self.fetched = [] - self._adapter_syslog = type("Syslog", (), {"fetch": lambda _, host, local_ip: self.fetched.append((host, local_ip))})() + self.watched = [] + self._adapter_syslog = type("Syslog", (), {"fetch": lambda _, host, local_ip, force=False: self.fetched.append((host, local_ip, force)), + "watch": lambda _, host, local_ip, done: self.watched.append((host, local_ip))})() def car_type(self): if self._car_type is None: @@ -126,7 +128,7 @@ def save(supervisor, endpoint=True): def test_supervisor_fetches_only_for_the_aawireless_adapter(): - assert save(FakeSupervisor()) == [("10.42.1.1", "10.42.1.77")] + assert save(FakeSupervisor()) == [("10.42.1.1", "10.42.1.77", True)] assert save(FakeSupervisor(profile="unofficial")) == [] assert save(FakeSupervisor(profile="")) == [] assert save(FakeSupervisor(car_type="builtin")) == [] @@ -161,3 +163,52 @@ def test_a_trickling_download_stops_at_the_deadline(monkeypatch): assert clock.now - 100.0 <= adapter_syslog.DEADLINE_S + 1 else: raise AssertionError("trickling download was not cut off") + + +def test_while_connected_the_supervisor_starts_the_copier(): + import threading + from types import SimpleNamespace + from openpilot.starpilot.system.starport.supervisor import Supervisor + supervisor = FakeSupervisor() + result = SimpleNamespace(endpoint=SimpleNamespace(ip="10.42.1.1", port=5000)) + Supervisor._save_adapter_syslog(supervisor, result, SimpleNamespace(local_ip="10.42.1.77"), threading.Event()) + assert supervisor.watched == [("10.42.1.1", "10.42.1.77")] and supervisor.fetched == [] + other = FakeSupervisor(profile="unofficial") + Supervisor._save_adapter_syslog(other, result, SimpleNamespace(local_ip=""), threading.Event()) + assert other.watched == [] + + +def test_copies_are_taken_while_connected_and_stop_with_the_session(tmp_path, monkeypatch): + import threading + monkeypatch.setattr(adapter_syslog, "FIRST_COPY_S", 0.01) + monkeypatch.setattr(adapter_syslog, "MIN_INTERVAL_S", 0.01) + copied = threading.Event() + + def download(host, local_ip): + copied.set() + return 200, b"live line\n" + + syslog = AdapterSyslog(lambda *a, **k: None, log_dir=tmp_path, download=download) + done = threading.Event() + thread = syslog.watch("10.42.1.1", "", done) + assert copied.wait(2) + done.set() + thread.join(2) + assert not thread.is_alive() + assert (tmp_path / adapter_syslog.FILE_NAME).read_text().endswith("live line\n") + + +def test_the_end_of_session_copy_ignores_the_throttle(tmp_path): + calls = [] + syslog, _, clock = make(tmp_path, lambda host, local_ip: (calls.append(host), (200, b"x\n"))[1]) + assert syslog.fetch("10.42.1.1") + clock.now += 1 + assert not syslog.fetch("10.42.1.1") + assert syslog.fetch("10.42.1.1", force=True) + assert len(calls) == 2 + + +def test_overlapping_copies_are_skipped(tmp_path): + syslog, _, _ = make(tmp_path, lambda host, local_ip: (200, b"x\n")) + with syslog.busy: + assert not syslog.fetch("10.42.1.1", force=True)