framework.dart 9.38 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:stack_trace/stack_trace.dart';
14

15
import 'adb.dart';
16
import 'running_processes.dart';
17
import 'task_result.dart';
18 19
import 'utils.dart';

20 21 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.
const int maxiumRuns = 30;

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

bool _isTaskRegistered = false;

/// Registers a [task] to run, returns the result when it is complete.
///
38
/// The task does not run immediately but waits for the request via the
39 40
/// VM service protocol to run it.
///
41
/// It is OK for a [task] to perform many things. However, only one task can be
42
/// registered per Dart VM.
43
Future<TaskResult> task(TaskFunction task) async {
44
  if (_isTaskRegistered)
45
    throw StateError('A task is already registered');
46 47 48

  _isTaskRegistered = true;

49
  // TODO(ianh): allow overriding logging.
50 51 52 53 54
  Logger.root.level = Level.ALL;
  Logger.root.onRecord.listen((LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.message}');
  });

55
  final _TaskRunner runner = _TaskRunner(task);
56 57 58 59 60 61
  runner.keepVmAliveUntilTaskRunRequested();
  return runner.whenDone;
}

class _TaskRunner {
  _TaskRunner(this.task) {
62 63
    registerExtension('ext.cocoonRunTask',
        (String method, Map<String, String> parameters) async {
64 65
      final Duration taskTimeout = parameters.containsKey('timeoutInMinutes')
        ? Duration(minutes: int.parse(parameters['timeoutInMinutes']))
66
        : null;
67 68 69 70 71
      // This is only expected to be passed in unit test runs so they do not
      // kill the Dart process that is running them and waste time running config.
      final bool runFlutterConfig = parameters['runFlutterConfig'] != 'false';
      final bool runProcessCleanup = parameters['runProcessCleanup'] != 'false';
      final TaskResult result = await run(taskTimeout, runProcessCleanup: runProcessCleanup, runFlutterConfig: runFlutterConfig);
72
      return ServiceExtensionResponse.result(json.encode(result.toJson()));
73
    });
74 75
    registerExtension('ext.cocoonRunnerReady',
        (String method, Map<String, String> parameters) async {
76
      return ServiceExtensionResponse.result('"ready"');
77 78 79
    });
  }

80 81 82 83 84 85 86 87 88 89 90
  final TaskFunction task;

  // TODO(ianh): workaround for https://github.com/dart-lang/sdk/issues/23797
  RawReceivePort _keepAlivePort;
  Timer _startTaskTimeout;
  bool _taskStarted = false;

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

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

91 92 93
  /// Signals that this task runner finished running the task.
  Future<TaskResult> get whenDone => _completer.future;

94 95 96 97
  Future<TaskResult> run(Duration taskTimeout, {
    bool runFlutterConfig = true,
    bool runProcessCleanup = true,
  }) async {
98 99
    try {
      _taskStarted = true;
100
      print('Running task with a timeout of $taskTimeout.');
101
      final String exe = Platform.isWindows ? '.exe' : '';
102 103 104 105 106 107 108 109 110 111 112 113
      Set<RunningProcessInfo> beforeRunningDartInstances;
      if (runProcessCleanup) {
        section('Checking running Dart$exe processes');
        beforeRunningDartInstances = await getRunningProcesses(
          processName: 'dart$exe',
        ).toSet();
        final Set<RunningProcessInfo> allProcesses = await getRunningProcesses().toSet();
        beforeRunningDartInstances.forEach(print);
        for (final RunningProcessInfo info in allProcesses) {
          if (info.commandLine.contains('iproxy')) {
            print('[LEAK]: ${info.commandLine} ${info.creationDate} ${info.pid} ');
          }
114
        }
115 116
      } else {
        section('Skipping check running Dart$exe processes');
117
      }
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

      if (runFlutterConfig) {
        print('enabling configs for macOS, Linux, Windows, and Web...');
        final int configResult = await exec(path.join(flutterDirectory.path, 'bin', 'flutter'), <String>[
          'config',
          '-v',
          '--enable-macos-desktop',
          '--enable-windows-desktop',
          '--enable-linux-desktop',
          '--enable-web',
          if (localEngine != null) ...<String>['--local-engine', localEngine],
        ], canFail: true);
        if (configResult != 0) {
          print('Failed to enable configuration, tasks may not run.');
        }
      } else {
        print('Skipping enabling configs for macOS, Linux, Windows, and Web');
135 136
      }

137
      Future<TaskResult> futureResult = _performTask();
138
      if (taskTimeout != null)
139
        futureResult = futureResult.timeout(taskTimeout);
140

141 142
      TaskResult result = await futureResult;

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      if (runProcessCleanup) {
        section('Checking running Dart$exe processes after task...');
        final List<RunningProcessInfo> afterRunningDartInstances = await getRunningProcesses(
          processName: 'dart$exe',
        ).toList();
        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');
            }
            final bool killed = await killProcess(info.pid);
            if (!killed) {
              print('Failed to kill process ${info.pid}.');
            } else {
              print('Killed process id ${info.pid}.');
            }
160 161
          }
        }
162 163
      } else {
        print('Skipping check running Dart$exe processes after task');
164
      }
165 166
      _completer.complete(result);
      return result;
167
    } on TimeoutException catch (err, stackTrace) {
168
      print('Task timed out in framework.dart after $taskTimeout.');
169 170
      print(err);
      print(stackTrace);
171
      return TaskResult.failure('Task timed out after $taskTimeout');
172
    } finally {
173 174
      await checkForRebootRequired();
      await forceQuitRunningProcesses();
175 176 177 178
      _closeKeepAlivePort();
    }
  }

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  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());
      } else {
        runCount = 0;
      }
      if (runCount < maxiumRuns) {
        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.
    }
  }

209 210 211
  /// Causes the Dart VM to stay alive until a request to run the task is
  /// received via the VM service protocol.
  void keepVmAliveUntilTaskRunRequested() {
212
    if (_taskStarted)
213
      throw StateError('Task already started.');
214 215 216

    // 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.
217
    _keepAlivePort = RawReceivePort();
218 219

    // Timeout if nothing bothers to connect and ask us to run the task.
220
    const Duration taskStartTimeout = Duration(seconds: 60);
221
    _startTaskTimeout = Timer(taskStartTimeout, () {
222 223 224 225 226 227 228 229
      if (!_taskStarted) {
        logger.severe('Task did not start in $taskStartTimeout.');
        _closeKeepAlivePort();
        exitCode = 1;
      }
    });
  }

230
  /// Disables the keepalive port, allowing the VM to exit.
231 232 233 234 235
  void _closeKeepAlivePort() {
    _startTaskTimeout?.cancel();
    _keepAlivePort?.close();
  }

236
  Future<TaskResult> _performTask() {
237
    final Completer<TaskResult> completer = Completer<TaskResult>();
238 239 240
    Chain.capture(() async {
      completer.complete(await task());
    }, onError: (dynamic taskError, Chain taskErrorStack) {
241
      final String message = 'Task failed: $taskError';
242 243 244 245
      stderr
        ..writeln(message)
        ..writeln('\nStack trace:')
        ..writeln(taskErrorStack.terse);
246 247 248 249 250
      // 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.
251
      if (!completer.isCompleted)
252
        completer.complete(TaskResult.failure(message));
253 254
    });
    return completer.future;
255 256
  }
}
257 258 259 260 261 262 263 264 265 266

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'));
}