Repository navigation
[flutter_tools] Guard ExtensionManager initialization against concurrent disposal - #196459
Conversation
…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
…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
There was a problem hiding this comment.
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.
|
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 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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) {| try { | ||
| await _initFuture; | ||
| } on Object { | ||
| // Ignore initialization errors during disposal so cleanup still runs. |
There was a problem hiding this comment.
It might be worth logging a trace here for debugging purposes, but I don't feel strongly
|
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. |
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
Addresses review feedback from #195908:
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.ExtensionManager.ensureInitialized()andExtensionManager.initialize()asyncno-ops once disposal begins, marks test-onlyinitialize()as@visibleForTestingwithassert(_initFuture == null), and checks_isDisposedafter eachawaitduring initialization so late calls cannot re-initialize extensions or leak clients.ToolExtensionService.shutdown()documentation so partner docs cannot go stale if the host timeout changes.Part of #190755
Part of #190746
Pre-launch Checklist
///).