diff --git a/packages/flutter_tools/lib/src/base/process.dart b/packages/flutter_tools/lib/src/base/process.dart index dc77d0abf5..b5be56b7a5 100644 --- a/packages/flutter_tools/lib/src/base/process.dart +++ b/packages/flutter_tools/lib/src/base/process.dart @@ -275,13 +275,23 @@ Future runAsync( int exitCode; exitCode = await process.exitCode.timeout(timeout, onTimeout: () { + // The process timed out. Kill it. + processManager.killPid(process.pid); return null; }); String stdoutString; String stderrString; try { - await Future.wait(>[stdoutFuture, stderrFuture]); + Future stdioFuture = + Future.wait(>[stdoutFuture, stderrFuture]); + if (exitCode == null) { + // If we had to kill the process for a timeout, only wait a short time + // for the stdio streams to drain in case killing the process didn't + // work. + stdioFuture = stdioFuture.timeout(const Duration(seconds: 1)); + } + await stdioFuture; } catch (_) { // Ignore errors on the process' stdout and stderr streams. Just capture // whatever we got, and use the exit code @@ -299,9 +309,6 @@ Future runAsync( return runResult; } - // The process timed out. Kill it. - processManager.killPid(process.pid); - // If we are out of timeoutRetries, throw a ProcessException. if (timeoutRetries < 0) { throw ProcessException(cmd[0], cmd.sublist(1), diff --git a/packages/flutter_tools/test/general.shard/base/process_test.dart b/packages/flutter_tools/test/general.shard/base/process_test.dart index 6a92c3558e..9c8981cbe1 100644 --- a/packages/flutter_tools/test/general.shard/base/process_test.dart +++ b/packages/flutter_tools/test/general.shard/base/process_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; + import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/platform.dart'; @@ -103,13 +105,13 @@ void main() { // MockProcessManager has an implementation of start() that returns the // result of processFactory. flakyProcessManager = MockProcessManager(); + }); + + testUsingContext('flaky process fails without retry', () async { flakyProcessManager.processFactory = flakyProcessFactory( flakes: 1, delay: delay, ); - }); - - testUsingContext('flaky process fails without retry', () async { final RunResult result = await runAsync( ['dummy'], timeout: delay + const Duration(seconds: 1), @@ -120,6 +122,10 @@ void main() { }); testUsingContext('flaky process succeeds with retry', () async { + flakyProcessManager.processFactory = flakyProcessFactory( + flakes: 1, + delay: delay, + ); final RunResult result = await runAsync( ['dummy'], timeout: delay - const Duration(milliseconds: 500), @@ -131,6 +137,22 @@ void main() { }); testUsingContext('flaky process generates ProcessException on timeout', () async { + final Completer> flakyStderr = Completer>(); + final Completer> flakyStdout = Completer>(); + flakyProcessManager.processFactory = flakyProcessFactory( + flakes: 1, + delay: delay, + stderr: () => Stream>.fromFuture(flakyStderr.future), + stdout: () => Stream>.fromFuture(flakyStdout.future), + ); + when(flakyProcessManager.killPid(any)).thenAnswer((_) { + // Don't let the stderr stream stop until the process is killed. This + // ensures that runAsync() does not delay killing the process until + // stdout and stderr are drained (which won't happen). + flakyStderr.complete([]); + flakyStdout.complete([]); + return true; + }); expect(() async => await runAsync( ['dummy'], timeout: delay - const Duration(milliseconds: 500),