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 f028271bca..b1e269b9ed 100644 --- a/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart +++ b/packages/flutter_tools/lib/src/isolated/resident_web_runner.dart @@ -608,20 +608,17 @@ Please provide a valid TCP port (an integer between 0 and 65535, inclusive). }) async { if (_chromiumLauncher != null) { final Chromium chrome = await _chromiumLauncher!.connectedInstance; - 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; - } + final ChromeTab? chromeTab = await getChromeTabGuarded( + chrome.chromeConnection, + (ChromeTab chromeTab) { + return !chromeTab.url.startsWith('chrome-extension'); + }, + retryFor: const Duration(seconds: 5), + onIoError: (Object error, StackTrace stackTrace) { + // We were unable to unable to communicate with Chrome. + _logger.printError(error.toString(), stackTrace: stackTrace); + } + ); if (chromeTab == null) { appFailedToStart(); throwToolExit('Failed to connect to Chrome instance.'); 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 a7da4a91d5..75f21ef5a4 100644 --- a/packages/flutter_tools/lib/src/test/flutter_web_platform.dart +++ b/packages/flutter_tools/lib/src/test/flutter_web_platform.dart @@ -454,9 +454,6 @@ 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 7f9914f5a4..f10f3ac9ce 100644 --- a/packages/flutter_tools/lib/src/web/chrome.dart +++ b/packages/flutter_tools/lib/src/web/chrome.dart @@ -6,7 +6,8 @@ import 'dart:async'; import 'package:meta/meta.dart'; import 'package:process/process.dart'; -import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart'; +import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart' + hide StackTrace; import '../base/async_guard.dart'; import '../base/common.dart'; @@ -563,12 +564,37 @@ class Chromium { } } -// TODO(andrewkolos): Remove when https://github.com/dart-lang/sdk/issues/56566 -// is fixed. + +/// Wrapper for [ChromeConnection.getTab] that will catch any [IOException] or +/// [StateError], delegate it to the [onIoError] callback, and return null. +/// +/// This is useful for callers who are want to retrieve a [ChromeTab], but +/// are okay with the operation failing (e.g. due to an network IO issue or +/// the Chrome process no longer existing). Future getChromeTabGuarded( ChromeConnection chromeConnection, bool Function(ChromeTab tab) accept, { Duration? retryFor, -}) { - return asyncGuard(() => chromeConnection.getTab(accept, retryFor: retryFor)); + void Function(Object error, StackTrace stackTrace)? onIoError, +}) async { + try { + return asyncGuard( + () => chromeConnection.getTab( + accept, + retryFor: retryFor, + ), + ); + } on IOException catch (error, stackTrace) { + if (onIoError != null) { + onIoError(error, stackTrace); + } + return null; + // The underlying HttpClient will throw a StateError when it tries to + // perform a request despite the connection already being closed. + } on StateError catch (error, stackTrace) { + if (onIoError != null) { + onIoError(error, stackTrace); + } + return null; + } } diff --git a/packages/flutter_tools/test/web.shard/chrome_test.dart b/packages/flutter_tools/test/web.shard/chrome_test.dart index 1051946112..3fa87afcc8 100644 --- a/packages/flutter_tools/test/web.shard/chrome_test.dart +++ b/packages/flutter_tools/test/web.shard/chrome_test.dart @@ -758,7 +758,7 @@ void main() { expect(logger.errorText, contains('SocketException')); }); - test('can recover if getTabs throws a connection exception', () async { + testWithoutContext('can recover if getTabs throws a connection exception', () async { final BufferLogger logger = BufferLogger.test(); final FakeChromeConnection chromeConnection = FakeChromeConnection(maxRetries: 4); final ChromiumLauncher chromiumLauncher = ChromiumLauncher( @@ -775,11 +775,11 @@ void main() { expect(logger.errorText, isEmpty); }); - test('can recover if getTabs throws an HttpException', () async { + testWithoutContext('can recover if getTabs throws an HttpException', () async { final BufferLogger logger = BufferLogger.test(); final FakeChromeConnection chromeConnection = FakeChromeConnection( maxRetries: 4, - exception: io.HttpException( + error: io.HttpException( 'Connection closed before full header was received', uri: Uri.parse('http://localhost:52097/json'), ), @@ -798,7 +798,27 @@ void main() { expect(logger.errorText, isEmpty); }); - test('exits if getTabs throws a connection exception consistently', () async { + testWithoutContext('chrome.close can recover if getTab throws a StateError', () async { + final BufferLogger logger = BufferLogger.test(); + final FakeChromeConnection chromeConnection = FakeChromeConnection( + maxRetries: 4, + error: StateError('Client is closed.'), + ); + 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,); + await chrome.close(); + expect(logger.errorText, isEmpty); + }); + + testWithoutContext('exits if getTabs throws a connection exception consistently', () async { final BufferLogger logger = BufferLogger.test(); final FakeChromeConnection chromeConnection = FakeChromeConnection(); final ChromiumLauncher chromiumLauncher = ChromiumLauncher( @@ -825,7 +845,7 @@ void main() { )); }); - test('Chromium close sends browser close command', () async { + testWithoutContext('Chromium close sends browser close command', () async { final BufferLogger logger = BufferLogger.test(); final List commands = []; void onSendCommand(String cmd) { commands.add(cmd); } @@ -845,7 +865,7 @@ void main() { expect(commands, contains('Browser.close')); }); - test('Chromium close handles a SocketException when connecting to Chrome', () async { + testWithoutContext('Chromium close handles a SocketException when connecting to Chrome', () async { final BufferLogger logger = BufferLogger.test(); final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab(); final ChromiumLauncher chromiumLauncher = ChromiumLauncher( @@ -863,7 +883,7 @@ void main() { await chrome.close(); }); - test('Chromium close handles a WebSocketException when closing the WipConnection', () async { + testWithoutContext('Chromium close handles a WebSocketException when closing the WipConnection', () async { final BufferLogger logger = BufferLogger.test(); final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab(throwWebSocketException: true); final ChromiumLauncher chromiumLauncher = ChromiumLauncher( @@ -887,8 +907,8 @@ 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, Exception? exception}) : _retries = 0 { - this.exception = exception ?? + FakeChromeConnection({this.maxRetries, Object? error}) : _retries = 0 { + this.error = error ?? ConnectionException( formatException: const FormatException('incorrect format'), responseStatus: 'OK,', @@ -899,7 +919,7 @@ class FakeChromeConnection extends Fake implements ChromeConnection { final List tabs = []; final int? maxRetries; int _retries; - late final Exception exception; + late final Object error; @override Future getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async { @@ -910,7 +930,8 @@ class FakeChromeConnection extends Fake implements ChromeConnection { Future> getTabs({Duration? retryFor}) async { _retries ++; if (maxRetries == null || _retries < maxRetries!) { - throw exception; + // ignore: only_throw_errors -- This is fine for an ad-hoc test. + throw error; } return tabs; }