Retry when safaridriver fails (flutter/engine#48791)

Starting `safaridriver` is flakey sometimes on macOS 13. It will occasionally error with "Operation not permitted". As a workaround, if it fails with that message, retry starting `safaridriver`.

Fixes https://github.com/flutter/flutter/issues/136972.

Example of fix on macOS 13 bot: https://ci.chromium.org/ui/p/flutter/builders/try/Mac%20Engine%20Drone/564967/overview
Note: The test is still failing due to https://github.com/flutter/flutter/issues/136279, but you can see it first has error "Operation not permitted" and retries and connects on second attempt.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Victoria Ashworth
2023-12-07 15:55:06 -06:00
committed by GitHub
parent 301283a2dd
commit 31210e4a38
2 changed files with 56 additions and 3 deletions

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:test_api/src/backend/runtime.dart';
@@ -23,6 +24,58 @@ class SafariMacOsEnvironment extends WebDriverBrowserEnvironment {
@override
Uri get driverUri => Uri(scheme: 'http', host: 'localhost', port: portNumber);
late Process _driverProcess;
int _retryCount = 0;
static const int _waitBetweenRetryInSeconds = 1;
static const int _maxRetryCount = 10;
@override
Future<Process> spawnDriverProcess() => Process.start('safaridriver', <String>['-p', portNumber.toString()]);
@override
Future<void> prepare() async {
await _startDriverProcess();
}
/// Pick an unused port and start `safaridriver` using that port.
///
/// On macOS 13, starting `safaridriver` can be flaky so if it returns an
/// "Operation not permitted" error, kill the `safaridriver` process and try
/// again with a different port. Wait [_waitBetweenRetryInSeconds] seconds
/// between retries. Try up to [_maxRetryCount] times.
Future<void> _startDriverProcess() async {
_retryCount += 1;
if (_retryCount > 1) {
await Future<void>.delayed(const Duration(seconds: _waitBetweenRetryInSeconds));
}
portNumber = await pickUnusedPort();
print('Attempt $_retryCount to start safaridriver on port $portNumber');
_driverProcess = await spawnDriverProcess();
_driverProcess.stderr
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((String error) {
print('[Webdriver][Error] $error');
if (_retryCount > _maxRetryCount) {
print('[Webdriver][Error] Failed to start after $_maxRetryCount tries.');
} else if (error.contains('Operation not permitted')) {
_driverProcess.kill();
_startDriverProcess();
}
});
_driverProcess.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((String log) {
print('[Webdriver] $log');
});
}
@override
Future<void> cleanup() async {
_driverProcess.kill();
}
}

View File

@@ -13,14 +13,14 @@ import 'package:webdriver/async_io.dart' show WebDriver, createDriver;
import 'browser.dart';
abstract class WebDriverBrowserEnvironment extends BrowserEnvironment {
late final int portNumber;
late int portNumber;
late final Process _driverProcess;
Future<Process> spawnDriverProcess();
Uri get driverUri;
/// Finds and returns an unused port on the test host in the local port range.
Future<int> _pickUnusedPort() async {
Future<int> pickUnusedPort() async {
// Use bind to allocate an unused port, then unbind from that port to
// make it available for use.
final ServerSocket socket = await ServerSocket.bind('localhost', 0);
@@ -33,7 +33,7 @@ abstract class WebDriverBrowserEnvironment extends BrowserEnvironment {
@override
Future<void> prepare() async {
portNumber = await _pickUnusedPort();
portNumber = await pickUnusedPort();
_driverProcess = await spawnDriverProcess();