From cf1cf6b3e443650bcb64437d43d1c0849b9d5ea0 Mon Sep 17 00:00:00 2001 From: DoronZ Date: Mon, 13 Jan 2025 00:50:10 +0200 Subject: [PATCH] crash_reports: fix `crash watch` for incomplete files --- pymobiledevice3/services/crash_reports.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pymobiledevice3/services/crash_reports.py b/pymobiledevice3/services/crash_reports.py index 2dd644e3d..c4e0cdd4e 100644 --- a/pymobiledevice3/services/crash_reports.py +++ b/pymobiledevice3/services/crash_reports.py @@ -3,13 +3,15 @@ import re import time from collections.abc import Generator +from json import JSONDecodeError from typing import Callable, Optional from pycrashreport.crash_report import get_crash_report_from_buf from xonsh.built_ins import XSH from xonsh.cli_utils import Annotated, Arg -from pymobiledevice3.exceptions import AfcException, NotificationTimeoutError, SysdiagnoseTimeoutError +from pymobiledevice3.exceptions import AfcException, AfcFileNotFoundError, NotificationTimeoutError, \ + SysdiagnoseTimeoutError from pymobiledevice3.lockdown import LockdownClient from pymobiledevice3.lockdown_service_provider import LockdownServiceProvider from pymobiledevice3.services.afc import AfcService, AfcShell, path_completer @@ -124,8 +126,14 @@ def watch(self, name: str = None, raw: bool = False) -> Generator[str, None, Non if posixpath.splitext(filename)[-1] not in ('.ips', '.panic'): continue - crash_report_raw = self.afc.get_file_contents(filename).decode() - crash_report = get_crash_report_from_buf(crash_report_raw, filename=filename) + while True: + try: + crash_report_raw = self.afc.get_file_contents(filename).decode() + crash_report = get_crash_report_from_buf(crash_report_raw, filename=filename) + break + except (AfcFileNotFoundError, JSONDecodeError): + # Sometimes we have to wait for the file to be readable + pass if name is None or crash_report.name == name: if raw: