From cb28dec614e481c5ebde52b545118202200c5d4c Mon Sep 17 00:00:00 2001 From: flutteractionsbot <154381524+flutteractionsbot@users.noreply.github.com> Date: Tue, 13 Aug 2024 10:55:49 -0700 Subject: [PATCH] [CP-stable]Handle a SocketException thrown when sending the browser close command to Chrome (#153294) This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: What is the link to the issue this cherry-pick is addressing? ### Changelog Description: Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples Prevent CLI tool crash that can occur when unable to exit a spawned Chromium instance that's already been exited. ### Impact Description: What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch) Noisy crashes for users. This crash represents 65.69% of tool crashes (with roughly half of that percentage of clients being affected). ### Workaround: Is there a workaround for this issue? There is no known workaround. ### Risk: What is the risk level of this cherry-pick? This simply adds a try-catch that will, at worst, obscure errors. ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: What are the steps to validate that this fix works? N/A. No reliable repro is known at this time. --- .../flutter_tools/lib/src/web/chrome.dart | 10 ++++++-- .../test/web.shard/chrome_test.dart | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/web/chrome.dart b/packages/flutter_tools/lib/src/web/chrome.dart index 90af6e5cf8c..996d79c4680 100644 --- a/packages/flutter_tools/lib/src/web/chrome.dart +++ b/packages/flutter_tools/lib/src/web/chrome.dart @@ -507,8 +507,14 @@ class Chromium { // Send a command to shut down the browser cleanly. Duration sigtermDelay = Duration.zero; if (_hasValidChromeConnection) { - final ChromeTab? tab = await chromeConnection.getTab( - (_) => true, retryFor: const Duration(seconds: 1)); + ChromeTab? tab; + try { + tab = await chromeConnection.getTab( + (_) => 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'); diff --git a/packages/flutter_tools/test/web.shard/chrome_test.dart b/packages/flutter_tools/test/web.shard/chrome_test.dart index 08bc8a0502f..a7cf10c786c 100644 --- a/packages/flutter_tools/test/web.shard/chrome_test.dart +++ b/packages/flutter_tools/test/web.shard/chrome_test.dart @@ -820,6 +820,24 @@ void main() { await chrome.close(); expect(commands, contains('Browser.close')); }); + + test('Chromium close handles a SocketException when connecting to Chrome', () async { + final BufferLogger logger = BufferLogger.test(); + final FakeChromeConnectionWithTab chromeConnection = FakeChromeConnectionWithTab(); + 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)); + chromeConnection.throwSocketExceptions = true; + await chrome.close(); + }); } /// Fake chrome connection that fails to get tabs a few times. @@ -863,14 +881,21 @@ class FakeChromeConnectionWithTab extends Fake implements ChromeConnection { : _tab = FakeChromeTab(onSendCommand); final FakeChromeTab _tab; + bool throwSocketExceptions = false; @override Future getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async { + if (throwSocketExceptions) { + throw const io.SocketException('test'); + } return _tab; } @override Future> getTabs({Duration? retryFor}) async { + if (throwSocketExceptions) { + throw const io.SocketException('test'); + } return [_tab]; }