Skip to content

Commit e6da335

Browse files
Mylio Sysopsclaude
andcommitted
[android] Guard the HCPP pending transaction list against the raster/platform race (OLYM-5308)
With EnableSurfaceControl (HCPP) on Android 14+, the raster thread appends a SurfaceControl.Transaction to PlatformViewsController2.pendingTransactions for every presented frame (FlutterJNI.createTransaction), while the platform thread drains the same unsynchronized ArrayList after every frame that has no platform view (applyTransaction -> applyTransactions). An append that races the drain leaves a null slot; merge(null) throws a NullPointerException on the main thread, and FML_CHECK(CheckException) after the JNI call in PlatformViewAndroidJNIImpl::applyTransaction (line 2298) aborts the app. A race on any frame, so an app left on screen dies within minutes. Every access to pendingTransactions now holds one lock; applyTransactions drains a snapshot and merges outside the lock. Same idea as upstream flutter#192606, kept minimal because this branch still calls applyTransaction from the no-platform-view frame path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 76aeb9e commit e6da335

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

‎engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController2.java‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ public class PlatformViewsController2 implements PlatformViewsAccessibilityDeleg
6767
private final SparseArray<FlutterMutatorView> platformViewParent;
6868
private final MotionEventTracker motionEventTracker;
6969

70+
// The raster thread appends to pendingTransactions (createTransaction, one per presented frame)
71+
// while the platform thread drains it (swapTransactions, applyTransactions), so every access to
72+
// it holds pendingTransactionsLock. Unguarded, an append that races a drain leaves a null slot
73+
// in the list, merge(null) throws on the platform thread, and the engine's
74+
// FML_CHECK(CheckException) after the JNI call aborts the process. activeTransactions is
75+
// platform-thread only.
7076
private final ArrayList<SurfaceControl.Transaction> pendingTransactions;
77+
private final Object pendingTransactionsLock = new Object();
7178
private final ArrayList<SurfaceControl.Transaction> activeTransactions;
7279
private Surface overlayerSurface = null;
7380
private SurfaceControl overlaySurfaceControl = null;
@@ -546,32 +553,38 @@ public void onEndFrame() {
546553
flutterView.getRootSurfaceControl().applyTransactionOnDraw(tx);
547554
}
548555

549-
// NOT called from UI thread.
556+
// Called on the platform thread (a task posted by the external view embedder).
550557
public synchronized void swapTransactions() {
551558
activeTransactions.clear();
552-
for (int i = 0; i < pendingTransactions.size(); i++) {
553-
activeTransactions.add(pendingTransactions.get(i));
559+
synchronized (pendingTransactionsLock) {
560+
activeTransactions.addAll(pendingTransactions);
561+
pendingTransactions.clear();
554562
}
555-
pendingTransactions.clear();
556563
}
557564

558-
// NOT called from UI thread.
565+
// Called on the raster thread (AHB swapchain present).
559566
@RequiresApi(API_LEVELS.API_34)
560567
public SurfaceControl.Transaction createTransaction() {
561568
SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
562-
pendingTransactions.add(tx);
569+
synchronized (pendingTransactionsLock) {
570+
pendingTransactions.add(tx);
571+
}
563572
return tx;
564573
}
565574

566-
// NOT called from UI thread.
575+
// Called on the platform thread, after every frame that has no platform view.
567576
@RequiresApi(API_LEVELS.API_34)
568577
public void applyTransactions() {
578+
final ArrayList<SurfaceControl.Transaction> drained;
579+
synchronized (pendingTransactionsLock) {
580+
drained = new ArrayList<>(pendingTransactions);
581+
pendingTransactions.clear();
582+
}
569583
SurfaceControl.Transaction tx = new SurfaceControl.Transaction();
570-
for (int i = 0; i < pendingTransactions.size(); i++) {
571-
tx = tx.merge(pendingTransactions.get(i));
584+
for (int i = 0; i < drained.size(); i++) {
585+
tx = tx.merge(drained.get(i));
572586
}
573587
tx.apply();
574-
pendingTransactions.clear();
575588
}
576589

577590
@RequiresApi(API_LEVELS.API_34)

0 commit comments

Comments
 (0)