Repository navigation
perf(jellycompat): resolve the viewer access scope once per request - #2959
Conversation
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
Warning Review limit reachedYou'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 2 minutes. View limit details
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. Comment |
|
The request-scoped scope cache can change which nested items or playback availability a Jellyfin response includes if library access or policy changes while the request is in progress. The Evidence section says there is no user-visible change and provides no evidence for this case. Please add before-and-after API response excerpts for the same compat request with an access-policy change between scope lookups, naming the surface and commit. Suggested fix: No diff — add the matching before-and-after response excerpts under Evidence. Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness. Posted via Macroscope — Visible change evidence |
|
That case is the one under Risks, and it isn't a change a client can see in normal use. Before this PR, a policy edit that landed between two scope lookups in the same request could build one response from two different scopes, say the episode under the old one and its season under the new one. Now the whole response uses the scope from the first lookup, and the next request picks up the edit, as it did before. When nothing is edited mid-request, which covers every request the new pin and the existing jellycompat tests drive, the response is the same as on |
Review summary (head 60b755b)No changes needed. I found no defect in this PR and pushed nothing. What I checked. The cache lives in a holder that Tests. Related jellycompat PRs. I merged main with this PR, #2960, #2949 and #2956. The only conflict is with #2960, in Minor notes, no action needed.
Second opinion. A read-only Codex review (gpt-6.1-sol) of the same diff found no authorization problems. It confirmed that PIN login, admin keys, Validated features. None reached. The only passed cases on the board are for person browsing (#1143), which uses the native API. #1246 (Jellyfin compatibility) has no recorded passes yet. A comment there reports C2 passing on base; its forbidden-item checks run through the access filter this PR caches, and the restriction logic is unchanged. User manual: unaffected (no setting, default or visible behavior changes). Apple/Android: no client change; responses are unchanged. Fit: 9/10 · Readiness: 9/10 — ready Fit: it removes repeated database work for Jellyfin clients and resolves the scope once per request, as the native API already does in Reviewed with Claude Code (claude-opus-5-5), with a cross-provider check by Codex (gpt-6.1-sol). |
Every catalog read in a Jellyfin request resolved the viewer's access scope again: an episode's detail resolved it three times (episode, series artwork, season) and an episode list with media sources five times, each with its own Postgres reads and policy evaluation. The compat session middleware now gives each request a place to keep the resolved scope. The access filter and the playback bitrate cap resolve the session viewer's scope once per request and hand every caller its own copy of the library lists. Other inputs resolve as before, failures are not kept, and the next request resolves again, so revocations and policy edits still apply from the next request on. A client disconnect during resolution now logs at debug instead of warn.
60b755b to
5bb6c3a
Compare
|
Rebased onto main and kept both |
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
Minor change — #1246 C1 and C2 now have recorded runs on the changed Jellyfin path. The October 23 task update records them on a local integration build based on
Suggested fix: No diff — update the Automated check: Macroscope check run agent (gpt-6-luna). No validation was performed. Posted via Macroscope — v1 validation impact |
Problem
Related issue: N/A
Validation tasks: none. No passed case reaches this path; #1246 C1 and C2 (Jellyfin compatibility, not run yet) will exercise it.
Requests from Jellyfin apps (Infuse, Swiftfin, Kodi, Jellyfin Web) do more database work than they need to. Each catalog read in a jellycompat handler resolves the viewer's access scope again, at four Postgres statements and a policy evaluation each for a default profile. Opening an episode resolves it three times (the episode, its series' artwork, its season), and an episode list with
Fields=MediaSources,MediaSourceCountfive times. This change resolves it once per request.For context, on a production deployment the jellycompat
/Users/{id}/Itemsroutes had a p95 of about 1.4 s over 72 hours, and the "access scope resolution failed" warning fired 225 times in the same window. That warning also fires at WARN when the client has already hung up (context canceled).Approach
The compat session middleware (
serveWithSessionContext) now gives each request a holder for the resolved scope. The resolver behind the access filter (NewScopeAccessFilter) and the playback bitrate cap (PlaybackScopeResolver) both use it. A call whose resolve input is the session viewer's own (UserID,ProfileID,SkipPINVerification) resolves once and gets the scope from the holder after that. Anything else resolves exactly as before: another profile on the same account, a different input such asContentAccessOnly, or code running outside an authenticated compat request.AccessFilterFn, becausewithDefaultswrapsAccessFilterFnon each of its two passes in production. The resolver wrapper is idempotent and leaves an unsetPlaybackScopeResolvernil.ctxerr.LogLevel, so a client that disconnected mid-resolution logs at debug instead of warn.Only
internal/jellycompatchanges. Responses are unchanged, so the clients and the manual need nothing, and the native API already resolves the scope once per request inRequireViewerAccess.Validation
go test ./internal/jellycompat/, no database): pass. New ones cover one resolution per request through the real middleware, a second request resolving again, edits to a returned filter not reaching later calls, another profile or another input resolving on its own, a failure not being kept, and nil and empty allowlists keeping their meaning. A reflection check fails if a top-level slice, map or pointer field is added toaccess.Scopewithout being copied.TestCompatEpisodeItemResolvesScopeOnceDB(added toscripts/ci/db-pins.txt): drives the two requests above through the compat router, the catalog repositories and the production policy viewer resolver. It fails onmain(3 resolutions for the episode) and passes here. It also checks the episode still comes back with its series and season.gofmtandgo veton the package: clean. I did not runmake lint-changedlocally; CI lints the changed lines.Benchmarks
main> this PRGET /Users/{id}/Items/{episodeId}GET /Users/{id}/Items?ParentId={seriesId}&IncludeItemTypes=Episode&Recursive=true&Fields=MediaSources,MediaSourceCountHow these were measured
mainat 8c90021. Changed: 60b755b. The baseline runs used this branch's test file withmain's sources.policy.ViewerResolverand catalog repositories behind the compat router. The statement counts come from a pgx tracer on the pool and were identical in every run.SILO_TEST_DATABASE_URL=… go test -run '^$' -bench CompatScopeOnce -benchtime=2s -count=2 ./internal/jellycompat/, three rounds per side alternatingmainand this branch (6 runs each), compared with benchstat. Both differences are p=0.002, n=6.GOMAXPROCS=6, no other load.Limitations: one client on a loopback database with no other load, so the times leave out network round trips and pool waits, which is where fewer statements help most on a busy server. I did not measure a viewer in an access group, whose resolution also reads the group's policy, or PlaybackInfo, which now shares one resolution between the item detail and the bitrate cap.
Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/pr-2959/
Surface and build: the Jellyfin compat API, the two requests in Benchmarks, served by the compat router over a local test database. Before:
mainat 8c90021. After: 60b755b. The response bodies are byte for byte the same on both builds; the page has both, with the statement and scope-resolution counts for each. Nothing a client shows changes.Risks
main. No call site reads the scope concurrently.resolveAccessFiltercall inhandlers_collections.go; it picks this up without changes.Checklist
AI Disclosure
main, read-only. It checked behaviour parity at every caller of the access filter and the bitrate cap, profile versus account and access-group scoping, nil versus empty allowlists, error handling, concurrency, multi-replica state, and the tests. It also ran mutation tests: breaking the copies, the per-request lifetime, the input check, or the no-failure rule each made a test fail. It found no correctness or security problems. Its low-severity findings were a lock held across the inner resolution (a deadlock if a wrapped resolver were ever wrapped again), the bitrate cap building its own resolve input, and some test and comment wording. All of those are fixed.