Skip to content

ABR: the lookahead fetch is bounded by the stall guard like every active fetch - #209

Merged
GLinnik21 merged 3 commits into
mainfrom
abr-prefetch-guard
Sep 23, 2026
Merged

GLinnik21 merged 3 commits into
mainfrom
abr-prefetch-guard

Conversation

@GLinnik21

Copy link
Copy Markdown
Collaborator

Why

On the TV, pipe_abr_down_collapse failed on main (28da566). It was 1 of 23 ABR cases; the other 22 passed. The link drops from 40 Mbps to 500 kbps while the controller is on rung 20000. Segment 11 (5.4 MB) then took 86.8 s, and the picture froze for 84.5 s (vgap=84522ms). Playback climbed 32 s where the case needs 45 s.

The current-rung abort rule, StallGuard, never fired: the log has no abr: stall abort line. The segment was fetched by the same-encoder lookahead that #89 added. hls_prefetch_same_encoder called hls_demux_segment(…, ReserveDeadlineState::new(None, false), None), which meant no stall guard at all. On any error it also requeued the segment and returned Ok(None), so an abort would have been swallowed too. The ordinary fetch, a few lines above, always armed the guard for the same cursor.

What

  • One required acquisition policy. hls_demux_segment used to take two independently optional arguments, and passing "neither" compiled. It now takes one SegmentAcquisition. The type lives in a child module (ff_acquisition.rs) whose inner enum is private, so ff cannot build one by hand.
    • Cursor fetches: for_cursor(Option<&abr::Controller>) is the only constructor. It reads the live reserve, floor status and hold flag itself, and arms the guard through the existing arm_active_stall_guard.
    • Candidate transfers: candidate(deadline), behaviour unchanged. All four callers are migrated.
    • Residual trust: choosing the role still relies on each caller passing the controller it holds. Every production caller does.
  • One abort handler. The ordinary branch's StallAbort handling was extracted into hls_stall_abort_outcome, and the lookahead now goes through it as well. Both paths requeue once, feed nothing, leave the timeline alone, and record the sample as .abandoned().
  • Tests.
    • The lookahead's control flow (hls_prefetch_same_encoder_with) is driven with a fake demux. The test went red under both reverts: making the lookahead unguarded again, and swallowing the abort again.
    • A second test checks that an unknown or zero reserve, and an already-held clock, still leave the guard unarmed.
    • A read_cb-level test covers the abort itself.
  • Not in scope, noted in code: the guard still does not cover the HTTP open or the NotReady wait, and it does not wake a read that is already blocked when a hold starts. The ordinary fetch has always had these gaps, and they did not cause this failure.

Verification

On the TV, with the final build deployed and verified by md5 before the run:

  • All 23 ABR cases pass: pipe_abr* 21/0 and pipe_auto_* 2/0.
  • pipe_abr_down_collapse: abr: stall abort seq=11 … reserve=5626ms … cause=terminal_reserve. The freeze drops from 84.5 s to 5.8 s, and playback reaches pos=111 s.
  • down_staircase and down_outrun still pass.

Committed tree: make check on 4b40837: test result: ok. 4199 passed; 0 failed, ok. 4250 passed; 0 failed, ok. 101 passed; 0 failed.

  • --no-default-features: clean.

This change touches no FFI or linkage. The doc-claim audit found one false claim, a test's own doc comment, and it is fixed here.

🤖 Generated with Claude Code

GLinnik21 and others added 3 commits September 23, 2026 13:00
… active fetch

pipe_abr_down_collapse (device): the link collapsed from 40 Mbps to 500 kbps while
the ABR controller sat on rung 20000. Segment 11 (5.4 MB) then took 86.8 s and froze
the picture for ~84 s, because hls_prefetch_same_encoder's same-encoder lookahead
called hls_demux_segment(..., ReserveDeadlineState::new(None, false), None) — no
stall guard at all — while the ordinary branch two lines above always armed one via
arm_active_stall_guard. Both requests read the SAME active cursor at the SAME rung,
but only one of them was ever able to observe the terminal-reserve boundary.

Fix: hls_demux_segment now takes one required, role-typed SegmentAcquisition instead
of the independently-optional (ReserveDeadlineState, Option<StallGuard>) pair that let
"no reserve deadline AND no stall guard" compile at all. The Active role is opaque and
buildable only through SegmentAcquisition::active, which performs the existing
arm_active_stall_guard evaluation from live inputs (buffer reserve, at_floor, the
shared terminal-hold flag) — so no active-cursor fetch, ordinary or lookahead, can
skip it. Active vs Fixed is derived from hls_demux's own adaptive Option, not chosen
per call site. Candidate warm-up/repeatable fetches move to a Candidate role that
still carries no StallGuard, unchanged behaviorally.

hls_prefetch_same_encoder's StallAbort is no longer swallowed by requeue-and-Ok(None)
or allowed to escape via `?`: it is routed through hls_stall_abort_outcome, the same
reducer the ordinary branch's StallAbort handling was extracted into, so both paths
log, requeue exactly once, and hand back an abandoned round identically. The
`prefetched` slot gained the abandoned-flag it was missing to carry that through.

Regression test (red on old code, green after): drives the production read_cb
callback under the ordinary policy and under the lookahead's actual policy against an
identical terminal-hold-with-bytes-remaining condition; pre-fix the lookahead policy
(hard-coded None) reads straight through the hold, post-fix it is built via
SegmentAcquisition::active — the same constructor hls_prefetch_same_encoder now
calls — and aborts identically. Also adds a unit test that the Active constructor
preserves arm_active_stall_guard's unarmed exclusions (unknown/zero reserve,
already-held clock).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hat the test drives

Round-2 review found the first fix incomplete on both must-fixes.

Must-fix 1 — the bypass was still representable. `SegmentAcquisition` and
`ActiveAcquisition` lived in `ff` itself, so anything in that one module could
still build `ActiveAcquisition { stall: None, .. }` by hand, or call the
private `SegmentAcquisition::Fixed`/`fixed()` shortcut, without ever running
`arm_active_stall_guard`. Moved both into a new child module
(`ff_acquisition.rs`, included the same way the `ff_*_tests.rs` files are)
whose payload lives in a private inner `Policy` enum `ff` cannot name — the
only way `ff` can produce a `SegmentAcquisition` at all now is this module's
`pub(super)` surface (`for_cursor`, `candidate`, `stall`, `into_parts`, and a
`#[cfg(test)]` `for_test`). There is no longer a `fixed()`: non-adaptive is
`for_cursor(None)`. `for_cursor` also stopped taking a caller-supplied
`at_floor: Option<bool>` — it now takes the authoritative adaptive context
itself (`Option<&abr::Controller>`, exactly what `hls_demux`'s own `adaptive`
holds) and reads the live buffer reserve and rebuffering hold itself, inside
the call, so a caller cannot hand it stale or fabricated inputs. Both the
ordinary fetch and the lookahead call this one constructor with the same
context.

Must-fix 2 — the regression test built its own `SegmentAcquisition` and drove
a hand-built `AvioState`/`read_cb` directly, so it could not see whether
`hls_prefetch_same_encoder`'s actual control flow ever reached that
construction. Split the lookahead into `hls_prefetch_same_encoder_with`, a
generic inner fn taking the demux call as an `FnOnce(&Segment, &mut
SegmentClock, SegmentAcquisition) -> Result<HlsSegmentOutput, HlsExit>`, and
a thin `hls_prefetch_same_encoder` wrapper that closes over the real
`hls_demux_segment`. The new test drives the inner fn with a fake demux that
records whether the acquisition it received was armed and returns
`HlsExit::StallAbort` on demand, then asserts the shared reducer's outcome:
`fetch_abandoned=true`, zero AUs, the segment requeued exactly once — never
`Ok(None)`. Proved as a real regression test: reverting the constructor call
to `for_cursor(None)` (the old unconditional-`None` bypass) turned it RED
(`left: Some(false), right: Some(true)`); reverting the `StallAbort` handling
to the old push_front+`Ok(None)` swallow also turned it RED (`a StallAbort
must still produce a round, never Ok(None)`); both are GREEN again restored.
Also adds `for_cursor_with_no_controller_is_unarmed` and renames the existing
constructor calls in the two prior tests from `active` to the `#[cfg(test)]`
`for_test` escape hatch the sealing now requires.

Small: the `for_cursor` doc corrected a wrong claim from round 1 — FFmpeg's
probe reads DO go through the guarded AVIO `read_cb`, so probing is covered;
the actually-uncovered legs are the HTTP open/connect+headers leg and the
`NotReady` wait inside `hls_demux_segment`, both of which precede any AVIO
read, plus a transport read already blocked when a hold transition arrives.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 23, 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-09-23T11:50:36.458438Z 4b40837 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.

@GLinnik21
GLinnik21 merged commit 15aef22 into main Sep 23, 2026
5 checks passed
@GLinnik21
GLinnik21 deleted the abr-prefetch-guard branch September 23, 2026 11:57
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