From 8cea58ec1df4ac3e90e0a27350e6c7b21dcd0aa0 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Tue, 20 Oct 2026 00:54:05 -0400 Subject: [PATCH] fix(#931): two defects from the second pre-PR review 1. existingFiles() walked hardcoded names with per-name exists(), so the periodic flush could rotate mid-walk and return a name it had just confirmed and rotation then renamed. Handing the share sheet a path that no longer resolves fails the WHOLE export, which is the exact failure the rotated-log fix existed to prevent. Now one directory listing, filtered and ordered by name. flushAndGetAllFiles() degrades to the active file if listing throws, because the logger cannot report its own listing failure anywhere, and one log beats none. shareLogs() also re-checks existence at the last moment, since a trace can be pruned between collection and share. 2. forceDartIsolateBlock used DateTime.now() for its deadline. The wall clock can move backwards, which would push the deadline away and leave the app frozen past the window or indefinitely, needing a force-kill on a test build. Now a Stopwatch, which is monotonic, matching what the Kotlin block already did with nanoTime. analyze clean, 1307 tests pass. Co-Authored-By: Claude Opus 5 --- lib/services/app_exit_info_service.dart | 8 ++++++-- lib/services/file_log_service.dart | 10 +++++++++- lib/utils/file_log_writer.dart | 21 ++++++++++++++++----- lib/utils/log_export.dart | 16 ++++++++++++---- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/lib/services/app_exit_info_service.dart b/lib/services/app_exit_info_service.dart index c490ea0c..dd2fa9cd 100644 --- a/lib/services/app_exit_info_service.dart +++ b/lib/services/app_exit_info_service.dart @@ -546,9 +546,13 @@ class AppExitInfoService { Future forceDartIsolateBlock({int millis = 20000}) async { if (!isSupportedPlatform) return; final payload = _blockPayload(); - final deadline = DateTime.now().add(Duration(milliseconds: millis)); + // Stopwatch, not DateTime.now(): the wall clock can move backwards (NTP, or + // the tester changing it) and would push the deadline away, leaving the app + // frozen well past the intended window or indefinitely. The Kotlin block + // already uses a monotonic source, so this matches it. + final elapsed = Stopwatch()..start(); var encoded = 0; - while (DateTime.now().isBefore(deadline)) { + while (elapsed.elapsedMilliseconds < millis) { encoded += jsonEncode(payload).length; } // Only reached when the process survived, so the tester gets told why. diff --git a/lib/services/file_log_service.dart b/lib/services/file_log_service.dart index e81aff77..3827a627 100644 --- a/lib/services/file_log_service.dart +++ b/lib/services/file_log_service.dart @@ -84,7 +84,15 @@ class FileLogService { await _flush(); final w = _writer; if (w == null) return const []; - return w.existingFiles(); + try { + return await w.existingFiles(); + } catch (_) { + // Degrade to the active file rather than failing the export outright: + // this is the logger, so it cannot report its own listing failure + // anywhere, and handing over one log beats handing over none. + final active = w.activeFile; + return await active.exists() ? [active] : const []; + } } /// Cancel the periodic flush and flush once more. The singleton normally lives diff --git a/lib/utils/file_log_writer.dart b/lib/utils/file_log_writer.dart index bc9a2ce1..c64693ff 100644 --- a/lib/utils/file_log_writer.dart +++ b/lib/utils/file_log_writer.dart @@ -31,14 +31,25 @@ class FileLogWriter { /// not have to duplicate the scheme. Needed because a line written once, at /// startup, can rotate out of the active file long before the user exports, /// and until now nothing could reach the rotated files at all (#906). + /// + /// One directory listing rather than a per-name `exists()` walk: the periodic + /// flush can rotate while this is awaiting, and a walk could return a name it + /// had just confirmed and that rotation then renamed, which would hand the + /// exporter a path that no longer resolves and fail the whole share. Future> existingFiles() async { - final files = []; - if (await activeFile.exists()) files.add(activeFile); + if (!await dir.exists()) return const []; + final byName = {}; + for (final entry in await dir.list(followLinks: false).toList()) { + if (entry is File) byName[entry.uri.pathSegments.last] = entry; + } + final ordered = []; + final active = byName['$baseName.log']; + if (active != null) ordered.add(active); for (var n = 1; n < maxFiles; n++) { - final f = _rotated(n); - if (await f.exists()) files.add(f); + final rotated = byName['$baseName.$n.log']; + if (rotated != null) ordered.add(rotated); } - return files; + return ordered; } /// Append [line] (a trailing newline is added), rotating first if it would diff --git a/lib/utils/log_export.dart b/lib/utils/log_export.dart index b49bc878..8d17f319 100644 --- a/lib/utils/log_export.dart +++ b/lib/utils/log_export.dart @@ -80,18 +80,26 @@ class LogExport { final active = await FileLogService.instance.flushAndGetActiveFile(); logs = active == null ? const [] : [active]; } - final extras = [ - ...logs.skip(1), + final candidates = [ + ...logs, if (_isMobile) ...await AppExitInfoService.traceFiles(), ]; + // Last-moment existence check. Rotation renames files underneath us and a + // trace can be pruned, so a path collected a moment ago may already be + // gone; handing a missing path to the share sheet fails the whole export + // rather than one attachment. + final present = []; + for (final f in candidates) { + if (await f.exists()) present.add(f); + } await _exportFile( messenger, - file: logs.isEmpty ? null : logs.first, + file: present.isEmpty ? null : present.first, fileName: _defaultFileName, subject: l10n.debugLog_shareSubject, unavailableMessage: l10n.debugLog_logUnavailable, savedMessage: l10n.debugLog_logSaved, - extraFiles: extras, + extraFiles: present.skip(1).toList(), ); }