Skip to content

fix(jellycompat): close a transcode that was starting when a Jellyfin client stopped playback - #4119

Merged
Quick104 merged 3 commits into
mainfrom
fix/jellyfin-stop-during-transcode-rebuild
Nov 14, 2026
Merged

Quick104 merged 3 commits into
mainfrom
fix/jellyfin-stop-during-transcode-rebuild

Conversation

@Quick104

@Quick104 Quick104 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #4112
Related issue: N/A
Validation tasks: none

Other clients: N/A (no client-visible change)

When a Jellyfin client stops playback while the server is starting or rebuilding that play's transcode, the stop finds no transcode to close, and the start then registers one for a play that has ended. FFmpeg keeps encoding with nobody watching. With this change the stop waits for a start in flight and closes what it registered.

A start or rebuild holds the session lifecycle lock from before it starts FFmpeg until the transcode is registered. The Jellyfin stop closed the transcode without that lock. On main the transcode that stays is one rebuilt for a playlist request. Since #4128 that rebuild checks the native session after it registers, but the stop removes the session only after its close, so a rebuild that registers between the two still passes the check, and with the session gone no expiry reaches it. A rebuild for a segment request already closes itself when it finds the session gone, and a fresh start is closed when it fails to store its recipe, after its manifest wait.

Approach

cleanupPlaySession keeps its immediate close and the session finish. It then takes the lifecycle lock on every stop and closes the local transcode again, before the recipe delete it already did under that lock. The native stop already takes the lock before it closes.

The shipped server always has a recipe store, so a Jellyfin stop already waited on this lock. The new part is the close under it.

The first close stays outside the lock because a segment or seek restart holds the lock through its restart hook, and that hook can wait on the FFmpeg context that only a close cancels.

Not covered here, and present on main: a fresh start that takes the lock only after the stop has finished, which #4130 tracks. Its publish step closes it once its manifest wait ends. A playlist rebuild in that position closes itself since #4128.

Validation

  • A new test sends a Stopped report while a start holds the lifecycle lock and registers a transcode after the play has ended. It fails on the merge base with the transcode still registered and passes with the fix, including 20 runs under the race detector.
  • The internal/jellycompat tests pass under the race detector, and make lint-changed reports 0 issues.
  • The rest of the Go gate passes (build, vet, gofmt, size ratchet, full test suite) apart from two tests that run the host's FFmpeg, TestCopyRemuxSubtitleRenditionRealFFmpeg and TestRunStatsWithRealFFmpeg. They are in internal/playback and internal/mediasample, which this change does not touch or reach, and they fail on FFmpeg 6.1.1 without it. The web gate was not run; no web file changes.
  • After merging current main (with fix(playback): don't hold a playback stop behind a transcode restart's stalled settings read #4127 and fix(jellycompat): close a transcode rebuilt for a playlist request after its play's session ended #4128), the test still fails on main at 91611b4 and passes on the head under the race detector, as do the rest of the internal/jellycompat tests.
  • Not reproduced on a deployment. The stop has to land inside the few milliseconds in which one request is starting FFmpeg. The proof is the source path, a run of the unedited transcode manager showing which rebuilds keep the transcode, and the test's failing run on the base.

Evidence

Evidence: https://evidence.siloserver.org/r/silo-server/issue-4112/

Risks

A stop that overlaps a start returns once the start has registered, which for a rebuild that waits on its first manifest can take up to the 30-second startup wait per attempt. The shipped server already waited there.

If a restart's hook stalls while it holds the lock, for example on a hung settings read, the stop waits behind it, for at most 5 seconds since #4127. A rebuild's first throttler start also runs under the lock with no time limit; open PR #4121 would bound it.

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 found nothing to cut. Sol raised the stalled restart hook described under Risks; main has the same wait, so the plan stayed, and Sol's alternative, a stop fence in the transcode manager, is larger than this bug needs. A code review at high effort (claude-fable-5-2 and claude-sonnet-5-5 finders, one verifier) found no defect the diff introduces and confirmed the two gaps listed under Approach as present on main. A simplify pass kept the recipe delete's error handling as it was. A background adversarial review by gpt-6.1-sol of 73e2cb6 found no defect in this diff and posted nothing; it listed the same gaps as present on main, since fixed by fix(jellycompat): close a transcode rebuilt for a playlist request after its play's session ended #4128 and fix(playback): don't hold a playback stop behind a transcode restart's stalled settings read #4127. A maintainer review by claude-opus-5-5 merged current main, ran code-review finders and a gpt-6.1-sol round on the merged head, found no defect the diff introduces, and corrected one code comment.

🤖 Generated with Claude Code

Note

Close transcodes that start while a Jellyfin stop request is in progress

  • Fixes a race in PlaybackHandler.cleanupPlaySession: a client can start a new transcode while a stopped-playback request waits on the session lifecycle lock, and that transcode was left open
  • After finishing the play, the handler now acquires the session lifecycle lock and closes any transcode registered before the lock was taken. It also holds the lock while deleting the node recipe, when a recipe store is configured. The initial transcode close stays outside the lock
  • Adds a regression test in playback_finish_test.go that registers a transcode while the lock is held during a stop request and verifies the stop removes it

Macroscope summarized 045e598.

… client stopped playback

A start or rebuild holds the session lifecycle lock from before it starts
FFmpeg until it has registered the transcode. The Jellyfin stop closed the
transcode without that lock, so a stop that arrived in between closed
nothing, and the start then registered a transcode for a play that had
ended. A transcode rebuilt for a playlist request stayed running, with no
session left to expire it.

The stop still closes what is registered at once. After finishing the
session it now takes the lifecycle lock on every stop and closes the local
transcode again, before the recipe delete it already did under that lock.

Closes #4112

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@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-14T22:31:41.032688Z 02934e4 New commits
🔒 Security Review ✅ Completed 2026-11-14T22:34:06.838086Z 02934e4 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.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

cleanupPlaySession now orders transcode closure and recipe deletion with session lifecycle activity. A concurrency test verifies that a transcode registered during stop is closed before the stop returns.

Changes: Jellyfin playback-stop cleanup

Layer / File(s) Summary
Serialize stop cleanup with lifecycle work
internal/jellycompat/streams.go, internal/jellycompat/playback_finish_test.go
cleanupPlaySession closes the transcode after acquiring the lifecycle lock and attempts recipe deletion before releasing the lock. The test checks that a transcode registered during stop is closed.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: rhainland, blurbery

Merge Risk: Minimal · up to 73e2c

A Jellyfin playback stop that overlaps a transcode start or rebuild now closes the transcode once it registers. The stop can wait up to the startup timeout in that case, which the author discloses. The playlist-request and session-expiry cases are explicitly out of scope and tracked separately. No merge-blocking risk is identified.

Security architecture risk: Minimal · up to 73e2c

The change closes a playback-cleanup race without expanding who may stop playback or adding remote actions. No material new security or resource-ownership risk was established in the inspected transition.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • observed — The added close targets the local transcode-map entry for the resolved upstream session. Its empty node URL makes remote deletion return without sending a request. The initial close retains the existing remote-stop behavior, and the node-recipe deletion already existed at base.

Trust Boundaries and Controls

  • observed — Playback-stop reports require an authenticated session context and resolve finalizable plays or aliases using the caller's token. Reports without a per-play identifier do not invoke teardown. ActiveEncodings independently checks the stored owner token before cleanup. These admission controls are unchanged by the PR.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a Jellyfin stop now closes a transcode that was starting when playback stopped. The changeset adds a second close under the lifecycle lock in cleanupPlaySession and a …
Linked Issues check Passed Directly linked issue #4112 requires that a Jellyfin stop arriving during a transcode rebuild wins, so the rebuilt transcode is closed or never registered once the play ends. The diff to `internal/jel…
Out of Scope Changes check Passed The whole-PR diff against the merge base touches two files. internal/jellycompat/streams.go changes only cleanupPlaySession: it adds the lifecycle lock around a second transcode close and moves th…
Description check Passed The description is closely related to the changeset. It explains the race between a Jellyfin playback stop and a transcode start or rebuild, and it describes the fix. The fix closes the local transcod…
Full details: Title check

Explanation

The title names the main change: a Jellyfin stop now closes a transcode that was starting when playback stopped. The changeset adds a second close under the lifecycle lock in cleanupPlaySession and a test for that path. The title is specific and matches the objectives of issue #4112 and the diff.

Full details: Linked Issues check

Explanation

Directly linked issue #4112 requires that a Jellyfin stop arriving during a transcode rebuild wins, so the rebuilt transcode is closed or never registered once the play ends. The diff to internal/jellycompat/streams.go in cleanupPlaySession takes h.tm.LockSessionLifecycle(UpstreamSessionID) after the existing immediate close, then calls h.tm.CloseTranscodeSession(UpstreamSessionID, "") under that lock. A rebuild or fresh start holds the same lock until it registers its transcode, so the second close finds and closes what the start registered. The recipe delete now runs under the same lock, and the lock is released after it. The new test TestJellyfinStopClosesTranscodeStartedDuringIt in internal/jellycompat/playback_finish_test.go models this: it holds the lifecycle lock, lets the stop end the play, registers a transcode, releases the lock, and asserts that no transcode remains. The residual cases the PR excludes (a playlist request that takes the lock only after the stop returns, and session-expiry and administrator-end closes that do not take the lock) are outside the lock-held race described in #4112's reproduction and technical notes. The PR states they will be tracked separately. Evidence is from the source diff only. I did not run the test or confirm its failure on the merge base.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base touches two files. internal/jellycompat/streams.go changes only cleanupPlaySession: it adds the lifecycle lock around a second transcode close and moves the existing recipe-delete lock scope and unlock. internal/jellycompat/playback_finish_test.go adds one regression test for the same stop path. Both changes are tied to #4112. The PR does not change other close call sites, the transcode manager, or the native stop path. The PR description lists the test and risk details, and no unrelated changes appear in the diff.

Full details: Description check

Explanation

The description is closely related to the changeset. It explains the race between a Jellyfin playback stop and a transcode start or rebuild, and it describes the fix. The fix closes the local transcode again under the session lifecycle lock in cleanupPlaySession, and the description names the changed code path and the regression test. It also covers validation, risks, and the scope exclusions. The description is detailed and specific, so the check passes.

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: de7b9571-24a1-44ee-a5ab-10cc8f46e91a
Commits

Reviewing files that changed from the base of the PR and between b6b7dd3 and 73e2cb6.

Files selected for processing: 2

  • internal/jellycompat/playback_finish_test.go
  • internal/jellycompat/streams.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 bug Something isn't working go Pull requests that update go code priority: P2 Limited scope, workaround exists, or polish labels Nov 13, 2026
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.

Review of 02934e4, which merges current main into the branch. I found one stale code comment, below. The summary comment covers the rest.

🤖 Claude Code (claude-opus-5-5)

Comment thread internal/jellycompat/streams.go Resolved
…ecycle lock now that restart hooks time out

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@silo-kody

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

Copy link
Copy Markdown
Contributor Author

Review summary

Reviewed head: 045e598. I merged current main into the branch (02934e4) and then pushed one comment fix.

Fit: 9/10 · Readiness: 9/10 — ready (assuming CI is green)

Fit. This fixes a real resource leak on the Jellyfin surface: FFmpeg keeps encoding for a play that has ended. It fixes it at the stop, with a small change that brings the Jellyfin stop in line with the native stop, which already takes the session lifecycle lock before it closes. It complements #4128 and doesn't duplicate it. #4128's check runs after a rebuild registers, and a stop that removes the session only after its own close lets that check pass. #4128's description names this case as left to this PR.

Readiness. The diff introduces no defect. TestJellyfinStopClosesTranscodeStartedDuringIt fails on main at 91611b4 with the transcode still registered, and passes on the head under -race. The rest of internal/jellycompat passes too. It wasn't reproduced on a deployment: the stop has to land in the few milliseconds while a start holds the lock.

Addressed

Checked and not a problem in this diff

  • No caller of cleanupPlaySession holds the same session's lifecycle lock, so there's no self-deadlock. Finish hooks run before the new lock is taken, and no restart can be mid-flight while the stop holds the lock.
  • Stop latency is unchanged in the shipped configuration. The server always has a recipe store, so a Jellyfin stop already waited on this lock for the recipe delete.
  • When the stop closes a fresh start's transcode, the start's publish step then fails and runs a teardown. main already takes that path when the publish step rejects a play that ended during the start.
  • The second close passes an empty node URL, which sends no remote request. Remote starts check the play under the same lock when they publish.

For the maintainers (present on main, not this PR)

Validation, manual, and other clients

  • Validated features: I couldn't read the 1.0 validation board from here. Judged from the change instead: Jellyfin stop responses and timing are unchanged in the shipped configuration, and a transcode is now closed where it used to leak. Validation tasks: none stands, but the board itself wasn't checked.
  • User manual: unaffected. No setting, label, or documented behavior changes.
  • Other clients: none needed. This is server-side, and the native API already behaves this way.

Method

Code review at high effort with three finders on the merged head, plus a cross-provider round (gpt-6.1-sol, xhigh). Neither found a defect the diff introduces.

Evidence: https://evidence.siloserver.org/r/silo-server/pr-4119/

🤖 Claude Code (claude-opus-5-5)

@Quick104
Quick104 merged commit b4584cb into main Nov 14, 2026
18 checks passed
@Quick104
Quick104 deleted the fix/jellyfin-stop-during-transcode-rebuild branch November 14, 2026 22:44
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 priority: P2 Limited scope, workaround exists, or polish size/M 50–199 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 rebuild that finishes after a Jellyfin client stops playback leaves FFmpeg running

1 participant