From 913d5933c1fa13b43a4da9e3c3046ecb1fd36c6a Mon Sep 17 00:00:00 2001 From: Ben Konyi Date: Tue, 19 Jan 2021 22:34:03 -0800 Subject: [PATCH] Check VM service URI is valid before attempting to start DDS (Re-upload #73998) (#74280) --- .../flutter_tools/lib/src/resident_runner.dart | 14 ++++++++++++++ .../test/general.shard/resident_runner_test.dart | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 5b9bad2c83..82d748927f 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -242,6 +242,20 @@ class FlutterDevice { completer.completeError('failed to connect to $observatoryUri', st); } } + // First check if the VM service is actually listening on observatoryUri as + // this may not be the case when scraping logcat for URIs. If this URI is + // from an old application instance, we shouldn't try and start DDS. + try { + service = await connectToVmService(observatoryUri); + service.dispose(); + } on Exception catch (exception) { + globals.printTrace('Fail to connect to service protocol: $observatoryUri: $exception'); + if (!completer.isCompleted && !_isListeningForObservatoryUri) { + completer.completeError('failed to connect to $observatoryUri'); + } + return; + } + // This first try block is meant to catch errors that occur during DDS startup // (e.g., failure to bind to a port, failure to connect to the VM service, // attaching to a VM service with existing clients, etc.). 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 3166445d6e..7289073004 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -2889,6 +2889,16 @@ void main() { if (!caught) { fail('Expected a StateError to be thrown.'); } + }, overrides: { + VMServiceConnector: () => (Uri httpUri, { + ReloadSources reloadSources, + Restart restart, + CompileExpression compileExpression, + GetSkSLMethod getSkSLMethod, + PrintStructuredErrorLogMethod printStructuredErrorLogMethod, + io.CompressionOptions compression, + Device device, + }) async => mockVMService, })); testUsingContext('nextPlatform moves through expected platforms', () {