[flutter_tools] Instantiate shutdown hooks before localfilesystem (#110693)

This commit is contained in:
Christopher Fujino
2022-09-02 10:38:56 -07:00
committed by GitHub
parent 1560d0cef7
commit 08d5d51a98
11 changed files with 79 additions and 48 deletions

View File

@@ -177,17 +177,18 @@ File getUniqueFile(Directory dir, String baseName, String ext) {
/// directories and files that the tool creates under the system temporary
/// directory when the tool exits either normally or when killed by a signal.
class LocalFileSystem extends local_fs.LocalFileSystem {
LocalFileSystem(this._signals, this._fatalSignals, this._shutdownHooks);
LocalFileSystem(this._signals, this._fatalSignals, this.shutdownHooks);
@visibleForTesting
LocalFileSystem.test({
required Signals signals,
List<ProcessSignal> fatalSignals = Signals.defaultExitSignals,
}) : this(signals, fatalSignals, null);
}) : this(signals, fatalSignals, ShutdownHooks());
Directory? _systemTemp;
final Map<ProcessSignal, Object> _signalTokens = <ProcessSignal, Object>{};
final ShutdownHooks? _shutdownHooks;
final ShutdownHooks shutdownHooks;
Future<void> dispose() async {
_tryToDeleteTemp();
@@ -206,7 +207,7 @@ class LocalFileSystem extends local_fs.LocalFileSystem {
_systemTemp?.deleteSync(recursive: true);
}
} on FileSystemException {
// ignore.
// ignore
}
_systemTemp = null;
}
@@ -239,7 +240,7 @@ class LocalFileSystem extends local_fs.LocalFileSystem {
}
// Make sure that the temporary directory is cleaned up when the tool
// exits normally.
_shutdownHooks?.addShutdownHook(
shutdownHooks.addShutdownHook(
_tryToDeleteTemp,
);
}

View File

@@ -4,6 +4,7 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import '../convert.dart';
@@ -13,7 +14,7 @@ import 'logger.dart';
typedef StringConverter = String? Function(String string);
/// A function that will be run before the VM exits.
typedef ShutdownHook = FutureOr<dynamic> Function();
typedef ShutdownHook = FutureOr<void> Function();
// TODO(ianh): We have way too many ways to run subprocesses in this project.
// Convert most of these into one or more lightweight wrappers around the
@@ -22,17 +23,16 @@ typedef ShutdownHook = FutureOr<dynamic> Function();
// for more details.
abstract class ShutdownHooks {
factory ShutdownHooks({
required Logger logger,
}) => _DefaultShutdownHooks(
logger: logger,
);
factory ShutdownHooks() => _DefaultShutdownHooks();
/// Registers a [ShutdownHook] to be executed before the VM exits.
void addShutdownHook(
ShutdownHook shutdownHook
);
@visibleForTesting
List<ShutdownHook> get registeredHooks;
/// Runs all registered shutdown hooks and returns a future that completes when
/// all such hooks have finished.
///
@@ -40,16 +40,17 @@ abstract class ShutdownHooks {
/// hooks within a given stage will be started in parallel and will be
/// guaranteed to run to completion before shutdown hooks in the next stage are
/// started.
Future<void> runShutdownHooks();
///
/// This class is constructed before the [Logger], so it cannot be direct
/// injected in the constructor.
Future<void> runShutdownHooks(Logger logger);
}
class _DefaultShutdownHooks implements ShutdownHooks {
_DefaultShutdownHooks({
required Logger logger,
}) : _logger = logger;
_DefaultShutdownHooks();
final Logger _logger;
final List<ShutdownHook> _shutdownHooks = <ShutdownHook>[];
@override
final List<ShutdownHook> registeredHooks = <ShutdownHook>[];
bool _shutdownHooksRunning = false;
@@ -58,16 +59,18 @@ class _DefaultShutdownHooks implements ShutdownHooks {
ShutdownHook shutdownHook
) {
assert(!_shutdownHooksRunning);
_shutdownHooks.add(shutdownHook);
registeredHooks.add(shutdownHook);
}
@override
Future<void> runShutdownHooks() async {
_logger.printTrace('Running shutdown hooks');
Future<void> runShutdownHooks(Logger logger) async {
logger.printTrace(
'Running ${registeredHooks.length} shutdown hook${registeredHooks.length == 1 ? '' : 's'}',
);
_shutdownHooksRunning = true;
try {
final List<Future<dynamic>> futures = <Future<dynamic>>[];
for (final ShutdownHook shutdownHook in _shutdownHooks) {
for (final ShutdownHook shutdownHook in registeredHooks) {
final FutureOr<dynamic> result = shutdownHook();
if (result is Future<dynamic>) {
futures.add(result);
@@ -77,7 +80,7 @@ class _DefaultShutdownHooks implements ShutdownHooks {
} finally {
_shutdownHooksRunning = false;
}
_logger.printTrace('Shutdown hooks complete');
logger.printTrace('Shutdown hooks complete');
}
}

View File

@@ -313,7 +313,6 @@ Future<T> runInContext<T>(
platform: globals.platform,
usage: globals.flutterUsage,
),
ShutdownHooks: () => ShutdownHooks(logger: globals.logger),
Stdio: () => Stdio(),
SystemClock: () => const SystemClock(),
Usage: () => Usage(

View File

@@ -248,7 +248,11 @@ PlistParser? _plistInstance;
/// The global template renderer.
TemplateRenderer get templateRenderer => context.get<TemplateRenderer>()!;
ShutdownHooks? get shutdownHooks => context.get<ShutdownHooks>();
/// Global [ShutdownHooks] that should be run before the tool process exits.
///
/// This is depended on by [localFileSystem] which is called before any
/// [Context] is set up, and thus this cannot be a Context getter.
final ShutdownHooks shutdownHooks = ShutdownHooks();
// Unless we're in a test of this class's signal handling features, we must
// have only one instance created with the singleton LocalSignals instance