diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index d217e53d84..6d4aed4064 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -500,7 +500,19 @@ class HotRunner extends ResidentRunner { .getIsolateOrNull(view.uiIsolate.id); operations.add(reloadIsolate.then((vm_service.Isolate isolate) async { if ((isolate != null) && isPauseEvent(isolate.pauseEvent.kind)) { - // Resume the isolate so that it can be killed by the embedder. + // The embedder requires that the isolate is unpaused, because the + // runInView method requires interaction with dart engine APIs that + // are not thread-safe, and thus must be run on the same thread that + // would be blocked by the pause. Simply unpausing is not sufficient, + // because this does not prevent the isolate from immediately hitting + // a breakpoint, for example if the breakpoint was placed in a loop + // or in a frequently called method. Instead, all breakpoints are first + // disabled and then the isolate resumed. + final List> breakpointRemoval = >[ + for (final vm_service.Breakpoint breakpoint in isolate.breakpoints) + device.vmService.removeBreakpoint(isolate.id, breakpoint.id) + ]; + await Future.wait(breakpointRemoval); await device.vmService.resume(view.uiIsolate.id); } })); 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 c0dc95878d..ce491f24b0 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -65,7 +65,17 @@ final vm_service.Isolate fakePausedIsolate = vm_service.Isolate( kind: vm_service.EventKind.kPauseException, timestamp: 0 ), - breakpoints: [], + breakpoints: [ + vm_service.Breakpoint( + breakpointNumber: 123, + id: 'test-breakpoint', + location: vm_service.SourceLocation( + tokenPos: 0, + script: vm_service.ScriptRef(id: 'test-script', uri: 'lib/foo.dart'), + ), + resolved: true, + ), + ], exceptionPauseMode: null, libraries: [], livePorts: 0, @@ -968,6 +978,81 @@ void main() { Usage: () => MockUsage(), })); + testUsingContext('ResidentRunner can remove breakpoints from paused isolate during hot restart', () => testbed.run(() async { + fakeVmServiceHost = FakeVmServiceHost(requests: [ + listViews, + listViews, + listViews, + FakeVmServiceRequest( + method: 'getIsolate', + args: { + 'isolateId': fakeUnpausedIsolate.id, + }, + jsonResponse: fakePausedIsolate.toJson(), + ), + FakeVmServiceRequest( + method: 'getVM', + jsonResponse: vm_service.VM.parse({}).toJson(), + ), + const FakeVmServiceRequest( + method: 'removeBreakpoint', + args: { + 'isolateId': '1', + 'breakpointId': 'test-breakpoint', + } + ), + const FakeVmServiceRequest( + method: 'resume', + args: { + 'isolateId': '1', + } + ), + listViews, + const FakeVmServiceRequest( + method: 'streamListen', + args: { + 'streamId': 'Isolate', + }, + ), + FakeVmServiceRequest( + method: kRunInViewMethod, + args: { + 'viewId': fakeFlutterView.id, + 'mainScript': 'lib/main.dart.dill', + 'assetDirectory': 'build/flutter_assets', + }, + ), + FakeVmServiceStreamResponse( + streamId: 'Isolate', + event: vm_service.Event( + timestamp: 0, + kind: vm_service.EventKind.kIsolateRunnable, + ) + ) + ]); + when(mockDevice.sdkNameAndVersion).thenAnswer((Invocation invocation) async { + return 'Example'; + }); + when(mockDevice.targetPlatform).thenAnswer((Invocation invocation) async { + return TargetPlatform.android_arm; + }); + when(mockDevice.isLocalEmulator).thenAnswer((Invocation invocation) async { + return false; + }); + when(mockDevice.supportsHotRestart).thenReturn(true); + final Completer onConnectionInfo = Completer.sync(); + final Completer onAppStart = Completer.sync(); + unawaited(residentRunner.attach( + appStartedCompleter: onAppStart, + connectionInfoCompleter: onConnectionInfo, + )); + + final OperationResult result = await residentRunner.restart(fullRestart: true); + + expect(result.isOk, true); + expect(fakeVmServiceHost.hasRemainingExpectations, false); + })); + testUsingContext('ResidentRunner Can handle an RPC exception from hot restart', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [ listViews,