framework.dart 10.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11
// 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';
12
import 'package:path/path.dart' as path;
13
import 'package:process/process.dart';
14
import 'package:stack_trace/stack_trace.dart';
15

16 17
import 'devices.dart';
import 'host_agent.dart';
18
import 'running_processes.dart';
19
import 'task_result.dart';
20 21
import 'utils.dart';

22 23 24 25 26 27 28 29
/// 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.
30
const int maximumRuns = 30;
31

32 33
/// Represents a unit of work performed in the CI environment that can
/// succeed, fail and be retried independently of others.
34
typedef TaskFunction = Future<TaskResult> Function();
35 36 37 38 39

bool _isTaskRegistered = false;

/// Registers a [task] to run, returns the result when it is complete.
///
40
/// The task does not run immediately but waits for the request via the
41 42
/// VM service protocol to run it.
///
43
/// It is OK for a [task] to perform many things. However, only one task can be
44
/// registered per Dart VM.
45 46 47 48
///
/// If no `processManager` is provided, a default [LocalProcessManager] is created
/// for the task.
Future<TaskResult> task(TaskFunction task, { ProcessManager? processManager }) async {
49
  if (_isTaskRegistered) {
50
    throw StateError('A task is already registered');
51
  }
52 53
  _isTaskRegistered = true;

54 55
  processManager ??= const LocalProcessManager();

56
  // TODO(ianh): allow overriding logging.
57 58 59 60 61
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.message}');
  });

62
  final _TaskRunner runner = _TaskRunner(task, processManager);
63 64 65 66 67
  runner.keepVmAliveUntilTaskRunRequested();
  return runner.whenDone;
}

class _TaskRunner {
68
  _TaskRunner(this.task, this.processManager) {
69 70
    registerExtension('ext.cocoonRunTask',
        (String method, Map<String, String> parameters) async {
71 72
      final Duration? taskTimeout = parameters.containsKey('timeoutInMinutes')
        ? Duration(minutes: int.parse(parameters['timeoutInMinutes']!))
73
        : null;
74
      final bool runFlutterConfig = parameters['runFlutterConfig'] != 'false'; // used by tests to avoid changing the configuration
75
      final bool runProcessCleanup = parameters['runProcessCleanup'] != 'false';
76
      final String? localEngine = parameters['localEngine'];
77
      final String? localEngineHost = parameters['localEngineHost'];
78 79 80 81 82
      final TaskResult result = await run(
        taskTimeout,
        runProcessCleanup: runProcessCleanup,
        runFlutterConfig: runFlutterConfig,
        localEngine: localEngine,
83
        localEngineHost: localEngineHost,
84
      );
85
      return ServiceExtensionResponse.result(json.encode(result.toJson()));
86
    });
87 88
    registerExtension('ext.cocoonRunnerReady',
        (String method, Map<String, String> parameters) async {
89
      return ServiceExtensionResponse.result('"ready"');
90 91 92
    });
  }

93
  final TaskFunction task;
94
  final ProcessManager processManager;
95

96
  Future<Device?> _getWorkingDeviceIfAvailable() async {
97 98 99 100 101 102 103
    try {
      return await devices.workingDevice;
    } on DeviceException {
      return null;
    }
  }

104
  // TODO(ianh): workaround for https://github.com/dart-lang/sdk/issues/23797
105 106
  RawReceivePort? _keepAlivePort;
  Timer? _startTaskTimeout;
107 108 109 110 111 112
  bool _taskStarted = false;

  final Completer<TaskResult> _completer = Completer<TaskResult>();

  static final Logger logger = Logger('TaskRunner');

113 114 115
  /// Signals that this task runner finished running the task.
  Future<TaskResult> get whenDone => _completer.future;

116
  Future<TaskResult> run(Duration? taskTimeout, {
117 118
    bool runFlutterConfig = true,
    bool runProcessCleanup = true,
119
    required String? localEngine,
120
    required String? localEngineHost,
121
  }) async {
122 123
    try {
      _taskStarted = true;
124
      print('Running task with a timeout of $taskTimeout.');
125
      final String exe = Platform.isWindows ? '.exe' : '';
126
      late Set<RunningProcessInfo> beforeRunningDartInstances;
127 128 129 130
      if (runProcessCleanup) {
        section('Checking running Dart$exe processes');
        beforeRunningDartInstances = await getRunningProcesses(
          processName: 'dart$exe',
131 132 133
          processManager: processManager,
        );
        final Set<RunningProcessInfo> allProcesses = await getRunningProcesses(processManager: processManager);
134 135 136 137 138
        beforeRunningDartInstances.forEach(print);
        for (final RunningProcessInfo info in allProcesses) {
          if (info.commandLine.contains('iproxy')) {
            print('[LEAK]: ${info.commandLine} ${info.creationDate} ${info.pid} ');
          }
139 140
        }
      }
141 142

      if (runFlutterConfig) {
143
        print('Enabling configs for macOS and Linux...');
144 145 146 147 148
        final int configResult = await exec(path.join(flutterDirectory.path, 'bin', 'flutter'), <String>[
          'config',
          '-v',
          '--enable-macos-desktop',
          '--enable-linux-desktop',
149
          if (localEngine != null) ...<String>['--local-engine', localEngine],
150
          if (localEngineHost != null) ...<String>['--local-engine-host', localEngineHost],
151 152 153 154
        ], canFail: true);
        if (configResult != 0) {
          print('Failed to enable configuration, tasks may not run.');
        }
155 156
      }

157
      final Device? device = await _getWorkingDeviceIfAvailable();
158 159 160 161

      // Some tests assume the phone is in home
      await device?.home();

162 163
      late TaskResult result;
      IOSink? sink;
164 165
      try {
        if (device != null && device.canStreamLogs && hostAgent.dumpDirectory != null) {
166
          sink = File(path.join(hostAgent.dumpDirectory!.path, '${device.deviceId}.log')).openWrite();
167 168
          await device.startLoggingToSink(sink);
        }
169

170
        Future<TaskResult> futureResult = _performTask();
171
        if (taskTimeout != null) {
172
          futureResult = futureResult.timeout(taskTimeout);
173
        }
174 175 176 177 178

        result = await futureResult;
      } finally {
        if (device != null && device.canStreamLogs) {
          await device.stopLoggingToSink();
179
          await sink?.close();
180 181
        }
      }
182

183
      if (runProcessCleanup) {
184 185
        section('Terminating lingering Dart$exe processes after task...');
        final Set<RunningProcessInfo> afterRunningDartInstances = await getRunningProcesses(
186
          processName: 'dart$exe',
187 188
          processManager: processManager,
        );
189 190 191 192 193 194
        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');
            }
195
            if (await info.terminate(processManager: processManager)) {
196
              print('Killed process id ${info.pid}.');
197 198
            } else {
              print('Failed to kill process ${info.pid}.');
199
            }
200 201 202
          }
        }
      }
203 204
      _completer.complete(result);
      return result;
205
    } on TimeoutException catch (err, stackTrace) {
206
      print('Task timed out in framework.dart after $taskTimeout.');
207 208
      print(err);
      print(stackTrace);
209
      return TaskResult.failure('Task timed out after $taskTimeout');
210
    } finally {
211 212
      await checkForRebootRequired();
      await forceQuitRunningProcesses();
213 214 215 216
      _closeKeepAlivePort();
    }
  }

217 218 219 220 221 222 223 224 225 226
  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()) {
227
        runCount = int.tryParse(rebootFile.readAsStringSync().trim()) ?? 0;
228 229 230
      } else {
        runCount = 0;
      }
231
      if (runCount < maximumRuns) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        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.
    }
  }

247 248 249
  /// Causes the Dart VM to stay alive until a request to run the task is
  /// received via the VM service protocol.
  void keepVmAliveUntilTaskRunRequested() {
250
    if (_taskStarted) {
251
      throw StateError('Task already started.');
252
    }
253 254 255

    // 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.
256
    _keepAlivePort = RawReceivePort();
257 258

    // Timeout if nothing bothers to connect and ask us to run the task.
259
    const Duration taskStartTimeout = Duration(seconds: 60);
260
    _startTaskTimeout = Timer(taskStartTimeout, () {
261 262 263 264 265 266 267 268
      if (!_taskStarted) {
        logger.severe('Task did not start in $taskStartTimeout.');
        _closeKeepAlivePort();
        exitCode = 1;
      }
    });
  }

269
  /// Disables the keepalive port, allowing the VM to exit.
270 271 272 273 274
  void _closeKeepAlivePort() {
    _startTaskTimeout?.cancel();
    _keepAlivePort?.close();
  }

275
  Future<TaskResult> _performTask() {
276
    final Completer<TaskResult> completer = Completer<TaskResult>();
277 278 279
    Chain.capture(() async {
      completer.complete(await task());
    }, onError: (dynamic taskError, Chain taskErrorStack) {
280
      final String message = 'Task failed: $taskError';
281 282 283 284
      stderr
        ..writeln(message)
        ..writeln('\nStack trace:')
        ..writeln(taskErrorStack.terse);
285 286 287 288 289
      // 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.
290
      if (!completer.isCompleted) {
291
        completer.complete(TaskResult.failure(message));
292
      }
293 294
    });
    return completer.future;
295 296
  }
}
297 298 299

File _rebootFile() {
  if (Platform.isLinux || Platform.isMacOS) {
300
    return File(path.join(Platform.environment['HOME']!, '.reboot-count'));
301 302 303 304
  }
  if (!Platform.isWindows) {
    throw StateError('Unexpected platform ${Platform.operatingSystem}');
  }
305
  return File(path.join(Platform.environment['USERPROFILE']!, '.reboot-count'));
306
}