Skip to content

fix(playback): demux a whole subtitle track when the file's random reads are slow - #4302

Merged
Quick104 merged 2 commits into
mainfrom
fix/keptsub-whole-track-slow-storage
Nov 15, 2026
Merged

Quick104 merged 2 commits into
mainfrom
fix/keptsub-whole-track-slow-storage

Conversation

@Quick104

@Quick104 Quick104 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: none

Other clients: Apple N/A, Android N/A, Roku N/A (server-side fix; clients get it without a change)

On storage where every random read is slow and pulls a large chunk, such as a FUSE mount of remote storage with a local read cache, reading a whole embedded subtitle track through the file's index takes minutes. Those reads also starve the playback stream reading the same file. A client asking for a whole PGS track during direct play got nothing in 150 seconds of retries, and the video stalled. This change makes that read give way after its first round of reads, and the request demuxes the file in order instead. #4272 fixed the same problem for subtitle windows. This fixes it for whole tracks.

A whole-track read makes one random read per cue, 16 at a time. It runs for up to 3 minutes, keeps going after the request stops waiting at 90 seconds, and is the only way a node reads a whole track before it is kept. These callers start it: sidecar requests without a position (including offline-download sidecars), windows open to the end, jellycompat track requests and its PlaybackInfo text warm, AI translation, and the subtitle prefetch.

Approach

  • embeddedsub.ReadOptions gains SlowRead. The reader times each read; once the last 16 averaged more than SlowRead, reads that start afterwards fail with ErrSlowReads. Reads already sent finish, so a track whose reads all went out in the first round still comes back whole.
  • keptsub sets it to 500 ms for whole reads, and for a window open to the end that Store.Window reads from the source after a kept copy fails. The time includes waiting behind the read's own 16 requests. Network storage answers in tens of milliseconds, and a disk that serves reads one at a time in up to a few hundred. The simulated mount in the runs below averaged 780 ms to 1.18 s, and the [bug] Web player stalls with an embedded PGS track on uncached remuxes on network storage #4199 reporter's storage took seconds per read.
  • Index-only tracks (Matroska S_TEXT/WEBVTT) have no demux to fall back to, so they don't give way.
  • Callers already demux on any error from the source: sidecars through demuxAndKeep, which keeps the track; jellycompat and translation through their own demux. The prefetch skips the track for an hour, as it does after any failure. A PGS window open to the end whose whole read gave way warms the cached .sup with the demux, as fix(playback): demux a subtitle window the file's index can't read in time #4272 does after a slow window, instead of reading through the index again. Burn-in staging and HLS rendition windows are unchanged: staging has its own 15-second bound, and a rendition window reads only the cues from its segment's start.

Validation

  • Reproduced live on an isolated dev sandbox. Direct play of a 7.3 GB, 40 Mbit/s H.264 file with PGS tracks, its library on a FUSE mount that models remote storage with a read cache: a new download stream waits 0.8 s and fetches 16 MiB past the read, all streams share 40 MB/s, and fetched blocks are cached. Each run started cold (no kept track, no cached subtitle, empty storage cache). A client read the media stream and requested the whole PGS track with a 30-second stall timeout and the web player's retry backoff, for 150 seconds. Builds alternated between main (7115ee702, which includes fix(playback): demux a subtitle window the file's index can't read in time #4272) and this change:

    Build Whole-track subtitle request Video received by 120 s, median of 5 (range) Storage after 150 s
    main 3–4 attempts, 0 bytes each 45.9 s (45.5–46.1) ~3.8 GB through ~435 download streams
    this change first byte at 9.5 s (9.2–10.3) 910.8 s (904.6–912.9) ~5.4 GB through 21 streams, 1 active

    The playback stream needs 120 s of video in 120 s to keep up.

    On main the server log shows each subtitle request abandoned after 30–50 s with 0 bytes, and the read handing the request to the demux after 90 s while it kept reading. With the change, the read gave way 5–7 s after the request, and the storage settled to one or two streams.

  • Playback starting 3.6 GB into the file, where the demux and the playback can't share the storage cache, three alternating runs each: main 43.4 s of video by 120 s (39.5–45.0) and no subtitle bytes; this change 458.4 s (418.4–461.3), with the first subtitle byte at 12.7 s (12.2–14.7).

  • On one more run the demux kept the track (method=ffmpeg, 559 packets, complete) about 3 minutes 40 seconds after the request, after its client had left. A second whole-track request then got the whole track in 0.1 s.

  • A new test drives a whole read over a reader that takes a second per read. With SlowRead the read stops after its first round instead of reading every cue, and a track whose reads all went out in that round still comes back whole. The test can't run on main, where the option doesn't exist; with the refusal disabled it fails (64 reads instead of at most 32). The live runs above are the proof of the fix. A new row in the slow-window test covers the PGS warm after an open-ended window gives way: it fails on the previous head (the warm read the whole track once more) and passes now.

  • Go build, vet, gofmt, go mod tidy -diff, make lint-changed, make lint-router-recovery, make verify-local-paths, make verify-size-ratchet, make verify-bidi and make verify-case-collisions pass. make test-go passes except TestCopyRemuxSubtitleRenditionRealFFmpeg and TestRunStatsWithRealFFmpeg, which fail the same way on main with the test host's FFmpeg 6.1. Two tests in packages that don't call the changed code (librarymonitor, streamtelemetry) failed once under the full suite's load and passed ten times in a row alone. Web and contract checks don't apply: nothing under web/ or contracts/ changed.

Evidence: https://evidence.siloserver.org/r/silo-server/keptsub-whole-track-slow-storage/

Risks

  • The threshold is a judgement. Storage that answers random reads slowly without fetching extra data now demuxes whole tracks the index would have read in a minute or two. Examples: a disk that serves one read at a time at 30 ms or more, or PGS over a link under about 25 Mbit/s. The demux doesn't starve playback, but a viewer far into a large file waits for it to reach their position.
  • The decision lasts for the rest of one read. A storage stall of more than half a second across a whole round sends that track to the demux. Nothing remembers the verdict: each new whole read of an unkept track repeats the first round (16 reads) before it gives way.
  • On a local disk that honours I/O priority, a prefetch reads at idle priority, and its reads can look slow while playback uses the disk. The prefetch then skips the track for an hour, and a request that joins it demuxes. Network filesystems ignore I/O priority.
  • On such storage, jellycompat and translation no longer keep tracks: their demux doesn't keep a copy, and the index read that used to keep one now gives way. The node's subtitle cache still serves repeats. Sidecar requests keep the track as before.

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.

Follow-ups

  • jellycompat's PlaybackInfo warm and the subtitle prefetch each pay one round of reads per text track before giving way on such storage. If that matters, the warm could stop asking after the first slow track.
  • Fallback demuxes of whole-track subtitle requests aren't counted by any admission limit, on main too (a file without Cues demuxes at once); this PR makes them start sooner on slow storage. A separate fix will bound concurrent foreground subtitle demuxes.
  • User manual: unaffected; it doesn't describe subtitle extraction or storage timing.

AI Disclosure

  • Harness: Claude Code in T3 Code
  • Tool(s): Claude Code; Codex (plan review)
  • Model(s): claude-opus-5-5 (implementation, plan review, code review); gpt-6.1-sol (plan review); claude-sonnet-5-5 (code review)
  • Involvement: Fully AI-generated; human review pending
  • Adversarial review: Before implementation, gpt-6.1-sol attacked the plan (its first attempt ended without a verdict and was retried once) and claude-opus-5-5 cut it. The round dropped planned changes to the prefetch and the jellycompat warm, moved the check to the start of each read so a finished track is never thrown away, covered the window that falls back to the source, and added the mid-file playback runs. A multi-agent code review at xhigh effort checked the diff, its callers, failure modes and conventions. It led to raising the threshold from 250 ms to 500 ms (queueing behind the read's own requests on disks that serve one read at a time), correcting the architecture doc for callers that don't keep tracks, and the risks above. A simplify pass ran on the change. A background adversarial review (gpt-6.1-sol, reviewed 5f73e2d) found nothing. A bot review then found that an open-ended PGS window whose read gave way warmed through the index again; fixed in 83ba1fe.

🤖 Generated with Claude Code

Note

Fall back to whole-track demux for embedded subtitles when random reads are slow

  • Adds ReadOptions.SlowRead and the ErrSlowReads sentinel in read.go. fileReader.ReadAt tracks the duration of its latest 16 reads in a ring; once their average exceeds the limit, new reads return ErrSlowReads while reads already in progress finish.
  • keptsub sets a 500 ms average latency threshold for whole reads (windows open to the end and whole-track copies), but not for IndexOnly tracks, which keep being read through the index as before. See store.go.
  • SubtitleCache.serveWindowedSUP in subtitle_cache.go treats ErrSlowReads like the existing slow-window error and falls back to demuxAndKeep.
  • Behavioral Change: indexed subtitle reads on slow storage now give up mid-track and fall back to FFmpeg demux instead of continuing; the tracker's error is sticky, so a track that trips the threshold will not retry indexed reading within that reader.

Macroscope summarized 83ba1fe.

…ads are slow

A whole read of an embedded subtitle track through the file's index makes
one random read per cue. On storage that fetches a large chunk for each
small read, such as a mount of remote storage with a local read cache,
that takes minutes and starves any playback stream reading the same file.

The read now gives way once its last 16 reads averaged over 500 ms: it
sends no more reads and returns ErrSlowReads, and the request demuxes the
source, which reads the file in order. Reads already sent finish, so a
track they complete is still read. A track only the index can read has no
demux to give way to and is read as before.

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

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

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).

  • Ask an admin to enable usage-based reviews

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Next included review available in 12 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

  • 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: 3e7183dd-28ac-4769-ae19-a900dea3d314
Commits

Reviewing files that changed from the base of the PR and between fd0d8c6 and 83ba1fe.

Files selected for processing: 8

  • docs/architecture/embedded-subtitle-index.md
  • internal/embeddedsub/embeddedsub.go
  • internal/embeddedsub/file.go
  • internal/embeddedsub/read.go
  • internal/embeddedsub/read_test.go
  • internal/keptsub/store.go
  • internal/playback/subtitle_cache.go
  • internal/playback/subtitle_source_test.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@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 ❌

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T05:32:11.735255Z 83ba1fe New commits
🔒 Security Review ✅ Completed 2026-11-15T05:35:03.977783Z 83ba1fe New commits

Security findings

Advisory findings (1)

ℹ️ 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-triage silo-triage Bot added bug Something isn't working impact: playback Can't play, stalls, or wrong media priority: P1 Core-path bug; painful workaround — next after P0 labels Nov 15, 2026
Comment thread internal/keptsub/store.go Resolved

@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 Security Review · Automatically triggered

Here are some automated security review suggestions for this pull request.

Reviewed commit: 5f73e2d990

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Comment thread internal/keptsub/store.go Resolved
…emux

A PGS window open to the end reads the track whole. When that read gives
way to slow reads, the warm it starts went through the index again, paying
the slow first round once more before it demuxed. Treat ErrSlowReads like
a slow window read and warm with the demux directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@silo-triage silo-triage Bot added size/L 200–499 added + deleted lines (all files); change volume, not priority. and removed size/M 50–199 added + deleted lines (all files); change volume, not priority. labels 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 ❌

@Quick104
Quick104 merged commit 3864d62 into main Nov 15, 2026
23 checks passed
@Quick104
Quick104 deleted the fix/keptsub-whole-track-slow-storage branch November 15, 2026 05:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working impact: playback Can't play, stalls, or wrong media priority: P1 Core-path bug; painful workaround — next after P0 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.

1 participant