Handle a SocketException thrown when sending the browser close command to Chrome (#151197)

https://github.com/flutter/flutter/pull/150645 tries to shut down Chrome by sending a browser close command through a debug protocol.  The webkit_inspection_protocol library used to send the command may throw a SocketException if Chrome has already been shut down.
This commit is contained in:
Jason Simmons 2024-07-03 06:23:20 -07:00 committed by GitHub
parent 84b3f0ab79
commit b373fa2c6e
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(
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];
}