From 9ec2588fd0b5974d9df975f2eaa63d8626080716 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Tue, 27 Aug 2024 09:19:28 -0700 Subject: [PATCH] handle `IOException`s coming from `ChromeTab.connect` (#153978) Fixes https://github.com/flutter/flutter/issues/153972 (unless the cause of https://github.com/flutter/flutter/issues/153064#issuecomment-2305662791 happens to also prevent this fix from working). In this PR, I've looked for all non-test call sites of `ChromeConnection.getTabs` and made sure are all wrapped in `try` blocks that handle `IOException` (`HttpException` is what we see in crash reporting, but I figure any `IOException` might as well be the same for all intents and purposes). I plan on cherry-picking this the stable branch. --- .../lib/src/isolated/resident_web_runner.dart | 18 +++++++-- .../lib/src/test/flutter_web_platform.dart | 12 ++++-- .../flutter_tools/lib/src/web/chrome.dart | 17 ++++++++- .../test/web.shard/chrome_test.dart | 38 ++++++++++++++++--- 4 files changed, 73 insertions(+), 12 deletions(-) diff --git a/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart b/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart index f70ca02206..f028271bca 100644 --- a/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart +++ b/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart @@ -608,10 +608,22 @@ Please provide a valid TCP port (an integer between 0 and 65535, inclusive). }) async { if (_chromiumLauncher != null) { final Chromium chrome = await _chromiumLauncher!.connectedInstance; - final ChromeTab? chromeTab = await chrome.chromeConnection.getTab((ChromeTab chromeTab) { - return !chromeTab.url.startsWith('chrome-extension'); - }, retryFor: const Duration(seconds: 5)); + ChromeTab? chromeTab; + try { + chromeTab = await getChromeTabGuarded( + chrome.chromeConnection, + (ChromeTab chromeTab) { + return !chromeTab.url.startsWith('chrome-extension'); + }, + retryFor: const Duration(seconds: 5), + ); + } on HttpException catch (error, stackTrace) { + // We were unable to unable to communicate with Chrome. + _logger.printError(error.toString(), stackTrace: stackTrace); + chromeTab = null; + } if (chromeTab == null) { + appFailedToStart(); throwToolExit('Failed to connect to Chrome instance.'); } _wipConnection = await chromeTab.connect(); diff --git a/packages/flutter_tools/lib/src/test/flutter_web_platform.dart b/packages/flutter_tools/lib/src/test/flutter_web_platform.dart index cd91c6ea52..a7da4a91d5 100644 --- a/packages/flutter_tools/lib/src/test/flutter_web_platform.dart +++ b/packages/flutter_tools/lib/src/test/flutter_web_platform.dart @@ -426,9 +426,12 @@ class FlutterWebPlatform extends PlatformPlugin { // web once we transition off the HTML renderer. See: // https://github.com/flutter/flutter/issues/135700 try { - final ChromeTab chromeTab = (await _browserManager!._browser.chromeConnection.getTab((ChromeTab tab) { - return tab.url.contains(_browserManager!._browser.url!); - }))!; + final ChromeTab chromeTab = (await getChromeTabGuarded( + _browserManager!._browser.chromeConnection, + (ChromeTab tab) { + return tab.url.contains(_browserManager!._browser.url!); + }, + ))!; final WipConnection connection = await chromeTab.connect(); final WipResponse response = await connection.sendCommand('Page.captureScreenshot', { // Clip the screenshot to include only the element. @@ -451,6 +454,9 @@ class FlutterWebPlatform extends PlatformPlugin { } on FormatException catch (ex) { _logger.printError('Caught FormatException: $ex'); return shelf.Response.ok('Caught exception: $ex'); + } on IOException catch (ex) { + _logger.printError('Caught IOException: $ex'); + return shelf.Response.ok('Caught exception: $ex'); } } final String? errorMessage = await _testGoldenComparator.compareGoldens(testUri, bytes, goldenKey, updateGoldens); diff --git a/packages/flutter_tools/lib/src/web/chrome.dart b/packages/flutter_tools/lib/src/web/chrome.dart index fb92ecd740..b98be8481a 100644 --- a/packages/flutter_tools/lib/src/web/chrome.dart +++ b/packages/flutter_tools/lib/src/web/chrome.dart @@ -8,6 +8,7 @@ import 'package:meta/meta.dart'; import 'package:process/process.dart'; import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart'; +import '../base/async_guard.dart'; import '../base/common.dart'; import '../base/file_system.dart'; import '../base/io.dart'; @@ -490,6 +491,10 @@ class Chromium { if (i == attempts) { rethrow; } + } on IOException { + if (i == attempts) { + rethrow; + } } await Future.delayed(const Duration(milliseconds: 25)); } @@ -508,7 +513,7 @@ class Chromium { Duration sigtermDelay = Duration.zero; if (_hasValidChromeConnection) { try { - final ChromeTab? tab = await chromeConnection.getTab( + final ChromeTab? tab = await getChromeTabGuarded(chromeConnection, (_) => true, retryFor: const Duration(seconds: 1)); if (tab != null) { final WipConnection wipConnection = await tab.connect(); @@ -551,3 +556,13 @@ class Chromium { }); } } + +// TODO(andrewkolos): Remove when https://github.com/dart-lang/sdk/issues/56566 +// is fixed. +Future getChromeTabGuarded( + ChromeConnection chromeConnection, + bool Function(ChromeTab tab) accept, { + Duration? retryFor, +}) { + return asyncGuard(() => chromeConnection.getTab(accept, retryFor: retryFor)); +} diff --git a/packages/flutter_tools/test/web.shard/chrome_test.dart b/packages/flutter_tools/test/web.shard/chrome_test.dart index 67c1711995..2eea9b7ae4 100644 --- a/packages/flutter_tools/test/web.shard/chrome_test.dart +++ b/packages/flutter_tools/test/web.shard/chrome_test.dart @@ -774,6 +774,29 @@ void main() { expect(logger.errorText, isEmpty); }); + test('can recover if getTabs throws an HttpException', () async { + final BufferLogger logger = BufferLogger.test(); + final FakeChromeConnection chromeConnection = FakeChromeConnection( + maxRetries: 4, + exception: io.HttpException( + 'Connection closed before full header was received', + uri: Uri.parse('http://localhost:52097/json'), + ), + ); + final ChromiumLauncher chromiumLauncher = ChromiumLauncher( + fileSystem: fileSystem, + platform: platform, + processManager: processManager, + operatingSystemUtils: operatingSystemUtils, + browserFinder: findChromeExecutable, + logger: logger, + ); + final FakeProcess process = FakeProcess(); + final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger); + expect(await chromiumLauncher.connect(chrome, false), equals(chrome)); + expect(logger.errorText, isEmpty); + }); + test('exits if getTabs throws a connection exception consistently', () async { final BufferLogger logger = BufferLogger.test(); final FakeChromeConnection chromeConnection = FakeChromeConnection(); @@ -863,11 +886,19 @@ class FakeChromeConnection extends Fake implements ChromeConnection { /// Create a connection that throws a connection exception on first /// [maxRetries] calls to [getTabs]. /// If [maxRetries] is `null`, [getTabs] calls never succeed. - FakeChromeConnection({this.maxRetries}): _retries = 0; + FakeChromeConnection({this.maxRetries, Exception? exception}) : _retries = 0 { + this.exception = exception ?? + ConnectionException( + formatException: const FormatException('incorrect format'), + responseStatus: 'OK,', + responseBody: ' ...', + ); + } final List tabs = []; final int? maxRetries; int _retries; + late final Exception exception; @override Future getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async { @@ -878,10 +909,7 @@ class FakeChromeConnection extends Fake implements ChromeConnection { Future> getTabs({Duration? retryFor}) async { _retries ++; if (maxRetries == null || _retries < maxRetries!) { - throw ConnectionException( - formatException: const FormatException('incorrect format'), - responseStatus: 'OK,', - responseBody: ' ...'); + throw exception; } return tabs; }