From 53dce17b35353f795faa6e0509c6fa321a378a32 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Tue, 10 Mar 2020 15:31:02 -0700 Subject: [PATCH] [flutter_tools] Fallback discovery: Don't crash when an Isolate has no root library (#52354) --- .../lib/src/ios/fallback_discovery.dart | 2 +- .../ios/fallback_discovery_test.dart | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/ios/fallback_discovery.dart b/packages/flutter_tools/lib/src/ios/fallback_discovery.dart index 169712aa2d..c1144bc5da 100644 --- a/packages/flutter_tools/lib/src/ios/fallback_discovery.dart +++ b/packages/flutter_tools/lib/src/ios/fallback_discovery.dart @@ -167,7 +167,7 @@ class FallbackDiscovery { throw Exception('Expected Isolate but found Sentinel: $isolateResponse'); } final LibraryRef library = (isolateResponse as Isolate).rootLib; - if (library.uri.startsWith('package:$packageName')) { + if (library != null && library.uri.startsWith('package:$packageName')) { UsageEvent( _kEventName, 'success', diff --git a/packages/flutter_tools/test/general.shard/ios/fallback_discovery_test.dart b/packages/flutter_tools/test/general.shard/ios/fallback_discovery_test.dart index 042c4be0f7..03b0d2e63c 100644 --- a/packages/flutter_tools/test/general.shard/ios/fallback_discovery_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/fallback_discovery_test.dart @@ -67,6 +67,31 @@ void main() { ), Uri.parse('http://localhost:1')); }); + testUsingContext('Selects assumed port when another isolate has no root library', () async { + when(mockVmService.getVM()).thenAnswer((Invocation invocation) async { + return VM()..isolates = [ + IsolateRef()..id = '1', + IsolateRef()..id = '2', + ]; + }); + when(mockVmService.getIsolate('1')).thenAnswer((Invocation invocation) async { + return Isolate() + ..rootLib = null; + }); + when(mockVmService.getIsolate('2')).thenAnswer((Invocation invocation) async { + return Isolate() + ..rootLib = (LibraryRef()..uri = 'package:hello/main.dart'); + }); + expect(await fallbackDiscovery.discover( + assumedDevicePort: 23, + deivce: null, + hostVmservicePort: 1, + packageId: null, + usesIpv6: false, + packageName: 'hello', + ), Uri.parse('http://localhost:1')); + }); + testUsingContext('Selects mdns discovery if VM service connecton fails due to Sentinel', () async { when(mockVmService.getVM()).thenAnswer((Invocation invocation) async { return VM()..isolates = [