Skip to content

fix(playback): bound the Cues a Matroska keyframe index reads - #2492

Merged
Quick104 merged 1 commit into
mainfrom
fix/issue-2487-cues-memory
Oct 14, 2026
Merged

Quick104 merged 1 commit into
mainfrom
fix/issue-2487-cues-memory

Conversation

@Quick104

@Quick104 Quick104 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

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

A crafted Matroska file could make one keyframe-index read allocate about ten times the size of its Cues element: around 250 MiB for 16 MiB of Cues, and close to 1 GiB at the old 64 MiB limit, even when the read then failed. Anyone who can add a file to a library could trigger that each time playback loads the file's keyframe index, and #2484 adds a second caller for seek previews. This change keeps every read to at most about three times the Cues size and lowers the Cues limit to 16 MiB. Normal files read the same as before.

The cost came from the parser itself. It grew the list of cue points one append at a time, and before reading any cluster it built a map of every cue time and another of every cluster position. The background full check built two more maps of the same size. Found in review of #2484.

Approach

cuePoints now walks the Cues twice: once to count the video cue points, then again to fill sorted, deduplicated lists of cue times and cluster positions, each allocated once. The coverage check and the full check look keyframes up in the sorted times by binary search instead of in maps. Which clusters get sampled, what counts as covered, and every error stay the same.

maxCuesSize drops from 64 MiB to 16 MiB. FFmpeg writes a video cue point in 17 to 24 bytes, so 16 MiB holds about 700,000 points: every frame of 8 hours at 24 fps, or of 3 hours at 60 fps. A film with a keyframe every few seconds has Cues well under a megabyte. A file over the limit gets the existing size error, and playback uses FFmpeg's own playlist, as it already does for files without Cues. A rare case could change: very long high-frame-rate all-intra files, or files whose Cues also index many other tracks.

Only internal/playback/keyframes changes. There are no client, API, or jellycompat changes.

Validation

  • A new test reads 16 MiB of small cue points, each with its own time and cluster position, and requires at most 4 times the Cues size in allocations. On main it allocates 247 MiB and fails. With this change it allocates 31 MiB.
  • The oversized-Cues test now also reads a well-formed file just under 16 MiB, which still gives the right keyframes, and the same file with one more cue point, which gets the size error. On main the second file reads without error and the test fails.
  • go test ./internal/playback/... passes, including the tests that compare the index with ffprobe and with FFmpeg's HLS cuts.
  • make lint-changed, go build ./..., gofmt -l ., go vet ./..., and make verify-local-paths pass.
  • make test-go passes except two internal/mediasample tests that run real FFmpeg (TestRunStatsWithRealFFmpeg, TestRunSheetsOpenGOPHEVCWithRealFFmpeg). They fail the same way with main's keyframes code on the same machine, so the local FFmpeg build is the cause.
  • Not reproduced on a deployment: this is a parser limit, and the focused tests above are the proof.

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

Evidence

No user-visible change. The evidence page linked under Validation holds the test results.

Risks

A Matroska file with more than 16 MiB of Cues now falls back to FFmpeg's playlist instead of the planned keyframe playlist. Normal library files have Cues far below that.

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 CLI in T3 Code
  • Tool(s): Claude Code; Codex CLI for the plan review
  • Model(s): claude-opus-5-5 (implementation); gpt-6.1-sol (plan review)
  • Involvement: Fully AI-generated, human verified
  • Adversarial review: before implementation, gpt-6.1-sol at xhigh reasoning reviewed the plan read-only through the Codex CLI. Its first attempt could not read the source and was retried. It confirmed that cluster sampling and the full check's cue matching stay the same, and it found that the plan would have dropped only -1 cluster positions while main skips every negative one. The change now skips every negative position. It agreed with the 16 MiB limit and pointed out that cues for other tracks make real Cues larger than a video-only count, which the Risks section covers. A background adversarial review by Codex (gpt-6.1-sol) of 4f156cb found nothing to report: the playback tests passed, and 40,000 differential comparisons against the previous parser matched.

🤖 Generated with Claude Code

Note

Bound the Cues a Matroska keyframe index reads to 16 MiB

Lowers the maximum accepted Matroska Cues element size from 64 MiB to 16 MiB in matroska.go, so memory use stays bounded when reading a keyframe index.

  • cuePoints now counts cues in a first pass, allocates its result slices once at final capacity, and returns sorted, duplicate-free cue times and cluster positions.
  • Coverage checking and the full-index verifier use binary search over the sorted cue times instead of building maps.
  • Adds tests for the 16 MiB boundary and for allocation cost, requiring total allocations during a read to stay within 4x the Cues byte size.
  • Behavioral Change: files with Cues elements between 16 MiB and 64 MiB are now rejected; cuePoints and checkCueCoverage signatures changed from cue-point structs to parallel slices.

Macroscope summarized 4f156cb.

A crafted Matroska file could make one keyframe-index read allocate
about ten times its Cues element, up to roughly 1 GiB at the old 64 MiB
limit. The reader grew a slice of cue points one append at a time and
built maps of every cue time and cluster position before checking any
cluster.

Count the video cue points first and allocate the sorted cue times and
cluster positions once, look keyframes up by binary search instead of
in maps, and lower the Cues limit to 16 MiB. FFmpeg writes a video cue
point in 17 to 24 bytes, so 16 MiB covers every frame of 8 hours at
24 fps or 3 hours at 60 fps. A read now costs at most about three times
the Cues size.

Closes #2487

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

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

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T22:00:04.494439Z 4f156cb PR opened
🔒 Security Review ✅ Completed 2026-10-14T22:01:36.658910Z 4f156cb 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.

@silo-triage silo-triage Bot added bug Something isn't working go Pull requests that update go code priority: P2 Limited scope, workaround exists, or polish labels Oct 14, 2026
@macroscopeapp

macroscopeapp Bot commented Oct 14, 2026

Copy link
Copy Markdown

The playback playlist can change for Matroska files whose Cues exceed 16 MiB: internal/playback/keyframes/matroska.go now rejects the index, causing playback to fall back from the planned keyframe playlist to FFmpeg’s playlist. The linked page should show before-and-after playback or playlist behavior for the same oversized-Cues file, not only parser and allocation test results.

Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness.

Posted via Macroscope — Visible change evidence

@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bad90aa2-7b84-4bc3-89bd-de7c4a8fc9e1
📥 Commits

Reviewing files that changed from the base of the PR and between ece16f8 and 4f156cb.

📒 Files selected for processing: 3
  • internal/playback/keyframes/coverage.go
  • internal/playback/keyframes/matroska.go
  • internal/playback/keyframes/matroska_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 0 remain after this review.

📝 Walkthrough

Matroska keyframe indexing now limits Cues elements to 16 MiB, stores sorted and deduplicated cue ticks, and checks cue coverage using binary search. Tests cover the size boundary and memory allocation during reads.

Changes: Matroska cue indexing

Layer / File(s) Summary
Parse and compact Matroska cues
internal/playback/keyframes/matroska.go, internal/playback/keyframes/matroska_test.go
Cues parsing now returns sorted, deduplicated ticks and cluster positions. The size limit is 16 MiB. Tests cover boundary sizes and allocations for large Cues elements.
Check cue and keyframe coverage
internal/playback/keyframes/coverage.go
Coverage checks search sorted cue ticks and track distinct matched entries. Missing keyframe or cue matches return ErrIncompleteIndex.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: rangodj

Merge Risk: ⚪ Minimal · up to 4f156

No actionable issue remains in the reviewed changes; the PR is mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Issue #2487 requires bounded memory for Matroska keyframe-index reads in internal/playback/keyframes. The diff lowers maxCuesSize from 64 MiB to 16 MiB, counts cue points before allocation, allocates …
Out of Scope Changes check Passed The diff changes only internal/playback/keyframes and its tests. The coverage changes, parser refactor, size-limit change, and tests directly support issue #2487. No unrelated client, API, or jellycom…
Description check Passed The description clearly explains the memory-allocation problem, the Matroska Cues parser changes, the 16 MiB limit, risks, and validation results. It directly relates to the changeset.
Title check Passed The title clearly and concisely identifies the primary change: bounding the Cues data read by the Matroska keyframe index.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • 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.

@Quick104
Quick104 merged commit 7fadc3c into main Oct 14, 2026
16 checks passed
@Quick104
Quick104 deleted the fix/issue-2487-cues-memory branch October 14, 2026 23:02
rangoDJ added a commit to rangoDJ/silo-server that referenced this pull request Oct 15, 2026
Silo-Server#2492 split cuePoints into a counting walkCuePoints and a single
allocation. Take that structure; the walk keeps this branch's rule that a
video cue with a CueReference or a nonzero CueCodecState makes the Cues
incomplete.

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 priority: P2 Limited scope, workaround exists, or polish

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Reading a Matroska file's keyframe index can allocate hundreds of megabytes

1 participant