Skip to content

Native Android client for Spotify, built with Jetpack Compose

Signing out or switching accounts drops the play, pause or skip still waiting for a slow or loading page instead of sending it once the next account's page is ready, and a command whose wait for a Connect mirror's end or a device check outlasted the session is not sent [skip ci]

kota-zo6e05c4f6381f
diff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt
index 9fd6844a..bbbd092a 100644
--- a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt
+++ b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt
@@ -443,6 +443,11 @@ object SongPlayer {
/** Call on the main thread before navigating to logout. */
fun clearSession() {
main.removeCallbacksAndMessages(null)
+ // A play, pause or skip still waiting (a slow page, a page still loading) belongs to the
+ // session that ends: it is dropped, not sent once the next account's page is ready.
+ sessionGen.incrementAndGet()
+ transportGen.incrementAndGet()
+ runningTransport?.cancel()
pendingTrackId = null
pendingItemUri = null
pendingUntil = 0L
@@ -870,6 +875,9 @@ object SongPlayer {
private val transportLock = kotlinx.coroutines.sync.Mutex()
private val transportGen = java.util.concurrent.atomic.AtomicInteger()
+ /** Moved by [clearSession]: a transport started for an account that signed out is never sent. */
+ private val sessionGen = java.util.concurrent.atomic.AtomicInteger()
+
/**
* Play, pause, skip: sent one at a time and only the newest one counts. Taps sent all
* at once raced in the Web Player (a skip and a play crossing, a player stuck on one
@@ -898,6 +906,7 @@ object SongPlayer {
block: suspend (isLatest: () -> Boolean) -> Unit,
) {
val gen = transportGen.incrementAndGet()
+ val session = sessionGen.get()
runningTransport?.let { job ->
val held = SystemClock.elapsedRealtime() - runningSince
if (job.isActive && held >= TRANSPORT_STUCK_MS) {
@@ -932,6 +941,8 @@ object SongPlayer {
ConnectMirror.awaitEnded()
val checkLater = pullBack && checkAfter && _activeDevice.value == null
if (pullBack && !checkLater && api.ready.value) takeBackPlayback()
+ // Both may wait (a mirror's end, a device check): the session may have ended meanwhile.
+ if (session != sessionGen.get()) return@withLock
// A cancellation of the caller is never swallowed (it must unwind); only the
// transport's own timeout and a real failure are logged here.
try {