handle IOExceptions coming from ChromeTab.connect (#153978)
Fixes https://github.com/flutter/flutter/issues/153972 (unless the cause of https://github.com/flutter/flutter/issues/153064#issuecomment-2305662791 happens to also prevent this fix from working). In this PR, I've looked for all non-test call sites of `ChromeConnection.getTabs` and made sure are all wrapped in `try` blocks that handle `IOException` (`HttpException` is what we see in crash reporting, but I figure any `IOException` might as well be the same for all intents and purposes). I plan on cherry-picking this the stable branch.
This commit is contained in:
@@ -608,10 +608,22 @@ Please provide a valid TCP port (an integer between 0 and 65535, inclusive).
|
||||
}) async {
|
||||
if (_chromiumLauncher != null) {
|
||||
final Chromium chrome = await _chromiumLauncher!.connectedInstance;
|
||||
final ChromeTab? chromeTab = await chrome.chromeConnection.getTab((ChromeTab chromeTab) {
|
||||
return !chromeTab.url.startsWith('chrome-extension');
|
||||
}, retryFor: const Duration(seconds: 5));
|
||||
ChromeTab? chromeTab;
|
||||
try {
|
||||
chromeTab = await getChromeTabGuarded(
|
||||
chrome.chromeConnection,
|
||||
(ChromeTab chromeTab) {
|
||||
return !chromeTab.url.startsWith('chrome-extension');
|
||||
},
|
||||
retryFor: const Duration(seconds: 5),
|
||||
);
|
||||
} on HttpException catch (error, stackTrace) {
|
||||
// We were unable to unable to communicate with Chrome.
|
||||
_logger.printError(error.toString(), stackTrace: stackTrace);
|
||||
chromeTab = null;
|
||||
}
|
||||
if (chromeTab == null) {
|
||||
appFailedToStart();
|
||||
throwToolExit('Failed to connect to Chrome instance.');
|
||||
}
|
||||
_wipConnection = await chromeTab.connect();
|
||||
|
||||
@@ -426,9 +426,12 @@ class FlutterWebPlatform extends PlatformPlugin {
|
||||
// web once we transition off the HTML renderer. See:
|
||||
// https://github.com/flutter/flutter/issues/135700
|
||||
try {
|
||||
final ChromeTab chromeTab = (await _browserManager!._browser.chromeConnection.getTab((ChromeTab tab) {
|
||||
return tab.url.contains(_browserManager!._browser.url!);
|
||||
}))!;
|
||||
final ChromeTab chromeTab = (await getChromeTabGuarded(
|
||||
_browserManager!._browser.chromeConnection,
|
||||
(ChromeTab tab) {
|
||||
return tab.url.contains(_browserManager!._browser.url!);
|
||||
},
|
||||
))!;
|
||||
final WipConnection connection = await chromeTab.connect();
|
||||
final WipResponse response = await connection.sendCommand('Page.captureScreenshot', <String, Object>{
|
||||
// Clip the screenshot to include only the element.
|
||||
@@ -451,6 +454,9 @@ class FlutterWebPlatform extends PlatformPlugin {
|
||||
} on FormatException catch (ex) {
|
||||
_logger.printError('Caught FormatException: $ex');
|
||||
return shelf.Response.ok('Caught exception: $ex');
|
||||
} on IOException catch (ex) {
|
||||
_logger.printError('Caught IOException: $ex');
|
||||
return shelf.Response.ok('Caught exception: $ex');
|
||||
}
|
||||
}
|
||||
final String? errorMessage = await _testGoldenComparator.compareGoldens(testUri, bytes, goldenKey, updateGoldens);
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
|
||||
|
||||
import '../base/async_guard.dart';
|
||||
import '../base/common.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/io.dart';
|
||||
@@ -490,6 +491,10 @@ class Chromium {
|
||||
if (i == attempts) {
|
||||
rethrow;
|
||||
}
|
||||
} on IOException {
|
||||
if (i == attempts) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
await Future<void>.delayed(const Duration(milliseconds: 25));
|
||||
}
|
||||
@@ -508,7 +513,7 @@ class Chromium {
|
||||
Duration sigtermDelay = Duration.zero;
|
||||
if (_hasValidChromeConnection) {
|
||||
try {
|
||||
final ChromeTab? tab = await chromeConnection.getTab(
|
||||
final ChromeTab? tab = await getChromeTabGuarded(chromeConnection,
|
||||
(_) => true, retryFor: const Duration(seconds: 1));
|
||||
if (tab != null) {
|
||||
final WipConnection wipConnection = await tab.connect();
|
||||
@@ -551,3 +556,13 @@ class Chromium {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(andrewkolos): Remove when https://github.com/dart-lang/sdk/issues/56566
|
||||
// is fixed.
|
||||
Future<ChromeTab?> getChromeTabGuarded(
|
||||
ChromeConnection chromeConnection,
|
||||
bool Function(ChromeTab tab) accept, {
|
||||
Duration? retryFor,
|
||||
}) {
|
||||
return asyncGuard(() => chromeConnection.getTab(accept, retryFor: retryFor));
|
||||
}
|
||||
|
||||
@@ -774,6 +774,29 @@ void main() {
|
||||
expect(logger.errorText, isEmpty);
|
||||
});
|
||||
|
||||
test('can recover if getTabs throws an HttpException', () async {
|
||||
final BufferLogger logger = BufferLogger.test();
|
||||
final FakeChromeConnection chromeConnection = FakeChromeConnection(
|
||||
maxRetries: 4,
|
||||
exception: 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);
|
||||
expect(await chromiumLauncher.connect(chrome, false), equals(chrome));
|
||||
expect(logger.errorText, isEmpty);
|
||||
});
|
||||
|
||||
test('exits if getTabs throws a connection exception consistently', () async {
|
||||
final BufferLogger logger = BufferLogger.test();
|
||||
final FakeChromeConnection chromeConnection = FakeChromeConnection();
|
||||
@@ -863,11 +886,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, Exception? exception}) : _retries = 0 {
|
||||
this.exception = exception ??
|
||||
ConnectionException(
|
||||
formatException: const FormatException('incorrect format'),
|
||||
responseStatus: 'OK,',
|
||||
responseBody: '<html> ...',
|
||||
);
|
||||
}
|
||||
|
||||
final List<ChromeTab> tabs = <ChromeTab>[];
|
||||
final int? maxRetries;
|
||||
int _retries;
|
||||
late final Exception exception;
|
||||
|
||||
@override
|
||||
Future<ChromeTab?> getTab(bool Function(ChromeTab tab) accept, {Duration? retryFor}) async {
|
||||
@@ -878,10 +909,7 @@ 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> ...');
|
||||
throw exception;
|
||||
}
|
||||
return tabs;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user