mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Rebase ios-experimental branch onto main. This will make the PRs experimenting with newer versions of Xcode (like https://github.com/flutter/flutter/pull/173123) smaller and easier to reason about. Rebases #168860 and #170274 ``` $ git rebase main -Xtheirs ``` --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Co-authored-by: Siva <a-siva@users.noreply.github.com> Co-authored-by: engine-flutter-autoroll <engine-flutter-autoroll@skia.org> Co-authored-by: Jamil Saadeh <jssaadeh@outlook.com> Co-authored-by: Dara Adedeji <76637177+SunkenInTime@users.noreply.github.com> Co-authored-by: Greg Price <gnprice@gmail.com> Co-authored-by: Ben Konyi <bkonyi@google.com> Co-authored-by: Ricardo Dalarme <ricardodalarme@outlook.com> Co-authored-by: Flutter GitHub Bot <fluttergithubbot@gmail.com> Co-authored-by: Justin McCandless <jmccandless@google.com> Co-authored-by: Alex Talebi <31685655+SalehTZ@users.noreply.github.com> Co-authored-by: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> Co-authored-by: Mouad Debbar <mdebbar@google.com> Co-authored-by: Zuckjet <1083941774@qq.com> Co-authored-by: Loïc Sharma <737941+loic-sharma@users.noreply.github.com> Co-authored-by: auto-submit[bot] <98614782+auto-submit[bot]@users.noreply.github.com> Co-authored-by: auto-submit[bot] <flutter-engprod-team@google.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: yim <ybz975218925@gmail.com> Co-authored-by: bufffun <chenmingding.cmd@alibaba-inc.com> Co-authored-by: Chinmay Garde <chinmaygarde@google.com> Co-authored-by: Hannah Jin <jhy03261997@gmail.com> Co-authored-by: Kate Lovett <katelovett@google.com> Co-authored-by: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Co-authored-by: Derek Xu <derekx@google.com> Co-authored-by: Yash Dhrangdhariya <72062416+Yash-Dhrangdhariya@users.noreply.github.com> Co-authored-by: bungeman <bungeman@chromium.org> Co-authored-by: Ahmed Mohamed Sameh <ahmedsameha1@gmail.com> Co-authored-by: John "codefu" McDole <codefu@google.com> Co-authored-by: Dmitry Grand <dmgr@google.com> Co-authored-by: Kostia Sokolovskyi <sokolovskyi.konstantin@gmail.com> Co-authored-by: Reid Baker <1063596+reidbaker@users.noreply.github.com> Co-authored-by: Matthew Kosarek <matt.kosarek@canonical.com> Co-authored-by: Jason Simmons <jason-simmons@users.noreply.github.com> Co-authored-by: Jim Graham <flar@google.com> Co-authored-by: Michael Goderbauer <goderbauer@google.com> Co-authored-by: Gray Mackall <34871572+gmackall@users.noreply.github.com> Co-authored-by: Gray Mackall <mackall@google.com> Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com> Co-authored-by: Jon Ihlas <jon.i@hotmail.fr> Co-authored-by: Micael Cid <micaelcid10@gmail.com> Co-authored-by: Alexander Aprelev <aam@google.com> Co-authored-by: hellohuanlin <41930132+hellohuanlin@users.noreply.github.com> Co-authored-by: Luke Memet <1598289+lukemmtt@users.noreply.github.com> Co-authored-by: Victoria Ashworth <15619084+vashworth@users.noreply.github.com> Co-authored-by: Mairramer <50643541+Mairramer@users.noreply.github.com> Co-authored-by: Florin Malita <fmalita@gmail.com> Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> Co-authored-by: Salem Iranloye <127918074+salemiranloye@users.noreply.github.com> Co-authored-by: Kevin Moore <kevmoo@google.com> Co-authored-by: Sydney Bao <sydneybao@google.com> Co-authored-by: Wdestroier <Wdestroier@gmail.com> Co-authored-by: Matt Boetger <matt.boetger@gmail.com> Co-authored-by: Reid Baker <reidbaker@google.com> Co-authored-by: Victor Sanni <victorsanniay@gmail.com> Co-authored-by: Jessy Yameogo <jessy.yameogo@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: romain.gyh <11901536+romaingyh@users.noreply.github.com> Co-authored-by: Robert Ancell <robert.ancell@canonical.com> Co-authored-by: TheLastFlame <131446187+TheLastFlame@users.noreply.github.com> Co-authored-by: masato <returnymgstokh@icloud.com> Co-authored-by: Albin PK <56157868+albinpk@users.noreply.github.com> Co-authored-by: Huy <huy@nevercode.io> Co-authored-by: Matan Lurey <matanlurey@users.noreply.github.com> Co-authored-by: Azat Chorekliyev <azat24680@gmail.com> Co-authored-by: EdwynZN <edwinzn9@gmail.com> Co-authored-by: Bruno Leroux <bruno.leroux@gmail.com> Co-authored-by: Dev TtangKong <ttankkeo112@gmail.com> Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Co-authored-by: Houssem Eddine Fadhli <houssemeddinefadhli81@gmail.com>
340 lines
11 KiB
Dart
340 lines
11 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:developer';
|
|
import 'dart:io';
|
|
import 'dart:isolate';
|
|
|
|
import 'package:logging/logging.dart';
|
|
import 'package:path/path.dart' as path;
|
|
import 'package:process/process.dart';
|
|
import 'package:stack_trace/stack_trace.dart';
|
|
|
|
import 'devices.dart';
|
|
import 'host_agent.dart';
|
|
import 'running_processes.dart';
|
|
import 'task_result.dart';
|
|
import 'utils.dart';
|
|
|
|
/// Identifiers for devices that should never be rebooted.
|
|
final Set<String> noRebootForbidList = <String>{
|
|
'822ef7958bba573829d85eef4df6cbdd86593730', // 32bit iPhone requires manual intervention on reboot.
|
|
};
|
|
|
|
/// The maximum number of test runs before a device must be rebooted.
|
|
///
|
|
/// This number was chosen arbitrarily.
|
|
const int maximumRuns = 30;
|
|
|
|
/// Represents a unit of work performed in the CI environment that can
|
|
/// succeed, fail and be retried independently of others.
|
|
typedef TaskFunction = Future<TaskResult> Function();
|
|
|
|
bool _isTaskRegistered = false;
|
|
|
|
/// Registers a [task] to run, returns the result when it is complete.
|
|
///
|
|
/// The task does not run immediately but waits for the request via the
|
|
/// VM service protocol to run it.
|
|
///
|
|
/// It is OK for a [task] to perform many things. However, only one task can be
|
|
/// registered per Dart VM.
|
|
///
|
|
/// If no `processManager` is provided, a default [LocalProcessManager] is created
|
|
/// for the task.
|
|
Future<TaskResult> task(TaskFunction task, {ProcessManager? processManager}) async {
|
|
if (_isTaskRegistered) {
|
|
throw StateError('A task is already registered');
|
|
}
|
|
_isTaskRegistered = true;
|
|
|
|
processManager ??= const LocalProcessManager();
|
|
|
|
// TODO(ianh): allow overriding logging.
|
|
Logger.root.level = Level.ALL;
|
|
Logger.root.onRecord.listen((LogRecord rec) {
|
|
print('${rec.level.name}: ${rec.time}: ${rec.message}');
|
|
});
|
|
|
|
final _TaskRunner runner = _TaskRunner(task, processManager);
|
|
runner.keepVmAliveUntilTaskRunRequested();
|
|
return runner.whenDone;
|
|
}
|
|
|
|
class _TaskRunner {
|
|
_TaskRunner(this.task, this.processManager) {
|
|
final String successResponse = json.encode(const <String, String>{'result': 'success'});
|
|
|
|
registerExtension('ext.cocoonRunTask', (String method, Map<String, String> parameters) async {
|
|
final Duration? taskTimeout = parameters.containsKey('timeoutInMinutes')
|
|
? Duration(minutes: int.parse(parameters['timeoutInMinutes']!))
|
|
: null;
|
|
final bool runFlutterConfig =
|
|
parameters['runFlutterConfig'] !=
|
|
'false'; // used by tests to avoid changing the configuration
|
|
final bool runProcessCleanup = parameters['runProcessCleanup'] != 'false';
|
|
final String? localEngine = parameters['localEngine'];
|
|
final String? localEngineHost = parameters['localEngineHost'];
|
|
final TaskResult result = await run(
|
|
taskTimeout,
|
|
runProcessCleanup: runProcessCleanup,
|
|
runFlutterConfig: runFlutterConfig,
|
|
localEngine: localEngine,
|
|
localEngineHost: localEngineHost,
|
|
);
|
|
const Duration taskResultReceivedTimeout = Duration(seconds: 30);
|
|
_taskResultReceivedTimeout = Timer(taskResultReceivedTimeout, () {
|
|
logger.severe(
|
|
'Task runner did not acknowledge task results in $taskResultReceivedTimeout.',
|
|
);
|
|
_closeKeepAlivePort();
|
|
exitCode = 1;
|
|
});
|
|
return ServiceExtensionResponse.result(json.encode(result.toJson()));
|
|
});
|
|
registerExtension('ext.cocoonRunnerReady', (
|
|
String method,
|
|
Map<String, String> parameters,
|
|
) async {
|
|
return ServiceExtensionResponse.result(successResponse);
|
|
});
|
|
registerExtension('ext.cocoonTaskResultReceived', (
|
|
String method,
|
|
Map<String, String> parameters,
|
|
) async {
|
|
_closeKeepAlivePort();
|
|
return ServiceExtensionResponse.result(successResponse);
|
|
});
|
|
}
|
|
|
|
final TaskFunction task;
|
|
final ProcessManager processManager;
|
|
|
|
Future<Device?> _getWorkingDeviceIfAvailable() async {
|
|
try {
|
|
return await devices.workingDevice;
|
|
} on DeviceException {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// TODO(ianh): workaround for https://github.com/dart-lang/sdk/issues/23797
|
|
RawReceivePort? _keepAlivePort;
|
|
Timer? _startTaskTimeout;
|
|
Timer? _taskResultReceivedTimeout;
|
|
bool _taskStarted = false;
|
|
|
|
final Completer<TaskResult> _completer = Completer<TaskResult>();
|
|
|
|
static final Logger logger = Logger('TaskRunner');
|
|
|
|
/// Signals that this task runner finished running the task.
|
|
Future<TaskResult> get whenDone => _completer.future;
|
|
|
|
Future<TaskResult> run(
|
|
Duration? taskTimeout, {
|
|
bool runFlutterConfig = true,
|
|
bool runProcessCleanup = true,
|
|
required String? localEngine,
|
|
required String? localEngineHost,
|
|
}) async {
|
|
try {
|
|
_taskStarted = true;
|
|
print('Running task with a timeout of $taskTimeout.');
|
|
final String exe = Platform.isWindows ? '.exe' : '';
|
|
late Set<RunningProcessInfo> beforeRunningDartInstances;
|
|
if (runProcessCleanup) {
|
|
section('Checking running Dart$exe processes');
|
|
beforeRunningDartInstances = await getRunningProcesses(
|
|
processName: 'dart$exe',
|
|
processManager: processManager,
|
|
);
|
|
final Set<RunningProcessInfo> allProcesses = await getRunningProcesses(
|
|
processManager: processManager,
|
|
);
|
|
beforeRunningDartInstances.forEach(print);
|
|
for (final RunningProcessInfo info in allProcesses) {
|
|
if (info.commandLine.contains('iproxy')) {
|
|
print('[LEAK]: ${info.commandLine} ${info.creationDate} ${info.pid} ');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (runFlutterConfig) {
|
|
print('Enabling configs for macOS and Linux...');
|
|
final int configResult = await exec(
|
|
path.join(flutterDirectory.path, 'bin', 'flutter'),
|
|
<String>[
|
|
'config',
|
|
'-v',
|
|
'--enable-macos-desktop',
|
|
'--enable-linux-desktop',
|
|
if (localEngine != null) ...<String>['--local-engine', localEngine],
|
|
if (localEngineHost != null) ...<String>['--local-engine-host', localEngineHost],
|
|
],
|
|
canFail: true,
|
|
);
|
|
if (configResult != 0) {
|
|
print('Failed to enable configuration, tasks may not run.');
|
|
}
|
|
}
|
|
|
|
final Device? device = await _getWorkingDeviceIfAvailable();
|
|
|
|
// Some tests assume the phone is in home
|
|
await device?.home();
|
|
|
|
late TaskResult result;
|
|
IOSink? sink;
|
|
try {
|
|
if (device != null && device.canStreamLogs && hostAgent.dumpDirectory != null) {
|
|
sink = File(
|
|
path.join(hostAgent.dumpDirectory!.path, '${device.deviceId}.log'),
|
|
).openWrite();
|
|
await device.startLoggingToSink(sink);
|
|
}
|
|
|
|
Future<TaskResult> futureResult = _performTask();
|
|
if (taskTimeout != null) {
|
|
futureResult = futureResult.timeout(taskTimeout);
|
|
}
|
|
|
|
result = await futureResult;
|
|
} finally {
|
|
if (device != null && device.canStreamLogs) {
|
|
await device.stopLoggingToSink();
|
|
await sink?.close();
|
|
}
|
|
}
|
|
|
|
if (runProcessCleanup) {
|
|
section('Terminating lingering Dart$exe processes after task...');
|
|
final Set<RunningProcessInfo> afterRunningDartInstances = await getRunningProcesses(
|
|
processName: 'dart$exe',
|
|
processManager: processManager,
|
|
);
|
|
for (final RunningProcessInfo info in afterRunningDartInstances) {
|
|
if (!beforeRunningDartInstances.contains(info)) {
|
|
print('$info was leaked by this test.');
|
|
if (result is TaskResultCheckProcesses) {
|
|
result = TaskResult.failure('This test leaked dart processes');
|
|
}
|
|
if (await info.terminate(processManager: processManager)) {
|
|
print('Killed process id ${info.pid}.');
|
|
} else {
|
|
print('Failed to kill process ${info.pid}.');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
_completer.complete(result);
|
|
return result;
|
|
} on TimeoutException catch (err, stackTrace) {
|
|
print('Task timed out in framework.dart after $taskTimeout.');
|
|
print(err);
|
|
print(stackTrace);
|
|
return TaskResult.failure('Task timed out after $taskTimeout');
|
|
} finally {
|
|
await checkForRebootRequired();
|
|
await forceQuitRunningProcesses();
|
|
}
|
|
}
|
|
|
|
Future<void> checkForRebootRequired() async {
|
|
print('Checking for reboot');
|
|
try {
|
|
final Device device = await devices.workingDevice;
|
|
if (noRebootForbidList.contains(device.deviceId)) {
|
|
return;
|
|
}
|
|
final File rebootFile = _rebootFile();
|
|
int runCount;
|
|
if (rebootFile.existsSync()) {
|
|
runCount = int.tryParse(rebootFile.readAsStringSync().trim()) ?? 0;
|
|
} else {
|
|
runCount = 0;
|
|
}
|
|
if (runCount < maximumRuns) {
|
|
rebootFile
|
|
..createSync()
|
|
..writeAsStringSync((runCount + 1).toString());
|
|
return;
|
|
}
|
|
rebootFile.deleteSync();
|
|
print('rebooting');
|
|
await device.reboot();
|
|
} on TimeoutException {
|
|
// Could not find device in order to reboot.
|
|
} on DeviceException {
|
|
// No attached device needed to reboot.
|
|
}
|
|
}
|
|
|
|
/// Causes the Dart VM to stay alive until a request to run the task is
|
|
/// received via the VM service protocol.
|
|
void keepVmAliveUntilTaskRunRequested() {
|
|
if (_taskStarted) {
|
|
throw StateError('Task already started.');
|
|
}
|
|
|
|
// Merely creating this port object will cause the VM to stay alive and keep
|
|
// the VM service server running until the port is disposed of.
|
|
_keepAlivePort = RawReceivePort();
|
|
|
|
// Timeout if nothing bothers to connect and ask us to run the task.
|
|
const Duration taskStartTimeout = Duration(seconds: 60);
|
|
_startTaskTimeout = Timer(taskStartTimeout, () {
|
|
if (!_taskStarted) {
|
|
logger.severe('Task did not start in $taskStartTimeout.');
|
|
_closeKeepAlivePort();
|
|
exitCode = 1;
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Disables the keepalive port, allowing the VM to exit.
|
|
void _closeKeepAlivePort() {
|
|
_startTaskTimeout?.cancel();
|
|
_taskResultReceivedTimeout?.cancel();
|
|
_keepAlivePort?.close();
|
|
}
|
|
|
|
Future<TaskResult> _performTask() {
|
|
final Completer<TaskResult> completer = Completer<TaskResult>();
|
|
Chain.capture(
|
|
() async {
|
|
completer.complete(await task());
|
|
},
|
|
onError: (dynamic taskError, Chain taskErrorStack) {
|
|
final String message = 'Task failed: $taskError';
|
|
stderr
|
|
..writeln(message)
|
|
..writeln('\nStack trace:')
|
|
..writeln(taskErrorStack.terse);
|
|
// IMPORTANT: We're completing the future _successfully_ but with a value
|
|
// that indicates a task failure. This is intentional. At this point we
|
|
// are catching errors coming from arbitrary (and untrustworthy) task
|
|
// code. Our goal is to convert the failure into a readable message.
|
|
// Propagating it further is not useful.
|
|
if (!completer.isCompleted) {
|
|
completer.complete(TaskResult.failure(message));
|
|
}
|
|
},
|
|
);
|
|
return completer.future;
|
|
}
|
|
}
|
|
|
|
File _rebootFile() {
|
|
if (Platform.isLinux || Platform.isMacOS) {
|
|
return File(path.join(Platform.environment['HOME']!, '.reboot-count'));
|
|
}
|
|
if (!Platform.isWindows) {
|
|
throw StateError('Unexpected platform ${Platform.operatingSystem}');
|
|
}
|
|
return File(path.join(Platform.environment['USERPROFILE']!, '.reboot-count'));
|
|
}
|