Skip to content

Replace dynamic free-port probing with OS port-0 binding - #194419

Merged
auto-submit[bot] merged 4 commits into
flutter:masterfrom
bkonyi:issue-194313
Oct 16, 2026
Merged

auto-submit[bot] merged 4 commits into
flutter:masterfrom
bkonyi:issue-194313

Conversation

@bkonyi

@bkonyi bkonyi commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Replaces dynamic socket free-port probing (OperatingSystemUtils.findFreePort and findAvailablePortAndPossiblyCauseFlakyTests) with OS port-0 binding across Chrome, ChromeDriver, and web test runners to prevent TOCTOU race conditions and test flakes.

Part of #194313

Replaces dynamic socket free-port probing (OperatingSystemUtils.findFreePort
and findAvailablePortAndPossiblyCauseFlakyTests) with OS port-0 binding to
prevent TOCTOU race conditions and test flakes across Chrome, ChromeDriver,
and web test runners.

Chromium is launched with --remote-debugging-port=0 when debugPort is not
explicitly set, parsing the bound DevTools WebSocket URL from stderr.
ChromeDriver is launched with --port=0, parsing the bound port from stdout.
Bot and devicelab test servers bind to port 0 directly.

Fixes flutter#194313
@bkonyi bkonyi added the CICD Run CI/CD label Oct 14, 2026
@github-actions github-actions Bot added tool Affects the "flutter" command-line tool. See also t: labels. team-web Owned by Web platform team labels Oct 14, 2026
@bkonyi
bkonyi requested a review from mdebbar October 14, 2026 22:42

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces flaky port-finding logic with port-0 binding, allowing the OS kernel to allocate ephemeral ports atomically when launching Chrome and ChromeDriver, and parses the bound port from the DevTools output. The review feedback suggests updating resolvePort to accept a nullable integer to prevent compilation errors, and throwing a StateError in browser.dart if the debugging port resolves to zero to avoid connection hangs.

mdebbar
mdebbar previously approved these changes Oct 15, 2026

@mdebbar mdebbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for tackling this!

Keep in mind this doesn't fully close #194313. That issue has other remaining items.

Comment thread dev/bots/browser.dart Resolved
Comment thread dev/bots/browser.dart Resolved
Comment thread dev/bots/browser.dart Resolved
mdebbar
mdebbar previously approved these changes Oct 15, 2026
@rmacnak-google

Copy link
Copy Markdown
Contributor

suryaKommana2662 pushed a commit to suryaKommana2662/flutter that referenced this pull request Oct 29, 2026
)

Replaces dynamic socket free-port probing
(`OperatingSystemUtils.findFreePort` and
`findAvailablePortAndPossiblyCauseFlakyTests`) with OS port-0 binding
across Chrome, ChromeDriver, and web test runners to prevent TOCTOU race
conditions and test flakes.

Part of flutter#194313
gaaclarke pushed a commit to gaaclarke/flutter that referenced this pull request Nov 9, 2026
…utter#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD team-web Owned by Web platform team tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants