runBounded function
Runs a child process with a hard cap: on timeout the process and its direct children are killed and a ProcessTimeoutError names the command and phase. Same result as Process.run, plus the phase shows in heartbeats and the call's timeout reply.
Implementation
Future<ProcessResult> runBounded(
String executable,
List<String> arguments, {
Duration timeout = kDefaultProcessTimeout,
String? phase,
Map<String, String>? environment,
String? workingDirectory,
Encoding? stdoutEncoding = systemEncoding,
Encoding? stderrEncoding = systemEncoding,
List<String> hints = const [],
}) async {
CallProgress.checkAlive();
final command = describeCommand(executable, arguments);
final label = phase ?? command;
return CallProgress.during(label, key: usageProcessKey(executable, phase), () async {
final process = ProcessRegistry.instance.track(
await Process.start(executable, arguments, environment: environment, workingDirectory: workingDirectory));
final call = CallProgress.current;
call?.registerProcess(process, label);
process.stdin.close().ignore();
final out = _Drain(process.stdout);
final err = _Drain(process.stderr);
try {
final code = await process.exitCode.timeout(timeout);
await Future.wait([out.done, err.done]).timeout(const Duration(seconds: 3), onTimeout: () => const []);
return ProcessResult(process.pid, code, out.decode(stdoutEncoding), err.decode(stderrEncoding));
} on TimeoutException {
await killProcessTree(process);
throw ProcessTimeoutError(
command: command,
phase: label,
timeout: timeout,
partialStdout: out.decode(systemEncoding) as String,
partialStderr: err.decode(systemEncoding) as String,
hints: hints,
);
} finally {
call?.unregisterProcess(process);
out.cancel();
err.cancel();
}
});
}