diff --git a/packages/flutter_tools/lib/src/base/process.dart b/packages/flutter_tools/lib/src/base/process.dart index 7654d7c1c207b..6a3400156369b 100644 --- a/packages/flutter_tools/lib/src/base/process.dart +++ b/packages/flutter_tools/lib/src/base/process.dart @@ -9,6 +9,7 @@ import 'package:process/process.dart'; import '../convert.dart'; import '../globals.dart' as globals; +import 'async_guard.dart'; import 'exit.dart'; import 'io.dart'; import 'logger.dart'; @@ -66,15 +67,33 @@ class _DefaultShutdownHooks implements ShutdownHooks { 'Running ${registeredHooks.length} shutdown hook${registeredHooks.length == 1 ? '' : 's'}', ); _shutdownHooksRunning = true; + final uncaught = <(Object, StackTrace)>[]; try { - final futures = >[ - for (final ShutdownHook shutdownHook in registeredHooks) - if (shutdownHook() case final Future result) result, - ]; - await Future.wait(futures); + final futures = >[]; + for (final ShutdownHook shutdownHook in registeredHooks) { + try { + final Future future = asyncGuard( + () async => shutdownHook(), + onError: (Object e, StackTrace s) { + uncaught.add((e, s)); + }, + ); + futures.add(future); + } on Object catch (e, s) { + uncaught.add((e, s)); + } + } + await Future.wait(futures); } finally { _shutdownHooksRunning = false; } + if (uncaught.isNotEmpty) { + logger.printWarning('One or more uncaught errors occurred shutting down:'); + for (final (Object e, StackTrace s) in uncaught) { + logger.printWarning('$e', indent: 2); + logger.printTrace('$s'); + } + } logger.printTrace('Shutdown hooks complete'); } } 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 fcc527dd4ba46..fb887fe20a352 100644 --- a/packages/flutter_tools/test/general.shard/base/process_test.dart +++ b/packages/flutter_tools/test/general.shard/base/process_test.dart @@ -440,6 +440,38 @@ void main() { }, overrides: {Analytics: () => analytics, Logger: () => logger}, ); + + testUsingContext( + '[sync] exceptions thrown from a hook do not crash the tool', + () async { + setExitFunctionForTests((int exitCode) {}); + + final shutdownHooks = ShutdownHooks(); + shutdownHooks.addShutdownHook(() => throw StateError('CRASH')); + await expectLater(exitWithHooks(0, shutdownHooks: shutdownHooks), completes); + expect( + logger.warningText, + stringContainsInOrder(['One or more uncaught errors occurred', 'CRASH']), + ); + }, + overrides: {Analytics: () => analytics, Logger: () => logger}, + ); + + testUsingContext( + '[async] exceptions thrown from a hook do not crash the tool', + () async { + setExitFunctionForTests((int exitCode) {}); + + final shutdownHooks = ShutdownHooks(); + shutdownHooks.addShutdownHook(() async => throw StateError('CRASH')); + await expectLater(exitWithHooks(0, shutdownHooks: shutdownHooks), completes); + expect( + logger.warningText, + stringContainsInOrder(['One or more uncaught errors occurred', 'CRASH']), + ); + }, + overrides: {Analytics: () => analytics, Logger: () => logger}, + ); }); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy