Skip to content

fix(jellycompat): close a first transcode whose play's session ended as it started - #4249

Merged
Quick104 merged 1 commit into
Silo-Server:mainfrom
rangoDJ:fix/compat-transcode-start-session-gone-4130
Nov 15, 2026
Merged

Quick104 merged 1 commit into
Silo-Server:mainfrom
rangoDJ:fix/compat-transcode-start-session-gone-4130

Conversation

@rangoDJ

@rangoDJ rangoDJ commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #4130
Related issue: #4120 (the rebuild half, fixed by #4128)
Validation tasks: none

Other clients: Jellyfin-compatible clients get a 404 for that one playlist request, as for a rebuild, and recover on the next one if they keep playing. Apple, Android and Roku N/A: the native API is unchanged.

A Jellyfin play with no stored recipe starts its first local transcode in ensureTranscodeSessionWithToneMapMode. When the play's native session went away while the start was running (a stop, an expiry or an administrator's end, each of which found no transcode yet and closed nothing), the start still registered and published the transcode. persistTranscodeRecipe only logged the missing session. If the client went away too, FFmpeg kept encoding with nothing left to expire it.

This PR has no dependencies and can merge in any order. #4128, which the issue said to follow, is merged, and this PR reuses its error, 404 answer and skipped teardown.

Approach

  • In the start branch, after the "is this still the live transcode" check and under the lifecycle lock the start already holds to publish, the native session is looked up. When it's gone, the start closes its transcode and returns errCompatUpstreamSessionGone. That is before the recipe write, so nothing is persisted for a gone session.
  • The playlist route already answers that error with 404 and skips the play's teardown (fix(jellycompat): close a transcode rebuilt for a playlist request after its play's session ended #4128), so the play stays resumable.
  • TranscodeManager.CloseTranscodeSessionIfSessionGone is split: the new CloseTranscodeSessionIfSessionGoneLocked does the check and close for a caller that holds the session's lifecycle lock. The rebuild keeps calling the locking form, unchanged.
  • The check runs only when the handler has a session manager; tests and deployments without one keep today's behavior.
  • persistTranscodeRecipe's comment and errCompatUpstreamSessionGone's now describe the start too.

Validation

Evidence

No visible change. The evidence is what a start does when its session goes away mid-start, from the test on WSL2 (Ubuntu, go1.26.4 linux/amd64), main at 48cae66d2, this PR at d2c4c0930.

Native session ends while a first transcode starts main This PR
Transcode registered and kept running closed
Start returns the transcode errCompatUpstreamSessionGone (playlist: 404)
Play row kept kept
$ go test -count=1 -run TestTranscodeStartClosesTranscodeOfEndedSession ./internal/jellycompat/   # check disabled
    playback_finish_test.go:300: start = &{...}, <nil>; want no transcode and errCompatUpstreamSessionGone
FAIL

$ go test -count=1 -v -run 'TestTranscodeStartClosesTranscodeOfEndedSession|TestPlaylistRebuild' ./internal/jellycompat/
--- PASS: TestPlaylistRebuildClosesTranscodeOfEndedSession (0.01s)
--- PASS: TestTranscodeStartClosesTranscodeOfEndedSession (0.00s)
--- PASS: TestPlaylistRebuildKeepsTranscodeAResumedPlayAdopted (0.01s)
ok  	github.com/Silo-Server/silo-server/internal/jellycompat	0.106s

Risks

  • A start that now ends in 404 leaves the client to request the playlist again, which registers the session and starts a transcode, as after a rebuild today.
  • The lookup adds one session read to every local start.

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 (Claude desktop app, Code tab)
  • Tool(s): Claude Code; GitHub CLI
  • Model(s): claude-opus-5-5
  • Involvement: Fully AI-generated, human verified
  • Adversarial review: n/a

🤖 Generated with Claude Code

Note

Close first transcode in jellycompat when its play session ends during startup

  • Fixes a leak where the upstream session for a play ends between transcode registration and startup completion. PlaybackHandler.ensureTranscodeSessionWithToneMapMode now checks the session after registering a newly started transcode, closes it, and returns errCompatUpstreamSessionGone before recording stream details or persisting the recipe.
  • Adds TranscodeManager.CloseTranscodeSessionIfSessionGoneLocked in transcode_manager.go so callers holding the session lifecycle lock can check-and-close atomically; it only removes the transcode if it is still the registered instance. The existing non-locked helper now delegates to it.
  • Adds a test in playback_finish_test.go covering a first transcode start whose session ends after registration.
  • Behavioral Change: a first transcode start whose session is gone no longer writes a recipe, but the play remains stored. Applies to both rebuilt and newly started transcodes.

Macroscope summarized d2c4c09.

…as it started

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. Silo-Server#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 Silo-Server#4130

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@silo-triage silo-triage Bot added the size/M 50–199 added + deleted lines (all files); change volume, not priority. label Nov 15, 2026
@silo-kody

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

@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 15, 2026
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Transcode startup now closes a newly registered transcode when its upstream session disappears before publication. The change adds a cleanup method for callers holding the session lifecycle lock and a test for the startup race.

Changes: Transcode Start Cleanup

Layer / File(s) Summary
Lock-aware transcode cleanup
internal/playback/transcode_manager.go
CloseTranscodeSessionIfSessionGone delegates to a new method that performs cleanup for callers already holding the session lifecycle lock.
Startup session check and coverage
internal/jellycompat/streams.go, internal/jellycompat/playback_finish_test.go, internal/jellycompat/handlers_playback.go
Startup checks whether the upstream session still exists after confirming the transcode is live. If the session is gone, it closes the transcode and returns errCompatUpstreamSessionGone. A test checks that the transcode is unregistered and the play remains stored without a recipe. Comments describe this behavior and distinguish it from tests with no session manager.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: quick104, rhainland

Merge Risk: Minimal · up to d2c4c

This change closes a locally started transcode when its Jellyfin session disappears during startup, and keeps the play resumable. No merge-blocking risk was identified.

Security architecture risk: Minimal · up to d2c4c

The change closes abandoned encoding work while preserving session ownership checks and the existing recovery response. No introduced or worsened security or failure-containment issue was established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed cleanup is scoped to the upstream session ID and expected runtime, rather than a bulk close operation. Its security-relevant benefit is tighter containment of encoding resources after upstream termination; the inspected change does not add authority over another play's runtime.

Trust Boundaries and Controls

  • observed — The inspected master-playlist path requires an authenticated session and resolves the client-supplied PlaySessionId through a token-ownership check before starting playback. The new cleanup uses the resulting upstream session ID and runtime pointer; it does not replace those controls with a client-selected cleanup target.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the change: a first Jellyfin-compat transcode is closed when the play's native session ends during startup. It matches the diff, which adds a session check in the start branch of ensur…
Description check Passed The description covers the problem, the approach, validation, and risks. It names the changed start branch, the new locked close method, the 404 response path, and the new test. This matches the raw s…
Linked Issues check Passed Issue #4130 requires that a Jellyfin local transcode start which finds its native session gone close its transcode and leave the play resumable. The PR summary for internal/jellycompat/streams.go stat…
Out of Scope Changes check Passed The changed areas are the start-path session check and error handling in internal/jellycompat/streams.go, the lock-held close helper in internal/playback/transcode_manager.go, the new regression test …
Full details: Title check

Explanation

The title names the change: a first Jellyfin-compat transcode is closed when the play's native session ends during startup. It matches the diff, which adds a session check in the start branch of ensureTranscodeSessionWithToneMapMode, a Locked variant of CloseTranscodeSessionIfSessionGone, and a test for the start path. The title is specific and does not use vague terms.

Full details: Description check

Explanation

The description covers the problem, the approach, validation, and risks. It names the changed start branch, the new locked close method, the 404 response path, and the new test. This matches the raw summary and the objectives. It is related to the changeset in detail.

Full details: Linked Issues check

Explanation

Issue #4130 requires that a Jellyfin local transcode start which finds its native session gone close its transcode and leave the play resumable. The PR summary for internal/jellycompat/streams.go states that, after the start registers and confirms its transcode, ensureTranscodeSessionWithToneMapMode checks for the upstream session while holding the publication lock. If the session is gone, it closes the transcode and returns errCompatUpstreamSessionGone, which occurs before the recipe write. The playlist route maps that error to 404 and skips teardown (from #4128), so the play row stays. internal/playback/transcode_manager.go adds CloseTranscodeSessionIfSessionGoneLocked for callers that already hold the lifecycle lock. The new test TestTranscodeStartClosesTranscodeOfEndedSession covers the no-recipe start path. The PR reports that it fails with the check disabled. The diff blobs could not be read in this review, so the implementation evidence comes from the PR summary and the author-reported test results, not from a direct diff read. The PR also reports that the rebuild tests from #4128 still pass. The behavior requirements are addressed by the described code and tests.

Full details: Out of Scope Changes check

Explanation

The changed areas are the start-path session check and error handling in internal/jellycompat/streams.go, the lock-held close helper in internal/playback/transcode_manager.go, the new regression test in internal/jellycompat/playback_finish_test.go, and a comment update in internal/jellycompat/handlers_playback.go. Each change supports the #4130 objective: the check, the helper it uses, the test that verifies it, and the comments that describe the new start behavior. The rebuild path keeps its locking method unchanged. No unrelated behavior change is identified in the summary. The diff blobs could not be read, so this verdict relies on the PR summary and description.

Finishing Touches

Generate unit tests (beta)

  • 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: ff7a5a0e-86fa-4384-beab-d38827b7aefa
Commits

Reviewing files that changed from the base of the PR and between 7fc39fb and d2c4c09.

Files selected for processing: 4

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

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

@Quick104

Copy link
Copy Markdown
Contributor

Review summary (head d2c4c09)

No findings. The fix closes the gap #4130 describes, at the point where the start already holds the lock it publishes under, and it reuses the rebuild branch's error, 404 response, and skipped teardown from #4128.

What I checked

  • TestTranscodeStartClosesTranscodeOfEndedSession fails on the base (48cae66) for the reported reason: the start returns the transcode with no error. It passes on this head, and fix(jellycompat): close a transcode rebuilt for a playlist request after its play's session ended #4128's rebuild tests still pass.
  • Every path that removes the native session is covered:
    • Idle expiry removes the session before it closes the transcode. If expiry runs before the new check, the check catches it. If it runs after, its close finds the registered transcode.
    • A client stop (cleanupPlaySession) already closes again under the lifecycle lock after it finishes the session.
    • A remote start already rechecks the session under its own publish lock.
  • Lock order is lifecycle lock, then transcodeMu, then the session manager's read lock. The rebuild check already uses this order, and nothing takes these locks in reverse.
  • The added GetSession call reads an in-memory map, so it adds no measurable cost to a start.
  • Every caller handles errCompatUpstreamSessionGone. The master playlist and segment routes both answer 404 through writeCompatTranscodeError, and the play row is kept.
  • A start that runs longer than the 90-second startup grace can now end in 404 where it used to serve a transcode that nothing would close. The rebuild branch already behaves this way, and a client that asks again gets a new session. I don't consider it a defect.

An adversarial review on a second model found nothing either. It also ran the start and rebuild tests with -race -count=10. Neither review tested against real Jellyfin clients or a multi-replica deployment; those were judged from the source.

Validated features: none affected. No passed case on the v1.0 board reaches the Jellyfin local transcode start, so Validation tasks: none is correct.

User manual: unaffected. This fixes a race and changes no setting or documented behavior.

Other clients: the PR's line is right. The native API doesn't change, and the Jellyfin surface is the change itself.

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

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

@Quick104
Quick104 merged commit ae83460 into Silo-Server:main Nov 15, 2026
17 checks passed
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/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 Jellyfin transcode that starts as its play's session goes away keeps running without a session

2 participants