Skip to content

Deliver the benchmark relay's delayed packets on time (851-4477) - #1489

Merged
pondorasti merged 2 commits into
mainfrom
alexandru/851-4477-screen-sharing-freezes-briefly-while
Nov 26, 2026
Merged

pondorasti merged 2 commits into
mainfrom
alexandru/851-4477-screen-sharing-freezes-briefly-while

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Part of 851-4477 (PR 1 of 2: benchmark only, no product code. PR 2, the product hardening, will close the ticket.)

What was wrong

The Tier 1 relay armed its delivery timer with leeway: .never. That reads like "no leeway" but is an infinite one, and macOS 27.2 never fires such a timer. So on every simulated network with a delay (N1 to N7), a packet left the relay only when the next packet in either direction reached it. This has been so since the benchmark was added (28 October, #904). It is what froze scroll on the simulated lossy network (N3) in a quarter to a third of repetitions and made the PR gate fail at random.

What changes

  • Relay: the timer is armed with no leeway, and each arming reads the clock again, so a long flush doesn't make the next delivery late.
  • Lint: SwiftLint rejects leeway: .never (and DispatchTimeInterval.never) everywhere it lints, product code included.
  • Report: the stored report gains stale_ltr_acknowledgements, the host's count of reference acknowledgements it rejected as stale (Tier 1 and Tier 2; absent in older reports).
  • READMEs: the bench README explains the bug and what it did to the numbers; the media crate's README notes that the Tier 1 runs it cites went through the broken relay.

Other timers

Searched every Swift, Objective-C and C file for leeway, makeTimerSource, dispatch_source_set_timer and timer scheduling. Dispatch timers in the repo:

Timer Where Leeway
Relay delivery bench, LoopbackRelay .never (this bug)
Encode lab pacer bench, EncodeLabPacer .nanoseconds(0)
Synthetic source tick bench, SourcePusher .nanoseconds(0)
Screen recording size monitor product, ComputerUseRecording default

No product timer has the pattern.

Tests

  • LoopbackRelayTests: one packet through a relay with a 5 ms delay and no other traffic, both directions. On the old timer line it fails (the packet never arrives within the 2 s guard); with the fix it passes in 7 ms.
  • scripts/swift-lint.test.mjs: the new rule rejects both spellings and accepts .nanoseconds(0). Running lint against the old relay file reports both lines.
  • Tier2V2ReadingsTests: the host's stale-acknowledgement count reaches the report's counters.
  • Not covered by a test: that the next timer is armed from a fresh clock read. It needs a slow send to show, and there is no seam for that which production would use.

Lab A/Bs on tamale

All runs are GitHub Actions lab jobs on tamale, baseline (main at 45ec532) against the same commit with only the relay file changed. Each cell is one 30 s repetition after 10 s of warm-up, as in the nightly matrix plan; the two halves of a pair ran within about 25 minutes of each other (other jobs sometimes ran in between). Times are capture to committed, median, in ms, shown as current → fixed.

Network Encoder Typing p50 Scroll p50 Video p50 Scroll updates/s Video updates/s Freezes, all scenes
N1: 1 ms, 1 Gbit/s fallback 17.0 → 9.1 18.1 → 9.2 15.5 → 11.1 60 → 60 60 → 60 0 → 0
N1: 1 ms, 1 Gbit/s shipped 14.2 → 9.0 18.3 → 9.5 16.1 → 12.9 60 → 60 60 → 60 0 → 0
N2: 5 ms ± 3 ms, 0.5% loss, 200 Mbit/s fallback 15.5 → 12.8 18.1 → 15.2 28.2 → 24.3 56 → 60 60 → 60 2 → 0
N2: 5 ms ± 3 ms, 0.5% loss, 200 Mbit/s shipped 17.9 → 13.5 16.0 → 16.0 65.3 → 36.2 59 → 58 19 → 52 20 → 0
N3: 1 ms, 1% loss, 1 Gbit/s (the "lossy network") fallback 17.9 → 8.9 18.1 → 9.2 15.5 → 10.9 60 → 60 60 → 60 0 → 0
N3: 1 ms, 1% loss, 1 Gbit/s (the "lossy network") shipped 16.7 → 9.0 16.6 → 9.4 17.4 → 14.5 46 → 60 60 → 60 2 → 0
N4: 1 ms, 1% loss in bursts, 1 Gbit/s fallback 14.9 → 9.1 18.1 → 9.2 16.0 → 11.5 60 → 60 60 → 60 0 → 0
N4: 1 ms, 1% loss in bursts, 1 Gbit/s shipped 17.0 → 8.9 11.6 → 9.5 16.1 → 13.0 60 → 60 60 → 60 0 → 0
N5: 10 ms, 30 Mbit/s, short queue fallback 18.8 → 18.0 22.0 → 18.8 33.9 → 31.9 59 → 60 36 → 60 14 → 0
N5: 10 ms, 30 Mbit/s, short queue shipped 20.6 → 18.4 21.9 → 19.0 29.4 → 31.9 60 → 60 29 → 60 21 → 0
N6: 25 ms ± 5 ms, 0.1% loss, 50 Mbit/s fallback 36.4 → 33.5 39.7 → 37.3 52.6 → 51.9 58 → 60 60 → 60 0 → 0
N6: 25 ms ± 5 ms, 0.1% loss, 50 Mbit/s shipped 36.5 → 34.3 39.6 → 36.8 53.1 → 52.6 52 → 60 56 → 60 10 → 0
N7: 50 ms, 5% loss, 1 Mbit/s fallback 65.1 → 62.7 96.9 → 74.1 81.1 → 74.8 20 → 12 37 → 24 52 → 57
N7: 50 ms, 5% loss, 1 Mbit/s shipped 68.0 → 62.1 80.0 → 79.9 85.6 → 76.1 13 → 6 6 → 26 38 → 56

Six of the seven networks get faster or steadier. The 1 ms networks drop from about 17 to 9 ms and every freeze on N2 to N6 goes away (video on the 30 Mbit/s network goes from 29 to 36 updates per second to a steady 60).

The 1 Mbit/s network (N7) gets worse on scroll, and this is the product, not the benchmark. Because one repetition is too few there, I ran scroll and video on N7 six times per side on each encoder:

N7, 6 repetitions Current relay Fixed relay
Scroll, fallback encoder: updates/s per repetition 20.9, 3.4, 20.1, 16.4, 16.1, 16.6 (median 16.5) 4.9, 7.2, 5.5, 3.0, 4.2, 18.4 (median 5.2)
Scroll, shipped encoder 20.6, 22.6, 12.2, 20.1, 6.2, 23.6 (median 20.4) 9.9, 7.5, 8.6, 19.9, 11.0, 4.9 (median 9.3)
Video, fallback encoder 30.8, 6.3, 30.2, 8.2, 32.8, 34.7 (median 30.5) 19.1, 31.2, 31.3, 30.6, 0 (nothing shown), 28.4
Video, shipped encoder 26.5, 9.6, 15.7, 10.2, 11.4, 33.6 (median 13.6) 0 (nothing shown), 25.2, 16.4, 27.4, 33.4, 28.6
Scroll repetitions where the host sent a second start-up keyframe 2 of 6 and 3 of 6 6 of 6 and 6 of 6

Runs: fallback 45757884876 (current) and 45756571502 (fixed); shipped 45759806854 and 45759740238. In every slow repetition the host sent a second start-up keyframe (about 400 KB after the first 193 KB, on a link that carries 125 KB a second), and in the two repetitions that showed nothing the keyframes added up to 1.5 MB. With packets delivered on time this happens to scroll every time; the old relay's clumping hid it most of the time. No product code changes in this PR, so this is what a real 1 Mbit/s link with 5% loss gets today. I have not traced why the first keyframe is given up; that belongs with PR 2 (it is the same family: the viewer asking for packets that are still on their way).

One note on pair 1: its fixed side ran the first version of the fix (the timer line only), before the review fix that re-reads the clock. The PR gate below ran the final code on the lossy network and got the same numbers (9.0, 9.2, 11.5 ms). Pairs 2 to 4 and the N7 runs used the final relay file.

Every cell, with the stored baseline (median of the last 7 nightlies; fallback encoder only, the nightly plans don't run the shipped one)

Fallback encoder (VideoToolbox), networks N1 to N4

Current relay: run 45739913441. Fixed relay (first version of the fix (timer line only)): run 45739926943.

Network Scene Stored baseline p50/p95 Current p50/p95 Fixed p50/p95 Updates/s Freezes Estimate at end, Mbit/s
N1 idle-cursor 12.5/15.9 14.6/18.7 9.8/10.6 4.0 → 4.0 – → – 58 → 1073
N1 scroll 18.1/18.2 18.1/18.2 9.2/9.7 60.0 → 60.0 0 → 0 58 → 857
N1 test-cards 52.0/130.8 60.1/150.3 19.0/30.6 0.3 → 0.3 – → – 79 → 597
N1 typing 15.5/16.4 17.0/18.1 9.1/10.6 15.0 → 15.0 0 → 0 100 → 100
N1 video 16.4/18.7 15.5/19.1 11.1/11.7 60.0 → 60.0 0 → 0 584 → 977
N1 window-drag 18.0/18.2 16.5/16.7 9.1/9.5 60.0 → 60.0 0 → 0 99 → 1057
N2 idle-cursor 18.3/27.2 18.6/18.8 14.2/19.0 4.0 → 4.0 – → – 45 → 69
N2 scroll 18.1/24.7 18.1/24.2 15.2/19.7 56.0 → 60.0 2 → 0 7 → 38
N2 test-cards 81.3/203.1 65.8/155.9 67.3/142.8 0.3 → 0.3 – → – 38 → 41
N2 typing 16.0/30.9 15.5/30.1 12.8/17.9 15.0 → 15.0 0 → 0 100 → 100
N2 video 26.3/31.2 28.2/35.0 24.3/28.9 60.0 → 60.0 0 → 0 52 → 88
N2 window-drag 18.1/22.9 15.5/23.0 15.3/19.6 60.0 → 60.0 0 → 0 90 → 78
N3 idle-cursor 15.9/17.8 10.7/15.0 9.9/10.5 4.0 → 4.0 – → – 96 → 417
N3 scroll 16.3/18.2 18.1/18.3 9.2/9.7 60.0 → 60.0 0 → 0 45 → 870
N3 test-cards 56.3/134.0 29.5/56.5 23.9/38.0 0.3 → 0.3 – → – 149 → 831
N3 typing 16.2/16.4 17.9/18.2 8.9/9.6 15.0 → 15.0 0 → 0 100 → 100
N3 video 15.1/18.1 15.5/18.3 10.9/14.4 60.0 → 60.0 0 → 0 532 → 995
N3 window-drag 13.4/15.3 18.1/18.4 9.2/9.6 60.0 → 60.0 0 → 0 411 → 860
N4 idle-cursor 16.2/17.8 14.7/18.8 9.8/10.3 4.0 → 4.0 – → – 69 → 462
N4 scroll 18.1/18.2 18.1/18.2 9.2/9.5 59.9 → 60.0 0 → 0 41 → 450
N4 test-cards 31.9/48.3 40.5/82.6 19.7/38.6 0.3 → 0.3 – → – 77 → 626
N4 typing 17.0/18.0 14.9/15.1 9.1/10.6 15.0 → 15.0 0 → 0 85 → 100
N4 video 15.8/19.5 16.0/19.5 11.5/14.8 59.9 → 60.0 0 → 0 644 → 1237
N4 window-drag 16.0/16.2 18.2/18.4 9.2/9.5 60.0 → 59.9 0 → 0 153 → 865

Fallback encoder (VideoToolbox), networks N5 to N7

Current relay: run 45745709795. Fixed relay (final relay code): run 45745699213.

Network Scene Stored baseline p50/p95 Current p50/p95 Fixed p50/p95 Updates/s Freezes Estimate at end, Mbit/s
N5 idle-cursor 28.4/29.5 26.2/28.6 18.9/19.4 4.0 → 4.0 – → – 39 → 36
N5 scroll 21.9/24.8 22.0/24.7 18.8/19.5 58.9 → 60.0 0 → 0 7 → 31
N5 test-cards 91.1/227.3 89.7/201.5 83.2/196.0 0.3 → 0.3 – → – 28 → 30
N5 typing 25.7/30.7 18.8/30.5 18.0/18.4 15.0 → 15.0 0 → 0 100 → 100
N5 video 32.4/73.7 33.9/85.1 31.9/35.7 36.5 → 60.0 14 → 0 39 → 30
N5 window-drag 21.9/22.1 21.9/22.1 18.8/19.2 60.0 → 60.0 0 → 0 24 → 30
N6 idle-cursor 35.4/44.9 35.5/47.6 35.5/41.3 4.0 → 4.0 – → – 42 → 24
N6 scroll 39.7/51.4 39.7/46.9 37.3/44.5 58.3 → 59.9 0 → 0 13 → 26
N6 test-cards 110.0/240.1 104.5/218.2 110.8/245.2 0.3 → 0.3 – → – 31 → 24
N6 typing 35.2/44.1 36.4/43.6 33.5/40.8 14.8 → 15.0 0 → 0 100 → 100
N6 video 53.5/59.7 52.6/59.6 51.9/57.7 60.0 → 60.0 0 → 0 36 → 50
N6 window-drag 38.6/51.4 39.5/48.0 36.3/43.6 60.0 → 60.0 0 → 0 46 → 161
N7 idle-cursor 68.7/264.2 67.7/234.7 62.9/255.7 3.9 → 3.8 – → – 1 → 1
N7 scroll 84.8/237.0 96.9/248.4 74.1/184.3 19.6 → 11.7 14 → 13 1 → 1
N7 test-cards 101.4/1253.9 523.7/1081.5 99.9/1278.3 0.3 → 0.3 – → – 1 → 1
N7 typing 65.7/160.6 65.1/174.8 62.7/167.5 12.7 → 13.4 8 → 11 100 → 100
N7 video 80.4/167.1 81.1/182.4 74.8/167.4 37.4 → 24.5 12 → 12 1 → 1
N7 window-drag 87.6/193.5 79.2/194.2 71.1/185.6 30.9 → 27.6 18 → 21 1 → 1

Shipped encoder (VCP), networks N1 to N4

Current relay: run 45750501961. Fixed relay (final relay code): run 45750491206.

Network Scene Stored baseline p50/p95 Current p50/p95 Fixed p50/p95 Updates/s Freezes Estimate at end, Mbit/s
N1 idle-cursor 12.5/15.9 15.8/16.0 13.5/14.3 4.0 → 4.0 – → – 63 → 1342
N1 scroll 18.1/18.2 18.3/18.4 9.5/10.0 60.0 → 60.0 0 → 0 65 → 476
N1 test-cards 52.0/130.8 105.7/220.5 27.9/44.2 0.3 → 0.3 – → – 45 → 1249
N1 typing 15.5/16.4 14.2/18.0 9.0/9.3 15.0 → 15.0 0 → 0 100 → 100
N1 video 16.4/18.7 16.1/17.6 12.9/13.9 60.0 → 60.0 0 → 0 901 → 864
N1 window-drag 18.0/18.2 17.5/18.4 9.3/9.7 60.0 → 60.0 0 → 0 664 → 422
N2 idle-cursor 18.3/27.2 17.9/27.5 17.2/22.3 4.0 → 4.0 – → – 19 → 57
N2 scroll 18.1/24.7 16.0/23.6 16.0/20.6 58.9 → 58.4 0 → 0 14 → 9
N2 test-cards 81.3/203.1 90.8/797.1 125.4/278.0 0.2 → 0.3 – → – 6 → 35
N2 typing 16.0/30.9 17.9/19.8 13.5/18.5 15.0 → 15.0 0 → 0 72 → 100
N2 video 26.3/31.2 65.3/335.2 36.2/68.1 18.9 → 52.1 20 → 0 47 → 41
N2 window-drag 18.1/22.9 18.3/23.5 15.2/19.4 60.0 → 60.0 0 → 0 4 → 41
N3 idle-cursor 15.9/17.8 19.1/19.3 13.6/14.2 4.0 → 4.0 – → – 72 → 679
N3 scroll 16.3/18.2 16.6/17.1 9.4/9.9 46.2 → 60.0 2 → 0 3 → 500
N3 test-cards 56.3/134.0 101.5/235.0 34.5/56.7 0.3 → 0.3 – → – 74 → 1213
N3 typing 16.2/16.4 16.7/16.8 9.0/9.6 15.0 → 15.0 0 → 0 85 → 100
N3 video 15.1/18.1 17.4/21.3 14.5/17.4 60.0 → 60.0 0 → 0 973 → 900
N3 window-drag 13.4/15.3 17.8/18.1 9.4/9.8 59.8 → 60.0 0 → 0 3 → 540
N4 idle-cursor 16.2/17.8 17.5/17.7 13.8/14.0 4.0 → 4.0 – → – 70 → 970
N4 scroll 18.1/18.2 11.6/15.9 9.5/9.9 60.0 → 59.9 0 → 0 30 → 472
N4 test-cards 31.9/48.3 104.2/247.6 28.9/46.3 0.3 → 0.3 – → – 43 → 1269
N4 typing 17.0/18.0 17.0/17.1 8.9/9.4 15.0 → 15.0 0 → 0 72 → 100
N4 video 15.8/19.5 16.1/21.8 13.0/17.5 60.0 → 60.0 0 → 0 915 → 1039
N4 window-drag 16.0/16.2 14.2/14.5 9.4/9.7 60.0 → 60.0 0 → 0 39 → 445

Shipped encoder (VCP), networks N5 to N7

Current relay: run 45754941819. Fixed relay (final relay code): run 45754774666.

Network Scene Stored baseline p50/p95 Current p50/p95 Fixed p50/p95 Updates/s Freezes Estimate at end, Mbit/s
N5 idle-cursor 28.4/29.5 25.8/30.0 18.9/19.8 4.0 → 4.0 – → – 24 → 32
N5 scroll 21.9/24.8 21.9/22.9 19.0/19.6 59.7 → 60.0 0 → 0 22 → 33
N5 test-cards 91.1/227.3 94.3/227.4 83.1/196.6 0.3 → 0.3 – → – 39 → 30
N5 typing 25.7/30.7 20.6/27.1 18.4/20.0 15.0 → 15.0 0 → 0 100 → 100
N5 video 32.4/73.7 29.4/113.4 31.9/35.7 29.1 → 60.0 21 → 0 37 → 30
N5 window-drag 21.9/22.1 21.9/24.7 18.8/19.2 60.0 → 60.0 0 → 0 18 → 31
N6 idle-cursor 35.4/44.9 35.6/44.9 35.8/43.0 4.0 → 4.0 – → – 36 → 19
N6 scroll 39.7/51.4 39.6/49.2 36.8/44.0 51.9 → 60.0 6 → 0 10 → 33
N6 test-cards 110.0/240.1 96.6/222.4 105.0/233.1 0.3 → 0.3 – → – 28 → 27
N6 typing 35.2/44.1 36.5/45.2 34.3/42.2 15.0 → 15.0 0 → 0 100 → 100
N6 video 53.5/59.7 53.1/61.0 52.6/59.5 55.8 → 60.0 4 → 0 39 → 41
N6 window-drag 38.6/51.4 40.2/45.6 37.2/44.2 60.0 → 60.0 0 → 0 3 → 3
N7 idle-cursor 68.7/264.2 64.2/199.9 80.0/261.3 3.9 → 3.6 – → – 1 → 1
N7 scroll 84.8/237.0 80.0/235.2 79.9/255.7 12.7 → 5.9 12 → 6 1 → 1
N7 test-cards 101.4/1253.9 102.7/940.8 96.5/962.7 0.3 → 0.3 – → – 1 → 1
N7 typing 65.7/160.6 68.0/144.0 62.1/154.5 13.3 → 12.4 8 → 8 100 → 68
N7 video 80.4/167.1 85.6/104.2 76.1/183.2 6.3 → 26.4 2 → 17 1 → 1
N7 window-drag 87.6/193.5 72.0/177.9 86.6/196.9 33.0 → 30.6 16 → 25 1 → 1

The PR gate and the nightly baseline

Does a faster result fail the gate? No. The gate's latency rules fire only on a rise, updates per second only on a fall, the freeze share only on a rise. This PR's own gate run (45745567913, the final code) passed: on the lossy network typing 9.0 and 9.1 ms against a stored 16.6, scroll 9.2 and 9.2 against 14.5, video 11.5 and 11.4 against 15.2, and 0 freezes in 18 of 18 repetitions. The good network (no delay) never went through the timer and is unchanged (8.0 → 7.5 and 7.9, 8.1 → 8.1, 10.3 → 10.3).

What misleads until the baseline rolls over. The baseline is the median of the last 7 nightlies of the same plan, so it keeps the old numbers until 4 of the 7 are new.

  • PR gate, 4 nights: its lossy-network latency rules are too lenient. With the stored medians a cell fails only above 19.6 ms (typing), 17.5 ms (scroll) and 18.2 ms (video) at the median, while the real numbers are now 9 to 11.5 ms. So a change that nearly doubles lossy-network latency would pass. The freeze and updates-per-second rules keep working as before (stored freeze share 0, 15 and 60 updates per second), and so does everything on the good network.
  • Nightly reports, up to 7 nights: the "change against the last 7 nightlies" column and the trend chart show a one-time drop on every cell with a delay (lossy network in the core plan; N1, N2 and N4 to N7 in the matrix plan). Nothing fails on it.
  • Open PRs: the gate measures a PR merged with main, so every PR gets the fixed relay as soon as this lands, without a rebase. No PR is compared old-relay against new-baseline.

Proposal to reset (not done here; say which you want).

  1. Seed the gate's baseline, no code: right after the merge, dispatch the gate plan as a nightly 4 times (gh workflow run bench-screen-sharing.yml -f kind=nightly -f plans=gate-v2, about 12 minutes each on tamale, one at a time because they share the screen-sharing-bench group). The gate's median is then all new the same evening. The core and matrix plans only feed reports, so they can roll over on their own. This is what I recommend.
  2. Cut the history in code: record a relay revision in the report's method, which the baseline already matches on, so nightlies from before never mix with later ones in any plan. Cleaner charts, but the good network's history restarts too, and the gate has no baseline at all until the first nightly after the merge.

What the nightly matrix report will show for N7: scroll's updates per second fall (stored median about 15, now about 5 on the fallback encoder). The PR gate doesn't cover N7, so nothing fails, but it is a real number and worth its own look.

🤖 Generated with Claude Code

pondorasti and others added 2 commits November 25, 2026 18:39
The Tier 1 relay armed its delivery timer with leeway .never, which macOS
27.2 never fires, so on every simulated network with a delay a packet left
only when the next packet reached the relay. That is what froze scroll on
the simulated lossy network. Arm the timer with no leeway, reject
leeway .never in SwiftLint, and store the host's count of stale reference
acknowledgements in the report.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…51-4477)

Review fixes: the next delivery was armed from the time the flush began, so
it left late by however long the flush took. The lint rule also catches the
fully qualified spelling and has a test. The relay test retries an
interrupted wait and is named for what it asserts.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@pondorasti
pondorasti merged commit 60a9643 into main Nov 26, 2026
21 of 22 checks passed
@pondorasti
pondorasti deleted the alexandru/851-4477-screen-sharing-freezes-briefly-while branch November 26, 2026 05:39
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