Skip to content

fix(moq-video): a failed drain keeps its boundary, colour switches stay bounded, and capture drains its encoder - #9061

Merged
kixelated merged 5 commits into
mainfrom
quest/m1/precut-fixes/video-decode-lifecycle
Nov 18, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m1/precut-fixes/video-decode-lifecycle

Conversation

@kixelated

@kixelated kixelated commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Closes the video-decode-lifecycle quest from the 2026-11-17 pre-cut audit. Each finding was reproduced with a test before it was fixed.

  • A failed drain keeps its boundary (decode/consumer.rs). At a Gap or End, self.drain().await? returned early and lost the consumed marker: no Read::Gap or Read::End, and no leading-picture recovery after a gap. Now the drain's result is held while the drained pictures, then the boundary, get queued and recovery gets armed. Only after that is the error returned. drain() also queues what a failed flush readied, so the track-end branch now shares it instead of keeping its own copy.
  • The threaded decode sink collects after a failed flush (decode/sink.rs). Found while reproducing the first bug. A flush that readied pictures and then failed left them in the backend. The next decode then pushed them out behind the boundary, or they were lost at track end.
  • Colour switches stay bounded (decode/decoder.rs). A switch is now recorded only after the backend accepts the keyframe; a refused one yields no picture to reach the switch. A new keyframe drops the switches at or after its timestamp, which only backwards timestamps leave behind. The queue is capped at SWITCHES = 32, the same sizing as the consumer's ARRIVALS. At the cap the oldest switch is applied early instead of kept.
  • Capture drains its encoder before dropping it (encode/capture.rs capture_loop, after merging moq-dev/moq#8964). A source end, a resize, and an on-demand release used to call Producer::release(), which drops the encoder without flushing. Now they call Producer::discontinuity(), which drains the tail into the track and then marks the break, and still marks it if the drain fails. If a drained frame moves the rendition to a new track, capture follows it there, as the in-loop path already does. release() is now used only after an encode was cancelled mid-call, since the sink refuses every call after that. Its doc now says so.

Test-only additions: probe-failing-flush, a decode probe whose flush readies its held picture and then fails. probe-pipelined, an encode probe that wraps OpenH264 and holds back one access unit, like Media Foundation. The H.265 SPS fixtures moved to vui.rs constants so the decoder test can share them. The probe-holding backend now also accepts H.265.

Public API: none. Wire: none.

Decisions

  • At the cap, the oldest switch is applied early instead of being dropped. Under hostile input this can label a few early pictures in the newer space. Dropping it would leave them labelled in a space even older than their own.
  • Every exit from the capture loop drains, except after an encode cancelled mid-call. A failed drain fails the loop, as a failed encode already does.

Validation

  • a_failed_drain_keeps_its_end and a_failed_drain_keeps_its_gap_and_recovery failed before the fix: the Gap and End were lost, leading pictures 2800 and 2900 leaked through, and the track-end tail was lost. They pass after it.
  • color_switches_stay_bounded: alternating spaces on 100 keyframes no picture comes back for stay at 32 entries; backwards timestamps keep at most one.
  • a_source_end_publishes_the_frames_the_encoder_holds: with the drain reverted, it fails in 38ms ("the encoder's last frame was never published"). With the drain it passes.
  • clock::a_drained_frame_that_moves_the_track_is_followed (from review round 1): without following the move, it fails fast because capture ends while watching the old track. With it, it passes.
  • After the merge, just test -p moq-video --features capture,openh264: 362 passed. Swapping discontinuity() back to release() fails both capture tests. Removing the track-follow fails a_drained_frame_that_moves_the_track_is_followed. just check: passed. Its first run hit the known libmoq test::server_accepts_a_session flake, which passed on the re-run.

Caused by

  • moq-dev/moq#6475 - drain().await? at a Gap or End dropped the consumed marker on error (found by reviewer, the pre-cut audit)
  • moq-dev/moq#5808 - the colour-switch queue had no bound (found by reviewer, the pre-cut audit)
  • moq-dev/moq#8372 - the threaded decode sink skipped collecting after a failed flush (found by check, this PR's reproduction)
  • The capture loop has dropped its encoder unflushed since it was written; moq-dev/moq#3214 only made the drop explicit, and moq-dev/moq#8964 carried it into release() (found by reviewer, the pre-cut audit)

Follow-ups

None.

Models

Size: S
Implementer: Claude Code, claude-opus-5-5, unknown effort (arm A)

Review

Rewritten by quest review after each round.

  • Head: 359274b

  • Base: origin/main at 928d5f3

  • Risk: medium, the base has no RISK.md to name what is always high or customer-facing, so every change is medium

  • Classified: medium by default

  • Reviewer: Codex, gpt-6.1-sol, medium effort

  • Session: 01a1ed14-ed3b-78f1-9c13-674e1017b854, round 3

  • Score: 10/10, The merge preserves encoder draining, break marking, and demand tracking through the new Producer API; source-only review found no new issues, and no builds or tests were run.

  • Rounds: 3; first round 1 (medium 1); findings 1 (medium 1): fixed 1, refuted 0, open 0

  • Findings: none

kixelated and others added 3 commits November 17, 2026 19:49
…s stay bounded

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title moq-video keeps a boundary through a failed drain and bounds its colour queue fix(moq-video): a failed drain keeps its boundary, colour switches stay bounded, and capture drains its encoder Nov 18, 2026
kixelated and others added 2 commits November 17, 2026 21:18
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…uity

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated marked this pull request as ready for review November 18, 2026 05:33
@coderabbitai

coderabbitai Bot commented Nov 18, 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 7 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: fca0e095-c3d1-4753-8b55-f441e4c9f063
Commits

Reviewing files that changed from the base of the PR and between a978267 and 359274b.

Files selected for processing: 11

  • quest/m1/precut-fixes/video-decode-lifecycle.md
  • rs/moq-video/src/decode/backend/mod.rs
  • rs/moq-video/src/decode/backend/probe.rs
  • rs/moq-video/src/decode/consumer.rs
  • rs/moq-video/src/decode/decoder.rs
  • rs/moq-video/src/decode/sink.rs
  • rs/moq-video/src/decode/vui.rs
  • rs/moq-video/src/encode/backend/mod.rs
  • rs/moq-video/src/encode/backend/probe.rs
  • rs/moq-video/src/encode/capture.rs
  • rs/moq-video/src/encode/producer.rs
  • 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

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Approach: sound — each fix lands at the seam that owns the invariant (the consumer's drain, the decoder's switch queue, the capture loop's encoder lifetime), and every one is reproduced by a probe backend that fails the way a real codec does rather than by a mock.

Reviewed changes

  • A failed drain keeps its boundary (decode/consumer.rs): the Gap and End arms hold the drain's result, queue the drained pictures, then the boundary, arm recovery for a gap, and only then report. drain() now collects after a failed flush and the track-end branch shares it, so readied pictures are never lost behind a boundary.
  • Threaded decode sink collects after a failed flush (decode/sink.rs): the worker collects before replying, so a flush that readies a picture and then fails no longer leaves it to surface behind the next boundary.
  • Colour switches stay bounded (decode/decoder.rs): a switch is recorded only once the backend accepts the keyframe, switches at or past a new keyframe's timestamp are dropped, and the queue is capped at SWITCHES = 32 with the oldest applied early.
  • Capture drains its encoder (encode/capture.rs): every normal exit from the inner loop now calls Producer::discontinuity() — except after a cancelled encode, which poisons the sink — and capture follows a rendition the drain moved to a new track.
  • Test-only plumbing: probe-failing-flush, probe-pipelined (an OpenH264 wrapper holding one access unit), shared H.265 SPS fixtures, and probe-holding accepting H.265.

Verified locally: the full moq-video lib suite passes with capture,openh264 (291 passed, including all four new tests), the crate's tests also compile with capture alone, and rustfmt --check is clean. I also read every in-tree decode backend's flush to confirm that receive() after a failed flush is safe (each either discards its tail or reports a clean error, and the worker only poisons on a cancelled call).

Pullfrog  | View workflow run | Using deepseek-v4.1-flash | 𝕏

@kixelated
kixelated added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit ef0de9c Nov 18, 2026
10 checks passed
@kixelated
kixelated deleted the quest/m1/precut-fixes/video-decode-lifecycle branch November 18, 2026 06:19
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.

1 participant