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 <noreply@anthropic.com>
pull/935/head
diffwireauto 1 month ago committed by Benjamin Wiechel
parent 4ab969b7a1
commit 8cea58ec1d

@ -546,9 +546,13 @@ class AppExitInfoService {
Future<void> 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.

@ -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() ? <File>[active] : const <File>[];
}
}
/// Cancel the periodic flush and flush once more. The singleton normally lives

@ -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<List<File>> existingFiles() async {
final files = <File>[];
if (await activeFile.exists()) files.add(activeFile);
if (!await dir.exists()) return const [];
final byName = <String, File>{};
for (final entry in await dir.list(followLinks: false).toList()) {
if (entry is File) byName[entry.uri.pathSegments.last] = entry;
}
final ordered = <File>[];
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

@ -80,18 +80,26 @@ class LogExport {
final active = await FileLogService.instance.flushAndGetActiveFile();
logs = active == null ? const <File>[] : <File>[active];
}
final extras = <File>[
...logs.skip(1),
final candidates = <File>[
...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 = <File>[];
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(),
);
}

Loading…
Cancel
Save

Powered by TurnKey Linux.