Unverified Commit d2f70a6d authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

kill leaked chrome processes (#39514)

parent 14cec30c
...@@ -14,7 +14,7 @@ class RunningProcessInfo { ...@@ -14,7 +14,7 @@ class RunningProcessInfo {
assert(commandLine != null); assert(commandLine != null);
final String commandLine; final String commandLine;
final int pid; final String pid;
final DateTime creationDate; final DateTime creationDate;
@override @override
...@@ -47,7 +47,7 @@ class RunningProcessInfo { ...@@ -47,7 +47,7 @@ class RunningProcessInfo {
} }
} }
Future<bool> killProcess(int pid, {ProcessManager processManager}) async { Future<bool> killProcess(String pid, {ProcessManager processManager}) async {
assert(pid != null, 'Must specify a pid to kill'); assert(pid != null, 'Must specify a pid to kill');
processManager ??= const LocalProcessManager(); processManager ??= const LocalProcessManager();
ProcessResult result; ProcessResult result;
...@@ -55,14 +55,14 @@ Future<bool> killProcess(int pid, {ProcessManager processManager}) async { ...@@ -55,14 +55,14 @@ Future<bool> killProcess(int pid, {ProcessManager processManager}) async {
result = await processManager.run(<String>[ result = await processManager.run(<String>[
'taskkill.exe', 'taskkill.exe',
'/pid', '/pid',
pid.toString(), pid,
'/f', '/f',
]); ]);
} else { } else {
result = await processManager.run(<String>[ result = await processManager.run(<String>[
'kill', 'kill',
'-9', '-9',
pid.toString(), pid,
]); ]);
} }
return result.exitCode == 0; return result.exitCode == 0;
...@@ -163,7 +163,7 @@ Iterable<RunningProcessInfo> processPowershellOutput(String output) sync* { ...@@ -163,7 +163,7 @@ Iterable<RunningProcessInfo> processPowershellOutput(String output) sync* {
time = '${hours + 12}${time.substring(2)}'; time = '${hours + 12}${time.substring(2)}';
} }
final int pid = int.parse(line.substring(0, processIdHeaderSize).trim()); final String pid = line.substring(0, processIdHeaderSize).trim();
final DateTime creationDate = DateTime.parse('$year-$month-${day}T$time'); final DateTime creationDate = DateTime.parse('$year-$month-${day}T$time');
final String commandLine = line.substring(commandLineHeaderStart).trim(); final String commandLine = line.substring(commandLineHeaderStart).trim();
yield RunningProcessInfo(pid, creationDate, commandLine); yield RunningProcessInfo(pid, creationDate, commandLine);
...@@ -254,7 +254,7 @@ Iterable<RunningProcessInfo> processPsOutput( ...@@ -254,7 +254,7 @@ Iterable<RunningProcessInfo> processPsOutput(
final DateTime creationDate = DateTime.parse('$year-$month-${day}T$time'); final DateTime creationDate = DateTime.parse('$year-$month-${day}T$time');
line = line.substring(24).trim(); line = line.substring(24).trim();
final int nextSpace = line.indexOf(' '); final int nextSpace = line.indexOf(' ');
final int pid = int.parse(line.substring(0, nextSpace)); final String pid = line.substring(0, nextSpace);
final String commandLine = line.substring(nextSpace + 1); final String commandLine = line.substring(nextSpace + 1);
yield RunningProcessInfo(pid, creationDate, commandLine); yield RunningProcessInfo(pid, creationDate, commandLine);
} }
......
...@@ -9,6 +9,7 @@ import 'dart:io'; ...@@ -9,6 +9,7 @@ import 'dart:io';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
import '../framework/framework.dart'; import '../framework/framework.dart';
import '../framework/running_processes.dart';
import '../framework/utils.dart'; import '../framework/utils.dart';
final Directory _editedFlutterGalleryDir = dir(path.join(Directory.systemTemp.path, 'edited_flutter_gallery')); final Directory _editedFlutterGalleryDir = dir(path.join(Directory.systemTemp.path, 'edited_flutter_gallery'));
...@@ -20,119 +21,140 @@ TaskFunction createWebDevModeTest() { ...@@ -20,119 +21,140 @@ TaskFunction createWebDevModeTest() {
'--hot', '-d', 'chrome', '--verbose', '--resident', '--target=lib/main.dart', '--hot', '-d', 'chrome', '--verbose', '--resident', '--target=lib/main.dart',
]; ];
int hotRestartCount = 0; int hotRestartCount = 0;
await inDirectory<void>(flutterDirectory, () async { String chromeProcessName;
rmTree(_editedFlutterGalleryDir); if (Platform.isMacOS) {
mkdirs(_editedFlutterGalleryDir); chromeProcessName = 'Chrome';
recursiveCopy(flutterGalleryDir, _editedFlutterGalleryDir); } else if (Platform.isLinux) {
await inDirectory<void>(_editedFlutterGalleryDir, () async { chromeProcessName = 'chrome';
{ } else if (Platform.isWindows) {
final Process packagesGet = await startProcess( chromeProcessName = 'chrome.exe';
path.join(flutterDirectory.path, 'bin', 'flutter'), }
<String>['packages', 'get'], final Set<String> beforeChromeProcesses = await getRunningProcesses(processName: chromeProcessName)
environment: <String, String>{ .map((RunningProcessInfo info) => info.pid)
'FLUTTER_WEB': 'true', .toSet();
}, try {
); await inDirectory<void>(flutterDirectory, () async {
await packagesGet.exitCode; rmTree(_editedFlutterGalleryDir);
final Process process = await startProcess( mkdirs(_editedFlutterGalleryDir);
path.join(flutterDirectory.path, 'bin', 'flutter'), recursiveCopy(flutterGalleryDir, _editedFlutterGalleryDir);
flutterCommandArgs('run', options), await inDirectory<void>(_editedFlutterGalleryDir, () async {
environment: <String, String>{ {
'FLUTTER_WEB': 'true', final Process packagesGet = await startProcess(
}, path.join(flutterDirectory.path, 'bin', 'flutter'),
); <String>['packages', 'get'],
environment: <String, String>{
'FLUTTER_WEB': 'true',
},
);
await packagesGet.exitCode;
final Process process = await startProcess(
path.join(flutterDirectory.path, 'bin', 'flutter'),
flutterCommandArgs('run', options),
environment: <String, String>{
'FLUTTER_WEB': 'true',
},
);
final Completer<void> stdoutDone = Completer<void>(); final Completer<void> stdoutDone = Completer<void>();
final Completer<void> stderrDone = Completer<void>(); final Completer<void> stderrDone = Completer<void>();
process.stdout process.stdout
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
.transform<String>(const LineSplitter()) .transform<String>(const LineSplitter())
.listen((String line) { .listen((String line) {
if (line.contains('To hot restart')) { if (line.contains('To hot restart')) {
process.stdin.write('R'); process.stdin.write('R');
}
if (line.contains('Restarted')) {
if (hotRestartCount == 0) {
// Update the file and reload again.
final File appDartSource = file(path.join(
_editedFlutterGalleryDir.path, 'lib/gallery/app.dart',
));
appDartSource.writeAsStringSync(
appDartSource.readAsStringSync().replaceFirst(
"'Flutter Gallery'", "'Updated Flutter Gallery'",
)
);
process.stdin.writeln('R');
++hotRestartCount;
} else {
// Quit after second hot restart.
process.stdin.writeln('q');
} }
} if (line.contains('Restarted')) {
print('stdout: $line'); if (hotRestartCount == 0) {
}, onDone: () { // Update the file and reload again.
stdoutDone.complete(); final File appDartSource = file(path.join(
}); _editedFlutterGalleryDir.path, 'lib/gallery/app.dart',
process.stderr ));
.transform<String>(utf8.decoder) appDartSource.writeAsStringSync(
.transform<String>(const LineSplitter()) appDartSource.readAsStringSync().replaceFirst(
.listen((String line) { "'Flutter Gallery'", "'Updated Flutter Gallery'",
print('stderr: $line'); )
}, onDone: () { );
stderrDone.complete(); process.stdin.writeln('R');
}); ++hotRestartCount;
} else {
// Quit after second hot restart.
process.stdin.writeln('q');
}
}
print('stdout: $line');
}, onDone: () {
stdoutDone.complete();
});
process.stderr
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((String line) {
print('stderr: $line');
}, onDone: () {
stderrDone.complete();
});
await Future.wait<void>(<Future<void>>[ await Future.wait<void>(<Future<void>>[
stdoutDone.future, stdoutDone.future,
stderrDone.future, stderrDone.future,
]); ]);
await process.exitCode; await process.exitCode;
} }
// Start `flutter run` again to make sure it loads from the previous // Start `flutter run` again to make sure it loads from the previous
// state. dev compilers loads up from previously compiled JavaScript. // state. dev compilers loads up from previously compiled JavaScript.
{ {
final Process process = await startProcess( final Process process = await startProcess(
path.join(flutterDirectory.path, 'bin', 'flutter'), path.join(flutterDirectory.path, 'bin', 'flutter'),
flutterCommandArgs('run', options), flutterCommandArgs('run', options),
environment: <String, String>{ environment: <String, String>{
'FLUTTER_WEB': 'true', 'FLUTTER_WEB': 'true',
}, },
); );
final Completer<void> stdoutDone = Completer<void>(); final Completer<void> stdoutDone = Completer<void>();
final Completer<void> stderrDone = Completer<void>(); final Completer<void> stderrDone = Completer<void>();
process.stdout process.stdout
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
.transform<String>(const LineSplitter()) .transform<String>(const LineSplitter())
.listen((String line) { .listen((String line) {
if (line.contains('To hot restart')) { if (line.contains('To hot restart')) {
process.stdin.write('R'); process.stdin.write('R');
} }
if (line.contains('Restarted')) { if (line.contains('Restarted')) {
process.stdin.writeln('q'); process.stdin.writeln('q');
} }
print('stdout: $line'); print('stdout: $line');
}, onDone: () { }, onDone: () {
stdoutDone.complete(); stdoutDone.complete();
}); });
process.stderr process.stderr
.transform<String>(utf8.decoder) .transform<String>(utf8.decoder)
.transform<String>(const LineSplitter()) .transform<String>(const LineSplitter())
.listen((String line) { .listen((String line) {
print('stderr: $line'); print('stderr: $line');
}, onDone: () { }, onDone: () {
stderrDone.complete(); stderrDone.complete();
}); });
await Future.wait<void>(<Future<void>>[ await Future.wait<void>(<Future<void>>[
stdoutDone.future, stdoutDone.future,
stderrDone.future, stderrDone.future,
]); ]);
await process.exitCode; await process.exitCode;
} }
});
}); });
}); } finally {
final Set<String> afterChromeProcesses = await getRunningProcesses(processName: chromeProcessName)
.map((RunningProcessInfo info) => info.pid)
.toSet();
final Set<String> newProcesses = afterChromeProcesses.difference(beforeChromeProcesses);
for (String processId in newProcesses) {
await killProcess(processId);
}
}
if (hotRestartCount != 1) { if (hotRestartCount != 1) {
return TaskResult.failure(null); return TaskResult.failure(null);
} }
......
...@@ -24,17 +24,17 @@ ProcessId CreationDate CommandLine ...@@ -24,17 +24,17 @@ ProcessId CreationDate CommandLine
results, results,
equals(<RunningProcessInfo>[ equals(<RunningProcessInfo>[
RunningProcessInfo( RunningProcessInfo(
6552, '6552',
DateTime(2019, 7, 3, 17, 0, 27), DateTime(2019, 7, 3, 17, 0, 27),
r'"C:\tools\dart-sdk\bin\dart.exe" .\bin\agent.dart ci', r'"C:\tools\dart-sdk\bin\dart.exe" .\bin\agent.dart ci',
), ),
RunningProcessInfo( RunningProcessInfo(
6553, '6553',
DateTime(2019, 7, 3, 22, 0, 27), DateTime(2019, 7, 3, 22, 0, 27),
r'"C:\tools\dart-sdk1\bin\dart.exe" .\bin\agent.dart ci', r'"C:\tools\dart-sdk1\bin\dart.exe" .\bin\agent.dart ci',
), ),
RunningProcessInfo( RunningProcessInfo(
6554, '6554',
DateTime(2019, 7, 3, 11, 0, 27), DateTime(2019, 7, 3, 11, 0, 27),
r'"C:\tools\dart-sdk2\bin\dart.exe" .\bin\agent.dart ci', r'"C:\tools\dart-sdk2\bin\dart.exe" .\bin\agent.dart ci',
), ),
...@@ -54,12 +54,12 @@ Sat Mar 9 20:13:00 2019 49 /usr/sbin/syslogd ...@@ -54,12 +54,12 @@ Sat Mar 9 20:13:00 2019 49 /usr/sbin/syslogd
results, results,
equals(<RunningProcessInfo>[ equals(<RunningProcessInfo>[
RunningProcessInfo( RunningProcessInfo(
1, '1',
DateTime(2019, 3, 9, 20, 12, 47), DateTime(2019, 3, 9, 20, 12, 47),
'/sbin/launchd', '/sbin/launchd',
), ),
RunningProcessInfo( RunningProcessInfo(
49, '49',
DateTime(2019, 3, 9, 20, 13, 00), DateTime(2019, 3, 9, 20, 13, 00),
'/usr/sbin/syslogd', '/usr/sbin/syslogd',
), ),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment