Skip to content

gRPC: drop media and poses queued past a delay, measured by in-band probes - #1006

Merged
transfix merged 2 commits into
masterfrom
perf/grpc-write-backpressure
Nov 3, 2026
Merged

transfix merged 2 commits into
masterfrom
perf/grpc-write-backpressure

Conversation

@transfix

@transfix transfix commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

Under bursty loss, the gRPC transport queued seconds of camera, mic and pose traffic per viewer. Now it drops the stale frames, so a loss burst costs a few dropped frames, not a multi-second backlog.

Why it queued

  • connection::write calls a synchronous Write. That returns once HTTP/2 flow control and the socket take the bytes.
  • gRPC's BDP probing grows the flow-control window to megabytes, so during a TCP loss burst no write blocks.
  • The 40 ms media stall drop fires only on a blocked write. It never fired: stalled 0 in every master run below.

The fix: in-band queue probes and a delay drop

  • Probes. Writers of loss-tolerant frames follow their writes with a Ping whose seq has the top bit set. That covers the relay's media fan, a node's own uplink, and pose snapshots. At most one goes out every 50 ms, after at least 1 KB more was written, with at most 8 outstanding.
  • Measuring the queue. The stream is in order, so a probe's Pong means the peer has read everything written before it. The oldest unanswered probe therefore dates the oldest byte that may still be queued. Its age minus the base round trip is the queue delay. The base round trip is the smallest probe round trip over the last 10 s.
  • set_media_delay_drop(ms), default 100 ms. Past that queue delay, the receiver's video is dropped, its pose snapshots are skipped past 2×, and its audio past 4×. Those are the WebSocket transport's ratios: a backed-up link sheds video first.
  • Peers that don't answer. A peer that has never answered a probe is never held back. Every peer already answers Ping, so old clients need nothing.
  • A Pong now goes through the admission hold. A held stream used to leave probes unanswered.
  • Non-essential writes skip a busy stream instead of waiting.
    • push_pose_snapshot_to runs on the relay's room step. It now waits at most 1 ms for the stream lock and then skips; the next snapshot carries what it would have.
    • The heartbeat uses try_write.
  • Each dialed peer gets its own connection (GRPC_ARG_USE_LOCAL_SUBCHANNEL_POOL). gRPC otherwise carries every channel in a process to the same target on one HTTP/2 connection. The bench's two clients were sharing one TCP connection, its flow-control window and every TCP stall, which two separate clients never do.
  • Flags and bench output. arisrv --media-delay-ms N (0 = off); arisrv_aoi_bench --media-delay-ms N sets the relay and the clients. The bench now reports the drops (relay fan, uplinks) and the skipped pose snapshots.

Results

Setup

  • catx-03, netem on loopback in a network namespace: delay 20ms loss gemodel 1% 30% 100% 0% (about 3 % loss in bursts of about 3 packets).
  • arisrv_aoi_bench --transport grpc --clients 2 --camera-hz 15 --video-bytes 5400 --mic opus --seconds 30 --warmup 4 --client-hz 60, in both pose modes.
  • 6 runs per row; the clean-link rows (delay 20ms, no loss) have 2 runs each.
  • Master is origin/master 3df4ec4; this PR is afb00e8. Both were built in the same tree and configuration.
build poses runs camera p95 ms, median (max) camera p99 max runs with camera p95 > 500 ms camera delivered, median (min) mic p95, median (max) pose age p95, median (max) relay out kb/s, median
master (before) tree 6 456 (2928) 3718 3 of 6 100.0% (99.3%) 460 (2921) 176 (751) 1644
master (before) stream 6 1414 (6753) 7882 3 of 6 100.0% (100.0%) 1447 (6760) 684 (6550) 1497
this PR tree 6 106 (107) 310 0 of 6 97.8% (94.8%) 113 (140) 102 (116) 1624
this PR stream 6 113 (121) 359 0 of 6 94.7% (88.5%) 125 (162) 93 (268) 1441
master, clean link tree 2 41 (41) 43 0 of 2 100.1% (100.0%) 41 (41) 69 (70) 1641
master, clean link stream 2 41 (41) 44 0 of 2 100.0% (100.0%) 41 (41) 35 (35) 1495
this PR, clean link tree 2 41 (41) 42 0 of 2 100.1% (100.0%) 41 (41) 67 (67) 1653
this PR, clean link stream 2 41 (41) 41 0 of 2 100.0% (100.0%) 41 (41) 35 (35) 1508

Camera p95 per run, in ms:

build tree stream
master 846, 113, 120, 791, 107, 2928 6494, 2479, 6753, 348, 114, 205
this PR 107, 107, 106, 106, 104, 101 107, 112, 121, 113, 105, 114
  • Under loss, the gate drops 4–146 frames per run on the relay's fan, 4–157 on the uplinks, and 0–180 pose snapshots. Nothing blocked: stall drop 0 and watchdog 0 in every run.
  • On a clean link the gate never fired (0 dropped anywhere), and latency and relay throughput match master.

Approaches evaluated

Same setup, 6 runs per row. Three builds were added:

  • master + separate connections: master's behaviour (the delay drop at 0) on this branch's build, with one TCP connection per client. It separates the topology fix from the drop.
  • approach 1: HTTP/2 windows capped at 64 KiB, with BDP probing off on both the server builder and the client channel, and the 40 ms stall drop on the clients' uplinks too. The delay drop is off.
  • this PR + tcp_thin_linear_timeouts: this PR with net.ipv4.tcp_thin_linear_timeouts=1 set in the namespace.

The first two ran from an experiment-only build: env-gated channel args, not in this PR.

build poses camera p95 ms, median (max) camera p99 max runs with camera p95 > 500 ms camera delivered, median (min) mic p95, median (max) pose age p95, median (max)
master tree 456 (2928) 3718 3 of 6 100.0% (99.3%) 460 (2921) 176 (751)
master stream 1414 (6753) 7882 3 of 6 100.0% (100.0%) 1447 (6760) 684 (6550)
master + separate connections tree 130 (968) 1995 1 of 6 100.1% (97.5%) 131 (955) 110 (817)
master + separate connections stream 111 (318) 929 0 of 6 100.0% (100.0%) 110 (330) 84 (266)
approach 1: 64 KiB windows, BDP off tree 120 (131) 1757 0 of 6 99.9% (99.5%) 118 (124) 102 (117)
approach 1: 64 KiB windows, BDP off stream 117 (126) 724 0 of 6 100.1% (100.0%) 120 (123) 84 (84)
this PR tree 106 (107) 310 0 of 6 97.8% (94.8%) 113 (140) 102 (116)
this PR stream 113 (121) 359 0 of 6 94.7% (88.5%) 125 (162) 93 (268)
this PR + tcp_thin_linear_timeouts tree 111 (127) 344 0 of 6 97.5% (94.5%) 120 (147) 102 (134)
this PR + tcp_thin_linear_timeouts stream 104 (111) 217 0 of 6 96.4% (82.8%) 114 (135) 84 (1434)

What the comparison shows:

  • The shared connection inflated the original problem. With one connection per client, as in production where every client is its own process, master's worst camera p95 is 968 ms, not 6.75 s. That still means stalls of about a second, and camera p99 reaches 2.0 s.
  • Approach 1 also bounds p95 in this bench, and it drops almost nothing. But its tails are long: camera p99 up to 1757 ms against 310 here. A full 64 KiB window is about 0.6 s of this bench's traffic, so a frame can still wait that long in the queue.
    • The window is a byte bound, so the delay it allows grows as the rate falls.
    • It also caps throughput at window/RTT. That is about 1.6 MB/s at this bench's 40 ms but about 320 KB/s at 200 ms, which a room of full-rate cameras would exceed. This is arithmetic, not measured here.
    • The relay's downstream window is set by each client, so only upgraded clients would get it.
    • The stall drop never fired in its runs either (stalled 0).
  • This PR bounds the queue in time. It holds about 100 ms whatever the rate and the round trip. The relay applies it to every client, old ones included; a client's own uplink needs this build. The price is 2–5 % of camera frames dropped under this loss (the user accepts losing stream data under loss), with p99 at 217–359 ms.
  • tcp_thin_linear_timeouts adds little on top here. The separate connections already removed the long RTO stalls from these 12 runs. One stream run still had pose age p95 of 1.4 s.

What I found along the way

  • The bench's clients shared one TCP connection. That came from gRPC's global subchannel pool, and it is fixed here.
  • A thinned stream recovers from loss slowly. Once the drop thins a stream, a tail loss is recovered by a retransmission timeout, not fast retransmit.
    • Netem's Gilbert-Elliott chain moves one step per packet, so on a quiet link it stays in its loss state and each retransmission can die in turn.
    • Before the separate connections, this froze 5 of 22 tree-mode runs for 5–30 s. ss -tin showed backoff:6 rto:15616 cwnd:1, with every retransmission dropped. gdb showed every reader idle in cq_pluck, so it was not a deadlock.
    • It is rarer with separate connections: 1 of 8 tree runs in an experiment stalled about 5 s, and none of the 12 final runs did.
    • net.ipv4.tcp_thin_linear_timeouts=1 stops the RTO doubling for thin streams. It is per network namespace, and with it 8 of 8 shared-connection tree runs ran clean. On the relay host it would be cheap insurance. That is a deployment change, not done here.

Tests

  • state_transport_grpc_test: 50 passed, 3 skipped (TLS, stress, perf), on Windows (static MSVC build) and Linux (catx-03). It includes the new ProbeQueueDelay and DelayDropSkipsAStalledReceiver (a receiver whose reader stalls 1.2 s: the sender drops media and skips poses, then delivers again once it catches up).
  • On Windows: room_relay_integration_test 16, room_aoi_relay_test 10, stream_relay_test 3, room_media_test 2, pose_stream_test 9, all pass.

Not done

No production deploy; that needs a go-ahead.

…robes

A gRPC Write returns once HTTP/2 flow control and the socket take the bytes,
and BDP probing grows the window to megabytes, so during a TCP loss burst no
write blocks, the media stall drop never fires, and seconds of camera, mic and
pose traffic queue per viewer.

Writers of loss-tolerant frames (the media fan, a node's own uplink, pose
snapshots) now follow them with a Ping whose seq has the top bit set, at most
every 50 ms. The stream is in order, so its Pong answers every earlier probe;
the oldest unanswered one dates the oldest byte that may still be queued.
Past the base round trip (the least probe round trip over 10 s) by more than
set_media_delay_drop (default 100 ms), video to that receiver is dropped,
pose snapshots past twice that and audio past four times. A peer that never
answers a probe is never held back.

Also:
- a Pong goes through the admission hold (a held stream left probes unanswered);
- push_pose_snapshot_to waits at most 1 ms for a busy stream, and the
  heartbeat skips one, instead of blocking the caller;
- each dialed peer gets its own connection (a local subchannel pool), so two
  transports dialing one relay no longer share a flow-control window and every
  TCP stall;
- arisrv and arisrv_aoi_bench take --media-delay-ms; the bench reports the
  drops (relay fan, uplinks) and the skipped pose snapshots.
@transfix
transfix merged commit 7e04a3d into master Nov 3, 2026
22 of 28 checks passed
@transfix
transfix deleted the perf/grpc-write-backpressure branch November 3, 2026 19:03
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