Skip to content

Commit cab3ea7

Browse files
authored
[flutter_tools] Retry chromedriver startup when its port is taken (flutter#196150)
Follow-up to flutter#194419. On Linux, `chromedriver --port=0` [binds `::1` first](https://github.com/chromium/chromium/blob/de4e5d6e13b995f5e04137970c0e8ec456017467/chrome/test/chromedriver/server/chromedriver_server.cc#L172-L175) and then [binds `127.0.0.1` on the same port, exiting if it's taken](https://github.com/chromium/chromium/blob/de4e5d6e13b995f5e04137970c0e8ec456017467/chrome/test/chromedriver/server/chromedriver_server.cc#L225-L231). The port the OS picks for `::1` can still be in use on `127.0.0.1`, so `hot_reload_web_server_test` and `hot_restart_web_server_test` occasionally fail with: ``` chromedriver stdout: IPv4 port not available. Exiting... ``` Example CI failures: - https://ci.chromium.org/b/8666997626110753073 - https://ci.chromium.org/b/8665892656502980097 This PR restarts chromedriver (up to 3 attempts) when it exits with that message. Each attempt gets a new port from the OS. I reproduced it locally by holding some IPv4 ports in a network namespace with a narrow ephemeral port range: successful launches went from 9/15 to 14/15. ChromeDriver bug: https://issues.chromium.org/issues/582923020 Part of flutter#194313
1 parent 236153d commit cab3ea7

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

‎packages/flutter_tools/test/web.shard/test_data/web_server_test_common.dart‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,45 @@ class WebServerDeviceTestRunner {
6363

6464
/// Starts the 'chromedriver' process and returns the port number that it is
6565
/// listening on.
66+
///
67+
/// `chromedriver --port=0` lets the OS pick a port for its first bind and
68+
/// then binds the same port on the other loopback address (`[::1]` then
69+
/// `127.0.0.1` on Linux and Windows; the reverse on macOS) without
70+
/// checking that it is free there (see `RunServer` in Chromium's
71+
/// `chrome/test/chromedriver/server/chromedriver_server.cc`). When another
72+
/// process already holds that port, chromedriver prints
73+
/// `IPv4 port not available. Exiting...` (or `IPv6 ...` on macOS) and exits.
74+
/// Each new attempt gets a fresh OS-assigned port.
6675
Future<int> _startChromeDriverProcess() async {
76+
// TODO(mdebbar): Remove the retry once https://issues.chromium.org/issues/582923020 is fixed.
77+
const maxAttempts = 3;
78+
for (var attempt = 1; ; attempt++) {
79+
try {
80+
return await _startChromeDriverProcessOnce();
81+
} on _ChromeDriverPortInUseException {
82+
await _chromeDriverProcess?.exitCode;
83+
if (attempt >= maxAttempts) {
84+
rethrow;
85+
}
86+
printOnFailure('chromedriver could not bind its port (attempt $attempt), retrying.');
87+
}
88+
}
89+
}
90+
91+
Future<int> _startChromeDriverProcessOnce() async {
6792
_chromeDriverProcess = await io.Process.start('chromedriver', <String>['--port=0']);
6893
final completer = Completer<int>();
6994
final portPattern = RegExp(r'started successfully on port (\d+)');
95+
var portInUse = false;
7096
_chromeDriverProcess!.stdout
7197
.transform(utf8.decoder)
7298
.transform(const LineSplitter())
7399
.listen(
74100
(String line) {
75101
printOnFailure('chromedriver stdout: $line');
102+
if (line.contains('port not available')) {
103+
portInUse = true;
104+
}
76105
final Match? match = portPattern.firstMatch(line);
77106
if (!completer.isCompleted && match != null) {
78107
completer.complete(int.parse(match.group(1)!));
@@ -90,7 +119,12 @@ class WebServerDeviceTestRunner {
90119
}
91120
},
92121
onDone: () {
93-
if (!completer.isCompleted) {
122+
if (completer.isCompleted) {
123+
return;
124+
}
125+
if (portInUse) {
126+
completer.completeError(const _ChromeDriverPortInUseException());
127+
} else {
94128
completer.completeError(
95129
'chromedriver stdout closed before finding the expected startup message.',
96130
);
@@ -219,3 +253,11 @@ class WebServerDeviceTestRunner {
219253
}
220254
}
221255
}
256+
257+
/// Thrown when chromedriver exits because the port it picked is already in use.
258+
class _ChromeDriverPortInUseException implements Exception {
259+
const _ChromeDriverPortInUseException();
260+
261+
@override
262+
String toString() => 'chromedriver exited because its port was not available.';
263+
}

0 commit comments

Comments
 (0)