Skip to content

Commit 8f37c46

Browse files
papmoderngmackall
andauthored
[Android] Do not schedule engine frames on a detached FlutterJNI (#191204)
`ImageReaderSurfaceProducer.onImage` is invoked from an `ImageReader` callback posted to the platform thread. When the Activity is destroyed while a texture producer still has a frame in flight, that callback can run after `FlutterJNI` has detached, and `scheduleEngineFrame` then raises `RuntimeException: Cannot execute operation because FlutterJNI is not attached to native.` on the main looper, crashing the app. Guard `FlutterRenderer.scheduleEngineFrame` with `flutterJNI.isAttached()` so all five call sites are covered, mirroring the check that `SurfaceTextureRegistryEntry`'s frame listener and `TextureFinalizerRunnable` already perform. Once the engine is detached there is no engine left to draw the frame, so dropping the request is the correct behaviour. Fixes #188300 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [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]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. Co-authored-by: Gray Mackall <[email protected]>
1 parent 58daa63 commit 8f37c46

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,13 @@ private void registerImageTexture(
13601360

13611361
@VisibleForTesting
13621362
/* package */ void scheduleEngineFrame() {
1363+
if (!flutterJNI.isAttached()) {
1364+
// Texture producers deliver frames asynchronously, so a frame can arrive after the engine
1365+
// has been detached (for example, an ImageReader callback that was already queued on the
1366+
// platform thread when the Activity was destroyed). There is no engine left to draw the
1367+
// frame, and asking a detached FlutterJNI to schedule one throws, so drop the request.
1368+
return;
1369+
}
13631370
flutterJNI.scheduleFrame();
13641371
}
13651372

‎engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,52 @@ public void ImageReaderSurfaceProducerSchedulesFrameIfQueueNotEmpty() throws Exc
988988
verify(flutterRenderer, times(3)).scheduleEngineFrame();
989989
}
990990

991+
@Test
992+
public void itDoesNotScheduleFramesOnADetachedFlutterJNI() {
993+
// Setup the test.
994+
FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
995+
996+
// While attached the frame request is forwarded.
997+
flutterRenderer.scheduleEngineFrame();
998+
verify(fakeFlutterJNI, times(1)).scheduleFrame();
999+
1000+
// Execute the behavior under test.
1001+
engineRule.setJniIsAttached(false);
1002+
flutterRenderer.scheduleEngineFrame();
1003+
1004+
// Verify the behavior under test: still only the call made while attached.
1005+
verify(fakeFlutterJNI, times(1)).scheduleFrame();
1006+
}
1007+
1008+
@Test
1009+
public void ImageReaderSurfaceProducerDoesNotScheduleFrameWhenDetached() throws Exception {
1010+
// Regression test for https://github.com/flutter/flutter/issues/188300.
1011+
FlutterRenderer flutterRenderer = spy(engineRule.getFlutterEngine().getRenderer());
1012+
TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer();
1013+
FlutterRenderer.ImageReaderSurfaceProducer texture =
1014+
(FlutterRenderer.ImageReaderSurfaceProducer) producer;
1015+
texture.disableFenceForTest();
1016+
texture.setSize(1, 1);
1017+
1018+
// The engine detaches, e.g. because the Activity was destroyed, while this producer still has
1019+
// a frame in flight.
1020+
engineRule.setJniIsAttached(false);
1021+
1022+
// Render a frame. The ImageReader callback is delivered on the platform thread and reaches
1023+
// scheduleEngineFrame after the detach.
1024+
Surface surface = texture.getSurface();
1025+
assertNotNull(surface);
1026+
Canvas canvas = surface.lockHardwareCanvas();
1027+
canvas.drawARGB(255, 255, 0, 0);
1028+
surface.unlockCanvasAndPost(canvas);
1029+
shadowOf(Looper.getMainLooper()).idle();
1030+
1031+
// The image still reaches scheduleEngineFrame, ...
1032+
verify(flutterRenderer, times(1)).scheduleEngineFrame();
1033+
// ... but it must not be forwarded to the detached FlutterJNI, which would throw.
1034+
verify(fakeFlutterJNI, never()).scheduleFrame();
1035+
}
1036+
9911037
@Test
9921038
public void getSurface_doesNotReturnInvalidSurface() {
9931039
FlutterRenderer flutterRenderer = spy(engineRule.getFlutterEngine().getRenderer());

0 commit comments

Comments
 (0)