Skip to content

[flutter_tools] Guard ExtensionManager initialization against concurrent disposal - #196459

Merged
auto-submit[bot] merged 9 commits into
flutter:masterfrom
bkonyi:ft-ext-shutdown-dispose-followup
Nov 14, 2026
Merged

auto-submit[bot] merged 9 commits into
flutter:masterfrom
bkonyi:ft-ext-shutdown-dispose-followup

Conversation

@bkonyi

@bkonyi bkonyi commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Addresses review feedback from #195908:

  • Updates ExtensionManager.dispose() to memoize _disposeFuture (_isDisposed => _disposeFuture != null), clear active capability lists synchronously, and await any in-flight _initFuture (catching startup errors) before disposing registered connections.
  • Makes ExtensionManager.ensureInitialized() and ExtensionManager.initialize() async no-ops once disposal begins, marks test-only initialize() as @visibleForTesting with assert(_initFuture == null), and checks _isDisposed after each await during initialization so late calls cannot re-initialize extensions or leak clients.
  • Removes the hardcoded 2-second grace period from ToolExtensionService.shutdown() documentation so partner docs cannot go stale if the host timeout changes.

Part of #190755
Part of #190746

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • 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.
  • All existing and new tests are passing.

…utdown, and base modifiers

Adds `ToolExtensionException` (`domainErrorCode = -32001`) to `package:flutter_tools_core` and maps it across `ToolExtensionEntryPoint.run`, `ExtensionConnection.sendRequest`, and host extension clients/managers so extensions can surface actionable operational errors without triggering crash reports. Wraps `ToolExtensionEntryPoint.run` in `IOOverrides.runZoned` with an `exit` guard so extension isolates cannot terminate the host CLI process via `exit()`, wires a bounded `extension.shutdown` RPC into `ExtensionConnection.dispose()`, `ExtensionDiscovery.dispose()`, and host `ShutdownHooks` so registered `ToolExtensionService.shutdown()` hooks run before isolate termination, and adds `abstract base class` modifiers to the service and target hierarchies in `package:flutter_tools_extension`.

Part of flutter#190755
Part of flutter#190746
…dening-and-shutdown

# Conflicts:
#	packages/flutter_tools/lib/src/experimental/extension_build_manager.dart
…dening-and-shutdown

# Conflicts:
#	packages/flutter_tools/test/general.shard/extension_protocol/extension_manager_test.dart
Adds `abstract base class` modifiers to `ToolExtensionService`, `ConfigurationExtension`, `DiagnosticsExtension`, `BuildTarget`, and `AssembleTarget` in `package:flutter_tools_extension` (updating subclasses across the host tool, Linux prototype, and unit tests to `final class`), and memoizes `_disposeFuture` in `ExtensionConnection.dispose()` so concurrent callers await in-flight `extension.shutdown` teardown before resolving.

Part of flutter#190755
Part of flutter#190746
…utdown doc

Avoids specifying a hardcoded 2-second grace period in `ToolExtensionService.shutdown()` partner documentation so it does not go stale if the host timeout changes.

Part of flutter#190755

Part of flutter#190746
…ent disposal

Addresses review feedback from flutter#195908:

- Make `ExtensionManager.dispose()` await any in-flight `_initFuture` (ignoring startup errors during teardown) and memoize `_disposeFuture` before disposing registered connections.

- Track `_isDisposed` in `ExtensionManager` so `ensureInitialized()` and `initialize()` become no-ops once disposal begins and do not re-initialize extensions during shutdown.

Part of flutter#190755

Part of flutter#190746
@bkonyi bkonyi added the CICD Run CI/CD label Nov 12, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Nov 12, 2026
…ispose-followup

# Conflicts:
#	packages/flutter_tools/packages/flutter_tools_extension/lib/src/protocol_base/service.dart
#	packages/flutter_tools/test/general.shard/extension_protocol/extension_manager_test.dart
@bkonyi
bkonyi marked this pull request as ready for review November 13, 2026 19:25

@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 disposal lifecycle management to the ExtensionManager to handle in-flight initializations and prevent operations after disposal. Specifically, it adds _isDisposed and _disposeFuture states, short-circuits initialization methods if disposed, and ensures dispose awaits any active initialization before cleaning up. Feedback suggests chaining concurrent initialization futures to prevent overwriting _initFuture, which could otherwise cause a race condition during disposal.

@bkonyi
bkonyi marked this pull request as draft November 13, 2026 19:47
@flutter-dashboard

Copy link
Copy Markdown

This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Make `ensureInitialized()` and `initialize()` `async` functions that execute their synchronous `_initFuture` registration before awaiting, removing manual `Future<void>.value()` boilerplate. Derive `_isDisposed` directly from `_disposeFuture != null` rather than maintaining a separate boolean field.

Mark `initialize()` as `@visibleForTesting` and assert `_initFuture == null` so test-only `initialize()` calls cannot overwrite an in-flight `_initFuture`. Clear active capability lists and gate `connections` synchronously when `dispose()` begins, and check `_isDisposed` immediately after each `await` in `_initialize()` so late `fetchTitle()` calls cannot leak clients or issue extra RPCs during disposal.
@bkonyi
bkonyi marked this pull request as ready for review November 13, 2026 20:32
@bkonyi
bkonyi requested a review from loic-sharma November 13, 2026 20:33

@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 modifies ExtensionManager to track and handle disposal state, ensuring in-flight initialization is awaited and cleaned up, and subsequent initialization calls are treated as no-ops. It also adds unit tests covering these disposal scenarios. The reviewer suggests adding an early return check for _isDisposed before starting the second loop in _initialize to prevent starting new asynchronous operations on connections that are already being disposed.

_diagnosticsExtensions.clear();
_configurationExtensions.clear();
_deviceExtensions.clear();
for (final ExtensionConnection connection in _discovery.connections) {

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.

medium

If _isDisposed becomes true during the first loop (e.g., during the last _discovery.spawn or connection.dispose), we should avoid entering the second loop and starting new asynchronous operations like client.fetchTitle() on connections that are already being disposed. Adding a check for _isDisposed before starting the second loop prevents these unnecessary and potentially failing operations.

    if (_isDisposed) {
      return;
    }
    for (final ExtensionConnection connection in _discovery.connections) {

@bkonyi
bkonyi requested a review from chingjun November 13, 2026 20:37
try {
await _initFuture;
} on Object {
// Ignore initialization errors during disposal so cleanup still runs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be worth logging a trace here for debugging purposes, but I don't feel strongly

@loic-sharma loic-sharma left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

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

auto-submit Bot commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

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

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 14, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 14, 2026
Merged via the queue into flutter:master with commit 5aa3061 Nov 14, 2026
28 of 29 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 14, 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
@bkonyi bkonyi added the t: extensions Issues related to Flutter Tools extensions label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD t: extensions Issues related to Flutter Tools extensions tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants