Repository navigation
fix(jellycompat): close a transcode rebuilt for a playlist request after its play's session ended - #4128
Conversation
…ter its play's session ended A Jellyfin playlist request that rebuilt a lost local transcode returned it without checking that the play's native session still existed. A stop, an expiry or an administrator's end that ran while the rebuild was starting found no transcode to close, and with the session gone nothing else closed the one the rebuild then registered, so FFmpeg kept encoding. The rebuild now looks the native session up after it registers the transcode. When the session is gone it closes the transcode and the request answers 404, as the segment route does. The playlist caller skips its teardown for this case: the play has either ended already or can still resume, and the teardown would delete its row. Closes #4120 Co-Authored-By: Claude Fable 5.2 <[email protected]>
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:
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7509cbd63a
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… under one lock The rebuild looked the native session up and then closed its transcode in two steps. A second playlist request that registered the session again and adopted the rebuilt transcode between them had it closed under a resumed play. The transcode manager now reads the session under its transcode map lock and removes the transcode in the same critical section. A request adopts a registered transcode under that lock after it has registered the session, so it either registered the session before the read, which then keeps the transcode, or reads the map after the removal and starts its own. Co-Authored-By: Claude Fable 5.2 <[email protected]>
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:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fc45b5cd51
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Closing the rejected transcode removes its output directory after FFmpeg exits. A retry that registered a new transcode in the same directory while that close was still waiting had its playlists and segments deleted. The close now runs under the session lifecycle lock, which a start or rebuild holds before it writes into the directory, as the other rollbacks on this path already do. Co-Authored-By: Claude Fable 5.2 <[email protected]>
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:
|
…as it started (#4249) A Jellyfin play with no stored recipe starts its first local transcode in ensureTranscodeSessionWithToneMapMode. When its native session went away while the start was running (a stop, an expiry or an administrator's end, any of which found no transcode to close), the start still published the transcode. persistTranscodeRecipe only logged the missing session, and FFmpeg kept encoding with nothing left to expire it. #4128 closed this for a rebuilt transcode. The start now checks the session under the lifecycle lock it already holds to publish, before writing the recipe, and closes its transcode when the session is gone. It returns errCompatUpstreamSessionGone, which the playlist route answers with 404 and no teardown of the play, as for a rebuild. The transcode manager's check gets a Locked form for a caller that holds that lock. Closes #4130 Co-authored-by: Claude Opus 5.5 <[email protected]>
Problem
Closes #4120
Related issue: N/A
Validation tasks: none
Other clients: N/A (no client-visible change)
When a Jellyfin play's session on the server ends while a playlist request is rebuilding that play's transcode, the rebuild registers FFmpeg for a session that no longer exists, and nothing closes it. FFmpeg keeps encoding with nobody watching until it fails, the client repeats its stop, or the server shuts down. With this change the rebuild looks for the session once its transcode is registered and closes the transcode when the session is gone.
A local transcode is closed through its native session: an expiry, a client stop and an administrator's end each remove the session and close the transcode. One that runs while a rebuild is starting finds no transcode to close. Two orderings reach this on
main: a stop that finishes after the request has read the play's row and before the rebuild starts, and an expiry or an administrator's end while the rebuild is in flight. A rebuild for a segment request already confirms the session afterwards and closes its transcode when the session is gone.Approach
After the rebuild returns, the playlist path looks the native session up. If it is gone, it closes the transcode it just registered and the request answers 404 "Upstream session not found", which is what the segment route answers in the same case.
The transcode is registered before the lookup, so an end that removes the session after the lookup finds the transcode and closes it itself.
The lookup and the removal happen under the transcode manager's map lock, in a new
CloseTranscodeSessionIfSessionGone. A second request that registers the session again adopts the rebuilt transcode under the same lock. So it either registered the session before the lookup, which then keeps the transcode, or it reads the map after the removal and starts its own.Closing a transcode removes its output directory once FFmpeg has exited. The close runs under the session lifecycle lock, which a start or rebuild holds before it writes into that directory, so a retry cannot start there until the directory is gone. The other rollbacks on this path already close under that lock.
The playlist handlers normally tear the play down when a transcode fails to start. They skip that here. The play has either ended already, or its session expired and it can still resume, and the teardown would delete its row. After an expiry the next playlist request rebuilds the session and the transcode.
Only the Jellyfin surface changes. The native API already confirms the session after a rebuild.
Not covered here, and present on
main:The last two were confirmed by a run on
main.Validation
main, which never closes the transcode, so it guards the fix and does not prove the bug. It passed 20 runs under the race detector.internal/playback, checks from inside the close that a start for the same session cannot take the lifecycle lock. It fails on this pull request's second commit, where the close ran outside the lock, and passes on the head.internal/jellycompattests and the tests of every package that imports it pass, andmake lint-changedreports 0 issues.go mod tidy -diff, router lint, full test suite) apart from two tests that run the host's FFmpeg 6.1.1,TestCopyRemuxSubtitleRenditionRealFFmpegininternal/playbackandTestRunStatsWithRealFFmpegininternal/mediasample. Neither reaches the changed code: the first runs a real remux, and the second's package is not touched. The full suite ran on the first commit; after the later commits, the tests of the two changed packages, ofinternal/transcodenode, and of the packages that importinternal/jellycompatwere rerun. The web gate was not run; no web file changes.Evidence
Evidence: https://evidence.siloserver.org/r/silo-server/issue-4120/
Risks
A playlist request that loses this race now gets a 404 where it used to get a playlist. After a stop or an administrator's end the play is over. After an expiry the play resumes on the next request, but a client that treats a failed playlist request as fatal stops there. A reviewer's reading of Jellyfin Web's source says its HLS error handler does; that was not tested here. The expiry has to land while the request is rebuilding, and by default a session expires after 45 seconds without activity and no sooner than 90 seconds after it was registered.
The session lookup now runs while the transcode map lock is held. The lookup takes the session manager's read lock. Nothing that holds the session manager's lock calls into the transcode manager, so the order cannot deadlock. A lookup that waits behind a session sweep holds up other readers of the transcode map for that long, only on a rebuild that finds its session gone.
That rebuild also waits for the session lifecycle lock before it closes. A segment or seek restart holds that lock through its restart hook, so a hook that stalls holds the playlist request up with it. #4122 tracks the stall.
The segment route's confirm and close on
mainare still two steps, with the same gap against a concurrent playlist request. That was traced, not run, and is not changed here.Checklist
AI Disclosure
go test)🤖 Generated with Claude Code
Note
Close rebuilt Jellyfin compat transcodes whose playback session has ended
errCompatUpstreamSessionGoneand a check inPlaybackHandler.ensureTranscodeSessionWithToneMapMode: after reconstructing a stored recipe, it verifies the upstream session still exists via the newTranscodeManager.CloseTranscodeSessionIfSessionGonebefore recording the transcode (streams.go)TranscodeManager.CloseTranscodeSessionIfSessionGoneremoves and closes the transcode only when the session is gone, holding the lifecycle lock so a concurrent start cannot write to the output directory during the close (transcode_manager.go)Macroscope summarized 20e3cfd.