Skip to content

Commit 3639929

Browse files
amaan-0331MylioScott
authored andcommitted
[Android] Guard PlatformViewsController2.onEndFrame against a detached FlutterView (flutter#190612)
`PlatformViewsController2.onEndFrame()` dereferenced `flutterView` and the result of `flutterView.getRootSurfaceControl()` without checking either for null. `AndroidExternalViewEmbedder2::SubmitFlutterView()` posts `onEndFrame2()` from the raster thread to the platform thread on every frame, including frames with no platform layers. By the time the task runs, the `FlutterView` may have been detached from the controller, in which case `detachFromView()` has set `flutterView` to null, or detached from its window, in which case `View.getRootSurfaceControl()` returns null. The resulting `NullPointerException` is not merely logged. The JNI call ends with `FML_CHECK(fml::jni::CheckException(env))`, which converts the pending Java exception into an `abort()`. This change guards both nullable values and closes the merged `SurfaceControl.Transaction` before returning early so that it is not leaked. It also moves `flutterView.invalidate()` below the guard. Fixes flutter#190609 (cherry picked from commit 8bcda46) Cherry-pick note: the import hunk was placed by hand because this branch does not carry flutter#184732 (rect clipping for surface views); nothing else changed.
1 parent c40b49d commit 3639929

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.content.Context;
1010
import android.graphics.PixelFormat;
1111
import android.util.SparseArray;
12+
import android.view.AttachedSurfaceControl;
1213
import android.view.MotionEvent;
1314
import android.view.MotionEvent.PointerCoords;
1415
import android.view.MotionEvent.PointerProperties;
@@ -542,8 +543,21 @@ public void onEndFrame() {
542543
tx = tx.merge(activeTransactions.get(i));
543544
}
544545
activeTransactions.clear();
546+
547+
// This runs on the platform thread but is posted from the raster thread, so by the time it
548+
// runs the FlutterView may have been detached from the controller, or detached from its
549+
// window (in which case getRootSurfaceControl() returns null). Throwing here is fatal rather
550+
// than merely wrong: the JNI caller, onEndFrame2(), turns a pending Java exception into an
551+
// abort() via FML_CHECK(fml::jni::CheckException(env)).
552+
final AttachedSurfaceControl rootSurfaceControl =
553+
flutterView == null ? null : flutterView.getRootSurfaceControl();
554+
if (rootSurfaceControl == null) {
555+
tx.close();
556+
return;
557+
}
558+
545559
flutterView.invalidate();
546-
flutterView.getRootSurfaceControl().applyTransactionOnDraw(tx);
560+
rootSurfaceControl.applyTransactionOnDraw(tx);
547561
}
548562

549563
// NOT called from UI thread.

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,44 @@ public SurfaceControl.Transaction createTransaction() {
553553
.applyTransactionOnDraw(any(SurfaceControl.Transaction.class));
554554
}
555555

556+
@Test
557+
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
558+
public void onEndFrameIsANoopAfterDetachFromView() {
559+
PlatformViewsController2 controller = new PlatformViewsController2();
560+
controller.setRegistry(new PlatformViewRegistryImpl());
561+
562+
FlutterView mockFlutterView = mock(FlutterView.class);
563+
AttachedSurfaceControl mockAttachedSurfaceControl = mock(AttachedSurfaceControl.class);
564+
when(mockFlutterView.getRootSurfaceControl()).thenReturn(mockAttachedSurfaceControl);
565+
566+
controller.attachToView(mockFlutterView);
567+
controller.detachFromView();
568+
569+
// onEndFrame is posted from the raster thread, so it can run after the view was detached.
570+
// It must not throw: the JNI caller turns a pending exception into an abort.
571+
controller.onEndFrame();
572+
573+
verify(mockAttachedSurfaceControl, never())
574+
.applyTransactionOnDraw(any(SurfaceControl.Transaction.class));
575+
}
576+
577+
@Test
578+
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
579+
public void onEndFrameIsANoopWhenFlutterViewHasNoRootSurfaceControl() {
580+
PlatformViewsController2 controller = new PlatformViewsController2();
581+
controller.setRegistry(new PlatformViewRegistryImpl());
582+
583+
// getRootSurfaceControl() returns null while the view is not attached to a window.
584+
FlutterView mockFlutterView = mock(FlutterView.class);
585+
when(mockFlutterView.getRootSurfaceControl()).thenReturn(null);
586+
587+
controller.attachToView(mockFlutterView);
588+
589+
controller.onEndFrame();
590+
591+
verify(mockFlutterView, never()).invalidate();
592+
}
593+
556594
private static ByteBuffer encodeMethodCall(MethodCall call) {
557595
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call);
558596
buffer.rewind();

0 commit comments

Comments
 (0)