From 3291750082f1c6397472328f6a0310e5e43e15f9 Mon Sep 17 00:00:00 2001 From: Lau Ching Jun Date: Tue, 20 Jun 2023 09:42:28 -0700 Subject: [PATCH] Use the new `getIsolatePauseEvent` method from VM service to check for pause event. (#128834) The `getIsolate` method returns the full list of libraries which can be huge for large apps. Using the more speficic API to only fetch what we need improves hot reload performance. *Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue.* *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* --- packages/flutter_tools/lib/src/run_hot.dart | 18 ++++----- packages/flutter_tools/lib/src/vmservice.dart | 16 ++++++++ .../general.shard/resident_runner_test.dart | 40 ++++++++++--------- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index 0e1c2af642..124ec827bd 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -1298,9 +1298,8 @@ Future _defaultReassembleHelper( for (final FlutterView view in views) { // Check if the isolate is paused, and if so, don't reassemble. Ignore the // PostPauseEvent event - the client requesting the pause will resume the app. - final vm_service.Isolate? isolate = await device!.vmService! - .getIsolateOrNull(view.uiIsolate!.id!); - final vm_service.Event? pauseEvent = isolate?.pauseEvent; + final vm_service.Event? pauseEvent = await device!.vmService! + .getIsolatePauseEventOrNull(view.uiIsolate!.id!); if (pauseEvent != null && isPauseEvent(pauseEvent.kind!) && pauseEvent.kind != vm_service.EventKind.kPausePostRequest) { @@ -1364,16 +1363,13 @@ Future _defaultReassembleHelper( int postReloadPausedIsolatesFound = 0; String? serviceEventKind; for (final FlutterView view in reassembleViews.keys) { - final vm_service.Isolate? isolate = await reassembleViews[view]! - .getIsolateOrNull(view.uiIsolate!.id!); - if (isolate == null) { - continue; - } - if (isolate.pauseEvent != null && isPauseEvent(isolate.pauseEvent!.kind!)) { + final vm_service.Event? pauseEvent = await reassembleViews[view]! + .getIsolatePauseEventOrNull(view.uiIsolate!.id!); + if (pauseEvent != null && isPauseEvent(pauseEvent.kind!)) { postReloadPausedIsolatesFound += 1; if (serviceEventKind == null) { - serviceEventKind = isolate.pauseEvent!.kind; - } else if (serviceEventKind != isolate.pauseEvent!.kind) { + serviceEventKind = pauseEvent.kind; + } else if (serviceEventKind != pauseEvent.kind) { serviceEventKind = ''; // many kinds } } diff --git a/packages/flutter_tools/lib/src/vmservice.dart b/packages/flutter_tools/lib/src/vmservice.dart index 24ad33db66..6da8494d24 100644 --- a/packages/flutter_tools/lib/src/vmservice.dart +++ b/packages/flutter_tools/lib/src/vmservice.dart @@ -1110,6 +1110,22 @@ class FlutterVmService { }); } + /// Attempt to retrieve the isolate pause event with id [isolateId], or `null` if it has + /// been collected. + Future getIsolatePauseEventOrNull(String isolateId) async { + return service.getIsolatePauseEvent(isolateId) + .then( + (vm_service.Event event) => event, + onError: (Object? error, StackTrace stackTrace) { + if (error is vm_service.SentinelException || + error == null || + (error is vm_service.RPCError && error.code == RPCErrorCodes.kServiceDisappeared)) { + return null; + } + return Future.error(error, stackTrace); + }); + } + /// Create a new development file system on the device. Future createDevFS(String fsName) { // Call the unchecked version of `callServiceExtension` because the caller diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index e37e2f65c3..35970ba5c1 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -45,12 +45,19 @@ import '../src/fake_vm_services.dart'; import '../src/fakes.dart'; import '../src/testbed.dart'; +final vm_service.Event fakeUnpausedEvent = vm_service.Event( + kind: vm_service.EventKind.kResume, + timestamp: 0 +); + +final vm_service.Event fakePausedEvent = vm_service.Event( + kind: vm_service.EventKind.kPauseException, + timestamp: 0 +); + final vm_service.Isolate fakeUnpausedIsolate = vm_service.Isolate( id: '1', - pauseEvent: vm_service.Event( - kind: vm_service.EventKind.kResume, - timestamp: 0 - ), + pauseEvent: fakeUnpausedEvent, breakpoints: [], extensionRPCs: [], libraries: [ @@ -72,10 +79,7 @@ final vm_service.Isolate fakeUnpausedIsolate = vm_service.Isolate( final vm_service.Isolate fakePausedIsolate = vm_service.Isolate( id: '1', - pauseEvent: vm_service.Event( - kind: vm_service.EventKind.kPauseException, - timestamp: 0 - ), + pauseEvent: fakePausedEvent, breakpoints: [ vm_service.Breakpoint( breakpointNumber: 123, @@ -537,11 +541,11 @@ void main() { listViews, listViews, FakeVmServiceRequest( - method: 'getIsolate', + method: 'getIsolatePauseEvent', args: { 'isolateId': '1', }, - jsonResponse: fakeUnpausedIsolate.toJson(), + jsonResponse: fakeUnpausedEvent.toJson(), ), FakeVmServiceRequest( method: 'ext.flutter.reassemble', @@ -604,11 +608,11 @@ void main() { }, ), FakeVmServiceRequest( - method: 'getIsolate', + method: 'getIsolatePauseEvent', args: { 'isolateId': '1', }, - jsonResponse: fakeUnpausedIsolate.toJson(), + jsonResponse: fakeUnpausedEvent.toJson(), ), FakeVmServiceRequest( method: 'ext.flutter.reassemble', @@ -728,11 +732,11 @@ void main() { }, ), FakeVmServiceRequest( - method: 'getIsolate', + method: 'getIsolatePauseEvent', args: { 'isolateId': '1', }, - jsonResponse: fakeUnpausedIsolate.toJson(), + jsonResponse: fakeUnpausedEvent.toJson(), ), FakeVmServiceRequest( method: 'ext.flutter.reassemble', @@ -791,11 +795,11 @@ void main() { }, ), FakeVmServiceRequest( - method: 'getIsolate', + method: 'getIsolatePauseEvent', args: { 'isolateId': '1', }, - jsonResponse: fakeUnpausedIsolate.toJson(), + jsonResponse: fakeUnpausedEvent.toJson(), ), FakeVmServiceRequest( method: 'ext.flutter.fastReassemble', @@ -882,11 +886,11 @@ void main() { }, ), FakeVmServiceRequest( - method: 'getIsolate', + method: 'getIsolatePauseEvent', args: { 'isolateId': '1', }, - jsonResponse: fakeUnpausedIsolate.toJson(), + jsonResponse: fakeUnpausedEvent.toJson(), ), FakeVmServiceRequest( method: 'ext.flutter.fastReassemble',