Flutter Rotation ANR After Hybrid Composition: The Deadlock and the Fix
Flutter PR #196255 is another strange issue. The scenario is mainly after an HC PlatformView has left the page: at this point the native View is gone, and the Raster Thread resumes independent operation, but previously allocated overlays remain in the SurfacePool. Then on the next screen size change, the Android main thread and Raster Thread can await each other, causing an ANR.
This fix has been merged, but a hotfix has not yet been released.
Why would rotation lock up both threads even after a PlatformView has exited? Here is a concrete example:
A page contains a native WebView, with Flutter-drawn buttons or overlays on top, running Android Hybrid Composition (HC). HC temporarily merges the Raster Thread into the Android Platform Thread (the main thread) so that both sides composite correctly.
But after the user exits the WebView page, the thread merge lease expires, Raster reverts to its own 1.raster, and SurfacePool may still cache OverlayLayers of the old size. If at this moment the screen rotates, a foldable expands, or a split-screen window resizes, Flutter receives a new frame_size, and this can happen:
In
AndroidExternalViewEmbedder::PrepareFlutterView(), Raster detects the size change and callsDestroySurfaces()to post the destruction work to the Platform Thread, then synchronously waits with aWaitableEvent. But the Platform Thread might be synchronously waiting on Raster for calls likeFlutterRenderer.getBitmap()snapshotting,surfaceChanged/NotifyChanged, so the two threads each wait for the other to finish, causing a deadlock.
So there are still some scenarios with limitations, but once triggered it's fatal. The new PR separates GPU release and Java destruction:
ReleaseOverlayLayers()takes old overlays from the pool viaSurfacePool::TakeLayers(), and the Raster thread directly releases the rendering Surface and Vulkan Swapchain they hold, without waiting for the Platform Thread.- The Java-side
ImageReaderstill needs to be closed on the Platform Thread, soDestroyPendingLayers()is posted there to run asynchronously, whilepending_java_destroy_marks that old resources are still outstanding. - If a new frame of HC creates overlays before the cleanup task runs,
SurfacePool::GetLayer()first processes the pending destruction flag, then allocates new resources, andTeardown()also cleans up pending destruction, preventing JNI callbacks from firing after teardown.
The PR was validated on a Pixel 8 real device, Android 14 / API 34, Impeller Vulkan, non-optimized Debug local engine. In one test series, each iteration first let the HC PlatformView exit, waited for thread split, then changed the window size, and artificially made the main thread busy for 55ms, for 500 iterations each before and after. The screenshot deadlock is another set with 20 repetitions per version:
| Metric | Before fix | After fix |
|---|---|---|
Resize + getBitmap() deadlock |
20/20 | 0/20 |
Raster GPURasterizer::Draw p50 |
81.76ms | 15.51ms |
| Raster sleep during resize p50 | 60.80ms | 0.37ms |
Before the fix, Raster spent most time waiting on the main thread's latch. After the fix, it can release GPU resources itself, then close the Java ImageReader when the main thread is idle. Also getBitmap() no longer deadlocks, though the median completion time still has a 252ms wait:
This problem also exists in HCPP (Hybrid Composition++). HCPP has been available since Flutter 3.44; when conditions are met, it uses Android's SurfaceControl transactions and SurfaceFlinger to composite Flutter with native Views, avoiding the overhead of merging Raster and Platform Threads like legacy HC.
HCPP requires Android API 34+ and Impeller Vulkan, which is a significant limitation.
In #190638, HCPP also had a rotation-induced Platform/Raster deadlock fixed this August. The approach then was to keep persistent overlays resized in place, avoiding synchronous destruction on each resize:
| Engine code version | Legacy HC on Frame Size change | HCPP on Frame Size change |
|---|---|---|
| 3.47.9 | sync DestroySurfaces() |
also sync DestroySurfaces() |
| 3.50.0 | sync DestroySurfaces() |
persistent overlay in-place resize |
| master (merged #196255) | ReleaseOverlayLayers() non-blocking release |
persistent overlay in-place resize |
So if you're still on Flutter 3.47.9, both HC and HCPP have a chance of deadlock; only at least 3.50.0 does HCPP solve the problem.
Moreover, the biggest issue is that Android 16 and 17 are starting to ignore user-forced portrait lock configurations, so these kinds of issues are more likely to surface anyway.
Top 1 from juejin.cn, machine-translated. The original thread is authoritative.
😊 Fell into the pit again.