From 4ab969b7a130c91ea9c006d95546d340124eb903 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Mon, 19 Oct 2026 23:38:36 -0400 Subject: [PATCH] feat(#931): second ANR trigger that blocks the Dart isolate, not Kotlin The existing trigger hangs the Android main thread, so its trace is a Java stack. That proved the capture works and that our Kotlin frames resolve via the R8 mapping, but it cannot say whether our own DART frames survive into a trace, which is the question #906 was filed to answer. This one blocks the Dart UI isolate the way the real ANRs do rather than with an artificial spin: it repeatedly JSON-encodes a large nested structure shaped like the stores #334 is about. The Play clusters show the real ANRs inside the JSON stringifier and under allocation pressure, so a trace from this block should match their shape, and whether any of our Dart frames appear above those is then readable. It spins deliberately. Any await in the loop would yield to the event loop, the isolate would keep answering input, and no ANR would fire. If the process survives the full duration it logs why, so the tester is not left guessing. Gating is unchanged and still doubled: the bool.fromEnvironment constant keeps both rows out of any shipped build, and the owner-only Experimental section hides them again in a test build. Both rows are relabelled native side and Dart side so they are distinguishable. Tests: both triggers asserted to be immediate no-ops off Android, which matters because a missing platform guard would hang a desktop build instead of failing visibly. Co-Authored-By: Claude Opus 5 --- lib/screens/settings_screen.dart | 34 +++++++++--- lib/services/app_exit_info_service.dart | 52 +++++++++++++++++++ test/services/app_exit_info_service_test.dart | 22 ++++++++ 3 files changed, 102 insertions(+), 6 deletions(-) diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index 98534e4e..9d91ca43 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -311,17 +311,39 @@ class _SettingsScreenState extends State { Card( child: ListTile( leading: const Icon(Icons.hourglass_disabled), - title: const Text('Force an ANR (test build only)'), + title: const Text('Force an ANR, native side (test build only)'), subtitle: const Text( - 'Freezes the UI for 20 seconds. KEEP TAPPING the screen while ' - 'it is frozen: Android raises an ANR only when an input event ' - 'goes unanswered for 5 seconds, so a freeze with nothing ' - 'pending just recovers. Then let the dialog close the app, ' - 'reopen, and export the debug log.', + 'Freezes the UI for 20 seconds in the Android layer. KEEP ' + 'TAPPING the screen while it is frozen: Android raises an ANR ' + 'only when an input event goes unanswered for 5 seconds, so a ' + 'freeze with nothing pending just recovers. Then let the dialog ' + 'close the app, reopen, and export the debug log.', ), onTap: () => AppExitInfoService.instance.forceMainThreadBlock(), ), ), + if (_showAnrTestRow) const SizedBox(height: 16), + if (_showAnrTestRow) + // The second trigger exists because the first one cannot answer the + // question the Epic was filed for (#931). Blocking the Android main + // thread yields a Java stack, which proved the capture works but says + // nothing about our Dart frames. The real ANRs block on the Dart UI + // isolate inside the JSON stringifier, so this one blocks there, the + // same way, and its trace should match the Play clusters' shape. + Card( + child: ListTile( + leading: const Icon(Icons.data_object), + title: const Text('Force an ANR, Dart side (test build only)'), + subtitle: const Text( + 'Freezes the UI for 20 seconds by encoding JSON on the Dart ' + 'isolate, which is what the real ANRs do. Same procedure: KEEP ' + 'TAPPING while frozen, let the dialog close the app, reopen, ' + 'then export. Use this one to see whether our own Dart function ' + 'names show up in the trace.', + ), + onTap: () => AppExitInfoService.instance.forceDartIsolateBlock(), + ), + ), const SizedBox(height: 16), // Disable, not just hide: turns off every experimental toggle and // re-locks the section, so nothing keeps running invisibly (#553). diff --git a/lib/services/app_exit_info_service.dart b/lib/services/app_exit_info_service.dart index fa15e9bf..c490ea0c 100644 --- a/lib/services/app_exit_info_service.dart +++ b/lib/services/app_exit_info_service.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:convert'; import 'dart:io'; import 'package:flutter/foundation.dart'; @@ -114,6 +115,14 @@ abstract final class ExitInfoMessages { static String forceBlockFailed(Object error) => 'forceMainThreadBlock failed: $error'; + /// Not an error. Logged when the Dart isolate block ran its full duration + /// without the system raising an ANR, which means no input went unanswered. + /// Says why, so the tester is not left guessing (#931). + static String dartBlockSurvived(int millis, int encodedBytes) => + 'dart isolate block ran ${millis}ms and encoded $encodedBytes bytes, ' + 'but no ANR was raised: Android raises one only when an input event goes ' + 'unanswered for 5 seconds, so keep tapping the screen during the freeze'; + /// Fragments that must never appear in a line produced on the happy path. static const List failureMarkers = [ 'unavailable:', @@ -521,6 +530,49 @@ class AppExitInfoService { } } + /// Test-only, same double gating as [forceMainThreadBlock]. Blocks the **Dart + /// UI isolate** instead of the Android main thread, and blocks it the way the + /// real ANRs do rather than with an artificial spin (#931). + /// + /// Why this exists separately. The Kotlin block produces a Java stack, so it + /// proved the capture works but could say nothing about whether our own Dart + /// frames survive into a trace. The Play clusters show the real ANRs blocking + /// on this isolate inside the JSON stringifier and under allocation pressure, + /// so encoding a large nested structure in a loop should yield a trace of the + /// same shape, and answer whether any of our Dart frames resolve. + /// + /// It spins on purpose. Any `await` inside the loop would yield to the event + /// loop, the isolate would keep answering input, and no ANR would fire. + Future forceDartIsolateBlock({int millis = 20000}) async { + if (!isSupportedPlatform) return; + final payload = _blockPayload(); + final deadline = DateTime.now().add(Duration(milliseconds: millis)); + var encoded = 0; + while (DateTime.now().isBefore(deadline)) { + encoded += jsonEncode(payload).length; + } + // Only reached when the process survived, so the tester gets told why. + _log('WARN', ExitInfoMessages.dartBlockSurvived(millis, encoded)); + await FileLogService.instance.flushAndGetActiveFile(); + } + + /// Shaped like the stores #334 is about: many keyed entries, each a nested + /// object, which is what gets JSON-encoded on this isolate in the real bug. + static Map _blockPayload() => { + for (var i = 0; i < 400; i++) + 'entry_$i': { + 'id': i, + 'name': 'contact-$i', + 'path': List.generate(24, (j) => (i + j) & 0xFF), + 'meta': { + 'lastSeen': i * 1000, + 'snr': -(i % 40), + 'flags': const ['a', 'b', 'c'], + }, + 'text': 'x' * 64, + }, + }; + /// Visible for tests so a second `collect()` can be exercised. @visibleForTesting void resetForTest() => _ran = false; diff --git a/test/services/app_exit_info_service_test.dart b/test/services/app_exit_info_service_test.dart index 79af5137..ac758fa4 100644 --- a/test/services/app_exit_info_service_test.dart +++ b/test/services/app_exit_info_service_test.dart @@ -348,6 +348,7 @@ void main() { ExitInfoMessages.watermarkReadFailed('boom'), ExitInfoMessages.watermarkWriteFailed('boom'), ExitInfoMessages.forceBlockFailed('boom'), + ExitInfoMessages.dartBlockSurvived(20000, 1234), ]; for (final m in messages) { expect(m, isNotEmpty); @@ -525,5 +526,26 @@ void main() { await AppExitInfoService.instance.collect(); expect(calls, isEmpty); }); + + // #931: the Dart block is a 20 second spin by design, so a missing platform + // guard would hang a desktop build rather than fail visibly. This proves it + // returns immediately off Android; the block itself is hardware-tested. + test('the Dart isolate block is an immediate no-op off Android', () async { + debugDefaultTargetPlatformOverride = TargetPlatform.windows; + final started = DateTime.now(); + await AppExitInfoService.instance.forceDartIsolateBlock(millis: 20000); + expect( + DateTime.now().difference(started).inSeconds, + lessThan(2), + reason: 'it must not spin on a platform with no exit records', + ); + }); + + test('the native block is an immediate no-op off Android', () async { + debugDefaultTargetPlatformOverride = TargetPlatform.windows; + handle((_) async => 20000); + await AppExitInfoService.instance.forceMainThreadBlock(); + expect(calls, isEmpty); + }); }); }