Skip to content

fix(jellycompat): close a transcode rebuilt for a playlist request after its play's session ended - #4128

Merged
Quick104 merged 3 commits into
mainfrom
fix/jellyfin-rebuilt-transcode-ended-play
Nov 13, 2026
Merged

Quick104 merged 3 commits into
mainfrom
fix/jellyfin-rebuilt-transcode-ended-play

Conversation

@Quick104

@Quick104 Quick104 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

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

  • A new test sends a playlist request for a play with a stored recipe and no running transcode, and removes the native session after the request has found it and before the rebuild starts FFmpeg. On the merge base the request answers 200 and the rebuilt transcode stays registered. With the fix it answers 404 and no transcode is left, the play is still routable, and a second request resumes it. The test passed 20 runs under the race detector.
  • With the fix but without the skipped teardown, the same test fails because the play is no longer routable.
  • A second test sends another playlist request at the moment the rebuild first finds the session gone. That request registers the session again and adopts the rebuilt transcode. With the lookup and the close as two steps (this pull request's first commit) the rebuild then closed the transcode and the test fails; with both under one lock it passes. It also passes on 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.
  • A third test, in 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.
  • The internal/jellycompat tests and the tests of every package that imports it pass, and make lint-changed reports 0 issues.
  • The rest of the Go gate passes (build, vet, gofmt, go mod tidy -diff, router lint, full test suite) apart from two tests that run the host's FFmpeg 6.1.1, TestCopyRemuxSubtitleRenditionRealFFmpeg in internal/playback and TestRunStatsWithRealFFmpeg in internal/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, of internal/transcodenode, and of the packages that import internal/jellycompat were rerun. The web gate was not run; no web file changes.
  • Not reproduced on a deployment. The session has to end inside the window in which one request is rebuilding. The proof is the source path and the test's failing run on the base.

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 main are still two steps, with the same gap against a concurrent playlist request. That was traced, not run, and is not changed here.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • Each test this pull request adds meets "Decide what to test" in CONTRIBUTING.md.
  • The Evidence section shows every change a user can see, or how I checked a change nobody sees.

AI Disclosure

  • Harness: Claude Code (T3 Code)
  • Tool(s): Claude Code review subagents, T3 Code delegated review, git, Go (go test)
  • Model(s): claude-fable-5-2 (wrote the change), claude-opus-5-5, claude-sonnet-5-5, gpt-6.1-sol
  • Involvement: Fully AI-generated (not yet human verified)
  • Adversarial review: Before the change was written, gpt-6.1-sol attacked the plan and claude-opus-5-5 tried to cut it down. Opus cut the test from two cases to one, which was adopted, and cut the 404 answer in favor of the route's default 500, which the code review later reversed. Sol raised the gap between the lookup and the close, which the second commit closes, and the answer a viewer gets after an expiry (under Risks), the crash handler ordering ([bug] A Jellyfin transcode rebuilt while a crashed FFmpeg is being cleaned up is left running without a session #4129), and missing test assertions, which were added. A code review at high effort (claude-fable-5-2 and claude-sonnet-5-5 finders, a validation check, one verifier) found that the new error answered 500, now 404, and an unreachable nil check, now removed; its verifier forced the gap between the lookup and the close into a torn-down play. Codex and Kody then raised the same gap on this pull request, and the second commit closes it. A simplify pass corrected one comment. A background adversarial review by gpt-6.1-sol of 7509cbd found that the close could delete the output of a retry that started while it was still waiting for FFmpeg to exit; the third commit closes under the session lifecycle lock.

🤖 Generated with Claude Code

Note

Close rebuilt Jellyfin compat transcodes whose playback session has ended

  • Adds errCompatUpstreamSessionGone and a check in PlaybackHandler.ensureTranscodeSessionWithToneMapMode: after reconstructing a stored recipe, it verifies the upstream session still exists via the new TranscodeManager.CloseTranscodeSessionIfSessionGone before recording the transcode (streams.go)
  • TranscodeManager.CloseTranscodeSessionIfSessionGone removes 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)
  • The manifest handler no longer tears down the stored play session on this error, so a later playlist request can resume the play
  • Behavioral Change: playlist requests whose session ended now return HTTP 404 with an upstream-session-not-found code instead of the prior response

Macroscope summarized 20e3cfd.

…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]>
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Playlist transcode rebuilds now verify that the upstream session still exists. If it has ended, the rebuilt transcode is closed, the request returns 404, and the play remains stored. A lifecycle-locked manager operation prevents a resumed session from having its transcode removed.

Changes: Playlist rebuild cleanup

Layer / File(s) Summary
Fence transcode close against session changes
internal/playback/transcode_manager.go, internal/playback/transcode_manager_test.go
Adds a lifecycle-locked operation that closes the expected transcode only when the playback session remains absent. A regression test checks that the lifecycle lock remains unavailable during close and that the transcode is removed.
Check session state during playlist rebuild
internal/jellycompat/streams.go, internal/jellycompat/playback_finish_test.go
Checks for an upstream session after reconstructing a transcode. If the session is gone, the request returns 404 without tearing down the stored play. Tests cover a retry that restores the session and a concurrent request that adopts the transcode.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: rhainland, neurekadev

Merge Risk: Minimal · up to 20e3c

This change closes an orphaned transcode and returns a 404 when a Jellyfin playlist rebuild finds its session gone, while keeping the play resumable. No concrete merge-blocking risk was identified.

Security architecture risk: Minimal · up to 20e3c

The change improves cleanup after playback ends while preserving resumed playback. The inspected changes do not establish a new or worsened access-control, resource-ownership, or failure-containment risk.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • observed — The new destructive operation is scoped to one upstream session ID and the exact expected runtime pointer. Reconstruction and its locks remain process-local; the existing documented requirement for single-front-end deployment or session affinity is not replaced by distributed ownership enforcement.

Trust Boundaries and Controls

  • observed — The inspected playlist path requires an authenticated context and a stored play whose compatibility token matches the requester. Resumption restores the native session before runtime lookup; reconstruction checks recipe/session identity and caller ownership. The new cleanup does not grant authority to select another user’s runtime or replace these controls.

Resilience and Maintainability Implications

  • observed — Cleanup rechecks session presence after acquiring the lifecycle and transcode-map locks, removes the map entry only on pointer identity, and retains the lifecycle lock through Close. This coordinates cleanup with adoption and reconstruction while preventing removal of a replacement entry. The added tests assert retry recovery, preservation of an adopted runtime, and exclusion of a new lifecycle operation during close; they were not executed in this review.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the Jellyfin playlist-request rebuild path and the close of a transcode whose play session ended. The changeset adds a session re-check that closes a rebuilt transcode when the session…
Linked Issues check Passed Issue #4120 requires that a Jellyfin playlist-request rebuild confirm the native session is still live and close the rebuilt transcode when it is gone, as the segment path does. The change meets this.…
Out of Scope Changes check Passed All changes connect to #4120. The changes are: the session check and close in the playlist rebuild path (streams.go), the 404 mapping and skipped teardown for the new sentinel error (streams.go), the …
Description check Passed The description is specific and related to the changeset. It states the problem (a Jellyfin play's session ending during a playlist rebuild leaves FFmpeg running), the approach (look up the native ses…
Full details: Title check

Explanation

The title names the Jellyfin playlist-request rebuild path and the close of a transcode whose play session ended. The changeset adds a session re-check that closes a rebuilt transcode when the session is gone, and it returns 404. The title is specific and matches the main change. It is long, but it is still one clear sentence.

Full details: Linked Issues check

Explanation

Issue #4120 requires that a Jellyfin playlist-request rebuild confirm the native session is still live and close the rebuilt transcode when it is gone, as the segment path does. The change meets this. After ReconstructTranscodeWithError registers the transcode, ensureTranscodeSessionWithToneMapMode checks the session. If the session is gone, CloseTranscodeSessionIfSessionGone closes the transcode and the request returns 404 (errCompatUpstreamSessionGone, mapped in writeCompatTranscodeError). The transcode is registered before the lookup. So a stop, expiry, or administrator end that removes the session after the lookup finds the transcode and closes it. The check and close run under the transcode-map lock, so a concurrent request that restores the session adopts the transcode. The manifest handler skips the play teardown for this error, so the play stays routable and can resume. The new tests in playback_finish_test.go cover the closed-rebuild path and the concurrent-adoption path. The PR explicitly leaves #4112, #4129, and #4130 out of scope, consistent with the issue's own scope. Evidence is taken from the PR summary and the author's description. The source was not independently re-read for this assessment, so the lock details rely on that summary.

Full details: Out of Scope Changes check

Explanation

All changes connect to #4120. The changes are: the session check and close in the playlist rebuild path (streams.go), the 404 mapping and skipped teardown for the new sentinel error (streams.go), the new CloseTranscodeSessionIfSessionGone method (transcode_manager.go), and tests for the rebuild race and concurrent adoption. The objectives also mention closing a rejected rebuild under the session lifecycle lock so cleanup does not remove output files a retry uses. This is part of the same rebuild-close race and is covered by TestCloseTranscodeSessionIfSessionGoneFencesTheOutputDirectory. The PR states that the segment route and the native API are unchanged. No unrelated change was identified in the provided summary. Uncertainty: the summary is AI-generated and was not checked against every hunk.

Full details: Description check

Explanation

The description is specific and related to the changeset. It states the problem (a Jellyfin play's session ending during a playlist rebuild leaves FFmpeg running), the approach (look up the native session after the rebuild, close the transcode and return 404 when the session is gone, skip the play teardown, and run the check and close under the transcode-map lock and session lifecycle lock), the validation (new tests and race-detector runs), and the risks. These match the raw summary's changes to streams.go, transcode_manager.go, and the two test files. The description has some detail that goes beyond the summary, such as the stop-ordering gaps and linked issues, but that detail does not conflict with the changeset. The description is not vague or off-topic.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0a0b008d-4eba-4ec8-84ee-e294d5c7570d
Commits

Reviewing files that changed from the base of the PR and between b6b7dd3 and 20e3cfd.

Files selected for processing: 4

  • internal/jellycompat/playback_finish_test.go
  • internal/jellycompat/streams.go
  • internal/playback/transcode_manager.go
  • internal/playback/transcode_manager_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

@silo-triage silo-triage Bot added the size/M 50–199 added + deleted lines (all files); change volume, not priority. label Nov 13, 2026
@silo-kody

silo-kody Bot commented Nov 13, 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

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T22:05:46.734608Z 20e3cfd New commits
🔒 Security Review ✅ Completed 2026-11-13T22:07:56.595874Z 20e3cfd New commits
ℹ️ 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.

@silo-triage silo-triage Bot added bug Something isn't working go Pull requests that update go code impact: compat Breaks a supported client or server path priority: P2 Limited scope, workaround exists, or polish labels Nov 13, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread internal/jellycompat/streams.go Resolved
Comment thread internal/jellycompat/streams.go Resolved

@Quick104 Quick104 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Adversarial review (gpt-6.1-sol, reviewed 7509cbd)

This review is AI-generated by gpt-6.1-sol.

Comment thread internal/jellycompat/streams.go Resolved
… 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

silo-kody Bot commented Nov 13, 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread internal/playback/transcode_manager.go Resolved
Comment thread internal/playback/transcode_manager.go Resolved
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-triage silo-triage Bot added size/L 200–499 added + deleted lines (all files); change volume, not priority. and removed size/M 50–199 added + deleted lines (all files); change volume, not priority. labels Nov 13, 2026
@silo-kody

silo-kody Bot commented Nov 13, 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 ❌

@Quick104
Quick104 merged commit 567ef59 into main Nov 13, 2026
26 checks passed
@Quick104
Quick104 deleted the fix/jellyfin-rebuilt-transcode-ended-play branch November 13, 2026 22:51
Quick104 pushed a commit that referenced this pull request Nov 15, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code impact: compat Breaks a supported client or server path priority: P2 Limited scope, workaround exists, or polish size/L 200–499 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] A transcode rebuilt for a Jellyfin playlist request keeps running when the play ended just before or during the rebuild

1 participant