[tool] fallback to sigkill when closing Chromium (#135521)
This implements https://github.com/flutter/flutter/issues/132654#issuecomment-1738221257, namely: Make `Chromium.close` more robust: * Send `SIGTERM` and wait up to 5 seconds, if the process exited, great! Return from the function. * If the process has not exited, then send a `SIGKILL`, which is a much firmer way to exit a process. Same as before, wait up to 5 seconds, if the process exited, great! Return from the function. * If it still hasn't exited then give up trying to exit Chromium, just print a warning to the console and return from the function. Bonus: a few nullability fixes and extra `-v` logging. Fixes https://github.com/flutter/flutter/issues/132654
This commit is contained in:
@@ -213,7 +213,7 @@ void main() {
|
||||
completer.complete();
|
||||
expect(secondLaunchResult.started, true);
|
||||
expect(secondLaunchResult.hasVmService, true);
|
||||
expect(await device.stopApp(iosApp), false);
|
||||
expect(await device.stopApp(iosApp), true);
|
||||
});
|
||||
|
||||
testWithoutContext('IOSDevice.startApp launches in debug mode via log reading on <iOS 13', () async {
|
||||
@@ -291,7 +291,7 @@ void main() {
|
||||
|
||||
expect(launchResult.started, true);
|
||||
expect(launchResult.hasVmService, true);
|
||||
expect(await device.stopApp(iosApp), false);
|
||||
expect(await device.stopApp(iosApp), true);
|
||||
expect(logger.errorText, contains('The Dart VM Service was not discovered after 30 seconds. This is taking much longer than expected...'));
|
||||
expect(utf8.decoder.convert(stdin.writes.first), contains('process interrupt'));
|
||||
completer.complete();
|
||||
@@ -336,7 +336,7 @@ void main() {
|
||||
|
||||
expect(launchResult.started, true);
|
||||
expect(launchResult.hasVmService, true);
|
||||
expect(await device.stopApp(iosApp), false);
|
||||
expect(await device.stopApp(iosApp), true);
|
||||
expect(logger.errorText, contains('The Dart VM Service was not discovered after 45 seconds. This is taking much longer than expected...'));
|
||||
expect(logger.errorText, contains('Click "Allow" to the prompt asking if you would like to find and connect devices on your local network.'));
|
||||
completer.complete();
|
||||
@@ -388,7 +388,7 @@ void main() {
|
||||
expect(processManager, hasNoRemainingExpectations);
|
||||
expect(launchResult.started, true);
|
||||
expect(launchResult.hasVmService, true);
|
||||
expect(await device.stopApp(iosApp), false);
|
||||
expect(await device.stopApp(iosApp), true);
|
||||
});
|
||||
|
||||
testWithoutContext('IOSDevice.startApp does not retry when ios-deploy loses connection if not in CI', () async {
|
||||
|
||||
@@ -39,6 +39,7 @@ import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/context.dart';
|
||||
import '../src/fake_process_manager.dart';
|
||||
import '../src/fake_vm_services.dart';
|
||||
|
||||
const List<VmServiceExpectation> kAttachLogExpectations =
|
||||
@@ -620,8 +621,9 @@ void main() {
|
||||
]);
|
||||
setupMocks();
|
||||
final TestChromiumLauncher chromiumLauncher = TestChromiumLauncher();
|
||||
final FakeProcess process = FakeProcess();
|
||||
final Chromium chrome =
|
||||
Chromium(1, chromeConnection, chromiumLauncher: chromiumLauncher);
|
||||
Chromium(1, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger);
|
||||
chromiumLauncher.setInstance(chrome);
|
||||
|
||||
flutterDevice.device = GoogleChromeDevice(
|
||||
@@ -687,8 +689,9 @@ void main() {
|
||||
]);
|
||||
setupMocks();
|
||||
final TestChromiumLauncher chromiumLauncher = TestChromiumLauncher();
|
||||
final FakeProcess process = FakeProcess();
|
||||
final Chromium chrome =
|
||||
Chromium(1, chromeConnection, chromiumLauncher: chromiumLauncher);
|
||||
Chromium(1, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger);
|
||||
chromiumLauncher.setInstance(chrome);
|
||||
|
||||
flutterDevice.device = GoogleChromeDevice(
|
||||
@@ -1025,8 +1028,9 @@ void main() {
|
||||
setupMocks();
|
||||
final FakeChromeConnection chromeConnection = FakeChromeConnection();
|
||||
final TestChromiumLauncher chromiumLauncher = TestChromiumLauncher();
|
||||
final FakeProcess process = FakeProcess();
|
||||
final Chromium chrome =
|
||||
Chromium(1, chromeConnection, chromiumLauncher: chromiumLauncher);
|
||||
Chromium(1, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger);
|
||||
chromiumLauncher.setInstance(chrome);
|
||||
|
||||
flutterDevice.device = GoogleChromeDevice(
|
||||
|
||||
@@ -213,10 +213,17 @@ class FakeProcess implements io.Process {
|
||||
/// The raw byte content of stdout.
|
||||
final List<int> _stdout;
|
||||
|
||||
/// The list of [kill] signals this process received so far.
|
||||
@visibleForTesting
|
||||
List<io.ProcessSignal> get signals => _signals;
|
||||
final List<io.ProcessSignal> _signals = <io.ProcessSignal>[];
|
||||
|
||||
@override
|
||||
bool kill([io.ProcessSignal signal = io.ProcessSignal.sigterm]) {
|
||||
_signals.add(signal);
|
||||
|
||||
// Killing a fake process has no effect.
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -400,6 +407,7 @@ abstract class FakeProcessManager implements ProcessManager {
|
||||
if (fakeProcess == null) {
|
||||
return false;
|
||||
}
|
||||
fakeProcess.kill(signal);
|
||||
if (fakeProcess._completer != null) {
|
||||
fakeProcess._completer.complete();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io' as io;
|
||||
|
||||
import 'package:fake_async/fake_async.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:file_testing/file_testing.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
@@ -16,7 +18,7 @@ import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/fake_process_manager.dart';
|
||||
import '../src/fakes.dart';
|
||||
import '../src/fakes.dart' hide FakeProcess;
|
||||
|
||||
const List<String> kChromeArgs = <String>[
|
||||
'--disable-background-timer-throttling',
|
||||
@@ -165,6 +167,116 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
testWithoutContext('exits normally using SIGTERM', () async {
|
||||
final BufferLogger logger = BufferLogger.test();
|
||||
final FakeAsync fakeAsync = FakeAsync();
|
||||
|
||||
fakeAsync.run((_) {
|
||||
() async {
|
||||
final FakeChromeConnection chromeConnection = FakeChromeConnection(maxRetries: 4);
|
||||
final ChromiumLauncher chromiumLauncher = ChromiumLauncher(
|
||||
fileSystem: fileSystem,
|
||||
platform: platform,
|
||||
processManager: processManager,
|
||||
operatingSystemUtils: operatingSystemUtils,
|
||||
browserFinder: findChromeExecutable,
|
||||
logger: logger,
|
||||
);
|
||||
|
||||
final FakeProcess process = FakeProcess(
|
||||
duration: const Duration(seconds: 3),
|
||||
);
|
||||
|
||||
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger);
|
||||
|
||||
final Future<void> closeFuture = chrome.close();
|
||||
fakeAsync.elapse(const Duration(seconds: 4));
|
||||
await closeFuture;
|
||||
|
||||
expect(process.signals, <io.ProcessSignal>[io.ProcessSignal.sigterm]);
|
||||
}();
|
||||
});
|
||||
|
||||
fakeAsync.flushTimers();
|
||||
expect(logger.warningText, isEmpty);
|
||||
});
|
||||
|
||||
testWithoutContext('falls back to SIGKILL if SIGTERM did not work', () async {
|
||||
final BufferLogger logger = BufferLogger.test();
|
||||
final FakeAsync fakeAsync = FakeAsync();
|
||||
|
||||
fakeAsync.run((_) {
|
||||
() async {
|
||||
final FakeChromeConnection chromeConnection = FakeChromeConnection(maxRetries: 4);
|
||||
final ChromiumLauncher chromiumLauncher = ChromiumLauncher(
|
||||
fileSystem: fileSystem,
|
||||
platform: platform,
|
||||
processManager: processManager,
|
||||
operatingSystemUtils: operatingSystemUtils,
|
||||
browserFinder: findChromeExecutable,
|
||||
logger: logger,
|
||||
);
|
||||
|
||||
final FakeProcess process = FakeProcess(
|
||||
duration: const Duration(seconds: 6),
|
||||
);
|
||||
|
||||
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger);
|
||||
|
||||
final Future<void> closeFuture = chrome.close();
|
||||
fakeAsync.elapse(const Duration(seconds: 7));
|
||||
await closeFuture;
|
||||
|
||||
expect(process.signals, <io.ProcessSignal>[io.ProcessSignal.sigterm, io.ProcessSignal.sigkill]);
|
||||
}();
|
||||
});
|
||||
|
||||
fakeAsync.flushTimers();
|
||||
expect(
|
||||
logger.warningText,
|
||||
'Failed to exit Chromium (pid: 1234) using SIGTERM. Will try sending SIGKILL instead.\n',
|
||||
);
|
||||
});
|
||||
|
||||
testWithoutContext('falls back to a warning if SIGKILL did not work', () async {
|
||||
final BufferLogger logger = BufferLogger.test();
|
||||
final FakeAsync fakeAsync = FakeAsync();
|
||||
|
||||
fakeAsync.run((_) {
|
||||
() async {
|
||||
final FakeChromeConnection chromeConnection = FakeChromeConnection(maxRetries: 4);
|
||||
final ChromiumLauncher chromiumLauncher = ChromiumLauncher(
|
||||
fileSystem: fileSystem,
|
||||
platform: platform,
|
||||
processManager: processManager,
|
||||
operatingSystemUtils: operatingSystemUtils,
|
||||
browserFinder: findChromeExecutable,
|
||||
logger: logger,
|
||||
);
|
||||
|
||||
final FakeProcess process = FakeProcess(
|
||||
duration: const Duration(seconds: 20),
|
||||
);
|
||||
|
||||
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger);
|
||||
|
||||
final Future<void> closeFuture = chrome.close();
|
||||
fakeAsync.elapse(const Duration(seconds: 30));
|
||||
await closeFuture;
|
||||
expect(process.signals, <io.ProcessSignal>[io.ProcessSignal.sigterm, io.ProcessSignal.sigkill]);
|
||||
}();
|
||||
});
|
||||
|
||||
fakeAsync.flushTimers();
|
||||
expect(
|
||||
logger.warningText,
|
||||
'Failed to exit Chromium (pid: 1234) using SIGTERM. Will try sending SIGKILL instead.\n'
|
||||
'Failed to exit Chromium (pid: 1234) using SIGKILL. Giving up. Will continue, assuming '
|
||||
'Chromium has exited successfully, but it is possible that this left a dangling Chromium '
|
||||
'process running on the system.\n',
|
||||
);
|
||||
});
|
||||
|
||||
testWithoutContext('does not crash if saving profile information fails due to a file system exception.', () async {
|
||||
final BufferLogger logger = BufferLogger.test();
|
||||
chromeLauncher = ChromiumLauncher(
|
||||
@@ -656,7 +768,8 @@ void main() {
|
||||
browserFinder: findChromeExecutable,
|
||||
logger: logger,
|
||||
);
|
||||
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher);
|
||||
final FakeProcess process = FakeProcess();
|
||||
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger);
|
||||
expect(await chromiumLauncher.connect(chrome, false), equals(chrome));
|
||||
expect(logger.errorText, isEmpty);
|
||||
});
|
||||
@@ -672,7 +785,8 @@ void main() {
|
||||
browserFinder: findChromeExecutable,
|
||||
logger: logger,
|
||||
);
|
||||
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher);
|
||||
final FakeProcess process = FakeProcess();
|
||||
final Chromium chrome = Chromium(0, chromeConnection, chromiumLauncher: chromiumLauncher, process: process, logger: logger);
|
||||
await expectToolExitLater(
|
||||
chromiumLauncher.connect(chrome, false),
|
||||
allOf(
|
||||
|
||||
Reference in New Issue
Block a user