Add session recovery with automatic playback resume#1690
Open
Arneball wants to merge 1 commit intolibrespot-org:devfrom
Open
Add session recovery with automatic playback resume#1690Arneball wants to merge 1 commit intolibrespot-org:devfrom
Arneball wants to merge 1 commit intolibrespot-org:devfrom
Conversation
772e90f to
ed7ccba
Compare
When the TCP connection to Spotify access points drops due to network instability, the keep-alive timeout fires after ~80s and invalidates the session. The old spirc shuts down and a new Session + Spirc is created. However, three things went wrong: 1. Session ID mismatch: The new Session generated a fresh random UUID, so Spotify could not match it to the previous playback session. The automatic transfer in handle_connection_id_update failed because the cluster session_id did not match the new session_id. 2. Volume reset: ConnectConfig.initial_volume was set once at startup and reused on every reconnection. The new SpircTask initialized the mixer to this stale value instead of the users current volume, causing a jarring volume jump on reconnect. 3. Playback not resuming: The transfer state from Spotify always has is_paused=true after a disconnect, since the device went offline. handle_transfer used this to set start_playing=false, so the track loaded paused even though the user was actively listening. Fixes: - Preserve session_id across reconnections for automatic transfer - Preserve mixer volume on reconnect, only override after first connect - Track user play intent via shared Arc<AtomicBool> was_playing flag - Add force_play parameter to handle_transfer for reconnection resume - Add shutdown reason strings to session.shutdown for debugging - Fast-fail audio key requests when session is invalid - Use RwLock<Session> in player for session hot-swap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the TCP connection to Spotify access points drops due to network instability, the keep-alive timeout fires after ~80s and invalidates the session. The old spirc shuts down and a new Session + Spirc is created. However, three things went wrong:
Session ID mismatch: The new Session generated a fresh random UUID, so Spotify could not match it to the previous playback session. The automatic transfer in handle_connection_id_update failed because the cluster session_id did not match the new session_id.
Volume reset: ConnectConfig.initial_volume was set once at startup and reused on every reconnection. The new SpircTask initialized the mixer to this stale value instead of the users current volume, causing a jarring volume jump on reconnect.
Playback not resuming: The transfer state from Spotify always has is_paused=true after a disconnect, since the device went offline. handle_transfer used this to set start_playing=false, so the track loaded paused even though the user was actively listening.
Fixes:
Testing librespot session recovery via iptables
1. Find the AP connection
Example output:
Note the destination IP (
34.158.1.133) and source port (57572).2. Block the connection
Using the example values:
Blocking the specific source port is important — it kills the existing connection while allowing librespot to establish a new one on a different port.
3. Wait for reconnection
The keep-alive timeout fires after ~80 seconds. After that, the session is invalidated and librespot creates a new session + spirc. The audio buffer may sustain playback for another 1-2 minutes before the reconnection completes.
Monitor with:
Look for:
Shutdown: Invalidating session: keep-alive timeoutPreserved session_id across reconnectionstart_playing=true(if music was playing)4. Clean up iptables rules