Skip to content

fix(player): enforce main-thread player access and thread-safe AutoDJ host queries - #424

Merged
ajisth69 merged 1 commit into
mainfrom
fix/player-thread-safety-autodj
Oct 26, 2026
Merged

ajisth69 merged 1 commit into
mainfrom
fix/player-thread-safety-autodj

Conversation

@ajisth69

Copy link
Copy Markdown
Member

Summary

Fixes java.lang.IllegalStateException: Player is accessed on the wrong thread crash reported on Android 16 (CPH2793).

Root Cause

  • \AutoDJController.attachHost\ collected settings in \�pplicationScope\ without specifying a dispatcher, running on \Dispatchers.Default\ (\DefaultDispatcher-worker-X).
  • When calling \host.getQueueSize()\ and \host.getCurrentIndex(), the host callback in \MusicPlayer\ directly accessed \player.mediaItemCount\ and \player.currentMediaItemIndex\ on background worker threads, triggering ExoPlayer's application thread check.
  • Secondary warm-prefetch in \AutoDJController\ also invoked \host.getQueueSize()\ and \host.getTrackAt()\ from \Dispatchers.IO.

Fix

  1. Guard \AutoDJPlaybackHost\ queries in \MusicPlayer: if on the main looper, query \player; otherwise safely query _state.value.queue\ and _state.value.currentIndex\ without touching ExoPlayer.
  2. Synchronize \playHistory\ operations to protect concurrent reads from \getRecentPlayedTrackIds().
  3. Guard \currentPositionMs()\ to only query \player.currentPosition\ when on the main looper.
  4. Launch \settingsPreferences.settings\ collection in \AutoDJController.attachHost\ on \Dispatchers.Main.immediate.

@ajisth69
ajisth69 merged commit acd6b41 into main Oct 26, 2026
1 check passed
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