[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.
This commit is contained in:
flutteractionsbot 2024-08-13 10:55:49 -07:00 committed by GitHub
parent 8997882808
commit cb28dec614
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 2 deletions

View File

@ -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');

View File

@ -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<ChromeTab?> getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async {
if (throwSocketExceptions) {
throw const io.SocketException('test');
}
return _tab;
}
@override
Future<List<ChromeTab>> getTabs({Duration? retryFor}) async {
if (throwSocketExceptions) {
throw const io.SocketException('test');
}
return <ChromeTab>[_tab];
}