Skip to content

fix(playback): cap concurrent subtitle demuxes of source files - #4318

Merged
Quick104 merged 1 commit into
mainfrom
fix/bound-foreground-subtitle-demuxes
Nov 15, 2026
Merged

Quick104 merged 1 commit into
mainfrom
fix/bound-foreground-subtitle-demuxes

Conversation

@Quick104

@Quick104 Quick104 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: none

Other clients: Apple Silo-Server/silo-apple#1183; Android Silo-Server/silo-android#940; Roku Silo-Server/silo-roku#86. Web N/A (its subtitle window and ASS loaders already retry failed requests)

When the file's index can't read a subtitle track (a Matroska file without Cues, an M2TS or another container the index doesn't read), Silo demuxes the whole source file with FFmpeg to get it. Nothing limited how many of those demuxes ran at once. A signed-in user who requested many uncached tracks or formats started one full-file read per request, and those reads starve the playback streams reading from the same storage. This change caps request-driven subtitle demuxes at four per server process. A request past the cap gets 503 with Retry-After: 5.

On a dev deployment, a 3 GB Matroska file without Cues sat on simulated slow storage. Eight concurrent whole-track requests for its eight subtitle tracks ran eight FFmpeg demuxes of the file at once for about 70 seconds. keptsub's admission (16 whole-track flights) counts only reads through the index. The demux it falls back to, the window fallbacks and jellycompat's text extracts all ran outside any limit, and streamFullTrack shares a demux only between requests for the same track and format.

Approach

The three subtitle caches in a process (native API, jellycompat, transcode node) share one set of four slots in internal/playback. A slot is taken just before FFmpeg reads the source, for:

  • the whole-track demux, holding the slot across its retry without the kept copy;
  • the text and PGS window fallbacks;
  • jellycompat's batch extract (one slot covers all of a file's text tracks) and its single-track fallback.

These take no slot: requests that join a running demux, conversions of kept or index-read packets, windows read from a cached .sup, background warms (they keep their two warm slots per cache), and AI translation jobs (bounded by their job slots).

Past the cap the request is refused at once with ErrSubtitleReadsBusy, the error keptsub already returns when its reads are full. The native, transcode-node and download routes already answer it with 503 and Retry-After: 5; jellycompat now does too, instead of a 500. Refusing instead of queueing keeps a burst of requests from holding connections, fill reservations and temp files while they wait. The new bound is documented in docs/architecture/embedded-subtitle-index.md and the jellycompat answer in docs/jellycompat-subtitles-api.md.

Validation

  • Live, dev deployment: the same eight-request burst on main ran 8 FFmpeg demuxes at once. On this branch it ran 4: four tracks answered 200 and four answered 503 with Retry-After: 5 within about a second. Requesting those four again once the first finished served all four, so finished demuxes free their slots.
  • Live, jellycompat: with four native demuxes running, a jellycompat Subtitles/{index}/stream.srt request on main started a fifth demux beside them. On this branch it answered 503 with Retry-After and the count stayed at 4.
  • Tests drive real blocked demuxes across two caches until one is refused. They fail on main (64 demuxes started, none refused) and pass here. Removing any one of the six admission points makes a test fail. The new tests passed 20 runs under -race.
  • make lint-changed: no issues. The playback, jellycompat, proxy, downloads, api/handlers and keptsub packages pass, except TestCopyRemuxSubtitleRenditionRealFFmpeg, which fails on main on the test host's FFmpeg 6.1.
  • Not checked: Apple, Android, Roku or Jellyfin clients against a saturated server.

Evidence

Evidence: https://evidence.siloserver.org/r/silo-server/bound-subtitle-demuxes/

Risks

  • A request refused after its response has started ends that response as a failed demux would, with no 503. This happens when a WebVTT whole track's index read runs past the 5-second keepalive, which has already sent header bytes, and when a fill's temp file fails and a viewer's own fallback demux is refused.
  • One user can still hold all four slots, either with whole-track requests on large files or by reading a large PGS window response very slowly (the rolling write deadline only cuts off a reader that stops completely). Playback I/O stays bounded, but meanwhile other users' uncached subtitles on files the index can't read answer busy.
  • Clients that don't retry a 503 lose the subtitle under load: Roku sidecars, and Apple and Android offline downloads, which drop the sidecar and don't fetch it again later. The same 503 already happens today when keptsub's reads are full. The parity issues above cover each one.
  • Four is a judgment, not a measurement. One full-speed demux can saturate a NAS link, so the cap keeps the count small while still letting a few first-time subtitle loads run together. Like keptsub's limits, it applies per process.
  • fix(playback): demux a whole subtitle track when the file's random reads are slow #4302 also edits docs/architecture/embedded-subtitle-index.md; whichever merges second needs a rebase.

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 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, client research)
  • Involvement: Fully AI-generated; human review pending
  • Adversarial review: Two plan rounds ran before and during implementation, with gpt-6.1-sol attacking and claude-opus-5-5 cutting. The first moved admission to the points where FFmpeg starts reading the source, kept the slot across the demux retry, replaced a slots-full test with one that drives real blocked demuxes, and surfaced the clients that don't retry a 503. The second rejected queueing jellycompat and committed WebVTT requests behind the cap, because the waiting requests would pile up without limit and starve native ones, so the change refuses everywhere and the risks above describe the cost. A multi-agent code review at xhigh effort (finders, a verifier, a sweep) led to the second round, a doc correction for AI translation, keeping the index read's failure in the refusal error, and the risks above. A simplify pass ran on the change. A background adversarial review (gpt-6.1-sol, reviewed a52b2c9) found nothing.

🤖 Generated with Claude Code

Note

Return 503 with Retry-After when subtitle demux limit is hit

  • Caps concurrent subtitle demuxes of source files; when a node is at its limit, jellycompat subtitle extraction now returns a retryable 503 with Retry-After instead of a 500
  • Adds jellycompat.writeSubtitleExtractError, which maps playback.ErrSubtitleReadsBusy to a 503 using playback.SubtitleReadsRetryAfter; all other extraction errors still return 500 (streams.go)
  • Updates docs to describe the source-demux bound, slot lifetime, and busy-response behavior (embedded-subtitle-index.md, jellycompat-subtitles-api.md)
  • Behavioral Change: ASS and SRT extraction failures in PlaybackHandler.HandleSubtitleStream can now return 503 ServiceUnavailable; clients that treated any failure as permanent should retry on 503

Macroscope summarized a52b2c9.

A whole-track subtitle request for a track the file's index can't read
(no Cues, an unsupported container) demuxes the whole source with FFmpeg,
and so do window fallbacks and jellycompat's text extracts. Nothing
counted these runs, so requests for many distinct tracks started as many
full-file reads at once.

Requests now share four demux slots per process across the native,
jellycompat and transcode-node subtitle caches. A demux past them is
refused with ErrSubtitleReadsBusy, which the native, node and download
routes already answer 503 with Retry-After; jellycompat now does too
instead of a 500. Requests joining a running demux, conversions of kept
or index-read packets, warms and AI translation jobs take no slot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@silo-triage silo-triage Bot added the size/L 200–499 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-15T07:24:52.832387Z a52b2c9 PR opened
🔒 Security Review ✅ Completed 2026-11-15T07:26:38.126834Z a52b2c9 PR opened
ℹ️ 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.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds a process-wide limit of four concurrent source subtitle demuxes. Subtitle extraction paths acquire admission before source reads, and jellycompat returns HTTP 503 with Retry-After when reads are busy.

Changes: Subtitle demux admission

Layer / File(s) Summary
Shared admission and source fallback
internal/playback/subtitle_source.go, internal/playback/subtitle_source_test.go, docs/architecture/embedded-subtitle-index.md
Source reads acquire one of four shared demux slots. Requests can join an existing demux without acquiring another slot. Tests cover shared limits, busy requests, joining, and slot release.
Window and batch extraction admission
internal/playback/subtitle_cache.go, internal/playback/subtitle_text_batch.go, internal/playback/subtitle_source_test.go, internal/playback/subtitle_text_batch_test.go
Window and batch extraction paths use admission. Busy batch errors return without invoking the single-track fallback. Tests cover busy window and batch requests.
Busy response handling
internal/jellycompat/streams.go, docs/jellycompat-subtitles-api.md
ASS and SRT handlers return HTTP 503 with Retry-After for busy extraction errors. Other extraction errors retain HTTP 500 responses.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant Jellycompat
  participant SubtitleCache
  participant admitDemux
  participant FFmpeg
  Client->>Jellycompat: request subtitle extraction
  Jellycompat->>SubtitleCache: extract subtitle
  SubtitleCache->>admitDemux: acquire source-demux slot
  alt slot available
    admitDemux-->>SubtitleCache: return release function
    SubtitleCache->>FFmpeg: run source demux
    FFmpeg-->>SubtitleCache: return extraction result
    SubtitleCache->>admitDemux: release slot
    SubtitleCache-->>Jellycompat: return extraction result
    Jellycompat-->>Client: return subtitle response
  else limit reached
    admitDemux-->>SubtitleCache: return ErrSubtitleReadsBusy
    SubtitleCache-->>Jellycompat: return busy error
    Jellycompat-->>Client: return HTTP 503 with Retry-After
  end
Loading

Suggested reviewers: rhainland, zzebrahz

Merge Risk: Minimal · up to a52b2

This change caps concurrent subtitle demuxes at four per server process and returns a retryable 503 when the cap is reached, instead of letting demuxes pile up. No merge-blocking issue was found. Clients that receive 503 may behave differently, and the limit of four is not a measured value.

Security architecture risk: Minimal · up to a52b2

The change reduces resource-exhaustion exposure by limiting concurrent subtitle extraction. The inspected admission, cleanup and response paths show no introduced or worsened risk that materially weakens security or failure containment.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — Foreground capacity is shared across users served by the process, without per-user reservations. Background warms remain outside this pool, and an uncached PGS window can start a separately admitted warm before its foreground request is refused. This pre-existing warm behavior means four is not a ceiling on all source-reading work.

Trust Boundaries and Controls

  • observed — The packet-input exemption does not make FFmpeg read InputPath: that branch supplies a subtitle-only stream through pipe:0 and returns its arguments before adding the source-file input. It is distinct from a source-demux admission bypass.

Resilience and Maintainability Implications

  • observed — A WebVTT keepalive can emit bytes before fallback admission is attempted, so not every refusal can become HTTP 503. The native handler maps busy to 503 only while the response is unstarted and otherwise aborts a failed stream rather than deliberately completing a partial subtitle successfully.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a cap on concurrent subtitle demuxes of source files in the playback package. The changeset adds a process-wide limit of four concurrent FFmpeg source demuxes shared a…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is specific and relevant to the changeset. It explains the problem (uncapped FFmpeg whole-track subtitle demuxes starving playback I/O), the approach (a shared four-slot admission boun…
Full details: Title check

Explanation

The title names the main change: a cap on concurrent subtitle demuxes of source files in the playback package. The changeset adds a process-wide limit of four concurrent FFmpeg source demuxes shared across subtitle caches, with refusals returning ErrSubtitleReadsBusy. The 'fix(playback)' prefix is a conventional-commit style scope and type, which is clear and specific.

Full details: Description check

Explanation

The description is specific and relevant to the changeset. It explains the problem (uncapped FFmpeg whole-track subtitle demuxes starving playback I/O), the approach (a shared four-slot admission bound across the native, jellycompat, and transcode-node subtitle caches), and the change to jellycompat (503 with Retry-After instead of 500). It also lists the affected files' documentation, validation steps, and risks, all of which match the raw summary of changes.

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: f1014bf1-37c5-428c-9f7c-42613fc2a40f
Commits

Reviewing files that changed from the base of the PR and between 06b2e82 and a52b2c9.

Files selected for processing: 8

  • docs/architecture/embedded-subtitle-index.md
  • docs/jellycompat-subtitles-api.md
  • internal/jellycompat/streams.go
  • internal/playback/subtitle_cache.go
  • internal/playback/subtitle_source.go
  • internal/playback/subtitle_source_test.go
  • internal/playback/subtitle_text_batch.go
  • internal/playback/subtitle_text_batch_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 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: P1 Core-path bug; painful workaround — next after P0 labels Nov 15, 2026
@Quick104
Quick104 merged commit e12193d into main Nov 15, 2026
18 checks passed
@Quick104
Quick104 deleted the fix/bound-foreground-subtitle-demuxes branch November 15, 2026 07:38
Quick104 added a commit that referenced this pull request Nov 19, 2026
Resolve conflicts with main's subtitle demux admission (#4318) and its
test pruning (#4465). serveWindowedSUP keeps this branch's rooted
Lookup and main's demux admission and slow-read warm. The refused-dir
kept-copy check moves to its own test, since main removed the table it
was a row of.

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: 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