Skip to content

[flutter_tools] Bound Browser.close in Chromium.close - #196211

Merged
auto-submit[bot] merged 9 commits into
flutter:masterfrom
aliammari1:fix/chrome-browser-close-timeout
Nov 16, 2026
Merged

auto-submit[bot] merged 9 commits into
flutter:masterfrom
aliammari1:fix/chrome-browser-close-timeout

Conversation

@aliammari1

@aliammari1 aliammari1 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Bounds the Browser.close wait in Chromium.close() (flutter_tools/lib/src/web/chrome.dart) with a 5-second timeout (kBrowserCloseTimeout). On timeout, close() falls through to the existing SIGTERM/SIGKILL escalation instead of hanging forever.

Root cause: when Chrome exits before its Browser.close reply arrives, the pending command future in webkit_inspection_protocol never completes (_handleClose closes its streams without completing pending completers), so await wipConnection.sendCommand('Browser.close') never returns. This hung flutter test --platform chrome after tests had already passed.

Fix: the graceful shutdown is bounded by kBrowserCloseTimeout at every step — the tab lookup (getChromeTabGuarded), the WebSocket handshake (tab.connect()) and the Browser.close command, which is also raced against _process.exitCode so a Chrome that already exited returns immediately. The resulting TimeoutException is caught by a dedicated on TimeoutException clause that logs a trace (visible with -v) and proceeds to _process.exitCode SIGTERM (immediate, since sigtermDelay stays zero) → SIGKILL → warning escalation. No new globals.* use, no new dart:io imports; timeout constant is @visibleForTesting like kDevToolsListeningTimeout.

Adds regression tests Chromium close does not hang when Browser.close never replies and Chromium close does not hang when Chrome never responds (test/web.shard/chrome_test.dart) with a FakeWipConnection whose Browser.close future never completes and a FakeChromeConnectionWithTab whose tab lookup never replies, driven by FakeAsync. On the old code these tests hang forever; with the fix they complete via SIGTERM fallback. Existing web.shard/chrome_test.dart passes (39 tests).

Fixes #196191.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Nov 9, 2026
@aliammari1
aliammari1 marked this pull request as ready for review November 9, 2026 15:08

@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 introduces a 5-second timeout (kBrowserCloseTimeout) when sending the Browser.close command to Chrome, preventing Chromium.close from hanging indefinitely if Chrome exits before replying. A corresponding test is added to verify this behavior. Feedback suggests refactoring the new test to avoid using an unawaited asynchronous IIFE inside FakeAsync.run, which is an anti-pattern that can lead to flaky tests, and instead use a synchronous flow with .then() callbacks.

@aliammari1

Copy link
Copy Markdown
Contributor Author

Addressed the Gemini inline feedback (medium) on test/web.shard/chrome_test.dart:1244: refactored the new Chromium close does not hang when Browser.close never replies test to avoid the unawaited () async {}() IIFE inside FakeAsync.run.

What changed: drove connect() and close() with explicit flushMicrotasks() + elapse() and .then() completions, with all assertions inside the run callback — so failures surface synchronously in-zone instead of depending on the outer flushTimers().

Verified locally:

  • dart analyze --fatal-infos on chrome.dart + chrome_test.dart — No issues
  • flutter-dev test test/web.shard/chrome_test.dart — 37 tests passed (including the new hang regression, now synchronous)

Per the template, Gemini feedback is trial/advisory and not authoritative — I verified the refactor myself (test still catches the unbounded Browser.close hang and falls back to SIGTERM). Awaiting human review/LGTM.

@leehack

leehack commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for picking this up. I filed #196191 and the change looks right to me (not a maintainer, so not an approval).

One optional refinement: in the case from the issue, Chrome has already exited when the reply goes missing, so close() now waits the full kBrowserCloseTimeout before moving on. Racing the reply against the process exit would return immediately in that case and keep the timeout for a Chrome that is alive but unresponsive:

await Future.any(<Future<void>>[
  wipConnection.sendCommand('Browser.close'),
  _process.exitCode,
]).timeout(kBrowserCloseTimeout);

Untested sketch; an error from the losing future after the race settles is dropped by Future.any. Not a blocker either way — 5 seconds beats hanging until the CI job times out.

For reference, the package-side fix is google/webkit_inspection_protocol.dart#143.

Wait for either the Browser.close reply or process exit, so an
already-exited Chrome does not wait out kBrowserCloseTimeout. The timeout
still covers a Chrome that is alive but unresponsive.

Adds a test for the already-exited case.

Co-authored-by: Jhin Lee <[email protected]>
@aliammari1
aliammari1 force-pushed the fix/chrome-browser-close-timeout branch from 14108ef to 52371a4 Compare November 10, 2026 12:48
@aliammari1

Copy link
Copy Markdown
Contributor Author

Thanks @leehack — took the refinement.

Chromium.close now races the Browser.close reply against _process.exitCode, so a Chrome that already exited returns immediately instead of waiting out kBrowserCloseTimeout. The timeout still covers a Chrome that is alive but never answers, and the success path is unchanged (reply arrives → 1s SIGTERM grace).

Added a regression test for the already-exited case (Chromium close returns promptly when Chrome already exited); the existing hang test still covers the alive-but-unresponsive path.

Verified: dart analyze clean, test/web.shard/chrome_test.dart 38/38.

Approach credited as Co-authored-by on the commit.

@bkonyi
bkonyi requested review from bkonyi and mdebbar November 11, 2026 20:35
@bkonyi bkonyi added the CICD Run CI/CD label Nov 11, 2026
bkonyi
bkonyi previously approved these changes Nov 11, 2026

@bkonyi bkonyi 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.

This LGTM! We'll just need one more review before we can land this change.

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 11, 2026
/// shutdown and escalating to SIGTERM/SIGKILL.
///
/// The reply is raced against process exit, so a Chrome that already exited
/// returns immediately; this covers one that is alive but never answers.

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.

If Chrome is alive but unresponsive, wouldn't we hang earlier in getChromeTabGuarded or tab.connect(), before reaching this timeout? Should the race + timeout wrap the whole graceful shutdown sequence instead of just Browser.close?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch — you're right, and yes, the earlier steps can block too: getChromeTabGuarded issues its HTTP request through HttpClient with no timeout of its own, and tab.connect() opens a WebSocket with no timeout either, so a Chrome that is alive but unresponsive could hang there before the Browser.close command is even sent.

I've bounded those steps with the same kBrowserCloseTimeout as well:

final ChromeTab? tab = await getChromeTabGuarded(
  chromeConnection,
  (_) => true,
  retryFor: const Duration(seconds: 1),
).timeout(kBrowserCloseTimeout);
...
final WipConnection wipConnection = await tab.connect().timeout(kBrowserCloseTimeout);

I bounded each step rather than wrapping the whole sequence in a single timeout: the steps are independent waits, so per-step timeouts give the same guarantee (no step of the graceful shutdown can hang forever) without restructuring close(). The Browser.close reply is still raced against the process exit, so a Chrome that already exited returns immediately.

Added a test for the case where Chrome never answers the tab lookup: Chromium close does not hang when Chrome never responds.

await Future.any<void>(<Future<void>>[
wipConnection.sendCommand('Browser.close'),
_process.exitCode,
]).timeout(kBrowserCloseTimeout);

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.

When this times out, we silently fall back to SIGTERM. Let's leave a trace so it's diagnosable with -v:

      } on TimeoutException {
        _logger.printTrace(
          'Chrome did not respond to Browser.close within '
          '${kBrowserCloseTimeout.inSeconds}s. Falling back to SIGTERM.',
        );
      } on IOException catch (_) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed — added a trace so the fallback is visible with -v:

} on TimeoutException {
  _logger.printTrace(
    'Chrome did not complete a graceful shutdown within '
    '${kBrowserCloseTimeout.inSeconds}s. Falling back to SIGTERM.',
  );
} on IOException catch (_) {

printTrace only prints with -v, so the default output is unchanged. The new test asserts on logger.traceText.

Chrome can block at any step of the graceful shutdown: the HTTP request
that locates the tab and the WebSocket handshake have no timeouts of their
own, so a Chrome that is alive but unresponsive could hang before the
Browser.close command is even sent.

Bound the tab lookup and the handshake with kBrowserCloseTimeout as well,
and log a trace when the timeout is hit so the fallback to SIGTERM is
visible with -v.
@aliammari1

Copy link
Copy Markdown
Contributor Author

@mdebbar Tell me what do you think now after the changes ?

@bkonyi bkonyi added the CICD Run CI/CD label Nov 13, 2026

@bkonyi bkonyi 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.

Still LGTM!

@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 fixing this!

@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 15, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 15, 2026
@auto-submit

auto-submit Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/196211, Failed to enqueue flutter/flutter/196211 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@aliammari1

aliammari1 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

@mdebbar or @bkonyi can you retry the labeling for auto submit ?

@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 15, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 15, 2026
@auto-submit

auto-submit Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/196211, Failed to enqueue flutter/flutter/196211 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 15, 2026
@mdebbar mdebbar added autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD labels Nov 15, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 15, 2026
@auto-submit

auto-submit Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/196211, because - The status or check suite Presubmit has failed. Please fix the issues identified (or deflake) before re-applying this label.

@aliammari1

Copy link
Copy Markdown
Contributor Author

@mdebbar and @bkonyi can you explain to me the issue ?

@mdebbar

mdebbar commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

The customer_testing failure is happening to all PRs. They have been suppressed with a P0 issue filed. They will be ignored next time they run.

The "Check Code Freeze" is apparently a bug in some Github Actions handling. The created checks remain pending until someone approves them manually in https://github.com/flutter/flutter/actions?query=is%3Aaction_required+workflow%3A%22Material+and+Cupertino+Code+Freeze%22

I approved them and will try again now.

@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 16, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 16, 2026
Merged via the queue into flutter:master with commit 29c22be Nov 16, 2026
29 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 16, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 16, 2026
flutter/flutter@1b95854...d8bd864

2026-11-16 [email protected] Roll Skia from f2790b0ea630 to 118eaff3deb4 (8 revisions) (flutter/flutter#196661)
2026-11-16 [email protected] [native_assets] Keep a dylib's install name in flutter test when its path does not fit (flutter/flutter#194802)
2026-11-16 [email protected] Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196579)
2026-11-16 [email protected] [flutter_tools] Bound Browser.close in Chromium.close (flutter/flutter#196211)
2026-11-16 [email protected] Roll Skia from e8ff7a4187ec to f2790b0ea630 (13 revisions) (flutter/flutter#196652)
2026-11-14 [email protected] Revert: Balance web_canvaskit_tests and web_skwasm_tests subshards by duration (flutter/flutter#196580)
2026-11-14 [email protected] Revert: Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196575)
2026-11-14 [email protected] [flutter_tools] Guard ExtensionManager initialization against concurrent disposal (flutter/flutter#196459)
2026-11-14 [email protected] [ImpellerC] Escape spaces in depfile paths (flutter/flutter#196555)
2026-11-14 [email protected] Reland "[android]: prewarms shaders when the context is created" (flutter/flutter#196549)
2026-11-14 [email protected] Balance web_canvaskit_tests and web_skwasm_tests subshards by duration (flutter/flutter#196260)
2026-11-13 [email protected] Safely return null on malformed stack trace line in StackFrame.fromStackTraceLine (flutter/flutter#194673)
2026-11-13 [email protected] Clarify that super.dispose() must be called synchronously (flutter/flutter#193416)
2026-11-13 [email protected] Consolidate exit-code parameters on runCommand with ExpectedExit (flutter/flutter#196479)
2026-11-13 [email protected] Support multi-view apps in widget test binding (flutter/flutter#194480)
2026-11-13 [email protected] Document using a ListView inside a Column (flutter/flutter#195145)
2026-11-13 [email protected] Add didChangeAlwaysUse24HourFormat to WidgetsBinding and update MediaQuery (flutter/flutter#196117)
2026-11-13 [email protected] Build local engine wasm configs on 32-core Linux bots (flutter/flutter#196284)
2026-11-13 [email protected] [Android] Fix Hybrid Composition resize deadlock (flutter/flutter#196255)
2026-11-13 [email protected] Roll Skia from 11f46cfe8838 to e8ff7a4187ec (9 revisions) (flutter/flutter#196557)
2026-11-13 [email protected] [Impeller] Fix SurfaceContextVK submission tracking and double HostBuffer reset (flutter/flutter#196064)
2026-11-13 [email protected] Manual roll Packages from 14f9933 to 015df97 (5 revisions) (flutter/flutter#196558)
2026-11-13 [email protected] Remove routes_transition_test from widgets (flutter/flutter#186620)
2026-11-13 [email protected] [flutter_tools] Replace TargetPlatform.simpleName with cpuArch (flutter/flutter#196264)
2026-11-13 [email protected] Add a deferred platform-view-created path to the shell and use it on Android (flutter/flutter#196342)
2026-11-13 [email protected] Optimize RenderSliverVariedExtentList and introduce protected itemExtentOf API in RenderSliverFixedExtentBoxAdaptor (flutter/flutter#192384)
2026-11-13 [email protected] Revert: [android]: prewarms shaders when the context is created (flutter/flutter#196468)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flutter test --platform chrome can hang after tests pass: Browser.close has no timeout

4 participants