Skip to content

Don't count a request for a packet that was only late as loss (851-4501) - #1515

Merged
pondorasti merged 9 commits into
mainfrom
alexandru/851-4501-spurious-loss
Nov 28, 2026
Merged

pondorasti merged 9 commits into
mainfrom
alexandru/851-4501-spurious-loss

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Fixes 851-4501

On the 50 Mbit/s lab network (25 ms ± 5, 0.1% loss) the host's rate estimate collapsed to about 3 Mbit/s for the rest of a run. This PR stops the host from counting a request for a packet that was only late as a loss.

What was wrong

A pause in arrivals during a large frame makes the viewer ask for packets that are still on their way: 30–170 of them in the traced runs. The host counted each request as a loss, so its one-second loss window read 13–32% on a link that loses 0.1%. The heavy-loss cut then threw the measured rate away, and the estimate fell to the next small sample, about 3 Mbit/s. In 16 traced local runs of fallback scroll on that network, 5 of 8 collapsed on main.

What changes

All of it is in the Rust transport (codevisor-media).

  • The viewer reports late originals. For each frame, it notes the packets it asked for whose first copy came within half the session's lowest round trip of the request. That is too soon for a resend. It reports them with the frame's receipt as up to eight index ranges, behind a new feedback flag.
  • Each frame belongs to the loss window it was sent in. A frame sent after a window's second is up counts, with the losses its requests name, in the next window. An ended window waits for the reports of its own frames whose losses could still be answered, at most until it is 2 s old.
  • The host takes late originals back from the window they were charged to, from the FEC trigger, and from the arrival count's lost bytes. A packet it never charged can't cancel a real loss.
  • Requested bytes reach the estimator's drop check only once their frame is answered, dated when they were requested. Late ones never count. An answered frame's bytes land no earlier than 250 ms before the answer (or the last send), so a slow answer still counts. A frame given up keeps the request times, so an outage's old losses don't read as a fresh burst.
  • The FEC trigger counts each frame's requested losses apart, so a frame whose losses were all only late stops keeping FEC on, without touching another frame's.
  • Some losses always stand. A frame stays a loss when its last packet was asked for while a tail probe's copy was on its way (that copy can arrive early), or when a repeat had to be sent again.
  • Mixed versions keep working. An older viewer sends no ranges, and the host counts as before. An older host ignores the flag and the bytes after it. Feedback naming more than eight ranges keeps the first eight; feedback cut short keeps none. Neither is dropped.

The crate README has the details.

Lab: 50 Mbit/s network, six repetitions, same slot

Main at 1a74aba against this branch at d628636 (before the last commit, which only affects lost bytes charged in the window before a roll). Runs 46074689338/46075450387 (fallback) and 46077293111/46078087630 (shipped).

Six runs each Main This PR
Fallback scroll: freezes 4 4
Fallback scroll: runs with the estimate collapsed 3 0
Fallback scroll: gaps over 250 ms (time) 2 (0.6 s) 2 (0.8 s)
Shipped scroll: freezes 8 0
Shipped scroll: runs collapsed 4 1
Shipped scroll: gaps over 250 ms (time) 3 (0.9 s) 0
Fallback video: freezes 1 0
Shipped video: freezes 5 3
Video: gaps over 250 ms, both encoders 3 (0.9 s) 2 (0.7 s)
Gaps over 500 ms, every scene and encoder 0 0

Median updates/s and latency are unchanged or better: shipped video p95 64.9 → 59.4 ms.

Low estimate at the start of some video runs: didn't repeat. In the pair above, 4 of 12 video runs with this PR started at 5–7 Mbit/s and none on main. Two video-only same-slot pairs followed, both encoders and six repetitions each, with the starting estimate (lowest in the first 6 s) per run:

Pair Main This PR
At 3a1ff75 (runs 46084645076/46085069490, 46085477941/46085935860) 0 of 12 under 10 Mbit/s, lowest 11.9 0 of 12, lowest 15.8
At e252d69, after your review's fixes (runs 46090048403/46090482935, 46090894352/46091320970) 0 of 12, lowest 21.1 0 of 12, lowest 21.4
Shipped video freezes, second pair 8 2
Shipped video gaps over 250 ms, second pair 5 1

No gaps over 500 ms anywhere. So the earlier 4 of 12 was chance. The second pair ran one commit before the head (b8c0566). That commit only changes when held losses of frames resolved early, or given up, reach the drop check.

At a session's start, this PR covers least: the first keyframe's queue can hold its packets past half the lowest round trip, and those aren't recognised as late.

The 1 Mbit/s hitches are a different path

The question from #1510 was whether the extra 250–500 ms gaps on the 1 Mbit/s network come from this spurious-loss path. They don't. In 4 interleaved local traced runs per side (fallback scroll on N7):

  • Only 2–9 of each run's 17–36 requested packets were merely late. The rest were the network's 5% loss.
  • With this fix, gaps over 250 ms were 20–27 per run against 18–25 on main, 92 against 85 in total.

These are local runs, not lab numbers.

Tests

New unit tests in sender.rs, estimator.rs, arrived.rs and wire.rs, each checked to fail without its fix. They cover:

  • Take-back: a late original comes off the window, the FEC trigger and the arrival count. Requested bytes reach the drop check only when answered, and late ones never do.
  • Overlapping frames: two frames both only late leave no loss, in either report order. One frame's late originals don't clear another frame's real loss.
  • Losses that stand: a probed last packet and a resent repeat.
  • Windows:
    • A frame sent after its window ended counts with its losses in the next window.
    • An ended window waits for a request that came during its wait.
    • A frame resolved by a later report holds the window, and is confirmed only by its own report or after the grace without it.
    • A parity repair counts in the window its frame was sent in.
  • Drop check timing: a loss answered late, or after the last send, still lands where the drop check looks. Given-up frames keep their request times.
  • Restarts and wire: the arrival count's give-back skips a count that restarted. Late ranges are sorted and deduplicated, and more than eight ranges, or feedback cut short, still decode.

Sim tests in tests/core.rs cover a late keyframe tail at any moment of the window, a real tail loss still counting, and cross-window cases. No existing assertion is loosened. Three earlier unit tests that expected requested bytes in the estimate at once now expect them on the answer.

Run locally (M1 Pro, with timeouts):

  • Rust: cargo test -p codevisor-media (78 unit, 63 sim, 6 session tests), plus clippy.
  • Swift: ScreenSharingV2Tests 206, ScreenSharingBenchTests 98, CodevisorBenchTests 61.

a_slow_link_doesnt_ask_again_for_packets_still_on_their_way sits close to its floor: its lowest estimate (500 kbit/s floor on 1 Mbit/s) dropped to 0.46 when the ended window's wait was bounded at one second plus two answer times. It passes with the 2 s bound.

Code review

Your review at 3a1ff75, all five fixed:

  1. Requests during an ended window's wait. Frames now belong to the window they were sent in. Charging requests that came during the wait to the next window instead cut the estimate in the 1 Mbit/s sim: a keyframe's real losses moved into a window that sent 74 packets and read 10.8%.
  2. Late packets in the drop check for a round trip. Requested bytes are held until the frame is answered. Adding them at the answer time instead of the request time misaligned the drop check on the slow link.
  3. The arrival count gets late packets' bytes back.
  4. README: it now describes the bound on the wait.
  5. from_indices sorts and deduplicates its input.

Code-review skill on the fixes (two rounds). Fixed:

  • Held bytes could fall outside the drop check. They are now clamped, for answered frames only.
  • The roll could confirm a frame before its own report could still take packets back. A frame whose report never comes now confirms after the same grace.
  • The arrival count's give-back could cross a restart; it now checks a generation.
  • A parity repair counted in the wrong window.
  • The clamp now measures from the last send.

Your review at b8c0566. Fixed, each with a test that fails without it:

  • An unanswered early-resolved frame is now confirmed when its wait ends, dated then. The test checks the drop-check window, not only the total.
  • Confirming clears the frame's charges, so a late report can't take back what the estimator already has.
  • A packet sent again for a repeated request reaches the estimator at once and stays a loss.

Also changed, though no test can fail without them:

  • A frame with nothing standing stops holding the window. Every report that takes charges back also removes its frame, so the old hold couldn't outlast it.
  • The late ranges are found from the LTR flag. A truncated LTR token already rejects the feedback, so both readings agreed.

Left:

  • A frame sent just before a roll and requested after it still counts in the new window, as on main.
  • The ended window can wait up to 2 s.
  • A packet counted lost before the arrival count restarted isn't counted again after it. This predates this PR.
  • A frame whose own report never comes keeps FEC on for up to 10 s. That is the conservative side.

🤖 Generated with Claude Code

pondorasti and others added 7 commits November 27, 2026 16:19
The viewer counts, per frame, the packets it asked for whose first copy
arrived too soon to be the resend, and reports them with the frame's
receipt behind a new feedback flag. The host takes them back off the
one-second loss window, and waits for frames that can still answer before
the heavy-loss rule reads it. On a jittery link a pause in arrivals during
a large frame made the viewer ask for packets still on their way, and the
heavy-loss cut collapsed the estimate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…fore it rolls (851-4501)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…and keep the probe's copy and resolved frames out of the way (851-4501)

What the last review found:
- A taken-back packet also comes off the estimator's lost bytes.
- When every loss of the frame that last set the FEC trigger is taken back, the trigger goes back to what it was.
- The last packet isn't charged when a tail probe's copy is on its way, so an early probe copy can't cancel a loss.
- A frame resolved by a later frame's report keeps its charges until its own report arrives.
- Feedback with more than eight late ranges keeps the first eight, and feedback cut short keeps none, instead of
  being dropped; ranges are clamped to the frame.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… back from the request that charged them (851-4501)

What the review of the take-back found:
- Restoring one earlier FEC-trigger time could leave a taken-back loss in place or clear another frame's real one.
  Each frame's requested losses now count apart, and a frame stops keeping FEC on only when none of them stands.
- A frame with a probed last packet or a resent repeat stays a loss.
- Lost bytes come off the estimator entry of the request that charged them, not the newest one.
- A frame resolved by a later frame's report also holds the loss window until its own report arrives.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… was charged in the window before (851-4501)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d bytes out of the drop check until answered (851-4501)

From the review of 3a1ff75:
- A request that came while an ended window waited was counted in it, and the window rolled before its report.
  Each frame now belongs to the window it was sent in: frames sent after a window's second count, with their
  losses, in the next one, and an ended window waits for each of its own frames' reports (at most 2 s old).
- Requested bytes reached the estimator's drop check at once and stayed after a take-back. They now reach it only
  when their frame is answered, at the time they were requested; late ones never do.
- A late packet the arrival count had taken as lost is given back to it.
- LateOriginals::from_indices sorts and dedups its input; the README matches the code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he frame's own answer, and fix two windows (851-4501)

What the review of d93998935 found:
- Held lost bytes dated at a request more than 500 ms before the answer missed the drop check; they now land no
  earlier than 250 ms before the answer.
- The roll confirmed frames resolved by a later report before their own report could take late packets back;
  only the frame's own report or its eviction confirms it now.
- A give-back after the arrival count restarted took bytes off another loss; it now checks the count's generation.
- A parity repair counted in the current window; it counts in the window its frame was sent in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pondorasti
pondorasti force-pushed the alexandru/851-4501-spurious-loss branch from 3a1ff75 to e252d69 Compare November 28, 2026 00:22
pondorasti and others added 2 commits November 27, 2026 17:14
…nd clamp only answered losses (851-4501)

What the review of the last commit found:
- A frame resolved by a later report whose own report was lost never confirmed its losses; it now confirms after
  the same two round trips and a frame interval the host already gives that report.
- Losses of frames given up after an outage all landed in the drop check at once; only answered frames are
  clamped into its 500 ms now, and given-up ones keep their request times.
- The clamp is measured from the last send when that is earlier than the answer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ken back once it reached the estimate (851-4501)

From the review of b8c0566:
- A frame resolved early whose own report never came reached the drop check only at the next roll, dated too early
  for it to look; it is now confirmed when its wait ends (timeout or feedback), dated then.
- Confirming clears the frame's charges, so a later report can't lower the window or give bytes back to the
  arrival count while the estimator keeps the loss.
- A packet sent again for a repeated request reaches the estimator at once and can't be taken back.
- A frame with nothing left standing stops holding the window; the late ranges sit after the LTR bytes whenever
  the LTR flag is set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pondorasti
pondorasti merged commit debfe2c into main Nov 28, 2026
11 checks passed
@pondorasti
pondorasti deleted the alexandru/851-4501-spurious-loss branch November 28, 2026 02:36
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