From 409e994cd03749d5a903f01cf4ec5b3ee074a2ee Mon Sep 17 00:00:00 2001 From: Ben Konyi Date: Fri, 18 Sep 2020 14:20:35 -0700 Subject: [PATCH] Ensure VmService instance is disposed after failed direct connection attempt (#66123) _attemptServiceConnection in FallbackDiscovery would fail if the root library URI was not a package URI even if the VM service connection attempt was successful. This resulted in a VM service connection being left alive, causing DDS to fail its connection to the VM service. Updated _attemptServiceConnection to ensure the VM service instance is disposed after a non-connection related failure and to allow for root library URIs with a file scheme. --- .../lib/src/ios/fallback_discovery.dart | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/flutter_tools/lib/src/ios/fallback_discovery.dart b/packages/flutter_tools/lib/src/ios/fallback_discovery.dart index 56c8147eff9..86934f3a76d 100644 --- a/packages/flutter_tools/lib/src/ios/fallback_discovery.dart +++ b/packages/flutter_tools/lib/src/ios/fallback_discovery.dart @@ -156,9 +156,10 @@ class FallbackDiscovery { // Attempt to connect to the VM service 5 times. int attempts = 0; Exception firstException; + VmService vmService; while (attempts < 5) { try { - final VmService vmService = await _vmServiceConnectUri( + vmService = await _vmServiceConnectUri( assumedWsUri.toString(), ); final VM vm = await vmService.getVM(); @@ -167,15 +168,17 @@ class FallbackDiscovery { isolateRefs.id, ); final LibraryRef library = isolateResponse.rootLib; - if (library != null && library.uri.startsWith('package:$packageName')) { + if (library != null && + (library.uri.startsWith('package:$packageName') || + library.uri.startsWith(RegExp(r'file:\/\/\/.*\/' + packageName)))) { UsageEvent( _kEventName, 'success', flutterUsage: _flutterUsage, ).send(); - // We absolutely must dispose this vmService instance, otherwise - // DDS will fail to start. + // This vmService instance must be disposed of, otherwise DDS will + // fail to start. vmService.dispose(); return Uri.parse('http://localhost:$hostPort'); } @@ -184,6 +187,10 @@ class FallbackDiscovery { // No action, we might have failed to connect. firstException ??= err; _logger.printTrace(err.toString()); + } finally { + // This vmService instance must be disposed of, otherwise DDS will + // fail to start. + vmService?.dispose(); } // No exponential backoff is used here to keep the amount of time the