Skip to content

fix: prevent "Bad state: Cannot add event after closing" in MdkVideoPlayer - #391

Draft
wang-bin with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-unhandled-exception-error
Draft

fix: prevent "Bad state: Cannot add event after closing" in MdkVideoPlayer#391
wang-bin with Copilot wants to merge 2 commits into
masterfrom
copilot/fix-unhandled-exception-error

Conversation

Copilot AI commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

After dispose() closes the StreamController, async and native callbacks can still fire and attempt to add events, throwing Bad state: Cannot add event after closing.

Changes

  • lib/src/video_player_mdk.dart — Added streamCtl.isClosed guards before every streamCtl.add() and streamCtl.addError() call:
    • textureSize.then(...) async callback (the primary race: future resolves after dispose)
    • onMediaStatus listener — bufferingStart / bufferingEnd events
    • onEvent listener — bufferingUpdate event
    • onStateChanged listener — completed / isPlayingStateUpdate events
    • create() error paths — media open failure and invalid video size
    • getPosition()bufferingUpdate event
textureSize.then((size) {
  if (size == null) return;
  if (streamCtl.isClosed) return;  // added
  streamCtl.add(VideoEvent(...));
});

Events arriving after disposal are silently dropped rather than throwing.

…oPlayer

Co-authored-by: wang-bin <785206+wang-bin@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix unhandled exception in MdkVideoPlayer fix: prevent "Bad state: Cannot add event after closing" in MdkVideoPlayer Aug 7, 2026
Copilot AI requested a review from wang-bin August 7, 2026 07:46
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.

Unhandled Exception: Bad state: Cannot add event after closing

2 participants