Skip to content

Commit d89e7f4

Browse files
[beta] [flutter_tools] Guard against null vmService and uiIsolate in debugDumpFocusTree and debugDumpLayerTree (#194741)
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 07502c7 commit d89e7f4

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
@@ -611,107 +611,76 @@ abstract class ResidentHandlers {
611611
}
612612

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

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

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

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

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

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

717686
/// 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
@@ -55,6 +55,15 @@ final listViews = FakeVmServiceRequest(
5555
},
5656
);
5757

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

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

0 commit comments

Comments
 (0)