Skip to content

fix(playback): don't hold a playback stop behind a transcode restart's stalled settings read - #4127

Merged
Quick104 merged 3 commits into
mainfrom
fix/transcode-restart-blocks-stop
Nov 13, 2026
Merged

Quick104 merged 3 commits into
mainfrom
fix/transcode-restart-blocks-stop

Conversation

@Quick104

@Quick104 Quick104 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

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

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

When the database stalled during a transcode restart (a seek, an audio switch, or a segment recovery), a playback stop for the same session waited for as long as the stall lasted, and FFmpeg kept running after the viewer had stopped. With this change the restart gives up on its settings read after five seconds, so the stop goes through and ends FFmpeg.

A restart runs its restart hook while it holds the session lifecycle lock. The hook reads the transcode throttle settings with the new FFmpeg process's context, which only a close cancels. Every stop takes the lifecycle lock before it closes the session, so the stop could not be the close that freed the hook. The settings read is an uncached database query, and the pool sets no acquire or statement timeout.

Approach

restart now gives the restart hook a context that also ends after restartHookTimeout, and logs a warning when the hook runs out of time. The timeout is five seconds, the bound #4121 uses for a transcode's first throttler start. The FFmpeg process keeps its own context, so a close still cancels the hook at once and shutdown behaves as before.

A restart stops the session's throttler and relies on the hook to start a new one. When the hook times out before it has, the restart starts a throttler with the threshold the session had before the restart, so a slow database does not turn the throttle off.

The bound sits where restart calls the hook, so it covers the native, Jellyfin-compatible, and rebuilt-session hooks on the API server. The transcode node's hook reads no settings and ignores its context.

Letting a stop close the session before it takes the lifecycle lock would remove the wait entirely, as #2524 did for shutdown. I left that out: the native stops take the lock first so that they wait for a session replacement to commit, and they can still fail without ending anything. It would also change four stop paths, one of them the lines #4119 changes.

No API, client, or Jellyfin wire change.

Validation

  • Not reproduced on a deployment, because a test deployment cannot stall the settings store on demand. Reproduced with tests that run the real transcode manager and a stand-in FFmpeg process.
  • An end-to-end reproduction wires the throttler start as the playback handlers do, over a settings store that never answers. It is not in this pull request because it waits on the real timeout. On the unfixed base, after 15 seconds the restart still held the lock, the stop was still waiting, and FFmpeg was still running. With the fix the restart returned after 5 seconds with FFmpeg running, and a stop queued behind it returned with FFmpeg closed.
  • The added test stalls the restart hook of a throttled session and requires a stop to get the lock while FFmpeg is still running and still throttled at its earlier threshold, then close it. It fails on the unfixed code, where the stop never gets the lock, and on this pull request's first commit, which lost the throttle. It passes on the head, 10 runs out of 10 under the race detector. It shortens the new timeout variable, so its failing run on the unfixed code used the base plus that one declaration, which nothing on the base reads.
  • The existing shutdown test now sets the timeout to an hour, so it still fails if shutdown waits on a hook.
  • Go gate, run in full on the first commit and repeated for the playback, transcode node, handler, and Jellyfin-compatible packages on the head: build, gofmt, go mod tidy -diff, go vet, make lint-changed, and make lint-router-recovery pass. make test-go passes except two tests that drive the host's FFmpeg 6.1.1 (subtitle rendition seek and media sample stats); both fail the same way on the unfixed base.
  • The contract and hygiene targets pass: settings bindings, playback fixtures, route inventory, the /api/v2 OpenAPI, web types, contract, and fixtures checks, local paths, case collisions, size ratchet, bidi, and migrations.
  • Not run: the web lint, build, budget, and test targets, verify-actionlint, verify-shellcheck, and verify-prometheus-rules. The change touches no web file, workflow, shell script, or alert rule.

Evidence

Nothing a user sees changes. The evidence page has the reproduction, its runs on the unfixed base and on the fix, and the added test's failing run.

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

Risks

  • A settings read that takes longer than five seconds during a restart no longer applies the current settings to that FFmpeg process. It keeps the threshold the session had before the restart, so a change an administrator made since then waits for the next restart, and a session that had no throttler stays without one. If only the second of the two reads times out, the throttle runs at the default threshold instead of the configured one. Before, the restart waited for the database and then read the settings. The warning log records the timeout with the session ID.
  • A stop still waits up to five seconds behind a stalled restart hook, and that long again for each further restart queued for the session ahead of it. As before, a restart also holds the lock across its tone-map source check, which is bounded at 20 seconds.
  • Not addressed here: a transcode's first throttler start makes the same read under the lifecycle lock. fix(playback): throttle a transcode whose starting request has ended #4121 bounds it.
  • Not addressed here: a throttler start that overlaps another leaves two throttlers on the session, which the native first start can do today. That predates this change and is filed as [bug] A transcode can run two throttlers when its first throttler start overlaps a restart #4131.

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): git, GitHub CLI, Go toolchain (go build, go test, go vet), golangci-lint through make lint-changed, T3 Code delegated review tasks, Codex
  • Model(s): claude-fable-5-2 (implementation and review), claude-opus-5-5 and gpt-6.1-sol (plan review), claude-sonnet-5-5 (review agents)
  • Involvement: Fully AI-generated (not yet human verified)
  • Adversarial review: Before implementation, gpt-6.1-sol attacked the plan and reported that it holds, with no findings; it reran the lock cycle on the unfixed base, and its caveats led to the test's check that FFmpeg is still running when the stop gets the lock. claude-opus-5-5 found the plan already minimal and narrowed the warning's condition. A multi-agent code review of the diff at high (claude-fable-5-2 and claude-sonnet-5-5 finders, a verifier, and a validation check) found no defect in the fix. It replaced a first test that only checked the hook's context had a deadline, corrected the warning's wording, and confirmed the throttle trade-off, which an automated review of this pull request then raised as well; the second commit keeps the session's earlier threshold in answer to both. A simplify pass changed nothing. The background adversarial review by gpt-6.1-sol reviewed the first commit (93013d3) and reported no findings, so it posted nothing. The two commits after it are this run's own: the second got another code review pass (claude-fable-5-2 and claude-sonnet-5-5 finders), which found no defect and led to the third, a refactor with no behavior change.

🤖 Generated with Claude Code

Note

Fix playback stop getting blocked by a stalled transcode restart hook

A transcode restart hook could block indefinitely on a settings read, holding up a playback stop. In transcode.go:

  • TranscodeSession.restart now calls the restart hook with a context bounded by a new five-second restartHookTimeout, and cancels it after the hook returns.
  • StopThrottler now returns the stopped throttler's threshold; if a hook returns after its deadline without installing a throttler, restart restores the previous throttle threshold and logs a warning.
  • The hook call stays synchronous, so restart still does not return while a hook is blocked — the timeout only protects the stop path.

Macroscope summarized bc3167f.

…s stalled settings read

A restart runs its restart hook under the session lifecycle lock, with the
new FFmpeg process's context, which only a close cancels. A stop takes that
lock before it closes the session, so while the hook's throttle settings read
waited on a stalled database, the stop waited too and FFmpeg kept running.

Give the hook a context that also ends after five seconds, and log a warning
when it does. The restart then returns, the stop takes the lock, and the
close ends FFmpeg. A hook that times out leaves that FFmpeg process's
throttle off, or at the default threshold, until its next restart.

Closes #4122

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
@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-13T21:44:22.060917Z bc3167f New commits
🔒 Security Review ✅ Completed 2026-11-13T21:47:07.557361Z bc3167f 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-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 ❌

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Restart hooks now run with a five-second timeout context. If a hook times out before starting a throttler, the restart restores the previous throttler threshold. Tests cover stalled hooks, throttler preservation, and session shutdown.

Changes: Restart Hook Timeout

Layer / File(s) Summary
Hook context contract
internal/playback/transcode.go
The SetRestartHook documentation describes the callback context lifetime and requires blocking callback work to honor the context. A five-second timeout is defined for restart hooks.
Hook timeout execution and validation
internal/playback/transcode.go, internal/playback/transcode_manager_test.go
The restart path saves the throttler threshold before stopping the throttler. It runs the hook with a timeout context and restores the threshold if the hook times out without starting a throttler. Tests cover a stalled hook, throttle preservation, and session-close cleanup.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: neurekadev, blurbery, rhainland

Merge Risk: Low · up to bc316

Restart hooks no longer hold the session lifecycle lock indefinitely, and the previous throttle setting is kept when the hook times out. One minor, pre-existing throttler-overlap race remains and is tracked in a follow-up issue; it is bounded and does not block merging.

Security architecture risk: Minimal · up to bc316

The timeout improves stop responsiveness while preserving process shutdown and throttling controls. The inspected change establishes no introduced or worsened security or failure-containment risk.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed transition operates on the existing session's process context and throttler threshold. The lifecycle lock is keyed by session ID, and the manager checks that the requested session object is still the live mapped instance before restarting it. The change does not broaden session identity or process ownership.

Trust Boundaries and Controls

  • observed — The hook timeout does not cancel FFmpeg's parent context. Close marks the session terminal and cancels the process context, which also cancels the hook. StartThrottler refuses to arm while closing, so timeout recovery cannot intentionally revive throttling after terminal shutdown begins.

Resilience and Maintainability Implications

  • observed — Restart and the inspected stop path retain the same per-session lifecycle gate. The hook is called synchronously, so cancellation cooperation remains required. Added test source checks that a deadline-responsive stalled hook releases a waiting stop, preserves the prior 120-second threshold, and does not terminate the replacement process; shutdown cancellation is also covered in the inspected test source.
  • observed — Recovery favors continuity over immediately refreshed throttle settings: deadline fallback restores the previous threshold only when no throttler exists. If the enabled setting succeeds but the threshold read fails, the existing settings resolver can install its default instead, preventing fallback. The timeout warning explicitly acknowledges temporary settings divergence.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names a real and central change: a playback stop should not wait behind a transcode restart's stalled settings read. The changeset adds a five-second timeout to the restart hook context and …
Description check Passed The description explains the problem, the approach (a five-second restart hook timeout, threshold restoration on timeout, a warning log), the validation and tests, and the risks. It relates directly t…
Linked Issues check Passed Issue #4122 asks that a playback stop end the transcode without waiting on a restart's stalled throttle-settings read. It allows either bounding the read or letting a stop cancel FFmpeg without the li…
Out of Scope Changes check Passed The whole-PR change set stays within the restart timeout path in internal/playback/transcode.go and its tests. It includes the SetRestartHook doc comment, the restartHookTimeout variable, the hookCtx …
Full details: Title check

Explanation

The title names a real and central change: a playback stop should not wait behind a transcode restart's stalled settings read. The changeset adds a five-second timeout to the restart hook context and a throttle-threshold fallback, which the title captures. The title is concise and specific, though its wording is somewhat long at 92 characters.

Full details: Description check

Explanation

The description explains the problem, the approach (a five-second restart hook timeout, threshold restoration on timeout, a warning log), the validation and tests, and the risks. It relates directly to the changeset.

Full details: Linked Issues check

Explanation

Issue #4122 asks that a playback stop end the transcode without waiting on a restart's stalled throttle-settings read. It allows either bounding the read or letting a stop cancel FFmpeg without the lifecycle lock. At head bc3167f, the whole-PR diff against merge base b6b7dd3 in internal/playback/transcode.go bounds the hook. restart() now passes hookCtx := context.WithTimeout(ctx, restartHookTimeout) to the hook, with restartHookTimeout = 5s. The FFmpeg process keeps its own context, so a close still cancels the hook at once. A stop queued behind a stalled restart therefore acquires the lock after about five seconds. The head adds no new settings read. Changes since the prior review (3df26d5 to bc3167f) add a warning, a StopThrottler return value, and a fallback StartThrottler call. These do not change the requirement outcome. The stop paths are unchanged, which the issue allows. Limits: the bound depends on the API and Jellyfin hooks passing their context to the settings read. I did not inspect those callers, and the inspection budget ran out before I could read the test file in full. The added test is taken from the PR description and the prior review, not from direct source read in this pass.

Full details: Out of Scope Changes check

Explanation

The whole-PR change set stays within the restart timeout path in internal/playback/transcode.go and its tests. It includes the SetRestartHook doc comment, the restartHookTimeout variable, the hookCtx wrapper, the deadline warning, the StopThrottler change that returns the prior threshold, and the fallback StartThrottler(previousThrottleSeconds) call when the hook times out with no throttler installed. Each change connects to #4122: the fallback keeps throttling after a timed-out hook, and the PR description names it as a trade-off. No API, client, Jellyfin wire, web, workflow, or alert change appears in the reviewed material. Unresolved: the source of StartThrottler was not readable within the inspection budget. When no throttler ran, previousThrottleSeconds is 0. Whether StartThrottler(0) starts a zero-threshold throttler or does nothing is unknown. The PR description's Risks section says a session with no throttler stays without one, which holds only if StartThrottler(0) does nothing. This does not make the change out of scope, but the behavior is unverified.

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: 21555608-494d-4159-9b6b-e8efb1148fab
Commits

Reviewing files that changed from the base of the PR and between 3df26d5 and bc3167f.

Files selected for processing: 1

  • internal/playback/transcode.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 impact: playback Can't play, stalls, or wrong media priority: P2 Limited scope, workaround exists, or polish labels Nov 13, 2026
Comment thread internal/playback/transcode.go Resolved
…s out

A restart stops the session's throttler and relies on the restart hook to
start a new one. When the hook's settings read ran out of time, the new
FFmpeg process was left without a throttle until its next restart, even
after the database recovered.

When the hook times out without starting a throttler, start one with the
threshold the session had before the restart.

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 ❌

…pThrottler

Drop the unexported twin that existed only to return it.

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 ❌

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

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: c4e9877a-6ef7-448a-b220-dd0c151c08b6
Commits

Reviewing files that changed from the base of the PR and between 93013d3 and 3df26d5.

Files selected for processing: 2

  • internal/playback/transcode.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.

Comment thread internal/playback/transcode.go Resolved
@Quick104
Quick104 merged commit 3a003ab into main Nov 13, 2026
24 checks passed
@Quick104
Quick104 deleted the fix/transcode-restart-blocks-stop branch November 13, 2026 22:46
Quick104 added a commit to rangoDJ/silo-server that referenced this pull request Nov 14, 2026
…meline wait

main's restart now keeps the stopped throttler's threshold for a restart
hook that times out (Silo-Server#4127). The planned restart still lets the run it
replaces set the session's timeline before stopping it.

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: playback Can't play, stalls, or wrong media 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 playback stop waits behind a transcode restart whose throttle-settings read has stalled

1 participant