Skip to content

fix(playback): keep subtitle rendition cues when a session is rebuilt - #2690

Merged
Quick104 merged 4 commits into
Silo-Server:mainfrom
Rhainland:fix/2650-rebuilt-session-subtitle-cues
Oct 21, 2026
Merged

Quick104 merged 4 commits into
Silo-Server:mainfrom
Rhainland:fix/2650-rebuilt-session-subtitle-cues

Conversation

@Rhainland

@Rhainland Rhainland commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #2650
Related issue: #2620
Validation tasks: none

A transcode that carries an embedded text subtitle as an HLS subtitle rendition loses its subtitles for earlier parts of the video after the session is rebuilt from its recipe. This affects iOS, tvOS, macOS and Android clients that send hls_subtitle_renditions_v1. A rebuild reuses the output directory when the server or node running the transcode went away without removing it: a crash, an OOM kill, or a SIGKILL after the shutdown grace period. A backward seek then plays the earlier process's video segments from disk, but every subtitle segment for that range comes back as an empty WebVTT document, which the player keeps. Each one is also answered only after the wait timeout of a few seconds.

Two things caused it. The record of which cue file covers which segments (subtitleSpans) lived only in memory, so the rebuilt session knew only its own range. The rebuilt process also numbered its cue files from 0 again and overwrote the earlier process's subtitles_0.vtt.

This change records the ranges in the output directory and reads them back when a rebuilt session starts. The rebuilt session then serves the earlier cues for every segment it serves from disk.

Approach

The session writes subtitle_spans.json to the output directory whenever a generation starts or closes. StartTranscode reads it before FFmpeg starts, while the playlist on disk still belongs to the earlier process. For the generation that was still running when the session went away, that playlist gives the last segment it finished. An entry is dropped when its cue file is gone, when that last generation never wrote a playlist of its own, or when the record was written for a different subtitle track or segment duration. The rebuilt session numbers its cue files after every recorded name, so earlier cues are never overwritten.

Seek restarts within one session work as before. They now also update the record, which is what lets a second rebuild keep the cues of both earlier processes. The change applies to the integrated server and to transcode nodes, which share StartTranscode. The API, jellycompat and the clients are unaffected.

Validation

  • New unit tests: a session rebuilt on a reused directory serves the earlier process's cues, uses a new cue file name and keeps doing so after a seek restart. Unusable records and entries are dropped. The rebuild test fails on main.
  • go test ./internal/playback/... ./internal/transcodenode/... passes, including the real-FFmpeg cue alignment test. The race detector passes on the subtitle and restart tests, and make lint-changed reports no issues.
  • End to end against a local server, with a 30-minute file holding an embedded SRT track and one cue every 10 seconds, played over /api/v2 with hls_subtitle_renditions_v1. The script played segments 0–320, SIGKILLed the server's process group, restarted it and requested segment 330, which rebuilt the session starting at 330. On main, subtitle segments 50–55 and 160–161 were empty and each took 3.5 s. With this change they carried their cues at once.
  • The same run also covered seek restarts with no rebuild (forward to 400, back to 20, back to 200) and two rebuilds in a row. All earlier ranges kept their cues.

Evidence

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

Surface: hls.js 1.6.15 in headless Google Chrome, playing the multivariant playlist from /api/v2/playback/start with hls_subtitle_renditions_v1, since the web player does not request subtitle renditions. Before is main at 365e3d4, after is this branch at 1c3c13a. The server was SIGKILLed mid-transcode, restarted, and the session rebuilt at 400 s; the player then opened at 1:41 on segments the first FFmpeg process wrote. Screens at 1:42.5 and 1:52.5: before shows no cue, after shows "CUE at 100s" and "CUE at 110s". Not recorded on the Apple and Android apps; they need no change.

Risks

One small JSON file is written to the transcode directory per generation start and close. If that write fails, it is logged and only a later rebuild loses the earlier cues, as before this change. A session rebuilt by an older server ignores the record and keeps the old behavior.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code, run inside T3 Code
  • Tool(s): Claude Code; Codex CLI (review only)
  • Model(s): claude-opus-5-5; gpt-6.1-sol (review only)
  • Involvement: Fully AI-generated, human verified
  • Adversarial review: the final diff was reviewed independently by Claude Opus 5.5 and GPT 6.1 Sol, and the results were reconciled against the code. Neither reported a finding. GPT reviewed the source only and did not run the tests.

Note

Keep subtitle rendition cues when a transcode session is rebuilt

  • Persists subtitle span state to subtitle_spans.json in the output directory, so a rebuilt session can serve cues from earlier video segments instead of losing them. Spans are saved at generation start/close and when an origin is observed.
  • loadSubtitleSpans restores only spans with a matching track and segment duration, drops missing cue files, and keeps only live spans that the current playlist proves the session produced. Cue-file generation numbers are parsed from existing file names so new spans never reuse a recorded name.
  • Adds writeFileAtomic for temp-file-and-rename writes, and shutdown now waits for in-flight record writes before deleting output, preventing writes into a removed directory.
  • Updates the v3 protocol docs in playback-protocol-v3.md. See subtitle_rendition.go and transcode.go.
  • Behavioral Change: a rebuilt session now serves earlier completed subtitle segments without waiting, and cue-file names carry a generation number instead of always starting at zero; restart adds the loaded generation base to the segment-generation counter.

Macroscope summarized 1c7a2ce.

@silo-triage silo-triage Bot added the size/L 200–499 added + deleted lines (all files); change volume, not priority. label Oct 19, 2026
@silo-kody

silo-kody Bot commented Oct 19, 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 Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 26 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c18d228a-fc84-43e1-b697-98143ca13910
📥 Commits

Reviewing files that changed from the base of the PR and between 0ae2e06 and 1c7a2ce.

📒 Files selected for processing: 4
  • docs/architecture/playback-protocol-v3.md
  • internal/playback/subtitle_rendition.go
  • internal/playback/subtitle_rendition_test.go
  • internal/playback/transcode.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@macroscopeapp

macroscopeapp Bot commented Oct 19, 2026

Copy link
Copy Markdown

The rebuilt playback now shows subtitle cues for earlier video segments, but the Evidence section has no captures or recording; add before-and-after playback evidence from a client that requests subtitle renditions, and identify the surface and builds. Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness.

Posted via Macroscope — Visible change evidence

@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 Oct 19, 2026
opts := s.opts
record := subtitleSpanRecord{Track: opts.SubtitleTrackIndex, SegmentDuration: renditionSegmentDuration(opts)}
for _, span := range s.subtitleSpans {
row := subtitleSpanRecordRow{File: span.file, Start: span.start, End: span.end}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

kody code-review Bug high

Timestamp mapping loss in internal/playback/subtitle_rendition.go occurs because subtitleSpanRecordRow discards cached origin/originKnown, so reconstruction loses an earlier generation’s mapping when a later span causes subtitleSpanOrigin to reject both probe candidates via subtitleSegmentRewrittenLocked. After an observed span covering 0–100 and a later span starting at 50, requesting preserved segment 80 after reconstruction falls back to the zero-clock grid and selects incorrect or empty cues for a nonzero-clock source; persist and restore known origins with each span.

Prompt for LLM

File internal/playback/subtitle_rendition.go:

Line 275:

Timestamp mapping loss in internal/playback/subtitle_rendition.go occurs because subtitleSpanRecordRow discards cached origin/originKnown, so reconstruction loses an earlier generation’s mapping when a later span causes subtitleSpanOrigin to reject both probe candidates via subtitleSegmentRewrittenLocked. After an observed span covering 0–100 and a later span starting at 50, requesting preserved segment 80 after reconstruction falls back to the zero-clock grid and selects incorrect or empty cues for a nonzero-clock source; persist and restore known origins with each span.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

​

​

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.

Confirmed and fixed in 917b886. A rebuilt session could not observe an earlier span's origin again once a later generation had started at or before that span's start. For example, segments 50–100 are encoded, the user seeks back to 40, and the node dies at 60. Each span's observed origin is now stored in the span record and restored on rebuild, and a regression test covers that sequence. 1c3c13a also makes Close wait for a late record write before it removes the output directory.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for confirming. Persisting and restoring each span’s observed origin addresses the reported mapping loss when later generations prevent re-observation, and the regression test covers that sequence. The additional Close synchronization addresses the late-write cleanup race.

Based on your description, this finding is addressed by 917b88618, with the cleanup fix in 1c3c13a7a. I haven’t independently reviewed those commits.

@silo-kody

silo-kody Bot commented Oct 19, 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 ❌
⚠️ 1 Kody Rule(s) were not evaluated

These rules declare that they need repository context beyond this diff, and the review could not retrieve it, so they were not judged on this pull request:

  • Preserve optional store capabilities through production wrappers

A transcode session rebuilt from its recipe on the same output directory
(after the server that ran it exited without removing it) serves the earlier
process's video segments from disk, but only knew its own subtitle span, so
those segments' subtitle rendition segments came back as empty WebVTT. The
rebuilt process also numbered cue files from 0 and overwrote the earlier
process's subtitles_0.vtt.

Record each generation's cue file and finished segments in
subtitle_spans.json when a generation starts and closes, read it back in
StartTranscode before FFmpeg starts, and number the rebuilt session's cue
files after the recorded ones. Spans whose cue file is gone, or whose live
generation never wrote its own playlist, are dropped.

Closes Silo-Server#2650
A rebuilt session could not observe the origin of an earlier span again when
a later generation started at or before that span's start, so its cues fell
back to the zero-clock grid on a source whose clock does not start at zero.
Record each observed origin with its span and restore it on rebuild.
…irectory

An origin probe can save the span record after the request that started it, so the save could land in the output directory while Close was removing it. Close now holds the record lock while it removes the directory, and the save skips once the directory is gone.

@Quick104 Quick104 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.

Review of 1c3c13a. One finding on this PR's code, below. I'm also rebasing the branch onto main, which now conflicts in subtitle_rendition_test.go after #2817 moved the subtitle test helpers. The summary comment will follow once the fix is pushed.

Comment thread internal/playback/subtitle_rendition.go Resolved
A rebuilt session read any stream.m3u8 that didn't match the recorded
inherited playlist as the live generation's progress. After a failed
record write, a later generation's playlist was credited to the stale
live row, and its cue segments were served as ready. A generation's own
playlist starts at its -start_number, so require the first listed
segment to equal the row's start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Quick104
Quick104 force-pushed the fix/2650-rebuilt-session-subtitle-cues branch from 1c3c13a to 1c7a2ce Compare October 21, 2026 15:56
@Quick104

Copy link
Copy Markdown
Contributor

Review summary (reviewed 1c7a2ce)

The approach holds. Recording each generation's cue file and finished range in the output directory is the right place: that directory already holds the video segments a rebuilt session serves from disk. The integrated server and transcode nodes both load the record through StartTranscode. The branch now merges cleanly and needs no further change from the author.

Branch rewritten. I rebased the branch onto main (ed9c902) and force-pushed it with a lease. Main's #2817 had moved the subtitle test helpers next to TestRebuiltSessionKeepsAnObservedOrigin, so subtitle_rendition_test.go conflicted. The resolution keeps both sides unchanged.

Addressed

  • A recorded live span could be credited with a playlist that a later generation wrote, for example after a failed record write. It is fixed in 1c7a2ce: the playlist must start at the row's own start segment. The thread is resolved.

Tests

  • TestRebuiltSessionKeepsEarlierGenerationCues fails on main: the rebuilt process writes subtitles_0.vtt again and segment 11 comes back with no cues. It passes on this branch.
  • TestRebuiltSessionKeepsAnObservedOrigin and TestLoadSubtitleSpansDropsUnusableSpans call functions this PR adds, so they don't build on main. The new case in the latter fails on 1c3c13a and passes on 1c7a2ce.
  • go test ./internal/playback/... ./internal/transcodenode/... passes. The race detector passes on the subtitle and restart tests, and make lint-changed reports no issues.

Left for maintainers

  • An older problem that this PR extends to rebuilds: subtitleSegmentSource falls through to an older closed span when the live generation hasn't finished the requested segment yet. That happens when a seek restart, or now a rebuild, starts inside a range an earlier generation covered. At the live edge, the earlier span's cues can be served for a video segment the new process rewrote. This only gives wrong cues when the two generations map that segment to different source times, as copy-mode keyframe origins can. The selection loop is unchanged from main, and an in-session backward seek restart hits the same path today. One independent reproduction supports it, using a test overlay rather than a client. I haven't confirmed it myself, so it isn't filed yet.
  • The inherited-playlist check compares size and mtime but not file identity, which main's sameManifestFile also checks. On a filesystem with 1–2 s mtime granularity, a same-size replacement playlist could still drop a live span. The first-segment check above narrows this to replacements that start at the same segment.

Cross-provider review: one round with GPT-6.1-Sol (xhigh). It raised the two items above. The second led to the fix, and the first is the older problem listed above.

Validated features: no passed case on the v1.0 board covers subtitles, transcode, or seek restarts. #2650 and #2620 aren't tied to a validation task. Validation tasks: none is correct.

User manual: unaffected. The change adds no setting, label, or step.

Apple/Android: no client change. The subtitle playlist and segment shapes are unchanged.

Evidence: the published page still matches. The fix only drops a recorded live span whose playlist starts at another segment, and the recorded scenario doesn't produce one.

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

  • Fit: this fixes lost subtitles after a node or server dies mid-stream, which Silo should treat as a normal event, and it does so where the video segments already persist. It also adds on-disk state that has to stay consistent with the playlist. The size and mtime match and the ordering of the live row are new ways for that state to go wrong. The change justifies that cost.
  • Readiness: the rebuild test fails on main and the author ran an end-to-end SIGKILL test. What remains is the older span-selection problem listed above, not a defect this PR introduced.

@silo-kody

silo-kody Bot commented Oct 21, 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 b4b52f2 into Silo-Server:main Oct 21, 2026
18 checks passed
rangoDJ added a commit to rangoDJ/silo-server that referenced this pull request Oct 21, 2026
Main's Silo-Server#2690 starts a TranscodeSession with its subtitle rendition spans
and cue base, beside this branch's group assembler and plan seek anchor.
Set all of them; neither depends on the other.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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/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 rebuilt transcode session loses subtitle rendition cues after a backward seek

2 participants