From 35127450714570d1f63add380422a605c6b7c8e8 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Mon, 21 Oct 2024 08:28:30 -0700 Subject: [PATCH] make `ChromiumDevice.stopApp` a no-op if it has already been called (#156778) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (Hopefully) fixes https://github.com/flutter/flutter/issues/156700 To summarize that thread, there are a few ways to trigger cleanup logic in `flutter run -d chrome`. For example, pressing `q` will close Chrome, but Chrome closing triggers cleanup logic in the resident runner (which includes trying to close Chrome, which is redundant). A simple way to fix this is to not call `Chrome.close` in `ChromiumDevice.stopApp` when we've already called it once. This solution isn't ideal—consider a scenario where two calls to `stopApp` are made concurrently. The second one would probably complete before the browser is actually closed. However, I don't foresee anything ever depending on `stopApp` finishing implying that the browser is closed. I feel like there might be a yak to shave here (`ResidentWebRunner` could keep track of its state and not initiate redundant cleanup operations), but I am not sure its worth the continued effort.
Pre-launch checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
[Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../flutter_tools/lib/src/web/web_device.dart | 4 +- .../test/general.shard/web/devices_test.dart | 62 ++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/web/web_device.dart b/packages/flutter_tools/lib/src/web/web_device.dart index ef729796b4..5be4a4e708 100644 --- a/packages/flutter_tools/lib/src/web/web_device.dart +++ b/packages/flutter_tools/lib/src/web/web_device.dart @@ -160,7 +160,9 @@ abstract class ChromiumDevice extends Device { ApplicationPackage? app, { String? userIdentifier, }) async { - await _chrome?.close(); + final Future? future = _chrome?.close(); + _chrome = null; + await future; return true; } diff --git a/packages/flutter_tools/test/general.shard/web/devices_test.dart b/packages/flutter_tools/test/general.shard/web/devices_test.dart index 06f4d0cfc7..b335123640 100644 --- a/packages/flutter_tools/test/general.shard/web/devices_test.dart +++ b/packages/flutter_tools/test/general.shard/web/devices_test.dart @@ -12,6 +12,7 @@ import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/web/chrome.dart'; import 'package:flutter_tools/src/web/web_device.dart'; +import 'package:test/fake.dart'; import '../../src/common.dart'; import '../../src/fake_process_manager.dart'; @@ -32,6 +33,29 @@ void main() { expect(await webDevices.pollingGetDevices(), isEmpty); }); + testWithoutContext('Successive calls of ChromiumDevice.stopApp() do not try to close chrome', () async { + final TestChromiumLauncher launcher = TestChromiumLauncher( + launcher: () => _OnceClosableChromium(), + ); + + final _FakeChromiumDevice chromiumDevice = _FakeChromiumDevice( + chromiumLauncher: launcher, + fileSystem: MemoryFileSystem.test(), + logger: BufferLogger.test(), + ); + + await chromiumDevice.startApp( + null, + debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug), + platformArgs: { + 'uri': '/', + } + ); + + await chromiumDevice.stopApp(null); + await chromiumDevice.stopApp(null); + }); + testWithoutContext('GoogleChromeDevice defaults', () async { final TestChromiumLauncher launcher = TestChromiumLauncher(); @@ -392,7 +416,11 @@ void main() { /// A test implementation of the [ChromiumLauncher] that launches a fixed instance. class TestChromiumLauncher implements ChromiumLauncher { - TestChromiumLauncher(); + TestChromiumLauncher({Chromium Function()? launcher}) { + _launcher = launcher ?? () => _UnimplementedChromium(); + } + + late Chromium Function() _launcher; @override Completer currentCompleter = Completer(); @@ -422,6 +450,7 @@ class TestChromiumLauncher implements ChromiumLauncher { Directory? cacheDir, List webBrowserFlags = const [], }) async { + currentCompleter.complete(_launcher()); return currentCompleter.future; } @@ -430,3 +459,34 @@ class TestChromiumLauncher implements ChromiumLauncher { return currentCompleter.future; } } + +class _FakeChromiumDevice extends ChromiumDevice { + _FakeChromiumDevice( + {required ChromiumLauncher chromiumLauncher, + required super.fileSystem, + required super.logger}) + : super( + name: 'fake name', + chromeLauncher: chromiumLauncher, + ); + + @override + Future get sdkNameAndVersion async => 'fake sdkNameAndVersion'; + + @override + String get name => 'fake name'; +} + +class _OnceClosableChromium extends Fake implements Chromium { + bool _closed = false; + + @override + Future close() async { + if (_closed) { + throw Exception('Already closed'); + } + _closed = true; + } +} + +class _UnimplementedChromium extends Fake implements Chromium { }