Skip to content

fix(android): move native player deletion off the UI isolate - #413

Open
LinXunFeng wants to merge 3 commits into
wang-bin:masterfrom
LinXunFeng:fix/android-player-disposal-anr
Open

LinXunFeng wants to merge 3 commits into
wang-bin:masterfrom
LinXunFeng:fix/android-player-disposal-anr

Conversation

@LinXunFeng

Copy link
Copy Markdown
Contributor

Fixes #412.

Problem and change

Native player destruction can block while waiting for codec shutdown. Running mdkPlayerAPI_delete on the UI isolate can therefore freeze the Android main thread and trigger the ANR reported in #412.

Move native deletion to a worker with Isolate.run, after texture and callback teardown on the owning isolate. Concurrent disposal calls share completion so the native player is deleted only once. Preserve upstream awaitable disposal and allow retry after a texture release error.

Production verification

We deployed this branch's fix in our newer app releases and have no longer observed this ANR there. The supplied Firebase Crashlytics screenshot shows the following counts for this specific issue in the displayed reporting scope:

App version ANR events
9.0.1 51
9.1.0 0
9.2.0 0

These are app versions, not fvp versions. The screenshot supports the absence of reported events in that scope; it does not establish that the underlying vendor codec stall itself has been eliminated.

Firebase Crashlytics: 51 events in app 9.0.1 and zero in 9.1.0 and 9.2.0

Local validation

  • flutter test --no-pub test/player_dispose_test.dart — passed on macOS. A native fixture delays destruction by 300 ms; the test checks UI-isolate responsiveness, exactly-once deletion, awaited completion, and retry after texture release failure.
  • flutter analyze --no-pub lib/src/player.dart lib/src/video_player_mdk.dart test/player_dispose_test.dart — no issues found.
  • git diff --check — passed.

The fixture models slow native destruction; it does not reproduce the affected Android vendor codec hang. The branch has been merged with current upstream master to resolve disposal-related conflicts.

Keep texture and callback teardown on the owning isolate, then await native deletion on a worker. Share disposal completion to prevent duplicate deletion and let the platform await resource release.

Add a native slow-destruction fixture to verify UI responsiveness and disposal completion. The production codec ANR has not been reproduced on test devices.
Preserve retry after texture release errors while sharing native deletion completion.
@abdelaziz-mahdy

abdelaziz-mahdy commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Verdict: this PR works for me. I'm shipping it (together with #418) in an IPTV app on Android TV.

TV data for this PR

Tested this PR (Android 16 emulator)

  • Closing 4 players: main isolate blocked 152 ms (median) instead of 2117 ms on 0.40.0.
  • 12 play/close cycles in a real app: no crash, no ANR.

Also found: a separate platform-view crash (surfaceDestroyed touching a deleted player), fixed in #418. Works together with this PR.

misobadev added a commit to misobadev/neostation-frontend that referenced this pull request Nov 23, 2026
…861)

* fix(video): stop freezing the app when a preview player is released

On some Android devices the hardware decoder never returns from
MediaCodec::stop. fvp deleted the native player on the UI isolate, which
Flutter runs on Android's main thread, so the app froze until Android
killed it with an ANR (wang-bin/fvp#412). A user's log from an AYANEO
Pocket FIT shows exactly that stack while browsing a list with video
previews.

Vendor fvp 0.40.0 into packages/fvp with the fix from wang-bin/fvp#413:
native deletion runs in Isolate.run, so a stuck decoder leaks that one
player instead of freezing the app. Hardware decoding stays on.

Also pin the MDK SDK on Android to the v0.40.1 release and its SHA-256.
fvp downloaded whatever nightly was current at build time, so 0.16.1 and
1.0.0-rc.2 shipped different libmdk.so builds from the same fvp version.

* style(fvp): format the patched player.dart
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.

[android] ANR:AMediaCodec_stop 不返回,mdkPlayerAPI_reset 阻塞主线程

2 participants