Skip to content

Commit abe8675

Browse files
[stable] [flutter_tools] Guard against null vmService and uiIsolate in debugDumpFocusTree and debugDumpLayerTree (#194740)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: #192934 ### Impact Description: In `flutter run` debug sessions, triggering `debugDumpFocusTree` (`f`) or `debugDumpLayerTree` (`t`) when a `FlutterDevice`'s VM service connection or a `FlutterView`'s attached `uiIsolate` is null (such as during isolate initialization or teardown) causes an unhandled `_TypeError: Null check operator used on a null value` crash in `flutter_tools`. This was observed as a top crasher in Flutter 3.47.3 crash telemetry (28 crashes in the first 7 days post-release). ### Changelog Description: [flutter/192934] When dumping the focus tree or layer tree from `flutter run` on any platform before the UI isolate finishes initializing or after teardown, `flutter_tools` crashed with a null check operator exception. ### Workaround: Wait until the app and UI isolate have fully initialized before invoking `f` (`debugDumpFocusTree`) or `t` (`debugDumpLayerTree`) in the `flutter run` terminal. ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: 1. Run `flutter test packages/flutter_tools/test/general.shard/terminal_handler_test.dart`. 2. Verify that `debugDumpFocusTree` and `debugDumpLayerTree` return gracefully without throwing when `vmService` or `uiIsolate` is null.
1 parent aa08f09 commit abe8675

2 files changed

Lines changed: 133 additions & 74 deletions

File tree

‎packages/flutter_tools/lib/src/resident_runner.dart‎

Lines changed: 43 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -610,107 +610,76 @@ abstract class ResidentHandlers {
610610
}
611611

612612
/// Dump the application's current widget tree to the terminal.
613-
Future<bool> debugDumpApp() async {
614-
if (!supportsServiceProtocol) {
615-
return false;
616-
}
617-
for (final FlutterDevice? device in flutterDevices) {
618-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
619-
for (final view in views) {
620-
final String data = await device.vmService!.flutterDebugDumpApp(
621-
isolateId: view.uiIsolate!.id!,
622-
);
623-
logger.printStatus(data);
624-
}
625-
}
626-
return true;
627-
}
613+
Future<bool> debugDumpApp() => _debugDumpTree(
614+
(FlutterVmService vmService, String isolateId) =>
615+
vmService.flutterDebugDumpApp(isolateId: isolateId),
616+
);
628617

629618
/// Dump the application's current render tree to the terminal.
630-
Future<bool> debugDumpRenderTree() async {
631-
if (!supportsServiceProtocol) {
632-
return false;
633-
}
634-
for (final FlutterDevice? device in flutterDevices) {
635-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
636-
for (final view in views) {
637-
final String data = await device.vmService!.flutterDebugDumpRenderTree(
638-
isolateId: view.uiIsolate!.id!,
639-
);
640-
logger.printStatus(data);
641-
}
642-
}
643-
return true;
644-
}
619+
Future<bool> debugDumpRenderTree() => _debugDumpTree(
620+
(FlutterVmService vmService, String isolateId) =>
621+
vmService.flutterDebugDumpRenderTree(isolateId: isolateId),
622+
);
645623

646624
/// Dump the application's current layer tree to the terminal.
647625
Future<bool> debugDumpLayerTree() async {
648-
if (!supportsServiceProtocol || !isRunningDebug) {
626+
if (!isRunningDebug) {
649627
return false;
650628
}
651-
for (final FlutterDevice? device in flutterDevices) {
652-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
653-
for (final view in views) {
654-
final String data = await device.vmService!.flutterDebugDumpLayerTree(
655-
isolateId: view.uiIsolate!.id!,
656-
);
657-
logger.printStatus(data);
658-
}
659-
}
660-
return true;
629+
return _debugDumpTree(
630+
(FlutterVmService vmService, String isolateId) =>
631+
vmService.flutterDebugDumpLayerTree(isolateId: isolateId),
632+
);
661633
}
662634

635+
/// Dump the application's current focus tree to the terminal.
663636
Future<bool> debugDumpFocusTree() async {
664-
if (!supportsServiceProtocol || !isRunningDebug) {
637+
if (!isRunningDebug) {
665638
return false;
666639
}
667-
for (final FlutterDevice? device in flutterDevices) {
668-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
669-
for (final view in views) {
670-
final String data = await device.vmService!.flutterDebugDumpFocusTree(
671-
isolateId: view.uiIsolate!.id!,
672-
);
673-
logger.printStatus(data);
674-
}
675-
}
676-
return true;
640+
return _debugDumpTree(
641+
(FlutterVmService vmService, String isolateId) =>
642+
vmService.flutterDebugDumpFocusTree(isolateId: isolateId),
643+
);
677644
}
678645

679646
/// Dump the application's current semantics tree to the terminal.
680647
///
681648
/// If semantics are not enabled, nothing is returned.
682-
Future<bool> debugDumpSemanticsTreeInTraversalOrder() async {
683-
if (!supportsServiceProtocol) {
684-
return false;
685-
}
686-
for (final FlutterDevice? device in flutterDevices) {
687-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
688-
for (final view in views) {
689-
final String data = await device.vmService!.flutterDebugDumpSemanticsTreeInTraversalOrder(
690-
isolateId: view.uiIsolate!.id!,
691-
);
692-
logger.printStatus(data);
693-
}
694-
}
695-
return true;
696-
}
649+
Future<bool> debugDumpSemanticsTreeInTraversalOrder() => _debugDumpTree(
650+
(FlutterVmService vmService, String isolateId) =>
651+
vmService.flutterDebugDumpSemanticsTreeInTraversalOrder(isolateId: isolateId),
652+
);
697653

698654
/// Dump the application's current semantics tree to the terminal.
699655
///
700656
/// If semantics are not enabled, nothing is returned.
701-
Future<bool> debugDumpSemanticsTreeInInverseHitTestOrder() async {
657+
Future<bool> debugDumpSemanticsTreeInInverseHitTestOrder() => _debugDumpTree(
658+
(FlutterVmService vmService, String isolateId) =>
659+
vmService.flutterDebugDumpSemanticsTreeInInverseHitTestOrder(isolateId: isolateId),
660+
);
661+
662+
/// Returns whether at least one Flutter view was dumped.
663+
Future<bool> _debugDumpTree(
664+
Future<String> Function(FlutterVmService vmService, String isolateId) dumpCall,
665+
) async {
702666
if (!supportsServiceProtocol) {
703667
return false;
704668
}
669+
var dumped = false;
705670
for (final FlutterDevice? device in flutterDevices) {
706-
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
707-
for (final view in views) {
708-
final String data = await device.vmService!
709-
.flutterDebugDumpSemanticsTreeInInverseHitTestOrder(isolateId: view.uiIsolate!.id!);
710-
logger.printStatus(data);
671+
if (device?.vmService case final vmService?) {
672+
final List<FlutterView> views = await vmService.getFlutterViews();
673+
for (final view in views) {
674+
if (view.uiIsolate?.id case final isolateId?) {
675+
final String data = await dumpCall(vmService, isolateId);
676+
logger.printStatus(data);
677+
dumped = true;
678+
}
679+
}
711680
}
712681
}
713-
return true;
682+
return dumped;
714683
}
715684

716685
/// Toggle the "paint size" debugging feature.

‎packages/flutter_tools/test/general.shard/terminal_handler_test.dart‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ final listViews = FakeVmServiceRequest(
5454
},
5555
);
5656

57+
final fakeFlutterViewWithoutIsolate = FlutterView(id: 'a', uiIsolate: null);
58+
59+
final listViewsWithoutIsolate = FakeVmServiceRequest(
60+
method: kListViewsMethod,
61+
jsonResponse: <String, Object>{
62+
'views': <Object>[fakeFlutterViewWithoutIsolate.toJson()],
63+
},
64+
);
65+
5766
void main() {
5867
testWithoutContext('keyboard input handling single help character', () async {
5968
final testRunner = TestRunner();
@@ -414,6 +423,87 @@ void main() {
414423
await terminalHandler.processTerminalInput('f');
415424
});
416425

426+
testWithoutContext('debugDump* with null vmService does not crash', () async {
427+
final TerminalHandler terminalHandler = setUpTerminalHandler(
428+
<FakeVmServiceRequest>[],
429+
nullVmService: true,
430+
);
431+
expect(await terminalHandler.residentRunner.debugDumpApp(), isFalse);
432+
expect(await terminalHandler.residentRunner.debugDumpRenderTree(), isFalse);
433+
expect(await terminalHandler.residentRunner.debugDumpLayerTree(), isFalse);
434+
expect(await terminalHandler.residentRunner.debugDumpFocusTree(), isFalse);
435+
expect(
436+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInTraversalOrder(),
437+
isFalse,
438+
);
439+
expect(
440+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInInverseHitTestOrder(),
441+
isFalse,
442+
);
443+
});
444+
445+
testWithoutContext('debugDump* with null uiIsolate does not crash', () async {
446+
final TerminalHandler terminalHandler = setUpTerminalHandler(<FakeVmServiceRequest>[
447+
for (var i = 0; i < 6; i++) listViewsWithoutIsolate,
448+
]);
449+
expect(await terminalHandler.residentRunner.debugDumpApp(), isFalse);
450+
expect(await terminalHandler.residentRunner.debugDumpRenderTree(), isFalse);
451+
expect(await terminalHandler.residentRunner.debugDumpLayerTree(), isFalse);
452+
expect(await terminalHandler.residentRunner.debugDumpFocusTree(), isFalse);
453+
expect(
454+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInTraversalOrder(),
455+
isFalse,
456+
);
457+
expect(
458+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInInverseHitTestOrder(),
459+
isFalse,
460+
);
461+
});
462+
463+
testWithoutContext(
464+
'debugDump* in profile mode dumps supported trees and skips debug-only trees',
465+
() async {
466+
final TerminalHandler terminalHandler = setUpTerminalHandler(<FakeVmServiceRequest>[
467+
listViews,
468+
const FakeVmServiceRequest(
469+
method: 'ext.flutter.debugDumpApp',
470+
args: <String, Object>{'isolateId': '1'},
471+
jsonResponse: <String, Object>{'data': 'WIDGET DATA'},
472+
),
473+
listViews,
474+
const FakeVmServiceRequest(
475+
method: 'ext.flutter.debugDumpRenderTree',
476+
args: <String, Object>{'isolateId': '1'},
477+
jsonResponse: <String, Object>{'data': 'RENDER DATA'},
478+
),
479+
listViews,
480+
const FakeVmServiceRequest(
481+
method: 'ext.flutter.debugDumpSemanticsTreeInTraversalOrder',
482+
args: <String, Object>{'isolateId': '1'},
483+
jsonResponse: <String, Object>{'data': 'SEMANTICS DATA 1'},
484+
),
485+
listViews,
486+
const FakeVmServiceRequest(
487+
method: 'ext.flutter.debugDumpSemanticsTreeInInverseHitTestOrder',
488+
args: <String, Object>{'isolateId': '1'},
489+
jsonResponse: <String, Object>{'data': 'SEMANTICS DATA 2'},
490+
),
491+
], buildMode: BuildMode.profile);
492+
expect(await terminalHandler.residentRunner.debugDumpApp(), isTrue);
493+
expect(await terminalHandler.residentRunner.debugDumpRenderTree(), isTrue);
494+
expect(
495+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInTraversalOrder(),
496+
isTrue,
497+
);
498+
expect(
499+
await terminalHandler.residentRunner.debugDumpSemanticsTreeInInverseHitTestOrder(),
500+
isTrue,
501+
);
502+
expect(await terminalHandler.residentRunner.debugDumpLayerTree(), isFalse);
503+
expect(await terminalHandler.residentRunner.debugDumpFocusTree(), isFalse);
504+
},
505+
);
506+
417507
testWithoutContext('o,O - debugTogglePlatform', () async {
418508
final TerminalHandler terminalHandler = setUpTerminalHandler(<FakeVmServiceRequest>[
419509
// Request 1.

0 commit comments

Comments
 (0)