Skip to content

fix(jellycompat): serve scrobble lock waiters in arrival order - #1886

Closed
Quick104 wants to merge 2 commits into
mainfrom
fix/jellycompat-stopped-report-order
Closed

Quick104 wants to merge 2 commits into
mainfrom
fix/jellycompat-stopped-report-order

Conversation

@Quick104

@Quick104 Quick104 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: none

A Jellyfin client can send a progress report and then a Stopped report without PositionTicks. If both reports wait on the play session's scrobble lock, the stop can reach watch providers with the position from before the progress report. This change gives the lock to waiting reports in the order they reached it.

sendCompatResumeStart holds the per-session lock while it calls the watch provider, so reports queue behind it. The lock was a sync.Mutex. If a Stopped report reaches the lock just as the start releases it, the mutex can let the stop take the lock ahead of a progress report that is already waiting. The stop is then staged at the earlier position, and the progress report finds the play ended and sends nothing. The window is narrow: the Stopped report has to arrive within microseconds of the release, or the two requests have to arrive almost together.

TestHandlePlaybackPositionlessStoppedUsesQueuedReportPosition failed in 33 to 148 of 300 runs on main, depending on machine load. Most of those failures came from the test, not from this race. The old lock counted a caller as queued before the caller waited on the mutex, so the test's "queued" signal fired too early and the test could not reliably set up the queue order. With a 5 ms pause after each signal, the old lock passed 300 of 300. The new lock fixes both problems because a caller counts as queued exactly when it joins the queue.

Approach

The lock now keeps a queue of waiting callers for each session key. Unlock gives ownership to the oldest waiter. The map entry is removed when nobody holds or waits for the lock. Callers keep the same lock(key) (unlock func()) interface, so the start, report, and stop paths do not change.

Releasing the same lock twice now panics, as sync.Mutex did. Without that check, a second release could delete an entry that a newer caller owned and let a third caller in beside it.

New tests:

  • A lock-level test queues eight callers one after another and checks that they get the lock in that order. Against the old mutex it failed 14 to 15 of 500 runs.
  • A second test checks that a double release panics and leaves the new owner's entry in place.

Validation

  • Target test, -count=500: 500 passed, 0 failed.
  • Lock-order and double-release tests, -count=500: 500 passed each.
  • The same three tests with -race -count=100: pass.
  • Package go test -race ./internal/jellycompat/: pass.
  • go vet and gofmt: clean.
  • make lint-changed BASE_REF=origin/main: 0 issues.

Risks

The lock still covers one session on one node. Callers still cannot cancel a wait once queued, as with the mutex. Ordering follows when callers reach the lock, not when their HTTP requests arrived, so two reports that race in the network can still be served in either order.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.

AI Disclosure

  • Harness: Claude Code (in T3 Code)
  • Tool(s): Claude Code subagents (implementer, independent reviewer)
  • Model(s): claude-opus-5-5[1m]
  • Involvement: Fully AI-generated, human review pending
  • Adversarial review: The first reviewer subagent did not deliver a final report. Its interim finding was that the new lock-order test sometimes checked the lock before the last waiter released it, and the test now waits for every release. A second, independent reviewer confirmed mutual exclusion, FIFO order, no lost wakeups, safe removal of map entries, and that every caller unlocks exactly once on each path. It ran the target test 500 times and -race on the package with no failures. It found that the CI flake came mostly from the test's early "queued" signal and that the production race is real but narrow. It also found that a double release could break mutual exclusion silently; this pull request now panics in that case.

🤖 Generated with Claude Code

Note

Fix jellycompat scrobble lock to serve waiters in arrival order

  • Replaces the per-key mutex and holder counter in compatScrobbleLocks with a FIFO queue of waiter channels, so queued callers acquire the lock in arrival order instead of being overtaken by newly arriving callers
  • Release removes the key entry when no waiters remain, or closes the oldest waiter's channel to hand over ownership
  • Invoking the same release closure twice now panics before changing lock ownership
  • Adds tests for arrival-order acquisition, entry cleanup after final release, and double-release panics in playback_scrobble_test.go
  • Risk: contended scrobble lock behavior changes from non-FIFO to FIFO in playback_scrobble.go; double release now panics instead of being silently ignored

Macroscope summarized fd791e0.

Quick104 and others added 2 commits October 4, 2026 02:48
sendCompatResumeStart holds the per-session scrobble lock across the
watch-provider call, so client reports queue behind it. The lock was a
sync.Mutex, which lets a position-less Stopped report that reaches the
lock just as the start releases it take the lock ahead of a progress
report already waiting. The stop is then staged from the earlier
position and providers get the wrong resume point.

TestHandlePlaybackPositionlessStoppedUsesQueuedReportPosition failed
intermittently (11-49% of runs locally) mostly for a test-side reason:
the old lock counted a caller as queued before it waited on the mutex,
so the test could not reliably set up the queue order. Handing the lock
to waiters in the order they reached it fixes both, because a caller now
counts as queued exactly when it joins the queue.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The FIFO lock's unlock closure was not idempotent. A second call with no
waiters deleted the map entry even if a newer caller had since taken the
lock, so a third caller could get in beside that owner. sync.Mutex
panicked in this case; the closure now does the same. No current caller
releases twice.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 21 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2c7c695c-ea16-4fd1-a4fb-03ab440feb26
📥 Commits

Reviewing files that changed from the base of the PR and between 3214602 and fd791e0.

📒 Files selected for processing (2)
  • internal/jellycompat/playback_scrobble.go
  • internal/jellycompat/playback_scrobble_test.go
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@silo-kody

silo-kody Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-04T06:50:52.865122Z fd791e0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Quick104 added a commit that referenced this pull request Oct 4, 2026
fix(jellycompat): serve scrobble lock waiters in arrival order (revamp copy of #1886)

Quick104 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Silo's pull request guidance changed today (#2016). A PR that changes what a user sees now needs before-and-after evidence in its Evidence section, and a PR with no visible change says Evidence: none, no user-visible change. This PR was opened before the rule existed, so it hasn't had a chance to meet it yet.

Missing here: Needs the line 'Evidence: none, no user-visible change'.

The rules are in Show visible changes, and the PR template shows the Evidence section layout. I've converted this PR to draft for now. Mark it ready for review once the Evidence section is filled in.


Generated by Claude Code

@Quick104
Quick104 marked this pull request as draft October 6, 2026 19:20
@Quick104

Copy link
Copy Markdown
Contributor Author

Closing: this change is already on main. It landed through #1887, the copy of this PR made for the collections revamp branch (commit 0a8efd9), so merging this branch would add nothing.

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