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(), ); }