Skip to content

android: don't touch a deleted player from surfaceDestroyed - #418

Open
abdelaziz-mahdy wants to merge 3 commits into
wang-bin:masterfrom
abdelaziz-mahdy:fix/platform-view-dispose-crash
Open

abdelaziz-mahdy wants to merge 3 commits into
wang-bin:masterfrom
abdelaziz-mahdy:fix/platform-view-dispose-crash

Conversation

@abdelaziz-mahdy

@abdelaziz-mahdy abdelaziz-mahdy commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

A VideoViewType.platformView player can crash the app when it is disposed before its SurfaceView is torn down.

What happens

FvpVideoView binds its surface to the player natively (players[surfaceId] in android/fvp_plugin.cpp), and that binding keeps the raw mdkPlayerAPI*. The binding is only removed by FvpVideoView.surfaceDestroyed / dispose. If Player.dispose() deletes the player first, the later surfaceDestroyed calls setDecoders() and updateNativeSurface() on the deleted player:

Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x3f800000 in tid <main>
  #00 pc 000000003f800000  <unknown>
  #01 pc ...               libfvp.so
  #02 pc ...               libfvp.so (Java_com_mediadevkit_fvp_FvpPlugin_nativeSetSurface)
  #05 pc ...               base.vdex (FvpVideoView.surfaceDestroyed)
  #06 pc ...               android.view.SurfaceView.notifySurfaceDestroyed
  ...                      android.view.ViewGroup.removeView

Repro

Android 16 emulator, fvp 0.40.0, registerWith(options: {'tunnel': true}):

final c = VideoPlayerController.networkUrl(url,
    viewType: VideoViewType.platformView);
await c.initialize();
await c.play();
// ... show VideoPlayer(c), wait a few seconds ...
setState(() => showVideo = false); // drop the VideoPlayer widget
await c.dispose();                 // in the same frame

This crashes on the first close, every time (2/2 runs of a 40-cycle loop). Removing the view 0.5 s before disposing does not crash, so it depends on which teardown runs first.

Fix

Player.disposeAsync() calls a new MdkReleasePlayerSurfaces(player) right before deleting the player. It releases every surface binding of that player, and in tunnel mode its decoder while the surface is still alive (the same steps nativeSetSurface(null) already takes, now in a shared releaseSurface() helper). A later surfaceDestroyed finds no binding and returns.

Same loop with this change: 20/20 cycles, no crash.

Relation to #413

Independent of #413 (which moves the delete off the UI isolate for #412), and the two compose: on top of #413 the release still runs on the owning isolate before the worker deletes the player. A branch with both is at abdelaziz-mahdy/fvp app/dispose-fixes. On the same emulator it ran the crash loop 20/20 clean, disposing four texture players together held the main isolate 77-325 ms (938-2704 ms on 0.40.0), and an IPTV app on it did 12 play/close cycles with no crash or ANR.


Stacked on #419, which fixes the three CI jobs that currently fail on every PR (Android on current Flutter, iOS, macOS). Merge #419 first and this PR reduces to its own commit (62a3d8f).

A VideoViewType.platformView player's native binding (players[surfaceId]
in fvp_plugin.cpp) holds the raw mdkPlayerAPI* and is only erased by
FvpVideoView.surfaceDestroyed/dispose. When the player is disposed before
the SurfaceView is torn down, surfaceDestroyed later calls setDecoders()
and updateNativeSurface() on the deleted player and the app dies with
SIGSEGV in libfvp (nativeSetSurface). On an Android 16 emulator this
happens on the first close every time if the VideoPlayer widget is
removed and the controller disposed in the same frame.

Player.disposeAsync() now calls MdkReleasePlayerSurfaces(player) right
before deleting it. That releases every binding of the player, and in
tunnel mode its decoder while the surface is still alive, the same way
nativeSetSurface(null) does; a later surfaceDestroyed finds nothing.
Three jobs fail on every PR now that the stable channel is Flutter 3.50:

- Android (any): Flutter 3.50 requires Gradle 9.3.1 and AGP 9.1.1 (and
  its Gradle plugin calls AGP 9 APIs, so skipping the version check does
  not help). The example moves to Gradle 9.3.1, AGP 9.1.1 and Kotlin
  2.3.20. AGP 9's optimized resource shrinking needs non-final resource
  IDs, so android.nonFinalResIds goes back to its default (true); nothing
  in the example or plugin relies on final IDs. The Flutter 3.22 job
  restores the AGP 8 toolchain in a step, since 3.22 predates AGP 9.
- iOS / macOS: video_player_avfoundation 2.12.2+ calls
  AVAsset.load(_:isolation:), which the Xcode 16.4 on macos-15 lacks.
  Both jobs move to macos-26 (Xcode 26.6).

Verified locally: the example builds a release APK on Flutter 3.50 with
JDK 17.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant