Skip to content

Android_hardware_smoke_test: Cache flutter engine when running vulkan backend instrumented tests#189026

Merged
auto-submit[bot] merged 12 commits into
flutter:masterfrom
andywolff:cache_engine_smoke_test
Jul 10, 2026
Merged

Android_hardware_smoke_test: Cache flutter engine when running vulkan backend instrumented tests#189026
auto-submit[bot] merged 12 commits into
flutter:masterfrom
andywolff:cache_engine_smoke_test

Conversation

@andywolff

@andywolff andywolff commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Improvement related to #182123

Repeatedly creating and destroying the native Vulkan engine context across multiple sequential Activity scenarios triggered a race condition between Android's system compositor (SurfaceControl / WindowManager) releasing platform view buffers in the background and the GPU driver cleaning up its device context. On devices using Mali GPUs (e.g., Pixel 7), this can result in a native crash:
Abort message: 'FORTIFY: pthread_mutex_lock called on a destroyed mutex (...)'

To avoid this crash, we cache the engine across Vulkan tests. But we can't do the same for OpenGLES tests.

When reusing a cached FlutterEngine instance across Activity transitions with OpenGL ES, the engine's EGL context fails to bind (eglMakeCurrent fails with EGL_BAD_ACCESS) to the new Activity's surface. This is because the context and surfaces are tied to the prior Activity/Window lifecycle. Launching a fresh engine instance for OpenGL ES avoids this failure.

E/libEGL  (21131): eglMakeCurrentImpl:1100 error 3002 (EGL_BAD_ACCESS)
E/flutter (21131): [ERROR:flutter/impeller/toolkit/egl/egl.cc(56)] EGL Error: Bad Access (12290) in ../../../flutter/impeller/toolkit/egl/context.cc:55
E/libEGL  (21131): call to OpenGL ES API with no current context (logged once per thread)
E/flutter (21131): [ERROR:flutter/shell/gpu/gpu_surface_gl_impeller.cc(76)] Could not make the context current to acquire the frame.

Pre-launch Checklist

@andywolff

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 caching and reuse of the FlutterEngine for the Vulkan backend in MainActivity to prevent teardown crashes and speed up test execution, along with corresponding cleanup in FlutterActivityTest. Feedback on these changes highlights potential memory leaks: a static reference to FlutterEngine should be wrapped in a WeakReference, and the MethodCallHandler registered on the cached engine should be cleared in cleanUpFlutterEngine to avoid capturing destroyed activity instances. Additionally, a more idiomatic Kotlin implementation is suggested for the engine caching logic.

@Piinks Piinks added engine flutter/engine related. See also e: labels. team-android Owned by Android platform team labels Jul 6, 2026
andywolff added 2 commits July 7, 2026 09:58
- use a weak reference for the engine
- clean up engine and method channel handlers
- kotlin idioms
@github-actions github-actions Bot removed engine flutter/engine related. See also e: labels. team-android Owned by Android platform team labels Jul 7, 2026

@andywolff andywolff left a comment

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.

Addressed all comments in 0b80f70

@andywolff andywolff added engine flutter/engine related. See also e: labels. team-android Owned by Android platform team CICD Run CI/CD labels Jul 7, 2026
@andywolff

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 implements caching for the FlutterEngine on the Vulkan backend in MainActivity to avoid teardown crashes, along with associated cleanup in FlutterActivityTest and checks to prevent duplicate view factory registration. The review feedback identifies a potential memory leak in MainActivity.kt caused by passing an Activity context to the globally cached FlutterEngine, and suggests using the application context instead.

@github-actions github-actions Bot removed engine flutter/engine related. See also e: labels. team-android Owned by Android platform team labels Jul 7, 2026
@andywolff
andywolff marked this pull request as ready for review July 9, 2026 17:16
@andywolff
andywolff requested a review from walley892 July 9, 2026 17:18

@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 engine caching for the Vulkan backend in the Android hardware smoke test to improve performance and prevent teardown crashes. It adds a tearDownClass method to the test suite and updates MainActivity to manage engine lifecycle, including tracking the last configured engine and cleaning up method channel handlers. Feedback was provided regarding the potential for memory leaks when reusing the NativeTextViewFactory and the incorrect approach to clearing method channel handlers in cleanUpFlutterEngine.

@andywolff
andywolff removed the request for review from walley892 July 9, 2026 17:24
@andywolff
andywolff marked this pull request as draft July 9, 2026 17:25
@andywolff

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 caching of the FlutterEngine for the Vulkan backend in MainActivity to prevent teardown crashes and speed up test execution, along with corresponding cleanup in FlutterActivityTest. The review feedback recommends tracking the active activity instance via a static reference to prevent a race condition during activity transitions, which could otherwise cause a destroying activity to prematurely clear the method call handlers of a newly created activity on the shared cached engine.

@andywolff

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 implements Flutter engine caching for the Vulkan backend in the Android hardware smoke test to prevent teardown crashes and optimize test execution. It introduces a teardown mechanism in the test class and manages activity transition states in MainActivity using weak references. The review feedback suggests optimizing the retrieval of the Impeller backend to avoid duplicate expensive PackageManager metadata queries, and performing subclass-specific cleanup in cleanUpFlutterEngine before calling the superclass method.

- read backend only once in provideFlutterEngine
- call super.cleanUpFlutterEngine after subclass cleanup
@andywolff
andywolff marked this pull request as ready for review July 9, 2026 18:21

@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 Flutter engine caching for the Vulkan backend in MainActivity to prevent teardown crashes and optimize test execution, alongside proper cleanup handling in both MainActivity and FlutterActivityTest. The review feedback suggests exposing the cached engine key constant as internal from MainActivity and referencing it in the test class to avoid duplicating the string literal.

@andywolff
andywolff requested a review from walley892 July 9, 2026 20:14

@walley892 walley892 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

@andywolff andywolff added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 10, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Jul 10, 2026
Merged via the queue into flutter:master with commit e329e87 Jul 10, 2026
20 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jul 10, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Jul 12, 2026
flutter/flutter@f7b66f3...cf9e8af

2026-07-12 [email protected] Flip from deprecated strict analysis modes to lint rules (flutter/flutter#187692)
2026-07-12 [email protected] Roll Skia from 6ecffccd32d5 to 8bf65996caba (5 revisions) (flutter/flutter#189333)
2026-07-11 [email protected] Roll Fuchsia Linux SDK from czpzDg9ABY2oKLAOY... to vhIlDkWIy21IrlB9E... (flutter/flutter#189309)
2026-07-11 [email protected] iOS: clean up swiftc.py after migration to target triples (flutter/flutter#189240)
2026-07-10 [email protected] Increase number of 'Mac tool_integration_tests' subshards from 5 to 10 (flutter/flutter#189040)
2026-07-10 [email protected] Fix PopupWindowControllerLinux missing WindowControllerLinux implementation (flutter/flutter#189189)
2026-07-10 [email protected] [android_hardware_smoke_test] Synchronize platform view compositing (flutter/flutter#189151)
2026-07-10 [email protected] UberSDF thin roundrect handling (flutter/flutter#189224)
2026-07-10 [email protected] Roll Skia from ab3a7b98c94d to 6ecffccd32d5 (15 revisions) (flutter/flutter#189270)
2026-07-10 [email protected] [skwasm][canvaskit] Honor Paint.filterQuality in Canvas.drawAtlas (flutter/flutter#186108)
2026-07-10 [email protected] Android_hardware_smoke_test: Cache flutter engine when running vulkan backend instrumented tests (flutter/flutter#189026)

If this roll has caused a breakage, revert this CL and stop the roller
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants