Skip to content

Commit 8bcda46

Browse files
amaan-0331gmackall
andauthored
[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 terminates the process with no Dart-side recovery or application-level workaround other than disabling HCPP. 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, because that call was itself unsafe when `flutterView` was null. The issue reproduces on every warm teardown when an HCPP platform view is visible and the `FlutterEngine` outlives its activity. In a 10-launch loop, the unpatched embedding produced five aborts because every abort killed the process and made the following launch a cold start. Verified locally on an Android 16 emulator by compiling the patched class into the prebuilt embedding JAR: 0 aborts across 12 teardowns. Platform-view rendering continued to work after reattaching the engine. The reproduction app and full before-and-after logs are included in the linked issue. Fixes flutter#190609 ## Pre-launch Checklist * [x] I read the [[Contributor Guide](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview)] and followed the process outlined there for submitting PRs. * [x] I read the [[AI contribution guidelines](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines)] and understand my responsibilities, or I am not using AI tools. * [x] I read the [[Tree Hygiene](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md)] wiki page, which explains my responsibilities. * [x] I read and followed the [[Flutter Style Guide](https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md)], including [[Features we expect every widget to implement](https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement)]. * [x] I signed the [[CLA](https://cla.developers.google.com/)]. * [x] I listed at least one issue that this PR fixes in the description above. * [x] I updated/added relevant documentation (doc comments with `///`). * [x] I added new tests to check the change I am making, or this PR is [[test-exempt](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests)]. * [x] I followed the [[breaking change policy](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes)] and added [[Data Driven Fixes](https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md)] where supported. * [ ] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md Co-authored-by: Gray Mackall <[email protected]>
1 parent f0b937d commit 8bcda46

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
@@ -12,6 +12,7 @@
1212
import android.graphics.Rect;
1313
import android.graphics.RectF;
1414
import android.util.SparseArray;
15+
import android.view.AttachedSurfaceControl;
1516
import android.view.Gravity;
1617
import android.view.MotionEvent;
1718
import android.view.MotionEvent.PointerCoords;
@@ -672,8 +673,21 @@ public void onEndFrame() {
672673
tx = tx.merge(activeTransactions.get(i));
673674
}
674675
activeTransactions.clear();
676+
677+
// This runs on the platform thread but is posted from the raster thread, so by the time it
678+
// runs the FlutterView may have been detached from the controller, or detached from its
679+
// window (in which case getRootSurfaceControl() returns null). Throwing here is fatal rather
680+
// than merely wrong: the JNI caller, onEndFrame2(), turns a pending Java exception into an
681+
// abort() via FML_CHECK(fml::jni::CheckException(env)).
682+
final AttachedSurfaceControl rootSurfaceControl =
683+
flutterView == null ? null : flutterView.getRootSurfaceControl();
684+
if (rootSurfaceControl == null) {
685+
tx.close();
686+
return;
687+
}
688+
675689
flutterView.invalidate();
676-
flutterView.getRootSurfaceControl().applyTransactionOnDraw(tx);
690+
rootSurfaceControl.applyTransactionOnDraw(tx);
677691
}
678692

679693
// 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)