Skip to content

[Android] Do not use the HCPP external view embedder from its platform tasks - #196490

Merged
auto-submit[bot] merged 6 commits into
flutter:masterfrom
gmackall:hcpp-embedder-platform-task-lifetime
Nov 20, 2026
Merged

auto-submit[bot] merged 6 commits into
flutter:masterfrom
gmackall:hcpp-embedder-platform-task-lifetime

Conversation

@gmackall

@gmackall gmackall commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

With HCPP, every frame posts a small task to the platform thread to apply that frame's platform-view updates, and that task reached back into the embedder through this. When an activity is finished, the platform thread destroys the surface and then the whole shell (and with it the embedder) in one go, before it gets around to running the last frame's task. The task then runs on a destroyed embedder and the app crashes with a null dereference on the main thread.

With this change, the task now carries everything it needs itself: the JNI facade, a snapshot of the frame's data, and shared ownership of the current surface generation's OverlayLayerState (kHidden, kShown, kDestroyed). DestroySurfaces marks the current generation's state kDestroyed on the platform thread before allocating a fresh kHidden state for the next surface generation, so stale tasks queued before teardown neither dereference a destroyed embedder nor call showOverlaySurface2() / hideOverlaySurface2() on an already-destroyed overlay surface. It no longer captures this at all, and with the default capture gone the compiler rejects any attempt to use an embedder member from it.

Stress testing

Launching an activity with an HCPP platform view and calling finish() after 0 to 2000 ms, Impeller Vulkan, debug unopt engine, API 35:

Device Without this change With this change
Pixel 9 7 crashes in 60 finishes 0 in 120 finishes
Pixel 8 12 crashes in 60 finishes 0 in 120 finishes

This before/after table was measured on an earlier base commit. On this PR's base commit, a Pixel 9 ran 80 finishes with this change with 0 occurrences.

Rotation, home/resume and screen off/on with the platform view on screen showed no crashes or rendering problems with this change.

AndroidExternalViewEmbedder2::SubmitFlutterView posts a task to the
platform thread for every frame. Both variants of that task used the
embedder through `this`. When an activity is finished, the platform
thread destroys the surface and then the shell, which destroys the
embedder, before it gets to run the task of the last frame, so the task
used a destroyed embedder and the app crashed.

The tasks now own what they use: they share ownership of the flag that
tracks whether the overlay layer is shown, and they no longer capture
`this`.
@github-actions github-actions Bot added platform-android Android applications specifically engine flutter/engine related. See also e: labels. team-android Owned by Android platform team labels Nov 13, 2026
[jni_facade = jni_facade_,
overlay_layer_is_shown = overlay_layer_is_shown_,
views_visible_last_frame = views_visible_last_frame_]() {
// This pointer is guaranteed to not be dangling as long as

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

deleting comment of shame

@gmackall gmackall added the CICD Run CI/CD label Nov 13, 2026
@gmackall
gmackall marked this pull request as ready for review November 13, 2026 00:52
@gmackall
gmackall requested a review from a team as a code owner November 13, 2026 00:52
@gmackall
gmackall requested review from camsim99 and removed request for a team November 13, 2026 00:52
@gmackall
gmackall requested a review from mboetger November 13, 2026 00:52

@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 refactors AndroidExternalViewEmbedder2 to prevent a crash when tasks posted to the platform thread outlive the embedder. It changes overlay_layer_is_shown_ to a std::shared_ptr<std::atomic_bool> to share ownership with the platform tasks, and moves ShowOverlayLayerIfNeeded and HideOverlayLayerIfNeeded to static helper functions. Regression tests are also added to verify this behavior. The review feedback recommends passing the modified parameters as pointers instead of non-const references, and updating the call sites to use .get() on the shared pointers, to comply with the Google C++ Style Guide.

…pers

Mark the current generation's OverlayLayerState as kDestroyed in
DestroySurfaces before allocating a fresh kHidden state for the next
surface generation, so platform tasks queued before teardown neither
show nor hide an already-destroyed overlay surface. Also pass pointer
parameters to ShowOverlayLayerIfNeeded and HideOverlayLayerIfNeeded per
the style guide.
mboetger
mboetger previously approved these changes Nov 19, 2026

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

LGTM. Minor nit

@gmackall
gmackall requested a review from mboetger November 19, 2026 22:01
@gmackall gmackall added autosubmit Merge PR when tree becomes green via auto submit App and removed autosubmit Merge PR when tree becomes green via auto submit App labels Nov 19, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 20, 2026
Merged via the queue into flutter:master with commit 1767922 Nov 20, 2026
23 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 20, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 20, 2026
Roll Flutter from e8cc6f3451b7 to b34b017b48ad (36 revisions)

flutter/flutter@e8cc6f3...b34b017

2026-11-20 [email protected] Fix OverflowBox dry layout when deferToChild overrides the constraints (flutter/flutter#196839)
2026-11-20 [email protected] Roll Dart SDK from f118f476ebce to 3b5f45d812f8 (1 revision) (flutter/flutter#197048)
2026-11-20 [email protected] [macOS] Register plugins in multiple_windows example (flutter/flutter#196068)
2026-11-20 [email protected] Roll Skia from 87027a9d4659 to a1edf5c2ea5c (1 revision) (flutter/flutter#197045)
2026-11-20 [email protected] Add highp directives to UberSDF to prevent overflow (flutter/flutter#196997)
2026-11-20 [email protected] [Impeller] Cap the Vulkan command pool recycler cache (flutter/flutter#194793)
2026-11-20 [email protected] Roll Skia from 12610b69511d to 87027a9d4659 (4 revisions) (flutter/flutter#197037)
2026-11-20 [email protected] [macOS] Unregister nil binary message handlers (flutter/flutter#193672)
2026-11-20 [email protected] Roll Dart SDK from b803c3ec01de to f118f476ebce (2 revisions) (flutter/flutter#197034)
2026-11-20 [email protected] Roll Skia from 2f1436ba8e64 to 12610b69511d (3 revisions) (flutter/flutter#197030)
2026-11-20 [email protected] [Android] Do not use the HCPP external view embedder from its platform tasks (flutter/flutter#196490)
2026-11-20 [email protected] Roll Dart SDK from 849e17ad3ae6 to b803c3ec01de (1 revision) (flutter/flutter#197012)
2026-11-20 [email protected] [iOS a11y] Synchronize hit-test children when replacing semantics objects (flutter/flutter#196439)
2026-11-20 [email protected] Sync CHANGELOG.md from stable (flutter/flutter#197019)
2026-11-19 [email protected] [flutter_tools] Reland "Extract Windows archives using native tar" (flutter/flutter#193515)
2026-11-19 [email protected] [Reland][flutter_tools] Migrate lib/runner.dart off runInContext and overrides (flutter/flutter#196931)
2026-11-19 [email protected] Include assigned issues in Android incoming triage link (flutter/flutter#197008)
2026-11-19 [email protected] [Flutter GPU] Record on the UI thread, encode and submit on the raster thread (flutter/flutter#194882)
2026-11-19 [email protected] Add Pixel 7 Pro Impeller benchmarks without fixed-performance mode (flutter/flutter#197010)
2026-11-19 [email protected] Reduce time of uiscene_plugin_lifecycle_test_ios (flutter/flutter#196363)
2026-11-19 [email protected] [native assets] Respect Apple deployment targets (flutter/flutter#194146)
2026-11-19 [email protected] iOS: Represent platform view frames as FlutterPlatformViewFrames (flutter/flutter#196955)
2026-11-19 [email protected] Roll Skia from 0bf961e4ff05 to 2f1436ba8e64 (3 revisions) (flutter/flutter#196999)
2026-11-19 [email protected] [ios] Record OCMReject expectations before the stimulus in FlutterPluginAppLifeCycleDelegateTest (flutter/flutter#193011)
2026-11-19 [email protected] Fix post-monorepo doc rot in docs/infra and dev/bots/README.md (flutter/flutter#193010)
2026-11-19 [email protected] Roll Skia from c3567e170362 to 0bf961e4ff05 (4 revisions) (flutter/flutter#196994)
2026-11-19 [email protected] Close Linux popups when their parent loses focuses (flutter/flutter#196825)
2026-11-19 [email protected] Roll Packages from dca2e58 to 81b7c44 (6 revisions) (flutter/flutter#196992)
2026-11-19 [email protected] Roll Dart SDK from d8d40cf9c21b to 849e17ad3ae6 (1 revision) (flutter/flutter#196986)
2026-11-19 [email protected] Fix RouteObserver didPushNext not firing when routes are pushed synchronously (flutter/flutter#189057)
2026-11-19 [email protected] [Impeller] Reuse render targets on text shadow cache misses (flutter/flutter#194769)
2026-11-19 [email protected] Roll Skia from 81de64af9806 to c3567e170362 (3 revisions) (flutter/flutter#196982)
2026-11-19 [email protected] Reland: "Move macOS desktop builds to ThinLTO" (flutter/flutter#196928)
2026-11-19 [email protected] [Linux] Desynchronize the Wayland subsurface so frames don't redraw the GTK window (flutter/flutter#195287)
2026-11-19 [email protected] Roll Skia from 3dfb69d709c7 to 81de64af9806 (1 revision) (flutter/flutter#196975)
2026-11-19 [email protected] Update `ProxyAnimation.didStartListening` and `ProxyAnimation. didStopListening` to use dart's `Conditional member access` (flutter/flutter#195762)

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:
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-android Android applications specifically team-android Owned by Android platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants