Skip to content

Commit d81842a

Browse files
mdebbarsuryaKommana2662
authored andcommitted
[flutter_tools] Fix flutter drive -d chrome hanging on web (flutter#193459)
## Description When `no-launch-chrome` is passed in platform args (e.g. during `flutter drive -d chrome`, where WebDriver launches the browser instead of Flutter), `ChromiumDevice.startApp` does not launch Chrome. As a result, `ChromiumLauncher.connectedInstance` never completes. This caused `flutter drive -d chrome` to hang: 1. `ResidentWebRunner.attach` awaited `ChromiumLauncher.connectedInstance` before signaling that the app started. Because `DriveCommand` awaits `driverService.start()` before calling `startTest()`, WebDriver was never started and the command hung indefinitely. 2. Even if `attach` was bypassed, DWDS defaulted to Chrome-based debugging for `ChromiumDevice`, whose connection provider also awaited `connectedInstance`. This PR fixes both issues by: - Only waiting on `connectedInstance` in `attach()` if the tool actually launched Chromium. - Switching DWDS to the WebSocket debug connection when `no-launch-chrome` is set, matching the `web-server` device behavior. Fixes flutter#192091 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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 in-code documentation (doc comments with `///`). - [ ] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [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 [flutter/website]: https://github.com/flutter/website [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
1 parent b2b941a commit d81842a

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

‎packages/flutter_tools/lib/src/isolated/resident_web_runner.dart‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,20 @@ class ResidentWebRunner extends ResidentRunner {
166166
debuggingOptions.startPaused ||
167167
useDwdsWebSocketConnection;
168168

169-
late final useDwdsWebSocketConnection = flutterDevice!.device is! ChromiumDevice;
169+
/// Whether the tool launches (and therefore owns a DevTools protocol
170+
/// connection to) a Chromium instance for this run.
171+
///
172+
/// This is false for non-Chromium devices and when the caller passes
173+
/// `no-launch-chrome` (e.g. `flutter drive`, where WebDriver launches the
174+
/// browser). In that case [ChromiumLauncher.connectedInstance] never
175+
/// completes, so nothing may wait on it.
176+
late final bool _toolLaunchesChromium =
177+
flutterDevice?.device is ChromiumDevice && platformArgs['no-launch-chrome'] != true;
178+
179+
/// Chrome-based DWDS debugging requires a DevTools protocol connection to a
180+
/// browser launched by the tool. Otherwise, use the DWDS WebSocket
181+
/// connection.
182+
late final bool useDwdsWebSocketConnection = !_toolLaunchesChromium;
170183

171184
@override
172185
// Web uses a different plugin registry.
@@ -816,7 +829,7 @@ class ResidentWebRunner extends ResidentRunner {
816829
Future<ConnectionResult?>? connectDebug,
817830
bool needsFullRestart = true,
818831
}) async {
819-
if (_chromiumLauncher != null) {
832+
if (_chromiumLauncher != null && _toolLaunchesChromium) {
820833
final Chromium chrome = await _chromiumLauncher!.connectedInstance;
821834
final ChromeTab? chromeTab = await getChromeTabGuarded(
822835
chrome.chromeConnection,

‎packages/flutter_tools/test/general.shard/resident_web_runner_test.dart‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,76 @@ name: my_app
14461446
},
14471447
);
14481448

1449+
// Regression test for https://github.com/flutter/flutter/issues/192091.
1450+
testUsingContext(
1451+
'ResidentWebRunner does not wait for a Chromium instance when no-launch-chrome is set',
1452+
() async {
1453+
fakeVmServiceHost = FakeVmServiceHost(requests: kAttachExpectations.toList());
1454+
setupMocks();
1455+
// The launcher of `chromeDevice` never produces an instance, mirroring
1456+
// `flutter drive`, where WebDriver (not the tool) launches the browser.
1457+
flutterDevice.device = chromeDevice;
1458+
final runner = ResidentWebRunner(
1459+
flutterDevice,
1460+
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
1461+
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
1462+
platformArgs: <String, Object?>{'no-launch-chrome': true},
1463+
fileSystem: fileSystem,
1464+
logger: BufferLogger.test(),
1465+
terminal: Terminal.test(),
1466+
platform: FakePlatform(),
1467+
outputPreferences: OutputPreferences.test(),
1468+
analytics: globals.analytics,
1469+
systemClock: globals.systemClock,
1470+
);
1471+
1472+
final appStartedCompleter = Completer<void>();
1473+
final connectionInfoCompleter = Completer<DebugConnectionInfo>();
1474+
unawaited(
1475+
runner.run(
1476+
appStartedCompleter: appStartedCompleter,
1477+
connectionInfoCompleter: connectionInfoCompleter,
1478+
),
1479+
);
1480+
await appStartedCompleter.future;
1481+
await connectionInfoCompleter.future;
1482+
1483+
expect(chromeDevice.lastPlatformArgs!['no-launch-chrome'], true);
1484+
// Chrome-based DWDS debugging would wait on the never-launched instance.
1485+
expect(runner.useDwdsWebSocketConnection, isTrue);
1486+
expect(appConnection.ranMain, isTrue);
1487+
},
1488+
overrides: <Type, Generator>{
1489+
FileSystem: () => fileSystem,
1490+
ProcessManager: () => processManager,
1491+
},
1492+
);
1493+
1494+
testUsingContext(
1495+
'ResidentWebRunner uses Chrome-based debugging when it launches Chromium',
1496+
() {
1497+
flutterDevice.device = chromeDevice;
1498+
final runner = ResidentWebRunner(
1499+
flutterDevice,
1500+
flutterProject: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
1501+
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
1502+
fileSystem: fileSystem,
1503+
logger: BufferLogger.test(),
1504+
terminal: Terminal.test(),
1505+
platform: FakePlatform(),
1506+
outputPreferences: OutputPreferences.test(),
1507+
analytics: globals.analytics,
1508+
systemClock: globals.systemClock,
1509+
);
1510+
1511+
expect(runner.useDwdsWebSocketConnection, isFalse);
1512+
},
1513+
overrides: <Type, Generator>{
1514+
FileSystem: () => fileSystem,
1515+
ProcessManager: () => processManager,
1516+
},
1517+
);
1518+
14491519
testUsingContext(
14501520
'Exits when initial compile fails',
14511521
() async {

0 commit comments

Comments
 (0)