diff --git a/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart b/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart index 5ea0d2c75d..1f1f79af85 100644 --- a/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart +++ b/packages/flutter_tools/lib/src/debug_adapters/flutter_adapter.dart @@ -59,11 +59,9 @@ class FlutterDebugAdapter extends DartDebugAdapter appStartedCompleter.isCompleted; - /// The VM Service URI received from the app.debugPort event. - Uri? _vmServiceUri; - /// The appId of the current running Flutter app. - String? _appId; + @visibleForTesting + String? appId; /// The ID to use for the next request sent to the Flutter run daemon. int _flutterRequestId = 1; @@ -311,13 +309,6 @@ class FlutterDebugAdapter extends DartDebugAdapter(>[ - sendFlutterRequest(method, {'appId': _appId}), - _process?.exitCode ?? Future.value(), - ]); + if (appId != null) { + final String method = isAttach ? 'app.detach' : 'app.stop'; + await Future.any(>[ + sendFlutterRequest(method, {'appId': appId}), + _process?.exitCode ?? Future.value(), + ]); + } terminatePids(ProcessSignal.sigterm); await _process?.exitCode; } /// Connects to the VM Service if the app.started event has fired, and a VM Service URI is available. - void _connectDebuggerIfReady() { - final Uri? serviceUri = _vmServiceUri; - - if (_receivedAppStarted && serviceUri != null) { + Future _connectDebugger(Uri vmServiceUri) async { if (enableDebugger) { - connectDebugger(serviceUri); + await connectDebugger(vmServiceUri); } else { // Usually, `connectDebugger` (in the base Dart adapter) will send this // event when it connects a debugger. Since we're not connecting a @@ -431,29 +421,30 @@ class FlutterDebugAdapter extends DartDebugAdapter{ - 'vmServiceUri': serviceUri.toString(), + 'vmServiceUri': vmServiceUri.toString(), }), eventType: 'dart.debuggerUris', ); } - } } /// Handles the app.start event from Flutter. void _handleAppStart(Map params) { - _appId = params['appId'] as String?; - assert(_appId != null); + appId = params['appId'] as String?; + if(appId == null) { + throw DebugAdapterException('Unexpected null `appId` in app.start event'); + } } /// Handles the app.started event from Flutter. - void _handleAppStarted() { + Future _handleAppStarted() async { appStartedCompleter.complete(); - _connectDebuggerIfReady(); // Send a custom event so the editor knows the app has started. // // This may be useful when there's no VM Service (for example Profile mode) // but the editor still wants to know that startup has finished. + await debuggerInitialized; // Ensure we're fully initialized before sending. sendEvent( RawEventBody({}), eventType: 'flutter.appStarted', @@ -472,13 +463,16 @@ class FlutterDebugAdapter extends DartDebugAdapter params) { + Future _handleDebugPort(Map params) async { // Capture the VM Service URL which we'll connect to when we get app.started. final String? wsUri = params['wsUri'] as String?; if (wsUri != null) { - _vmServiceUri = Uri.parse(wsUri); + final Uri vmServiceUri = Uri.parse(wsUri); + // Also wait for app.started before we connect, to ensure Flutter's + // initialization is all complete. + await appStartedCompleter.future; + await _connectDebugger(vmServiceUri); } - _connectDebuggerIfReady(); } /// Handles the Flutter process exiting, terminating the debug session if it has not already begun terminating. @@ -601,7 +595,7 @@ class FlutterDebugAdapter extends DartDebugAdapter{ - 'appId': _appId, + 'appId': appId, 'fullRestart': fullRestart, 'pause': enableDebugger, 'reason': reason, diff --git a/packages/flutter_tools/test/general.shard/dap/flutter_adapter_test.dart b/packages/flutter_tools/test/general.shard/dap/flutter_adapter_test.dart index fe770b7fda..4f6ae0ef3b 100644 --- a/packages/flutter_tools/test/general.shard/dap/flutter_adapter_test.dart +++ b/packages/flutter_tools/test/general.shard/dap/flutter_adapter_test.dart @@ -120,6 +120,30 @@ void main() { expect(adapter.flutterRequests, contains('app.stop')); }); + + test('does not call "app.stop" on terminateRequest if app was not started', () async { + final MockFlutterDebugAdapter adapter = MockFlutterDebugAdapter( + fileSystem: MemoryFileSystem.test(style: fsStyle), + platform: platform, + simulateAppStarted: false, + ); + + final FlutterLaunchRequestArguments args = FlutterLaunchRequestArguments( + cwd: '/project', + program: 'foo.dart', + ); + + await adapter.configurationDoneRequest(MockRequest(), null, () {}); + final Completer launchCompleter = Completer(); + await adapter.launchRequest(MockRequest(), args, launchCompleter.complete); + await launchCompleter.future; + + final Completer terminateCompleter = Completer(); + await adapter.terminateRequest(MockRequest(), TerminateArguments(restart: false), terminateCompleter.complete); + await terminateCompleter.future; + + expect(adapter.flutterRequests, isNot(contains('app.stop'))); + }); }); group('attachRequest', () { diff --git a/packages/flutter_tools/test/general.shard/dap/mocks.dart b/packages/flutter_tools/test/general.shard/dap/mocks.dart index fb44b77c6e..2cb8475b81 100644 --- a/packages/flutter_tools/test/general.shard/dap/mocks.dart +++ b/packages/flutter_tools/test/general.shard/dap/mocks.dart @@ -15,6 +15,7 @@ class MockFlutterDebugAdapter extends FlutterDebugAdapter { factory MockFlutterDebugAdapter({ required FileSystem fileSystem, required Platform platform, + bool simulateAppStarted = true, }) { final StreamController> stdinController = StreamController>(); final StreamController> stdoutController = StreamController>(); @@ -26,6 +27,7 @@ class MockFlutterDebugAdapter extends FlutterDebugAdapter { channel, fileSystem: fileSystem, platform: platform, + simulateAppStarted: simulateAppStarted, ); } @@ -35,10 +37,12 @@ class MockFlutterDebugAdapter extends FlutterDebugAdapter { ByteStreamServerChannel channel, { required FileSystem fileSystem, required Platform platform, + this.simulateAppStarted = true, }) : super(channel, fileSystem: fileSystem, platform: platform); final StreamSink> stdin; final Stream> stdout; + final bool simulateAppStarted; late String executable; late List processArgs; @@ -57,7 +61,10 @@ class MockFlutterDebugAdapter extends FlutterDebugAdapter { // Pretend we launched the app and got the app.started event so that // launchRequest will complete. - appStartedCompleter.complete(); + if (simulateAppStarted) { + appId = 'TEST'; + appStartedCompleter.complete(); + } } @override