diff --git a/packages/flutter_tools/lib/src/web/chrome.dart b/packages/flutter_tools/lib/src/web/chrome.dart index 996d79c4680..ca119df6dba 100644 --- a/packages/flutter_tools/lib/src/web/chrome.dart +++ b/packages/flutter_tools/lib/src/web/chrome.dart @@ -6,8 +6,10 @@ 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'; import '../base/file_system.dart'; import '../base/io.dart'; @@ -507,14 +509,8 @@ class Chromium { // Send a command to shut down the browser cleanly. Duration sigtermDelay = Duration.zero; if (_hasValidChromeConnection) { - ChromeTab? tab; - try { - tab = await chromeConnection.getTab( + final ChromeTab? tab = await getChromeTabGuarded(chromeConnection, (_) => true, retryFor: const Duration(seconds: 1)); - } on SocketException { - // Chrome is not responding to the debug protocol and probably has - // already been closed. - } if (tab != null) { final WipConnection wipConnection = await tab.connect(); await wipConnection.sendCommand('Browser.close'); @@ -552,3 +548,37 @@ class Chromium { }); } } + +/// 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, + void Function(Object error, StackTrace stackTrace)? onIoError, +}) async { + try { + return await 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 a7cf10c786c..51fac37547e 100644 --- a/packages/flutter_tools/test/web.shard/chrome_test.dart +++ b/packages/flutter_tools/test/web.shard/chrome_test.dart @@ -821,6 +821,64 @@ void main() { expect(commands, contains('Browser.close')); }); + testWithoutContext('chrome.close can recover if getTab throws an HttpException', () async { + final BufferLogger logger = BufferLogger.test(); + final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab( + onGetTab: () { + throw 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, + ); + await chromiumLauncher.connect(chrome, false); + await chrome.close(); + expect(logger.errorText, isEmpty); + }); + + testWithoutContext('chrome.close can recover if getTab throws a StateError', () async { + final BufferLogger logger = BufferLogger.test(); + final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab( + onGetTab: () { + throw 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 chromiumLauncher.connect(chrome, false); + await chrome.close(); + expect(logger.errorText, isEmpty); + }); + test('Chromium close handles a SocketException when connecting to Chrome', () async { final BufferLogger logger = BufferLogger.test(); final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab(); @@ -846,11 +904,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, Object? error}) : _retries = 0 { + this.error = error ?? + ConnectionException( + formatException: const FormatException('incorrect format'), + responseStatus: 'OK,', + responseBody: ' ...', + ); + } final List tabs = []; final int? maxRetries; int _retries; + late final Object error; @override Future getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async { @@ -861,10 +927,8 @@ 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: ' ...'); + // ignore: only_throw_errors -- This is fine for an ad-hoc test. + throw error; } return tabs; } @@ -877,14 +941,16 @@ typedef OnSendCommand = void Function(String); /// Fake chrome connection that returns a tab. class FakeChromeConnectionWithTab extends Fake implements ChromeConnection { - FakeChromeConnectionWithTab({OnSendCommand? onSendCommand}) + FakeChromeConnectionWithTab({OnSendCommand? onSendCommand, this.onGetTab}) : _tab = FakeChromeTab(onSendCommand); final FakeChromeTab _tab; + void Function()? onGetTab; bool throwSocketExceptions = false; @override Future getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async { + onGetTab?.call(); if (throwSocketExceptions) { throw const io.SocketException('test'); }