Skip to content

ottstackplayer: drop a load that was stopped while it was preparing

ottstack_player: drop a load that was stopped while it was preparing

Section titled “ottstack_player: drop a load that was stopped while it was preparing”

disposeMediaResources() cancels mediaLoader, but cancelling a CancelableOperation does not stop the future it wraps. When _loadMediaEntry is waiting on media.prepare (a network call, for example for fresh key headers) and the app stops playback, prepare eventually returns and _loadMediaEntry still calls kController.play(...). The stopped media starts playing, often behind another screen.

There is a second, smaller version of the same gap. A stop that lands while playNewMedia is awaiting kController.play(null) is followed by the loader being created anyway.

OSPlayerController keeps a _loadGeneration counter.

  • playNewMedia, retryCurrentMedia, the playlist advance and disposeMediaResources each bump it.
  • playNewMedia returns early if the counter moved during play(null).
  • _loadMediaEntry resolves prepare into a local and returns early if the counter moved. Only then does it set _activeMediaEntry and call native play.
  • catch and finally only touch _error and _loadingMedia for the current generation, so a stale load cannot overwrite a newer one’s state.

The Chorki app pins this with three cases in test/contract/player_lib_load_guard_test.dart:

  • a stop while prepare is pending
  • a stop while play(null) is in flight
  • a superseded load

All three fail without this change and pass with it.

🤖 Generated with Claude Code