Only write the pid-file while listening to SIGUSR signals. (#74533)
This commit is contained in:
@@ -52,6 +52,7 @@ import 'package:meta/meta.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import 'async_guard.dart';
|
||||
import 'context.dart';
|
||||
import 'file_system.dart';
|
||||
import 'process.dart';
|
||||
|
||||
export 'dart:io'
|
||||
@@ -364,34 +365,60 @@ class Stdio {
|
||||
Future<void> addStderrStream(Stream<List<int>> stream) => stderr.addStream(stream);
|
||||
}
|
||||
|
||||
// TODO(zra): Move pid and writePidFile into `ProcessInfo`.
|
||||
void writePidFile(String pidFile) {
|
||||
if (pidFile != null) {
|
||||
// Write our pid to the file.
|
||||
globals.fs.file(pidFile).writeAsStringSync(io.pid.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/// An overridable version of io.ProcessInfo.
|
||||
abstract class ProcessInfo {
|
||||
factory ProcessInfo() => _DefaultProcessInfo();
|
||||
factory ProcessInfo() => _DefaultProcessInfo(globals.fs);
|
||||
factory ProcessInfo.test(FileSystem fs) => _TestProcessInfo(fs);
|
||||
|
||||
static ProcessInfo get instance => context.get<ProcessInfo>();
|
||||
|
||||
int get currentRss;
|
||||
|
||||
int get maxRss;
|
||||
|
||||
File writePidFile(String pidFile);
|
||||
}
|
||||
|
||||
ProcessInfo get processInfo => ProcessInfo.instance;
|
||||
|
||||
/// The default implementation of [ProcessInfo], which uses [io.ProcessInfo].
|
||||
class _DefaultProcessInfo implements ProcessInfo {
|
||||
_DefaultProcessInfo(this._fileSystem);
|
||||
|
||||
final FileSystem _fileSystem;
|
||||
|
||||
@override
|
||||
int get currentRss => io.ProcessInfo.currentRss;
|
||||
|
||||
@override
|
||||
int get maxRss => io.ProcessInfo.maxRss;
|
||||
|
||||
@override
|
||||
File writePidFile(String pidFile) {
|
||||
assert(pidFile != null);
|
||||
return _fileSystem.file(pidFile)
|
||||
..writeAsStringSync(io.pid.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/// The test version of [ProcessInfo].
|
||||
class _TestProcessInfo implements ProcessInfo {
|
||||
_TestProcessInfo(this._fileSystem);
|
||||
|
||||
final FileSystem _fileSystem;
|
||||
|
||||
@override
|
||||
int currentRss = 1000;
|
||||
|
||||
@override
|
||||
int maxRss = 2000;
|
||||
|
||||
@override
|
||||
File writePidFile(String pidFile) {
|
||||
assert(pidFile != null);
|
||||
return _fileSystem.file(pidFile)
|
||||
..writeAsStringSync('12345');
|
||||
}
|
||||
}
|
||||
|
||||
/// The return type for [listNetworkInterfaces].
|
||||
|
||||
@@ -90,18 +90,26 @@ class AttachCommand extends FlutterCommand {
|
||||
'This parameter is case-insensitive.',
|
||||
)..addOption(
|
||||
'pid-file',
|
||||
help: 'Specify a file to write the process id to. '
|
||||
help: 'Specify a file to write the process ID to. '
|
||||
'You can send SIGUSR1 to trigger a hot reload '
|
||||
'and SIGUSR2 to trigger a hot restart.',
|
||||
'and SIGUSR2 to trigger a hot restart. '
|
||||
'The file is created when the signal handlers '
|
||||
'are hooked and deleted when they are removed.',
|
||||
)..addFlag(
|
||||
'report-ready',
|
||||
help: 'Print "ready" to the console after handling a keyboard command.\n'
|
||||
'This is primarily useful for tests and other automation, but consider '
|
||||
'using --machine instead.',
|
||||
hide: !verboseHelp,
|
||||
)..addOption(
|
||||
'project-root',
|
||||
hide: !verboseHelp,
|
||||
help: 'Normally used only in run target',
|
||||
help: 'Normally used only in run target.',
|
||||
)..addFlag('machine',
|
||||
hide: !verboseHelp,
|
||||
negatable: false,
|
||||
help: 'Handle machine structured JSON command input and provide output '
|
||||
'and progress in machine friendly format.',
|
||||
'and progress in machine-friendly format.',
|
||||
);
|
||||
usesTrackWidgetCreation(verboseHelp: verboseHelp);
|
||||
addDdsOptions(verboseHelp: verboseHelp);
|
||||
@@ -200,8 +208,6 @@ known, it can be explicitly provided to attach via the command-line, e.g.
|
||||
Future<FlutterCommandResult> runCommand() async {
|
||||
await _validateArguments();
|
||||
|
||||
writePidFile(stringArg('pid-file'));
|
||||
|
||||
final Device device = await findTargetDevice();
|
||||
|
||||
final Artifacts overrideArtifacts = device.artifactOverrides ?? globals.artifacts;
|
||||
@@ -362,9 +368,12 @@ known, it can be explicitly provided to attach via the command-line, e.g.
|
||||
logger: globals.logger,
|
||||
terminal: globals.terminal,
|
||||
signals: globals.signals,
|
||||
processInfo: processInfo,
|
||||
reportReady: boolArg('report-ready'),
|
||||
pidFile: stringArg('pid-file'),
|
||||
)
|
||||
..setupTerminal()
|
||||
..registerSignalHandlers();
|
||||
..registerSignalHandlers()
|
||||
..setupTerminal();
|
||||
}));
|
||||
result = await runner.attach(
|
||||
appStartedCompleter: onAppStart,
|
||||
|
||||
@@ -289,11 +289,18 @@ class RunCommand extends RunCommandBase {
|
||||
help: 'Stay resident after launching the application. Not available with "--trace-startup".',
|
||||
)
|
||||
..addOption('pid-file',
|
||||
help: 'Specify a file to write the process id to. '
|
||||
help: 'Specify a file to write the process ID to. '
|
||||
'You can send SIGUSR1 to trigger a hot reload '
|
||||
'and SIGUSR2 to trigger a hot restart.',
|
||||
)
|
||||
..addFlag('benchmark',
|
||||
'and SIGUSR2 to trigger a hot restart. '
|
||||
'The file is created when the signal handlers '
|
||||
'are hooked and deleted when they are removed.',
|
||||
)..addFlag(
|
||||
'report-ready',
|
||||
help: 'Print "ready" to the console after handling a keyboard command.\n'
|
||||
'This is primarily useful for tests and other automation, but consider '
|
||||
'using --machine instead.',
|
||||
hide: !verboseHelp,
|
||||
)..addFlag('benchmark',
|
||||
negatable: false,
|
||||
hide: !verboseHelp,
|
||||
help: 'Enable a benchmarking mode. This will run the given application, '
|
||||
@@ -514,8 +521,6 @@ class RunCommand extends RunCommandBase {
|
||||
final bool hotMode = shouldUseHotMode(buildInfo);
|
||||
final String applicationBinaryPath = stringArg('use-application-binary');
|
||||
|
||||
writePidFile(stringArg('pid-file'));
|
||||
|
||||
if (boolArg('machine')) {
|
||||
if (devices.length > 1) {
|
||||
throwToolExit('--machine does not support -d all.');
|
||||
@@ -620,34 +625,39 @@ class RunCommand extends RunCommandBase {
|
||||
//
|
||||
// Do not add more operations to the future.
|
||||
final Completer<void> appStartedTimeRecorder = Completer<void>.sync();
|
||||
|
||||
TerminalHandler handler;
|
||||
// This callback can't throw.
|
||||
unawaited(appStartedTimeRecorder.future.then<void>(
|
||||
(_) {
|
||||
appStartedTime = globals.systemClock.now();
|
||||
if (stayResident) {
|
||||
TerminalHandler(
|
||||
handler = TerminalHandler(
|
||||
runner,
|
||||
logger: globals.logger,
|
||||
terminal: globals.terminal,
|
||||
signals: globals.signals,
|
||||
processInfo: processInfo,
|
||||
reportReady: boolArg('report-ready'),
|
||||
pidFile: stringArg('pid-file'),
|
||||
)
|
||||
..setupTerminal()
|
||||
..registerSignalHandlers();
|
||||
..registerSignalHandlers()
|
||||
..setupTerminal();
|
||||
}
|
||||
}
|
||||
));
|
||||
|
||||
try {
|
||||
final int result = await runner.run(
|
||||
appStartedCompleter: appStartedTimeRecorder,
|
||||
enableDevTools: stayResident && boolArg(FlutterCommand.kEnableDevTools),
|
||||
route: route,
|
||||
);
|
||||
handler?.stop();
|
||||
if (result != 0) {
|
||||
throwToolExit(null, exitCode: result);
|
||||
}
|
||||
} on RPCError catch (err) {
|
||||
if (err.code == RPCErrorCodes.kServiceDisappeared) {
|
||||
} on RPCError catch (error) {
|
||||
if (error.code == RPCErrorCodes.kServiceDisappeared) {
|
||||
throwToolExit('Lost connection to device.');
|
||||
}
|
||||
rethrow;
|
||||
|
||||
@@ -177,7 +177,9 @@ class DevtoolsServerLauncher extends DevtoolsLauncher {
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
devToolsUrl = null;
|
||||
if (devToolsUrl != null) {
|
||||
devToolsUrl = null;
|
||||
}
|
||||
if (_devToolsProcess != null) {
|
||||
_devToolsProcess.kill();
|
||||
await _devToolsProcess.exitCode;
|
||||
|
||||
@@ -969,7 +969,7 @@ abstract class ResidentRunner {
|
||||
await residentDevtoolsHandler.shutdown();
|
||||
await stopEchoingDeviceLog();
|
||||
await preExit();
|
||||
await exitApp();
|
||||
await exitApp(); // calls appFinished
|
||||
await shutdownDartDevelopmentService();
|
||||
}
|
||||
|
||||
@@ -1452,17 +1452,27 @@ class TerminalHandler {
|
||||
@required Logger logger,
|
||||
@required Terminal terminal,
|
||||
@required Signals signals,
|
||||
@required io.ProcessInfo processInfo,
|
||||
@required bool reportReady,
|
||||
String pidFile,
|
||||
}) : _logger = logger,
|
||||
_terminal = terminal,
|
||||
_signals = signals;
|
||||
_signals = signals,
|
||||
_processInfo = processInfo,
|
||||
_reportReady = reportReady,
|
||||
_pidFile = pidFile;
|
||||
|
||||
final Logger _logger;
|
||||
final Terminal _terminal;
|
||||
final Signals _signals;
|
||||
final io.ProcessInfo _processInfo;
|
||||
final bool _reportReady;
|
||||
final String _pidFile;
|
||||
|
||||
final ResidentRunner residentRunner;
|
||||
bool _processingUserRequest = false;
|
||||
StreamSubscription<void> subscription;
|
||||
File _actualPidFile;
|
||||
|
||||
@visibleForTesting
|
||||
String lastReceivedCommand;
|
||||
@@ -1476,7 +1486,6 @@ class TerminalHandler {
|
||||
subscription = _terminal.keystrokes.listen(processTerminalInput);
|
||||
}
|
||||
|
||||
|
||||
final Map<io.ProcessSignal, Object> _signalTokens = <io.ProcessSignal, Object>{};
|
||||
|
||||
void _addSignalHandler(io.ProcessSignal signal, SignalHandler handler) {
|
||||
@@ -1485,19 +1494,30 @@ class TerminalHandler {
|
||||
|
||||
void registerSignalHandlers() {
|
||||
assert(residentRunner.stayResident);
|
||||
|
||||
_addSignalHandler(io.ProcessSignal.SIGINT, _cleanUp);
|
||||
_addSignalHandler(io.ProcessSignal.SIGTERM, _cleanUp);
|
||||
if (!residentRunner.supportsServiceProtocol || !residentRunner.supportsRestart) {
|
||||
return;
|
||||
if (residentRunner.supportsServiceProtocol && residentRunner.supportsRestart) {
|
||||
_addSignalHandler(io.ProcessSignal.SIGUSR1, _handleSignal);
|
||||
_addSignalHandler(io.ProcessSignal.SIGUSR2, _handleSignal);
|
||||
if (_pidFile != null) {
|
||||
_logger.printTrace('Writing pid to: $_pidFile');
|
||||
_actualPidFile = _processInfo.writePidFile(_pidFile);
|
||||
}
|
||||
}
|
||||
_addSignalHandler(io.ProcessSignal.SIGUSR1, _handleSignal);
|
||||
_addSignalHandler(io.ProcessSignal.SIGUSR2, _handleSignal);
|
||||
}
|
||||
|
||||
/// Unregisters terminal signal and keystroke handlers.
|
||||
void stop() {
|
||||
assert(residentRunner.stayResident);
|
||||
if (_actualPidFile != null) {
|
||||
try {
|
||||
_logger.printTrace('Deleting pid file (${_actualPidFile.path}).');
|
||||
_actualPidFile.deleteSync();
|
||||
} on FileSystemException catch (error) {
|
||||
_logger.printError('Failed to delete pid file (${_actualPidFile.path}): ${error.message}');
|
||||
}
|
||||
_actualPidFile = null;
|
||||
}
|
||||
for (final MapEntry<io.ProcessSignal, Object> entry in _signalTokens.entries) {
|
||||
_signals.removeHandler(entry.key, entry.value);
|
||||
}
|
||||
@@ -1623,6 +1643,9 @@ class TerminalHandler {
|
||||
rethrow;
|
||||
} finally {
|
||||
_processingUserRequest = false;
|
||||
if (_reportReady) {
|
||||
_logger.printStatus('ready');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user