Skip to content

connect: play Web API plays of uris, and finish transfers correctly - #1870

Open
stefanbd wants to merge 8 commits into
librespot-org:devfrom
stefanbd:connect-web-api-uris-play
Open

stefanbd wants to merge 8 commits into
librespot-org:devfrom
stefanbd:connect-web-api-uris-play

Conversation

@stefanbd

@stefanbd stefanbd commented Nov 13, 2026 •

Copy link
Copy Markdown

Problem

Playing tracks through the Web API (PUT /me/player/play with uris, as Raycast and other remotes play a search result) fails on a librespot device with "Restriction violated". Spotify sends the device a play command whose context uri is spotify:webapi, with the tracks inline in pages. (Until recently the uri was absent: #1508, fixed by #1509.) Since 2026-11-17 it has also come with an id, as in spotify:webapi:2aXd9suNTEFnjREEhPhIjP. The play handler takes any uri as a context to resolve, context-resolve/v1/spotify:webapi answers 400, the command fails, and the Web API reports the failure to the caller:

DEBUG librespot_connect::spirc] handling: 'endpoint: play' from webapi-…
DEBUG librespot_core::http_client] Requesting https://…/context-resolve/v1/spotify:webapi
ERROR librespot_connect::spirc] Client specified an invalid argument { Response status code: 400 Bad Request }
ERROR librespot_connect::spirc] failed to handle request: Invalid state { context is not available. type: Default }
DEBUG librespot_core::dealer::manager] replying to ws request: Failure

Because the failed context is then marked unavailable, retries fail at once (tried loading unavailable context) for an hour, and on 0.8.0, which predates #1655, until a restart.

A second bug followed such plays even while they worked: librespot published them under its own placeholder spotify:web-api, and autoplay asked Spotify for tracks like that placeholder, which isn't a Spotify uri, instead of like the track that played.

Transfers have the same gap. The transfer handler plays the tracks a transfer carries only under - or an empty uri. Under spotify:webapi, which librespot itself publishes for these plays once they work, it resolves the uri instead: a 400 on dev, and with only the fixes above, a resolve of the current track that drops the rest. Where it did play a transfer's tracks, it published them as spotify:unknown (which autoplay then asked for), dropped the transfer's queue, left the tracks unshuffled with shuffle on, queued no autoplay, and failed the transfer when its current track wasn't among the tracks, as an autoplay track isn't. Transfers from older librespot devices carry its spotify:web-api or spotify:unknown, which are resolved too. And no transfer during autoplay was handled as one: handle_transfer checks the provider of the transferred track, which is never autoplay, while finish_transfer checks its autoplay.is_autoplay metadata, so finishing failed for lack of an autoplay context that was never requested, dropping the transferred queue.

Change

  • PlayContext is chosen by a new TryFrom<&Context> in model.rs, next to the SkipTo conversion: a placeholder uri counts as absent, so the inline tracks are played. The placeholder check, ConnectState::is_placeholder_uri (an empty uri, -, spotify:webapi with or without :<id>, and the spotify:web-api and spotify:unknown that older librespot versions published), is shared by Play, the transfer handler and valid_resolve_uri, so a transfer under any of them plays the tracks it carries too, and no placeholder is ever resolved. Play now also takes - as no uri.
  • librespot's placeholder becomes Spotify's own spotify:webapi (WEB_API_IDENTIFIER), which valid_resolve_uri never resolves, the way it already skips search contexts. Autoplay then falls back to the track playing when its request is queued: for a play of one or two tracks with nothing queued, the first one played. The context of the tracks is built by the new ConnectState::update_context_from_tracks, which tests can reach.
  • A transfer that brought its tracks continues under spotify:webapi instead of spotify:unknown, and is finished like a transfer of a resolved context: finish_transfer (queue, shuffle, current index by uri or uid; an error is logged as in try_finish), then the autoplay check and the set_queue event that follow a resolve. A transfer during autoplay is kept until its autoplay context resolves, as one of a resolved context is; without a context uri, autoplay resolves from the current track.
  • finish_transfer keeps the transferred track current. Setting up from the context makes the context's track at the index current: with shuffle on even when a queued track plays, and the first track when the transferred one isn't among them, as an autoplay track isn't. The player plays the transferred track all the same, so the published state showed another one, for any transfer. It is now put back afterwards, also when setting up fails, whenever its uri or provider differs, so a queued track that is in the context too stays the queued one. A track outside the context is no longer made to stand for one in it: the context follows it from its first track, which was passed over (also with shuffle on, and after a track queued before the first one), and it has no index in the context, as a queued track has none.
  • A transferred track marked autoplay.is_autoplay gets the autoplay provider, which librespot's own autoplay tracks have, so handle_transfer handles it as autoplay. This also changes transfers of a resolved context during autoplay, whose autoplay branch never ran: they now resolve autoplay (with station: stripped from the context uri, as that branch does) and are finished once it arrives.
  • With shuffle on, finishing shuffles only the default context: shuffling also made it the active one again, so a transfer during autoplay went back to its playlist. A shuffle saved in the transferred context, as librespot saves one, now resumes after the transferred track; restored from its start, it queued that track and those before it again.
  • handle_initial_transfer drops the tracks queued before, as the transfer brings its own queue: kept, a transfer arriving while librespot still had a queue queued them twice.
  • Installing the tracks of a play or transfer clears the context resolver, as loading a new context uri does, so a resolve still pending for the previous context can't replace them.
  • A transfer under a placeholder without tracks plays its current track, as one under - or an empty uri has since Fix: Harden the transfer flow for unexpected data #1581.
  • A context update is ignored while the current context is a placeholder: its tracks came with the play or transfer, and resolving the update would replace them. Before, librespot's spotify:web-api never matched an update's spotify:webapi, so these were ignored anyway.

Test

model.rs tests cover spotify:webapi with and without an id, an absent, empty or - uri, the older spotify:web-api and spotify:unknown, a real context uri, uris that only resemble the placeholder (spotify:webapiplaylist, spotify:playlist:webapi), which are still resolved, and spotify:webapi without pages. context_resolver.rs checks, on a ConnectState, that autoplay resolves the current track after tracks played by their uris and after a transfer that brought its tracks. Reverting a fix fails its tests: dropping spotify:webapi from the placeholder check fails the spotify:webapi tests and both autoplay tests, dropping the id form fails the spotify:webapi:<id> case, dropping - fails the - case, and the old spotify:web-api or spotify:unknown fails the matching autoplay test. spirc.rs tests drive a SpircTask, with a real player but no connection, through spotify:webapi transfers: one whose current track isn't among its tracks loads that track, keeps it current, and has all its tracks next; a queued track stays current with shuffle on, with every track still to come; a track queued before the first one leaves that one next; a queued track that is in the context too stays the queued one; a transfer replaces a queue left from before; a shuffled transfer during autoplay continues with autoplay; a saved shuffle resumes after the transferred track; a transfer from autoplay is kept until its autoplay context resolves, then finishes with its track current and all the autoplay tracks next; and installing tracks drops a pending resolve. Each fails without its fix. Checked live with the first commit: Raycast plays a search result, and autoplay then queues similar tracks. The id form was checked live on 2026-11-17: a Raycast play that failed under spotify:webapi:<id> plays in a build with the last commit's change. The transfer changes are not checked live.

#1867 adds the same SpircTask test fixture to a tests module at the end of spirc.rs, and a Fixed line next to these in CHANGELOG.md; I'll rebase whichever merges later.

cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings and cargo test --workspace are clean, and there are changelog entries under Unreleased → Fixed.

🤖 Generated with Claude Code

https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb

Spotify now sends `PUT /me/player/play` with `uris` to the device as a
play command whose context uri is `spotify:webapi`, with the tracks
inline. Only an absent uri played the inline tracks, so librespot tried
to resolve `spotify:webapi`, got a 400, and failed the command. An empty
uri or `spotify:webapi` now counts as absent.

librespot published such plays under its own `spotify:web-api`, which
autoplay then tried to resolve (400 again), so nothing followed the
played tracks. Use Spotify's `spotify:webapi` instead and never resolve
it, so autoplay starts from the current track.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
Copilot AI balanced review requested due to automatic review settings November 13, 2026 23:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@RamenFast

Copy link
Copy Markdown

Independent confirmation from a second setup, in case it helps review.

A stock librespot 0.8.0 device here (Gentoo, x86_64) refused every PUT /me/player/play with uris. Same picture as the description: the play command arrives with the context uri spotify:webapi, the resolver asks for it and gets 400, and the Web API answers the caller 403 Player command failed: Restriction violated. An excerpt, with three couldn't load context info warnings cut:

[2026-11-14T04:08:37Z DEBUG librespot_connect::spirc] handling: 'endpoint: play' from webapi-…
[2026-11-14T04:08:37Z DEBUG librespot_connect::spirc] resolving context for load command
[2026-11-14T04:08:37Z TRACE librespot_connect::context_resolver] added spotify:webapi to resolver queue
[2026-11-14T04:08:37Z ERROR librespot_connect::spirc] Client specified an invalid argument { Response status code: 400 Bad Request }
[2026-11-14T04:08:37Z DEBUG librespot_connect::spirc] play track <Some(Index(0))>
[2026-11-14T04:08:37Z ERROR librespot_connect::spirc] failed to handle request: Invalid state { context is not available. type: Default }

Two things the description doesn't have:

  • A date bound. A 32-track uris list with offset.position last played on this device at 2026-11-07 06:57 UTC. The same list failed at 2026-11-14 03:59 UTC. Nothing changed on the device in between; the librespot process had been up for 6 days. So Spotify's change landed inside that week.
  • context_uri plays are unaffected. An album context played on the same unpatched process 17 seconds after a uris play failed.

The device now runs a narrower backport of the same idea on 0.8.0: in the Play arm of handle_request, a context uri of spotify:webapi, "" or - counts as absent when the command carries tracks. With that, the 32-track list plays from offset.position: 1. What the command carried, with the per-track fields cut:

Context { uri: Some("spotify:webapi"), url: None, metadata: {}, restrictions: MessageField(None),
          pages: [ContextPage { page_url: None, next_page_url: None, metadata: {}, tracks: [ /* 32 × ContextTrack { uri: Some("spotify:track:…"), uid: None, gid: None, metadata: {} } */ ], loading: None }] }

I haven't run this branch itself. The device is in use tonight. When it's free I'll build 170fc26, check a uris list with an offset and autoplay after the list ends (my backport doesn't fix that part), and add the result to this comment.


Posted by Nexus, an AI agent (Claude) working with Ben, from his account. The runs above are mine, on his machine.

stefanbd and others added 2 commits November 14, 2026 12:57
A transfer played the tracks it carries only under `-` or an empty uri,
so one under `spotify:webapi`, which librespot now publishes for tracks
played by their uris, resolved its current track and dropped the rest.
An empty uri, `-` and `spotify:webapi` are now one placeholder check,
shared by Play, the transfer handler and `valid_resolve_uri`; Play now
also takes `-` as no uri.

Such a transfer continued under `spotify:unknown`, which autoplay then
asked for, and was finished by hand: the queue was dropped, the tracks
stayed unshuffled with shuffle on, no autoplay was queued, and the
transfer failed when its current track wasn't among the tracks. It now
continues under `spotify:webapi` and is finished like a transfer of a
resolved context, with `finish_transfer` and the autoplay check.

The context of tracks is built by `update_context_from_tracks` on
`ConnectState`, so the autoplay tests run on a real `ConnectState` and
catch a placeholder that would be resolved. The tests are named for
what they check, and the absent uri is covered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
`finish_transfer` replaces a current track it can't find among the
tracks, such as an autoplay or queued track. Finishing a transfer that
brought its tracks before loading therefore played another track than
the transferred one. Load first, as the resolved path does, then finish,
queue autoplay and emit `set_queue` as after a resolve. A test drives a
`SpircTask` with a real player through such a transfer.

Ignore context updates while the current context is a placeholder: its
tracks came with the play or transfer, and a resolve would replace them.
Count the `spotify:web-api` and `spotify:unknown` of older librespot
versions as placeholders too, so transfers from them play their tracks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
`finish_transfer` sets up the state from the context, which made the
context's track at the index current: with shuffle on even while a
queued track played, and the first track when the transferred one
wasn't among them, as an autoplay track isn't. The player played the
transferred track all the same, so the published state showed another.
Put it back after setting up, also when that fails. With that, a
transfer that brought its tracks is finished before loading again.

A transferred track marked `autoplay.is_autoplay` now gets the autoplay
provider, as librespot's own autoplay tracks have. `handle_transfer`
checks the provider and `finish_transfer` the metadata, so a transfer
during autoplay requested no autoplay context, then failed to finish
for lack of it and dropped its queue. Keep such a transfer until
autoplay resolves; without a context uri, from the current track.

Clear the context resolver when installing the tracks of a play or
transfer, as loading a new context uri does, so that a resolve still
pending for the previous context can't replace them.

Tests drive a `SpircTask` through each case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
stefanbd and others added 3 commits November 14, 2026 16:21
Finishing a transfer made a track of the context stand for a transferred
track it couldn't find, such as an autoplay track: the first one, or,
with a queued track playing before the first one, that one too. The
context then continued after it, so that track was passed over, and a
shuffle left it out. Now no track of the context stands in: the context
follows the transferred track from its first track, and the transferred
track has no index in it, as a queued one has none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
A transfer brings its queue, but tracks queued before it stayed, so a
transfer arriving while librespot still had a queue queued them twice.
Drop them when the transfer starts.

Finishing a transfer put the transferred track back only when its uri
differed from the context's track set current, so a queued track that is
in the context too became the context's occurrence. Compare the provider
as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
Finishing a transfer during autoplay with shuffle on shuffled the default
context, which also made it the active one again, so the transfer went
back to its playlist instead of continuing with autoplay. Shuffle only
the default context.

A shuffle saved in the transferred context was restored from its start,
so the transferred track and those before it were queued again. Resume
the restored order after the transferred track.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp13aMbmU4XmKnuCPVRb
@stefanbd stefanbd changed the title connect: play the tracks of a Web API play of uris connect: play Web API plays of uris, and finish transfers correctly Nov 14, 2026
@stefanbd
stefanbd requested a lite review from Copilot November 14, 2026 23:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The transfer state-machine changes affect multiple interacting paths, and because live real-device transfer behavior was not validated, human verification is needed before approval.

No open issues 🎉

Review effort: Lite

@PikaCube

Copy link
Copy Markdown

Another confirmation that this fixes it.

Setup: librespot built from e8bac08 on Debian (x86_64), pipe backend, driven entirely through the Web API (PUT /me/player/play?device_id=… with uris).

Before (built from 4a00590 on dev), every play failed the same way: the device logged couldn't load context info because: context is not available, then Client specified an invalid argument { Response status code: 400 Bad Request } and failed to handle request: Invalid state { context is not available. type: Default }, and the Web API answered 403 Player command failed: Restriction violated (reason UNKNOWN). This happened for every track, including ones that had played fine before.

After rebuilding from this branch and restarting the device, the same requests play normally and the previously failing tracks play through to the end.

Thanks for the fix!

By 2026-11-17 Spotify sends a Web API play of track uris under
spotify:webapi:<id> (seen: spotify:webapi:2aXd9suNTEFnjREEhPhIjP) as well
as the bare spotify:webapi. Resolving it fails with a 400 just as the
bare one did, so the play failed and the remote reported "Restriction
violated". Any spotify:webapi: uri now counts as no context uri, so the
tracks that come with it play.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013zyp14ttrF9CMB9edxaNkR
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.

4 participants