From aac8762a7a16d931eebde40e7179f9913867ca8b Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Mon, 6 Mar 2017 13:23:03 -0800 Subject: [PATCH] Properly handle `ProcessExit` being thrown when `exit()` is called (#8592) --- packages/flutter_tools/lib/executable.dart | 23 +++++++++++++++---- packages/flutter_tools/lib/src/base/io.dart | 6 +++-- .../flutter_tools/lib/src/base/process.dart | 3 ++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/flutter_tools/lib/executable.dart b/packages/flutter_tools/lib/executable.dart index bfdcdedc49..5655d7b874 100644 --- a/packages/flutter_tools/lib/executable.dart +++ b/packages/flutter_tools/lib/executable.dart @@ -182,7 +182,12 @@ Future _handleToolError( return _exit(error.exitCode ?? 1); } else if (error is ProcessExit) { // We've caught an exit code. - return _exit(error.exitCode); + if (error.immediate) { + exit(error.exitCode); + return error.exitCode; + } else { + return _exit(error.exitCode); + } } else { // We've crashed; emit a log report. stderr.writeln(); @@ -217,7 +222,11 @@ Future _handleToolError( 'Unable to generate crash report due to secondary error: $error\n' 'please let us know at https://github.com/flutter/flutter/issues.', ); - return _exit(1); + // Any exception throw here (including one thrown by `_exit()`) will + // get caught by our zone's `onError` handler. In order to avoid an + // infinite error loop, we throw an error that is recognized above + // and will trigger an immediate exit. + throw new ProcessExit(1, immediate: true); } } } @@ -299,9 +308,13 @@ Future _exit(int code) async { // Give the task / timer queue one cycle through before we hard exit. Timer.run(() { - printTrace('exiting with code $code'); - exit(code); - completer.complete(); + try { + printTrace('exiting with code $code'); + exit(code); + completer.complete(); + } catch (error, stackTrace) { + completer.completeError(error, stackTrace); + } }); await completer.future; diff --git a/packages/flutter_tools/lib/src/base/io.dart b/packages/flutter_tools/lib/src/base/io.dart index 596b112f23..de6e06e915 100644 --- a/packages/flutter_tools/lib/src/base/io.dart +++ b/packages/flutter_tools/lib/src/base/io.dart @@ -25,10 +25,12 @@ /// about any additional exports that you add to this file, as doing so will /// increase the API surface that we have to test in Flutter tools, and the APIs /// in `dart:io` can sometimes be hard to use in tests. -import 'dart:io' as io show exit, exitCode; +import 'dart:io' as io show exit; import 'package:meta/meta.dart'; +import 'process.dart'; + export 'dart:io' show BytesBuilder, @@ -88,7 +90,7 @@ ExitFunction get exit => _exitFunction; @visibleForTesting void setExitFunctionForTests([ExitFunction exitFunction]) { _exitFunction = exitFunction ?? (int exitCode) { - throw new Exception('Exited with code ${io.exitCode}'); + throw new ProcessExit(exitCode, immediate: true); }; } diff --git a/packages/flutter_tools/lib/src/base/process.dart b/packages/flutter_tools/lib/src/base/process.dart index 52fd87fd17..7922904324 100644 --- a/packages/flutter_tools/lib/src/base/process.dart +++ b/packages/flutter_tools/lib/src/base/process.dart @@ -295,8 +295,9 @@ String _runWithLoggingSync(List cmd, { } class ProcessExit implements Exception { - ProcessExit(this.exitCode); + ProcessExit(this.exitCode, {this.immediate: false}); + final bool immediate; final int exitCode; String get message => 'ProcessExit: $exitCode';