"""Explicit real-system restart test, synchronized by a transient test session name.""" import json import threading import time from remote_support import connect, execute class RestartProbe: def __init__(self): self.stop = threading.Event() self.error = None self.completed = False self.thread = threading.Thread(target=self.run, daemon=True) def start(self): self.thread.start() def run(self): client = None try: client, fields = connect() code, _, _ = execute(client, 'command -v python3 >/dev/null && command -v systemctl >/dev/null') if code: raise RuntimeError('Required remote tools unavailable') deadline = time.monotonic() + 120 while not self.stop.wait(1): if time.monotonic() >= deadline: raise RuntimeError('Authenticated restart readiness was not observed') code, output, _ = execute(client, "python3 -c \"import urllib.request,json; s=json.load(urllib.request.urlopen('http://127.0.0.1:8080/api/status',timeout=5)); m=s.get('mobile',{}); print(json.dumps({'ready':m.get('connected') and m.get('active_transport')=='wifi' and m.get('client_name')=='android-test-restart-ready'}))\"") if code or not json.loads(output).get('ready'): continue code, _, _ = execute(client, 'systemctl restart matrix-screen-controller.service && systemctl is-active --quiet matrix-screen-controller.service', password=fields['密码'], timeout=60) if code: raise RuntimeError('Real service restart failed') self.completed = True return except Exception: self.error = 'Real service restart coordination failed; no private diagnostics emitted' finally: if client is not None: client.close() def finish(self): self.stop.set() self.thread.join(timeout=65) if self.thread.is_alive() or self.error or not self.completed: raise RuntimeError(self.error or 'Real service restart did not complete')