mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
[CP-stable] When shutting down chrome, guard the call to ChromeConnection.getTab (#154380)
This is a custom hotfix patch to fix https://github.com/flutter/flutter/pull/154366 and https://github.com/flutter/flutter/issues/153064 on the stable channel. The reason for a custom patch rather than a bot-created PR is that the fixes on master were done by two sequential PRs (https://github.com/flutter/flutter/pull/153978 and https://github.com/flutter/flutter/pull/154366). This CP patch is also a subset of those changes (calls to Chrome when shutting down at the end of `flutter run -d chrome`).
This commit is contained in:
parent
4833e79ad8
commit
90c7ddb667
@ -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<ChromeTab?> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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: '<html> ...',
|
||||
);
|
||||
}
|
||||
|
||||
final List<ChromeTab> tabs = <ChromeTab>[];
|
||||
final int? maxRetries;
|
||||
int _retries;
|
||||
late final Object error;
|
||||
|
||||
@override
|
||||
Future<ChromeTab?> getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async {
|
||||
@ -861,10 +927,8 @@ class FakeChromeConnection extends Fake implements ChromeConnection {
|
||||
Future<List<ChromeTab>> getTabs({Duration? retryFor}) async {
|
||||
_retries ++;
|
||||
if (maxRetries == null || _retries < maxRetries!) {
|
||||
throw ConnectionException(
|
||||
formatException: const FormatException('incorrect format'),
|
||||
responseStatus: 'OK,',
|
||||
responseBody: '<html> ...');
|
||||
// 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<ChromeTab?> getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async {
|
||||
onGetTab?.call();
|
||||
if (throwSocketExceptions) {
|
||||
throw const io.SocketException('test');
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user