Skip to content

fix(playback): stop seek-restart churn and premature paused-session reaping - #279

Merged
Quick104 merged 2 commits into
Silo-Server:mainfrom
fluxis:fix/playback-freeze-restart-reap
Jul 2, 2026
Merged

Quick104 merged 2 commits into
Silo-Server:mainfrom
fluxis:fix/playback-freeze-restart-reap

Conversation

@fluxis

@fluxis fluxis commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Problem

Cross-client playback freezes (web + tvOS) on three state changes: resume after a long pause, intro-skip, and manual seek near stream start (#243). Investigation found two independent latent defects, both present since the initial migration:

  1. Dueling transcode restarts. TranscodeSession.Restart had no concurrency guard, and the segment-recovery decision told concurrent requests to restart again while a restart was already in flight — the restart window runs with running=false, so requests hit the restart-happy transcode_not_running branch before the Restarting case was ever reached. Pipelined HLS segment requests therefore spawned restarts that kept killing the ffmpeg the player was waiting on: a 30s stall, another restart, surfacing as buffer-then-freeze. Near stream start (tiny produced head) almost any forward seek entered this path, and intro-skip is the same code path.
  2. Paused sessions reaped after 2 minutes, killing the transcode with no revival path — every subsequent manifest/segment/progress request 404s. Clients that stop progress-reporting while paused (tvOS; backgrounded/slept web tabs) froze on play after a >5 minute pause.

Approach

  • Restart is now single-flight: a second caller during an in-flight restart returns immediately as a no-op instead of killing the process the first restart just started.
  • SegmentRecoveryDecision checks Restarting before Running and returns wait-not-restart (WaitForSegment already polls politely through a restart window).
  • Paused grace raised 2m → 30m in both reapers (in-memory SessionManager and the DB SessionCleaner), so an intentional pause survives; abandoned sessions still reap.
  • Post-restart re-arming (throttler + exit monitor) moved into a restart hook installed at session creation and fired by Restart itself — previously the jellycompat seek path re-armed nothing and the audio-switch path re-armed only the throttler; now all three callers behave identically.

Testing

  • Unit tests: wait-during-restart decision, single-flight guard, restart hook firing, paused-session survival/reap boundaries.
  • Live-validated on a production deployment (QSV transcode, real media):
    • 3 concurrent far-seek segment requests → exactly one ffmpeg restart (restart_count=1); the concurrent request hit the new transcode_restarting wait branch and got its segment in 1.4s. Old code restarted for each.
    • Session paused then silent for 2m50s (past the old 2m reap) → session row survived, segment fetch on resume returned 200, resume progress accepted. Old code deleted the session at 2:00 and every resume request 404ed.

Risks / follow-up

  • A paused transcode now holds its ffmpeg process and output segments for up to 30 minutes instead of 2 — bounded by max-streams admission limits.
  • Follow-up (not in this PR): lazily revive a reaped session's transcode on resume instead of 404ing, so even multi-hour pauses recover. Worth its own issue.

Fixes #243

AI-use disclosure

Implemented with Claude Code (test-first, live-validated); human-reviewed before submission.

🤖 Generated with Claude Code

…eaping

Two latent defects (present since the initial migration) explain the
cross-client freezes on resume-after-pause, intro-skip, and manual seek
reported in Silo-Server#243:

1. Dueling transcode restarts. TranscodeSession.Restart had no
   concurrency guard, and SegmentRecoveryDecision told concurrent
   segment requests to restart again while a restart was already in
   flight (the restart window runs with running=false, so requests hit
   the restart-happy transcode_not_running branch before the Restarting
   check). Pipelined HLS segment requests therefore spawned restarts
   that kept killing the ffmpeg the player was waiting on — a 30s stall
   and another restart, surfacing as buffer-then-freeze. Restart is now
   single-flight and the recovery decision waits out an in-flight
   restart (WaitForSegment already polls politely through one).

2. Paused sessions were reaped after 2 minutes, killing the transcode
   with no revival path — every resume request then 404s. Clients that
   stop progress reporting while paused (tvOS; backgrounded web tabs)
   froze on play after a >5 minute pause. Paused grace is now 30
   minutes in both the in-memory session manager and the DB session
   cleaner.

Also unifies post-restart re-arming: the throttler and exit monitor are
re-armed via a restart hook installed at session creation and fired by
Restart itself, so the jellycompat seek path (which previously re-armed
nothing) and the audio-switch path (which re-armed only the throttler)
now behave identically to web segment recovery.

Follow-up (not in this PR): lazily revive a reaped session's transcode
on resume instead of 404ing, so even multi-hour pauses recover.

Fixes Silo-Server#243

Co-Authored-By: Claude Fable 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@Quick104, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0c67b7e0-a1c7-4dcb-a106-2101bb655efc

📥 Commits

Reviewing files that changed from the base of the PR and between 027b0e2 and 95327c9.

📒 Files selected for processing (6)
  • internal/api/handlers/playback.go
  • internal/playback/session.go
  • internal/playback/session_paused_grace_test.go
  • internal/playback/transcode.go
  • internal/playback/transcode_restart_guard_test.go
  • internal/worker/cleanup.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@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: 1b16cc0aef

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 1183 to 1187
case progress.Restarting:
decision.Wait = true
decision.WaitTimeout = activeSegmentWait
decision.RestartOnTimeout = false
decision.Reason = "transcode_restarting"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Re-decide instead of returning 404 after restart wait

When a segment request reaches this branch during an in-flight restart but the target segment is not produced within activeSegmentWait (for example a slow seek/transcode under load), RestartOnTimeout=false makes callers give up: the segment handlers only enter their recovery/restart path when the original decision has this flag set, so after WaitForSegment times out they fall through to a 404 rather than re-evaluating once the restart may have completed. This turns slow restarts into client-visible segment failures; the timeout path should re-decide or otherwise continue recovery instead of permanently disabling it here.

Useful? React with 👍 / 👎.

@Quick104

Quick104 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Code review

Found 2 issues (both advisory — the core concurrency changes verified clean):

  1. The new comment overclaims jellycompat coverage. The restart hook is installed only in HandleStartTranscode, but jellycompat creates its transcode sessions through its own playback.StartTranscode call in internal/jellycompat/streams.go and calls .Restart() directly — those sessions never receive the hook, so the compat seek path still re-arms nothing. Not a regression (that path never re-armed pre-PR either), but the comment asserting re-arm happens "no matter which caller triggered it (… or jellycompat seek …)" will mislead future maintainers; either wire the hook at the jellycompat creation site or scope the comment to handler-created sessions. (code comment guidance)

// Re-arm the throttler and exit monitor after every restart, no matter
// which caller triggered it (web segment recovery, audio switch, or
// jellycompat seek — the compat path has no handler access at all).
transcodeSession.SetRestartHook(func(ctx context.Context) {
h.maybeStartThrottler(ctx, transcodeSession)

  1. Coordination with feat(playback): unified restart-resilient playback (native + jellycompat) #174: the open feat/unified-playback-reconstruct branch introduces a competing manager-level restart lock (RestartSessionLocked) over the same function and same three files this PR touches (transcode.go, session.go, handlers playback.go), including jellycompat call-site coverage. Neither PR references the other; whichever merges second faces a non-trivial reconcile, so worth sequencing deliberately. (historical git context)

Verified clean: the single-flight restarting guard is race-free (checked/set under s.mu, all exit paths reset it, hook fires after unlock), the Restarting-before-!Running reorder fixes a genuine latent bug present since the initial migration, all five Restart call sites still behave correctly, both grace constants agree at 30m, and no prior fix (#52's RestartOnTimeout gating, #262's session liveness) is regressed.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

…sions

The comment above SetRestartHook claimed the hook covers restarts from
'jellycompat seek', but jellycompat creates its own TranscodeSessions via
playback.StartTranscode in a separate registry and calls Restart directly;
those sessions never get the hook and never had throttler/exit-monitor
wiring to re-arm. Reword the comment (and the matching test comment) to
scope the guarantee to handler-created sessions.

Also fix TestRestartInvokesRestartHook to resolve `true` via PATH instead
of hardcoding /bin/true, which does not exist on macOS.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Quick104

Quick104 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Addressed the review finding in 95327c9 (comment-only, as suggested): verified that jellycompat keeps its own transcode registry (internal/jellycompat PlaybackHandler.transcodes) and its Restart() calls never touch handler-created sessions, and that the throttler/exit monitor exist only in the API handler — so wiring the hook there would re-arm nothing. The comment in HandleStartTranscode and the test doc comment now scope the guarantee to handler-created sessions and state explicitly that jellycompat-created sessions never had this wiring.

Bonus: fixed a portability bug the test run exposed — transcode_restart_guard_test.go hardcoded /bin/true (absent on macOS); it now resolves true via exec.LookPath.

The overlap with #174 remains a sequencing decision for the maintainer — no code change belongs in this PR for it.

🤖 Generated with Claude Code

@Quick104
Quick104 merged commit 878492c into Silo-Server:main Jul 2, 2026
1 check passed
Quick104 added a commit to RXWatcher/silo-server that referenced this pull request Jul 10, 2026
…eaping (Silo-Server#279)

* fix(playback): stop seek-restart churn and premature paused-session reaping

Two latent defects (present since the initial migration) explain the
cross-client freezes on resume-after-pause, intro-skip, and manual seek
reported in Silo-Server#243:

1. Dueling transcode restarts. TranscodeSession.Restart had no
   concurrency guard, and SegmentRecoveryDecision told concurrent
   segment requests to restart again while a restart was already in
   flight (the restart window runs with running=false, so requests hit
   the restart-happy transcode_not_running branch before the Restarting
   check). Pipelined HLS segment requests therefore spawned restarts
   that kept killing the ffmpeg the player was waiting on — a 30s stall
   and another restart, surfacing as buffer-then-freeze. Restart is now
   single-flight and the recovery decision waits out an in-flight
   restart (WaitForSegment already polls politely through one).

2. Paused sessions were reaped after 2 minutes, killing the transcode
   with no revival path — every resume request then 404s. Clients that
   stop progress reporting while paused (tvOS; backgrounded web tabs)
   froze on play after a >5 minute pause. Paused grace is now 30
   minutes in both the in-memory session manager and the DB session
   cleaner.

Also unifies post-restart re-arming: the throttler and exit monitor are
re-armed via a restart hook installed at session creation and fired by
Restart itself, so the jellycompat seek path (which previously re-armed
nothing) and the audio-switch path (which re-armed only the throttler)
now behave identically to web segment recovery.

Follow-up (not in this PR): lazily revive a reaped session's transcode
on resume instead of 404ing, so even multi-hour pauses recover.

Fixes Silo-Server#243

Co-Authored-By: Claude Fable 5 <[email protected]>

* docs(playback): scope the restart-hook comment to handler-created sessions

The comment above SetRestartHook claimed the hook covers restarts from
'jellycompat seek', but jellycompat creates its own TranscodeSessions via
playback.StartTranscode in a separate registry and calls Restart directly;
those sessions never get the hook and never had throttler/exit-monitor
wiring to re-arm. Reword the comment (and the matching test comment) to
scope the guarantee to handler-created sessions.

Also fix TestRestartInvokesRestartHook to resolve `true` via PATH instead
of hardcoding /bin/true, which does not exist on macOS.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
Co-authored-by: Quick <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Playback freezes / won't resume on player state changes (resume after long pause, intro-skip, manual seek)

2 participants