[PR #2400] [MERGED] Serialize Spotify token refresh per agent to prevent overlapping grants #5582

Closed
opened 2026-10-22 04:17:34 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2400
Author: @Romuromylus
Created: 6/6/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/spotify-token-refresh-lock


📝 Commits (2)

  • 9bb978c Serialize Spotify token refresh per agent to prevent overlapping grants
  • db23028 Serialize the explicit Spotify refresh route on the per-agent lock

📊 Changes

1 file changed (+74 additions, -18 deletions)

View changed files

📝 src-tauri/src/commands/storage/integrations/spotify.rs (+74 -18)

📄 Description

Linked issue

Closes #2318

Why this change

  • resolve_credentials did an unsynchronized proactive token refresh. Two concurrent same-agent requests (the Spotify mini-player's player + devices queries, both on a 5s refetch) could each POST grant_type=refresh_token. Spotify rotates the refresh token, so the second grant used an already-invalidated token, and last-writer-wins save_spotify_tokens persisted the bad token — permanently breaking the connection until the user re-authorized.

What changed

  • src-tauri/src/commands/storage/integrations/spotify.rs: refresh is now serialized per agent via a process-global OnceLock<Mutex<HashMap<agent_id, Arc<tokio::sync::Mutex<()>>>>> (mirrors the existing per-agent track-index cache). After acquiring the per-agent async lock, resolve_credentials re-reads the stored token (double-checked locking) and skips the network grant entirely if another task already refreshed while it waited; otherwise it runs the original refresh path unchanged.

Refactor impact

Primary owner: Rust storage (integrations)

Impact areas reviewed:

  • src-tauri/src/commands/storage/integrations/spotify.rs (resolve_credentials, new spotify_refresh_lock)

Boundary notes:

  • None. File-private static + helper, no new public surface; SpotifyCredentials shape unchanged. Poisoned-mutex recovered via PoisonError::into_inner (matches the track-index cache). tokio sync feature already enabled.

Pressure points touched:

  • None.

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • cargo test --manifest-path src-tauri/Cargo.toml -p marinara-engine spotify — 10 passed (existing + local-only guard tests: per-agent lock returns the same handle and serializes same-agent / stays parallel cross-agent).
  • cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings — clean.

Feature Discoverability

  • Updated src/features/shell/discovery/
  • N/A because this PR is a concurrency/data-loss bugfix in an existing integration.

Reason:

  • No new discoverable surface.

Docs and release impact

  • No docs changes needed

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2400 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/6/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/spotify-token-refresh-lock` --- ### 📝 Commits (2) - [`9bb978c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9bb978cbc20bbf0f50703fbf91c33d2c420a00e9) Serialize Spotify token refresh per agent to prevent overlapping grants - [`db23028`](https://github.com/Pasta-Devs/Marinara-Engine/commit/db23028f3ade4c8de482bacf30265f7ab6bc5f43) Serialize the explicit Spotify refresh route on the per-agent lock ### 📊 Changes **1 file changed** (+74 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/src/commands/storage/integrations/spotify.rs` (+74 -18) </details> ### 📄 Description ## Linked issue Closes #2318 ## Why this change - `resolve_credentials` did an unsynchronized proactive token refresh. Two concurrent same-agent requests (the Spotify mini-player's player + devices queries, both on a 5s refetch) could each POST `grant_type=refresh_token`. Spotify rotates the refresh token, so the second grant used an already-invalidated token, and last-writer-wins `save_spotify_tokens` persisted the bad token — permanently breaking the connection until the user re-authorized. ## What changed - `src-tauri/src/commands/storage/integrations/spotify.rs`: refresh is now serialized per agent via a process-global `OnceLock<Mutex<HashMap<agent_id, Arc<tokio::sync::Mutex<()>>>>>` (mirrors the existing per-agent track-index cache). After acquiring the per-agent async lock, `resolve_credentials` re-reads the stored token (double-checked locking) and skips the network grant entirely if another task already refreshed while it waited; otherwise it runs the original refresh path unchanged. ## Refactor impact Primary owner: Rust storage (integrations) Impact areas reviewed: - `src-tauri/src/commands/storage/integrations/spotify.rs` (`resolve_credentials`, new `spotify_refresh_lock`) Boundary notes: - None. File-private static + helper, no new public surface; `SpotifyCredentials` shape unchanged. Poisoned-mutex recovered via `PoisonError::into_inner` (matches the track-index cache). `tokio` sync feature already enabled. Pressure points touched: - None. ## Validation - [x] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `cargo test --manifest-path src-tauri/Cargo.toml -p marinara-engine spotify` — 10 passed (existing + local-only guard tests: per-agent lock returns the same handle and serializes same-agent / stays parallel cross-agent). - `cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings` — clean. ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` - [x] N/A because this PR is a concurrency/data-loss bugfix in an existing integration. Reason: - No new discoverable surface. ## Docs and release impact - [x] No docs changes needed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:34 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5582
No description provided.