diff --git a/doc/bin/relay/config.md b/doc/bin/relay/config.md index a0022ec7046..ae0819595c2 100644 --- a/doc/bin/relay/config.md +++ b/doc/bin/relay/config.md @@ -122,9 +122,9 @@ idle_timeout = "10s" # Drop a connection after this long with no # keep_alive = "3s" # Ping interval; unset or "0s" derives it from the idle timeout. iroh keeps a separate 5s path heartbeat. gso = true # UDP segmentation offload. iroh cannot turn it off. mtu_discovery = false # Path MTU discovery. Default. -receive_window = 134217728 # Whole-connection window, in bytes. Default (128 MiB). -stream_receive_window = 4194304 # Per-stream window, in bytes. Default (4 MiB). -send_window = 16777216 # Unacknowledged outgoing data, in bytes. Default (16 MiB). +receive_window = 4194304 # Whole-connection window, in bytes. Default (4 MiB). +stream_receive_window = 1048576 # Per-stream window, in bytes. Default (1 MiB). +send_window = 4194304 # Unacknowledged outgoing data, in bytes. Default (4 MiB). qlog = "/var/log/moq/qlog" # Existing directory. Needs the `qlog` build feature. # keylog = "/private/keys.log" # Explicit secret log path; unset is off. ``` @@ -152,17 +152,23 @@ the idle timeout. A value no shorter than the negotiated idle timeout could never keep the connection up, so it is capped at the derived interval. iroh cannot see the peer's timeout, so it derives from the local `idle_timeout`. -Raise the receive windows when a fat, long path idles below the link rate. The -128 MiB `receive_window` default carries several Gbps at a 100 ms RTT and bounds -how much unread data and incomplete messages one peer can make the relay -buffer. It has to fit the largest message, just over 64 MiB, so a smaller -value, here or in `[limits.peer.quic]`, is refused at startup. When incomplete -messages fill the window, the relay cancels the least urgent stream: the -lowest subscription priority, then the group its subscription would deliver -last. Over iroh the window bounds only -unread data, and over WebSocket or TCP nothing bounds incomplete messages. -`stream_receive_window` only paces one stream: its credit returns as bytes are -read, whether or not their message is complete. +Raise the receive windows when a fat, long path idles below the link rate: a +connection carries at most its window per round trip. The 4 MiB +`receive_window` default carries about 270 Mbps at a 100 ms RTT, plenty for one +viewer or publisher, and bounds how much unread data and incomplete messages +one peer can make the relay buffer. It has to fit the largest unit a session +assembles, one 1 MiB segment, so a smaller value, here or in +`[limits.peer.quic]`, is refused at startup. When incomplete messages fill the +window, the relay cancels the least urgent stream: the lowest subscription +priority, then the group its subscription would deliver last. A group still +waiting for its track's TRACK\_INFO goes first, so that control message always +gets through. WebSocket and TCP sessions cancel the same way; the relay accepts +them at these windows too and widens peers to `[limits.peer.quic]`, and links +it dials take these settings. +Over iroh the window bounds only unread data. `stream_receive_window` only paces +one stream: its credit returns as bytes are read, whether or not their message +is complete, so the 1 MiB default caps one group stream at about 67 Mbps at a +100 ms RTT. ## \[limits.client] and \[limits.peer] @@ -180,17 +186,18 @@ announces = 100000 [limits.peer.quic] max_streams = 100000 # Concurrent QUIC streams per direction. -receive_window = 671088640 # Connection-wide receive window (640 MiB). -send_window = 671088640 # Unacknowledged outgoing data (640 MiB). +receive_window = 16777216 # Connection-wide receive window (16 MiB). +send_window = 16777216 # Unacknowledged outgoing data (16 MiB). ``` `[limits.peer.quic]` takes only `max_streams`, `receive_window`, and `send_window`; any other `[quic]` key is refused. They replace the `[quic]` values for a -peer's QUIC connection once its grant is known, before the SETUP reply. Links -the relay dials get them from the handshake on. A link carrying a whole cluster is then -not throttled by `[quic]` limits sized for one viewer, for example -`--limits-peer-quic-max-streams=200000`. Peers over WebSocket, TCP, or iroh keep -their transport's own limits. +peer's connection once its grant is known, before the SETUP reply, over QUIC, +WebSocket, or TCP alike. Links the relay dials get them from the handshake on. +A link carrying a whole cluster is then not throttled by `[quic]` limits sized +for one viewer, for example `--limits-peer-quic-max-streams=200000`. Peers over +iroh keep iroh's own limits. The 16 MiB windows carry about 450 Mbps at a 250 ms +RTT and about 1 Gbps at 100 ms; raise both for a busier link. These are the defaults. Each subscription and FETCH holds a stream, as does each PUBLISH a peer's track listing gets, so `[quic]` or `[limits.peer.quic]` @@ -397,8 +404,9 @@ flow control slows the upstream hop and the publisher. A slower reader is never waited on: one that trails the fastest by more than the rest of the 32 MB reaches a shed frame. A group nobody reads keeps arriving and shedding. A stopped stream keeps its receive credit, so the relay stops reading at most as -many streams per connection as fit in half of `quic.receive_window`; past that, -groups keep arriving and shedding as if unpaced. +many streams per connection as fit in half of its receive window, a stream +window each; past that, groups keep arriving and shedding as if unpaced. At the +defaults that is two streams per client session and eight per peer link. ## \[stats] diff --git a/quest/m1/unbounded-groups/fastest-reader.md b/quest/m1/unbounded-groups/fastest-reader.md index 71c725010e6..e63fbe07493 100644 --- a/quest/m1/unbounded-groups/fastest-reader.md +++ b/quest/m1/unbounded-groups/fastest-reader.md @@ -12,6 +12,14 @@ otherwise refreshes or ends with `Error::Evicted`. A byte stream over MoQ then loses nothing to a reader that keeps up, and each hop keeps its own congestion control and retransmissions. +The paced hops include qmux sessions (WebSocket, `tcp://`, `tls://`, +`unix://`, and WebRTC): they lease receive credit natively, so incomplete +messages are bounded by the session's receive window and the lowest-priority +holder is evicted when it is stuck, as on native QUIC. The per-session memory +a peer can make us hold is one deliberate, documented number across native +QUIC and qmux. The browser keeps reading unpaced until WebTransport offers a +real receive-credit contract. + ## Plan Decided 2026-10-25 (maintainer, mesh planning interview), reopening the @@ -112,28 +120,93 @@ in `rs/moq-net/src/transport/boxed.rs`; moq-quic reports its windows through moq-tokio and moq-uring, and a transport that reports none never pauses) and the public readiness future. +Decided 2026-11-02 (maintainer, /quest-grill interview on +[moq-dev/moq#7624](https://github.com/moq-dev/moq/pull/7624)): + +- Native only: finish the moq-quic path (tokio, io_uring, and qmux on its + mux). Browser parity waits on a real receive-credit contract for + WebTransport, since `Reader.closed` watches resets but nothing says how much + credit a paused stream holds; guessed window constants would move memory + outside flow control. Rejected: a browser read-ahead cap, and dropping the + browser from scope. +- qmux is handled here, absorbing the m2 qmux receive-lease quest: qmux's + `RecvStream` leases natively on moq-quic's mux instead of through + `poll::Buffered`, evicts by priority, and so closes the 2026-10-27 audit's + NET-3 gap (`Buffered` bounded each stream's incomplete message by the largest + message, and nothing bounded them in aggregate). Rejected: deferring qmux to + that quest, and adding hooks here while keeping leases in m2. +- The lease floor is one 1 MiB segment for every transport and version: the + lite-01/02 `ANNOUNCE_INIT` cap drops from 64 MiB to a segment less its 16 + bytes of prefixes, so `moq_net::transport::MIN_RECEIVE_WINDOW` is 1 MiB. A + legacy origin whose initial set exceeds it fails its announce loudly, on + both ends. Rejected: a 4 MiB cap, and keeping 64 MiB with a per-version + floor. +- Window defaults are one per-session number across native QUIC and qmux: + clients get a 4 MiB connection and a 1 MiB stream receive window, and qmux + takes the same client/peer split as native QUIC. The cluster peer windows + (`[limits.peer.quic]`, 640 MiB) and the client `send_window` (16 MiB) are + re-derived from measured bandwidth-delay needs, with the maintainer + suggesting about 16 MiB for peers. Rejected for clients: 16, 32, or 128 MiB + connection windows, and a 4 MiB stream window. +- Local readiness takes the next frame's `frame::Info` + (`group::Producer::poll_ready(waiter, &info)` and `ready(&info)`), reusing + ingest's admission semantics, including its whole-frame deadlock exception. + Rejected: a bare size on the same methods. +- Whole-batch fills move to [flights](/quest/m1/unbounded-groups/flights.md), + since the failure crosses groups while this mechanism paces one group copy. + +The adapter inventory this plan once listed is stale: moq-tokio has no Quinn +backend, and moq-ffi drives sessions through moq-tokio rather than a +transport of its own, so neither needs an adapter. Iroh, which returns credit +as it reads, leaves with its removal +([moq-dev/moq#7345](https://github.com/moq-dev/moq/pull/7345)); until then its +ingest never pauses. + +Decided 2026-11-02 (maintainer, settling the open items of +[moq-dev/moq#7624](https://github.com/moq-dev/moq/pull/7624)): + +- Peer windows stay at 16 MiB receive and send in `[limits.peer.quic]`, with + the 1 MiB stream window from `[quic]`. Rejected: 32 MiB, and a separate + per-stream peer window. +- The client `send_window` is 4 MiB, matching its receive window. Rejected: + keeping 16 MiB. +- A GROUP stream waiting unread for its TRACK_INFO reserves nothing from the + paused budget: it waits at the lowest receive priority, so a full window + evicts it before control streams, and TRACK_INFO still gets credit. This + replaces the 2026-11-01 wording that the paused cap covers such streams. + Rejected: stopping it once the budget is full. +- Every qmux session a server accepts takes the configured `[quic]` windows, + including the relay's own WebSocket listener. Rejected: leaving them at the + defaults until admission. +- Split: this PR lands the windows, the floor, qmux leases, sized readiness, + TRACK_INFO priority, and the accept paths, and the rest stays here. + Rejected: finishing everything in one PR. + +Landed in [moq-dev/moq#7624](https://github.com/moq-dev/moq/pull/7624): +sized local readiness; the one-segment floor; 4 MiB, 1 MiB, and 4 MiB client +receive, stream, and send windows in moq-tokio, moq-uring, and qmux; 16 MiB +cluster peer windows; native qmux leases with eviction, window reporting, and +`poll_reset`; qmux sessions starting at the server's `[quic]` windows on every +accept path and widening to `[limits.peer.quic]` for peers; and a Bun sender +exhausting a qmux receiver over WebSocket (`just test receive-credit`). At the +client defaults a session pauses two group streams (half the window, a stream +window each), and a peer link eight; past that, groups read and shed as +before pacing. The same PR ranks streams waiting on TRACK_INFO (lite GROUP +streams and FETCH answers, and ietf data streams waiting on their alias) +below every other stream, and they watch `poll_reset`, which now reports an +eviction, so an evicted stream is stopped and eviction resumes. + What remains: -- Receive-window reporting for the transports that report none yet (quinn, - iroh, qmux, WebTransport in the browser, and the FFI adapters), whose ingest - never pauses until they do. Each must also answer - `poll::RecvStream::poll_reset`, or a reset goes unseen while paused. -- A sized readiness check for local writers: `group::Producer::ready()` - waits for room but not for the next frame's size, so a local frame larger - than the room left still sheds what the egress has yet to send. -- JS parity. - A two-relay chain test with memory measured near the window. +- Per-session retained bytes and throughput across many qmux sessions at the + chosen window, in the + [transport x version benches](/quest/m2/bench-transport-sweep.md). - A quiet-host comparison against the base of `group_read_frames`, `group_contended`, and `group_relay_frames`, plus `group_paced` (a writer parked at the window, over 1, 8, and 64 readers), which the first slice added. The cursor's report is one relaxed load per frame or batch, and a `fetch_max` only for the fastest reader. On the busy host that landed it, runs swung by up to 50% either way between repeats. -- Fills that write a whole batch of groups at once evict their own earlier - groups before the reader takes them (the follow-up from - [moq-dev/moq#7339](https://github.com/moq-dev/moq/pull/7339)). Pacing within - a group does not cover this; decide whether it belongs here or with - [flights](/quest/m1/unbounded-groups/flights.md). - -qmux transports carry the same rule through their own stream credit -([qmux receive lease](/quest/m2/qmux-receive-lease.md)). +- JS parity, once the browser offers a receive-credit contract. The JS qmux + receiver could pace today but follows the browser. diff --git a/quest/m1/unbounded-groups/flights.md b/quest/m1/unbounded-groups/flights.md index 54c2f4adf9c..50981b7af3e 100644 --- a/quest/m1/unbounded-groups/flights.md +++ b/quest/m1/unbounded-groups/flights.md @@ -69,6 +69,13 @@ never does. flight without slowing the fast one; no flight fetches a frame twice; a track with no handler refreshes. - Benchmarks: a case swept over readers and how far apart they join. +- Fills that write a whole batch of groups at once evict their own earlier + groups before the reader takes them (the follow-up from + [moq-dev/moq#7339](https://github.com/moq-dev/moq/pull/7339)). Moved here + from [fastest-reader pacing](/quest/m1/unbounded-groups/fastest-reader.md) + on 2026-11-02 (maintainer): the failure crosses groups, while pacing holds + back one group copy, so a flight's fill paces the batch by its riders + instead. Public API: none. Wire: none, beyond the existing frame-offset fills. diff --git a/quest/m2/README.md b/quest/m2/README.md index b9430ee1ffc..3babee0cfcc 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -113,7 +113,6 @@ lite-07; bitrate enforcement is not part of this release. - [A parked read of a resumed group reads its budget without re-syncing](/quest/m2/recover-budget.md) - measure, then drop the per-frame reader sync - [Transcode rungs scale off the runtime and keep bounded state](/quest/m2/transcode-rungs.md) - resize on encoder threads, memoize rungs by shape, one writer per rung track - [One port](/quest/m2/one-port/README.md) - one UDP port carries QUIC, SRT, and WebRTC media through an embedder hook, and one TCP port carries HTTP, RTMP, and RTMPS -- [qmux receive leases](/quest/m2/qmux-receive-lease.md) - WebSocket, TCP, and WebRTC sessions bound incomplete messages by their receive window, at a per-session cost the maintainer picks - [WebSocket caps](/quest/m2/rate-websocket.md) - paced reads and writes over bounded socket buffers hold WebSocket to the same caps - [Publishers learn their cap](/quest/m2/rate-grant.md) - the AUTH grant carries the caps and publishers clamp their encoder to them - [moq-rtc pacing](/quest/m2/rtc-pacing.md) - a capped WHEP viewer never receives more than its cap plus burst, NACK resends included diff --git a/quest/m2/qmux-receive-lease.md b/quest/m2/qmux-receive-lease.md deleted file mode 100644 index f939a11ca4f..00000000000 --- a/quest/m2/qmux-receive-lease.md +++ /dev/null @@ -1,73 +0,0 @@ -# [M] qmux leases receive memory - -## Goal - -qmux sessions (WebSocket, `tcp://`, `tls://`, `unix://`, and WebRTC) bound -what a peer's incomplete messages hold by the session's receive window, and -evict the lowest-priority holder when that window is stuck, as native QUIC -does after [moq-dev/moq#5878](https://github.com/moq-dev/moq/pull/5878). The -per-session memory a peer can make us hold is a deliberate, documented number, -chosen after weighing its cost against today's. - -## Plan - -[moq-dev/moq#5878](https://github.com/moq-dev/moq/pull/5878) merged on -2026-10-22; re-read the landed lease API before designing. - -State with #5878: qmux's `RecvStream` wraps its reader in moq-net's -`poll::Buffered`, which returns connection credit as it reads. `max_data` -then bounds only unread bytes. Each stream's incomplete message is bounded by -the largest message alone, so nothing bounds them in aggregate and nothing is -evicted. moq-tokio's `qmux_config` grants 10,000 streams per direction and -keeps qmux's defaults of 1 MiB `max_data` and 256 KiB `max_stream_data`. -qmux already runs on moq-quic's mux `Connection`, whose stream state carries -the lease and eviction machinery the tokio and io_uring drivers use, so the -transport side should be wiring more than new machinery. - -The cost: a leasing transport must offer at least -`moq_net::transport::MIN_RECEIVE_WINDOW`, the lite-01/02 `AnnounceInit` cap -(64 MiB) plus 16 bytes of prefixes. A frame holds credit only for the 1 MiB -storage segment being written, so its size no longer sets the floor. -Native QUIC defaults to 128 MiB. qmux keeps reading its byte stream to see -control frames, so the window, not the socket, is what bounds the bytes a -slow reader's peer parks in userspace. Going from 1 MiB to 64 MiB multiplies -that by every browser fallback, TCP, and WebRTC session a relay serves. - -The 2026-10-27 security audit (NET-3) reached the same gap from -`poll::Buffered::poll_lease`, which buffers a peer-declared message size up -to the lite-01/02 `AnnounceInit` cap with nothing bounding the total across -streams; this quest owns it. - -Bring the options to the maintainer with a recommendation before building: - -- **The native floor.** Lease at `MIN_RECEIVE_WINDOW` or native's 128 MiB. - Simplest, and one behavior across transports, at the largest allowance. -- **A smaller qmux floor.** Only lite-01/02 needs the 64 MiB term; frames - need only a segment. MAX_DATA can only grow, so a session can start at the floor - its version needs and extend once SETUP (or a WebSocket subprotocol) names a - legacy version. A floor below some supported unit must refuse it loudly. -- **Grow on demand.** Advertise a small window and extend MAX_DATA only when - leases block progress, up to the floor, evicting only at the floor. Typical - sessions stay small. Check that ordinary media adds no round trips. - -Bounding `Buffered` in aggregate with a cap that only refuses is not an -option: it gives no backpressure. Lowering a protocol or frame size limit to -shrink the floor is a separate maintainer decision. - -Validate the configured windows against whatever floor is chosen and fail -loudly, as moq-tokio's `ReceiveWindowTooSmall` does, and align moq-tokio's -`qmux_config` and WebRTC config with it. Cover the native receive-credit -cases over qmux: many partial messages filling the window, eviction by -priority, and progress after eviction, with a JS sender over WebSocket. -Measure retained bytes per session and throughput across many sessions for -the chosen window. - -Public API: qmux's `RecvStream` leases natively instead of through -`Buffered`, and its window defaults or validation change (with a migration note). Wire: -QMux MAX_DATA and STOP_SENDING only, no MoQ format change. - - -## Related - -- [WebSocket caps](/quest/m2/rate-websocket.md) - paced reads on the same sessions, inside the window -- [Transport x version benches](/quest/m2/bench-transport-sweep.md) - where a per-session memory and throughput sweep belongs diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index b71f23912f6..17e25c9c834 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -4,6 +4,21 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Local readiness takes the next frame ([#7624](https://github.com/moq-dev/moq/pull/7624)) + +`group::Producer::poll_ready(waiter)` and `ready()` take the next frame's `&frame::Info`: `poll_ready(waiter, &info)` and `ready(&info).await`. +They wait until that frame fits within 16 MB of the fastest reader, so a large frame no longer sheds what the reader has yet to take. Pass the same `Info` to `create_frame`, or one with the payload's size before `write_frame`. + +### A legacy announcement set fits one segment ([#7624](https://github.com/moq-dev/moq/pull/7624)) + +lite-01/02 `ANNOUNCE_INIT` is capped at 1 MiB less its 16 bytes of prefixes, down from 64 MiB, on both ends: an origin whose initial set exceeds it fails the announce request, and a peer that sends one is refused. Serve such an origin over lite-03 or later, which announces one broadcast per message. +`transport::MIN_RECEIVE_WINDOW` is now one 1 MiB segment, so a leasing transport may offer a connection window that small. + +### `poll_reset` reports an eviction ([#7624](https://github.com/moq-dev/moq/pull/7624)) + +`transport::poll::RecvStream::poll_reset` also completes once the receiver evicts the stream to free a stuck window. +A leasing transport must report the eviction there: a paused group stream, and one waiting unread for its TRACK\_INFO, is watched only through it, and an evicted stream whose reader holds leased bytes keeps further eviction off until it is stopped. + ### A bounded subscription ends once its ranges are read When every one of a subscription's `ranges` has an end, `track::Subscriber::recv_group` and `track::Ordered::next_group` return `None` once each group in them was handed out, dropped, skipped as stale, or given up as unfillable, instead of waiting for an update to widen the ranges. diff --git a/rs/moq-net/benches/group.rs b/rs/moq-net/benches/group.rs index d695fbe8d5e..f7faa6c36df 100644 --- a/rs/moq-net/benches/group.rs +++ b/rs/moq-net/benches/group.rs @@ -915,8 +915,12 @@ fn bench_paced(c: &mut Criterion) { let writer = kio::Waiter::new(wakes.clone().into()); let reader = kio::Waiter::noop(); let mut parked = 0u64; + let next = moq_net::frame::Info { + size: payload.len() as u64, + timestamp: Some(Timestamp::ZERO), + }; b.iter(|| { - parked += ctx.group.poll_ready(&writer).is_pending() as u64; + parked += ctx.group.poll_ready(&writer, &next).is_pending() as u64; for consumer in &mut consumers { let Poll::Ready(Ok(Some(frame))) = consumer.poll_read_frame(&reader) else { panic!("a frame is ready"); diff --git a/rs/moq-net/src/coding/reader.rs b/rs/moq-net/src/coding/reader.rs index 0457e4e9f97..ee9f5b3202d 100644 --- a/rs/moq-net/src/coding/reader.rs +++ b/rs/moq-net/src/coding/reader.rs @@ -561,6 +561,14 @@ impl Reader { Poll::Ready(Ok(lease.has_remaining())) } + /// Poll until the stream fails without reading it: reset, evicted, or its session closed. + /// For a stream left unread until a control message says how to read it, so a failure + /// still ends it. See [`RecvStream::poll_reset`]. + pub(crate) fn poll_reset(&mut self, waiter: &kio::Waiter) -> Poll { + let err = ready!(self.stream.poll_reset(waiter)); + Poll::Ready(self.version.transport_error(err)) + } + /// Rank the stream for eviction under receive pressure. See [`RecvStream::set_priority`]. pub fn set_priority(&mut self, priority: i64) { self.stream.set_priority(priority); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 226568811cf..d839a80ba88 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -1526,11 +1526,12 @@ pub fn seeds() -> Vec { // Length prefixes at and one past each cap, ahead of a body far too short for them, so // the fuzzer starts beside every rejection path instead of searching for its bound: a - // moq-lite message's size (0xffff for most, 64 MiB for ANNOUNCE_INIT) and a + // moq-lite message's size (0xffff for most, a segment for ANNOUNCE_INIT) and a // moq-transport control message's two-byte length. for (index, version) in LITE_VERSIONS.iter().enumerate() { for kind in 0..LITE_KINDS { - for size in limits(&[0xffff, 64 << 20]) { + let announce_init = ::MAX_SIZE as u64; + for size in limits(&[0xffff, announce_init]) { let mut data = vec![index as u8, kind]; Encoder::new(&mut data, (*version).into()).varint(size).unwrap(); data.extend_from_slice(&[0; 4]); diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 75118c6434a..8f8a3db3a6f 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -4571,8 +4571,9 @@ impl Subscriber { stream: &mut Reader, ) -> Result<(), Error> { let group: ietf::GroupHeader = stream.decode().await?; - // Ranked as the least urgent subscription until its alias resolves. - stream.set_priority(crate::priority::recv_group(i8::MIN, group.group_id, Default::default())); + // Parked below every other stream until its alias resolves, so a full window stops it + // before the SUBSCRIBE_OK or PUBLISH it waits on. + stream.set_priority(crate::priority::RECV_PARKED); if group.sub_group_id != 0 { tracing::warn!(sub_group_id = %group.sub_group_id, "subgroup ID is not supported, dropping stream"); @@ -4582,7 +4583,15 @@ impl Subscriber { // SUBSCRIBE_OK or PUBLISH can be reordered behind this stream. Hold only the // subgroup header while waiting so the data stream cannot consume flow control. let aliases = self.state.lock().aliases.consume(); - let request_id = match resolve_track_alias(&self.runtime, aliases, group.track_alias).await { + let mut resolving = std::pin::pin!(resolve_track_alias(&self.runtime, aliases, group.track_alias)); + // Unread meanwhile, so a reset or an eviction would otherwise go unseen, and an evicted + // stream holds further eviction off until it is stopped. + let resolved = kio::wait(|waiter| match waiter.poll_future(resolving.as_mut()) { + Poll::Ready(resolved) => Poll::Ready(Ok(resolved)), + Poll::Pending => stream.poll_reset(waiter).map(Err), + }) + .await?; + let request_id = match resolved { Ok(request_id) => request_id, // Ours: we cancelled the subscription and the publisher has not stopped yet. Err(err @ Error::Cancel) => { diff --git a/rs/moq-net/src/lite/announce.rs b/rs/moq-net/src/lite/announce.rs index c63cf127c5d..83c962eb072 100644 --- a/rs/moq-net/src/lite/announce.rs +++ b/rs/moq-net/src/lite/announce.rs @@ -601,8 +601,11 @@ impl AnnounceInit { impl<'a> Message<'a> for AnnounceInit { const CONTROL: Option = Some(crate::stats::Message::Announce); - // Published lite01/02 carry the whole initial set in one message. - const MAX_SIZE: usize = 64 * 1024 * 1024; + // Published lite01/02 carry the whole initial set in one message. Capped so it fits one + // storage segment with its type and size prefixes (a varint of at most 8 bytes each), which + // keeps the smallest receive window a leasing transport may offer at one segment. A larger + // set is refused on both ends rather than raising every session's window for it. + const MAX_SIZE: usize = crate::frame::SEGMENT - 16; fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { Self::decode_suffixes(r, version, usize::MAX) diff --git a/rs/moq-net/src/lite/message.rs b/rs/moq-net/src/lite/message.rs index fd173efa4dc..725a4646ec0 100644 --- a/rs/moq-net/src/lite/message.rs +++ b/rs/moq-net/src/lite/message.rs @@ -152,15 +152,25 @@ mod tests { assert!(matches!(err, DecodeError::MessageTooLarge { .. }), "{err:?}"); } - /// ANNOUNCE_INIT carries the whole initial set in one message, so it keeps the room - /// a large origin needs. + /// ANNOUNCE_INIT carries the whole initial set in one message, so it keeps the room of a + /// storage segment, and no more. #[test] fn announce_init_waits_for_a_large_body() { - let mut wire = Vec::new(); - Encoder::new(&mut wire, Version::Lite02.into()) - .varint(1024 * 1024) - .unwrap(); - let err = super::super::AnnounceInit::decode_slice(&wire, Version::Lite02).unwrap_err(); - assert!(matches!(err, DecodeError::Short), "{err:?}"); + let max = ::MAX_SIZE; + let decode = |size: usize| { + let mut wire = Vec::new(); + Encoder::new(&mut wire, Version::Lite02.into()) + .varint(size as u64) + .unwrap(); + super::super::AnnounceInit::decode_slice(&wire, Version::Lite02).unwrap_err() + }; + assert!( + matches!(decode(max), DecodeError::Short), + "within the cap waits for the body" + ); + assert!( + matches!(decode(max + 1), DecodeError::MessageTooLarge { .. }), + "past the cap is refused" + ); } } diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index ad28adeff3c..6ed08f37ac0 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -7949,6 +7949,43 @@ mod tests { ); } + /// An initial set past ANNOUNCE_INIT's one-segment cap fails the announce loudly, + /// rather than sending a message the peer refuses or silently dropping broadcasts. + #[kio::test] + async fn announce_init_past_the_cap_fails() { + let self_origin = crate::Hop::new(1).unwrap(); + let origin = crate::origin::Config::new(self_origin).produce(); + let max = ::MAX_SIZE; + // Each suffix costs its length plus a varint prefix, so these overflow the cap. + let name = "x".repeat(500); + let _broadcasts: Vec<_> = (0..max / 500 + 1) + .map(|i| { + origin + .announce(format!("{name}{i}"), crate::origin::Route::default()) + .unwrap() + }) + .collect(); + + let gate = kio::Producer::new(true); + let session = SinkSession::gated_bi(gate.consume()); + let mut stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), Version::Lite02) + .await + .unwrap(); + let consumer = origin.consume(); + let mut announced = consumer.announced(); + let err = Publisher::run_announce( + &mut stream, + &consumer, + &mut announced, + self_origin, + Version::Lite02, + None, + ) + .await + .expect_err("an initial set past the cap"); + assert!(matches!(err, Error::Encode(crate::EncodeError::TooLarge)), "{err:?}"); + } + /// A broadcast sent in ANNOUNCE_INIT is live like any other, so its end reaches /// the peer instead of being treated as never advertised. #[kio::test] diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index fdb2dd74fa7..c202bfe075a 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -290,6 +290,28 @@ impl TrackEntry { opened } + /// Withdraw the parked stream `ticket`, which failed before taking its group: a group the + /// accept already opened for it ends with `err` rather than waiting for a writer. + fn withdraw(&mut self, ticket: u64, err: &Error) { + let Some(parked) = &self.parked else { + return; + }; + let (opened, done) = match parked.write() { + Ok(mut state) => { + state.waiting.retain(|(waiting, _)| *waiting != ticket); + let opened = state.opened.remove(&ticket); + (opened, state.opened.is_empty() && state.waiting.is_empty()) + } + Err(_) => return, + }; + if let Some(Ok(opened)) = opened { + let _ = opened.group.abort(err.clone()); + } + if done { + self.parked = None; + } + } + /// Open the group of a stream whose header is `hdr` in the copy. fn open(&mut self, hdr: &lite::Group, version: Version, now: crate::time::Instant) -> Result { let Some(Accepted { producer, framing }) = &mut self.copy else { @@ -1284,7 +1306,11 @@ impl Drop for SetupWait { #[kio::machine(self.state: GroupRecvState)] impl GroupRecv { - fn new(subscriber: Subscriber, reader: Reader) -> Self { + fn new(subscriber: Subscriber, mut reader: Reader) -> Self { + // Until its group opens, the stream may wait unread on the peer's SETUP or its + // subscription's TRACK_INFO, so a full window stops it before the stream either + // arrives on. + reader.set_priority(crate::priority::RECV_PARKED); Self { subscriber, reader, @@ -1300,7 +1326,8 @@ impl GroupRecv { let Poll::Ready(ietf) = self.subscriber.peer_setup.poll_ietf(waiter) else { let waits = &self.subscriber.setup_waits; self.setup_wait.get_or_insert_with(|| SetupWait::new(waits)); - return Poll::Pending; + // Unread meanwhile, so a reset or an eviction would otherwise go unseen. + return self.reader.poll_reset(waiter).map(Err); }; self.setup_wait = None; self.reader.poll_decode::(waiter).map_ok(|hdr| (ietf, hdr)) @@ -1319,17 +1346,26 @@ impl GroupRecv { } }; // Closed once the subscription went, which the lookup below reports. - let _ = wait!(Accept => match parked { - Some((parked, ticket)) => parked - .poll(waiter, |parked| match parked.opened.contains_key(ticket) { - true => Poll::Ready(()), - false => Poll::Pending, - }) - .map(|res| res.is_ok()), - None => Poll::Ready(true), + let accepted: Result<(), Error> = wait!(Accept => match parked { + Some((parked, ticket)) => match parked.poll(waiter, |parked| match parked.opened.contains_key(ticket) { + true => Poll::Ready(()), + false => Poll::Pending, + }) { + Poll::Ready(_) => Poll::Ready(Ok(())), + // Unread meanwhile, so a reset or an eviction would otherwise go unseen, and an + // evicted stream holds further eviction off until it is stopped. + Poll::Pending => self.reader.poll_reset(waiter).map(Err), + }, + None => Poll::Ready(Ok(())), }); self.park_wait = None; let (ietf, hdr) = header; + if let (Err(err), Some((_, ticket))) = (&accepted, &parked) + && let Some(entry) = self.subscriber.subscribes.lock().get_mut(&hdr.subscribe) + { + entry.withdraw(*ticket, err); + } + accepted?; let opened = { let mut subs = self.subscriber.subscribes.lock(); @@ -4965,6 +5001,67 @@ mod tests { ); } + /// A parked stream that fails before taking its group withdraws it: the accept opens none + /// for it, or the group it opened ends with the stream's error, and the rest still open. + #[kio::test] + async fn a_failed_parked_stream_withdraws_its_group() { + let version = Version::Lite07; + let header = |sequence| lite::Group { + subscribe: 0, + sequence, + frame_start: 0, + }; + let mut entry = TrackEntry::new(None, Default::default()); + let (_early_wait, early) = entry.park(&header(1)); + let (_late_wait, late) = entry.park(&header(2)); + let (_kept_wait, kept) = entry.park(&header(3)); + entry.withdraw(early, &Error::Cancel); + + let track = crate::track::Producer::new( + Arc::new(crate::broadcast::Info::default()), + "video", + crate::track::Info::default().with_timescale(Timescale::MILLI), + ); + let every = Subscription::default() + .with_order(crate::track::Order::Ascending) + .with_start(0) + .with_max_delay(Duration::from_secs(60)); + let mut consumer = track.consume().subscribe(every).await.unwrap(); + let framing = lite::Framing::new(track.info(), version); + entry.accept( + Accepted { + producer: track.clone(), + framing, + }, + version, + crate::time::sim().now(), + ); + let mut opened: Vec = match entry.parked.as_ref().unwrap().write() { + Ok(state) => state.opened.keys().copied().collect(), + Err(_) => panic!("the entry holds the parked streams"), + }; + opened.sort_unstable(); + assert_eq!(opened, [late, kept], "nothing opened for the withdrawn header"); + + entry.withdraw(late, &Error::Evicted); + let opened = match entry.parked.as_ref().unwrap().write() { + Ok(state) => state.opened.keys().copied().collect::>(), + Err(_) => panic!("the entry holds the parked streams"), + }; + assert_eq!(opened, [kept], "the withdrawn group is gone"); + let Opened { mut group, .. } = entry.unpark(kept).expect("the kept group"); + group.finish().unwrap(); + track.finish().unwrap(); + // Only the kept group reaches the subscriber: the withdrawn one ended, so nothing + // waits on it. + let mut sequences = Vec::new(); + while let Some(group) = consumer.recv_group().await.unwrap() { + sequences.push(group.sequence); + } + assert_eq!(sequences, [3]); + assert!(entry.parked.is_none(), "released once the last stream took its group"); + } + /// TRACK opens its stream before the fetches sent alongside it: TRACK fails fast without /// stream credit, where a fetch waits for it, so fetches opened first could leave TRACK /// none and fail the track with every fetch. @@ -9884,13 +9981,16 @@ impl kio::Task for FetchServeRun { } let FetchRunState::Send { request, - stream, + mut stream, frame_start, frame_end, } = std::mem::replace(&mut self.state, FetchRunState::Done) else { unreachable!() }; + // The answer may wait unread on the TRACK_INFO sent alongside, so a full + // window stops it before that TRACK_INFO. + stream.reader.set_priority(crate::priority::RECV_PARKED); self.state = FetchRunState::Answer { request, stream, @@ -9900,8 +10000,13 @@ impl kio::Task for FetchServeRun { } FetchRunState::Answer { stream, .. } => { // The answer is framed in the track's units: it stays unread in the stream - // until the TRACK_INFO sent alongside lands. - let info = match ready!(self.info.poll(waiter)) { + // until the TRACK_INFO sent alongside lands, watched meanwhile so a reset or + // an eviction still ends it. + let info = match self.info.poll(waiter) { + Poll::Ready(info) => info, + Poll::Pending => Err(ready!(stream.reader.poll_reset(waiter))), + }; + let info = match info { Ok(info) => info, Err(err) => { let FetchRunState::Answer { @@ -9924,7 +10029,7 @@ impl kio::Task for FetchServeRun { let answered = ready!(stream.reader.poll_has_more(waiter)); let FetchRunState::Answer { request, - stream, + mut stream, frame_start, frame_end, } = std::mem::replace(&mut self.state, FetchRunState::Done) @@ -9932,6 +10037,12 @@ impl kio::Task for FetchServeRun { unreachable!() }; let request = request.expect("request pending"); + // Read now, so ranked as the group it fills. + stream.reader.set_priority(crate::priority::recv_group( + request.priority(), + self.group, + Default::default(), + )); if let Err(err) = answered { tracing::debug!(track = %self.serve.name, group = self.group, %err, "fetch refused"); stream.writer.abort(&err); diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index c6c965f9724..30ce54706ae 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -2218,24 +2218,24 @@ impl Producer { pub async fn closed(&self) -> Result<()> { kio::wait(|waiter| self.handle.poll_closed(waiter)).await } - /// Poll until a reader holds the group and the writer is at most 16 MB past the end of - /// the last frame its fastest reader took, so a local writer runs no further ahead than - /// the live tail keeps behind that reader. + /// Poll until a reader holds the group and the next frame, described by `frame`, fits + /// within 16 MB past the end of the last frame its fastest reader took, so a local writer + /// runs no further ahead than the live tail keeps behind that reader. /// /// Ready once the group is closed, so the next write surfaces why. A reader that took - /// every committed frame is waiting on the writer, so it never holds the writer back, - /// and one whose next frame was shed has fallen off the group and is not waited on. - /// Writing without waiting never fails: the group sheds its front instead, as it also - /// does for a frame larger than the room left, which trims the tail to fit. - pub fn poll_ready(&self, waiter: &kio::Waiter) -> Poll<()> { - ready!(self.poll_paced(waiter, 0)); + /// every committed frame is waiting on the writer, so it never holds the writer back, even + /// for a frame larger than the window, and one whose next frame was shed has fallen off + /// the group and is not waited on. Writing without waiting never fails: the group sheds + /// its front instead. + pub fn poll_ready(&self, waiter: &kio::Waiter, frame: &frame::Info) -> Poll<()> { + ready!(self.poll_paced(waiter, frame.size)); // A closed group reports `Closed`, which is ready too. self.handle.state.poll_wanted_changed(waiter, false).map(|_| ()) } - /// Wait until a reader holds the group and the writer has room; see [`Self::poll_ready`]. - pub async fn ready(&self) { - kio::wait(|waiter| self.poll_ready(waiter)).await + /// Wait until a reader holds the group and `frame` fits; see [`Self::poll_ready`]. + pub async fn ready(&self, frame: &frame::Info) { + kio::wait(|waiter| self.poll_ready(waiter, frame)).await } /// Poll until the writer may take more from upstream, `partial` bytes into a frame diff --git a/rs/moq-net/src/model/pace.rs b/rs/moq-net/src/model/pace.rs index bb1ca8ff938..3118ca44fc6 100644 --- a/rs/moq-net/src/model/pace.rs +++ b/rs/moq-net/src/model/pace.rs @@ -160,6 +160,13 @@ mod tests { group } + fn info(size: usize) -> frame::Info { + frame::Info { + size: size as u64, + timestamp: None, + } + } + fn read(reader: &mut group::Consumer) { let Poll::Ready(Ok(Some(_))) = reader.poll_read_frame(&kio::Waiter::noop()) else { panic!("a committed frame is ready"); @@ -290,24 +297,63 @@ mod tests { #[test] fn a_local_writer_waits_for_a_reader_and_room() { let (flag, waiter) = flag(); + let small = info(0); let mut group = group::Info { sequence: 0 }.produce_untimed(); - assert_eq!(group.poll_ready(&waiter), Poll::Pending, "nobody reads yet"); + assert_eq!(group.poll_ready(&waiter, &small), Poll::Pending, "nobody reads yet"); let mut reader = group.consume(); assert!(flag.woken(), "a reader arriving wakes the writer"); - assert_eq!(group.poll_ready(&waiter), Poll::Ready(())); + assert_eq!(group.poll_ready(&waiter, &small), Poll::Ready(())); for _ in 0..17 { group.write_frame(None, Bytes::from(vec![0u8; MB])).unwrap(); } - assert_eq!(group.poll_ready(&waiter), Poll::Pending, "past the window"); + assert_eq!(group.poll_ready(&waiter, &small), Poll::Pending, "past the window"); read(&mut reader); assert!(flag.woken(), "the reader's read wakes the writer"); - assert_eq!(group.poll_ready(&waiter), Poll::Ready(())); + assert_eq!(group.poll_ready(&waiter, &small), Poll::Ready(())); drop(reader); - assert_eq!(group.poll_ready(&waiter), Poll::Pending, "the reader left"); + assert_eq!(group.poll_ready(&waiter, &small), Poll::Pending, "the reader left"); group.handle().abort(Error::Cancel).unwrap(); - assert_eq!(group.poll_ready(&waiter), Poll::Ready(()), "a closed group never waits"); + assert_eq!( + group.poll_ready(&waiter, &small), + Poll::Ready(()), + "a closed group never waits" + ); + } + + /// A local writer waits until its next frame fits in the room left, not just until some + /// room is left, so a large frame never sheds what the fastest reader has yet to take. + /// A reader at the edge is waiting on the writer, so a frame larger than the whole window + /// still goes ahead. + #[test] + fn a_local_writer_waits_for_room_for_its_frame() { + let (_flag, waiter) = flag(); + let mut group = group::Info { sequence: 0 }.produce_untimed(); + let mut reader = group.consume(); + group.write_frame(None, Bytes::from(vec![0u8; MB])).unwrap(); + read(&mut reader); + for _ in 0..8 { + group.write_frame(None, Bytes::from(vec![0u8; MB])).unwrap(); + } + assert_eq!( + group.poll_ready(&waiter, &info(MB)), + Poll::Ready(()), + "a small frame fits" + ); + assert_eq!( + group.poll_ready(&waiter, &info(8 * MB)), + Poll::Pending, + "a frame larger than the room left waits" + ); + for _ in 0..8 { + read(&mut reader); + } + assert_eq!( + group.poll_ready(&waiter, &info(20 * MB)), + Poll::Ready(()), + "a reader that took every frame never stalls a frame larger than the window" + ); } /// A reader that took every committed frame is waiting on the writer, so a frame diff --git a/rs/moq-net/src/priority.rs b/rs/moq-net/src/priority.rs index cd09c2864c1..c8e7e903970 100644 --- a/rs/moq-net/src/priority.rs +++ b/rs/moq-net/src/priority.rs @@ -526,6 +526,11 @@ impl PriorityHandle { /// stream sits below. pub const RECV_CONTROL: i64 = i64::MAX; +/// The receive priority of a group stream held unread until a control message says how to +/// read it, such as its subscription's TRACK_INFO: below every other stream, so a full +/// receive window stops it first and the control message it waits on still gets credit. +pub const RECV_PARKED: i64 = i64::MIN; + /// The receive priority of a group stream: always below a control stream or an /// unidentified one, ranked by its subscription's `priority` and then its `order`, like the /// sender ranks it. A [`Order::Descending`] subscription gives up its oldest group first, @@ -609,8 +614,8 @@ mod tests { } } - /// Every group sits below an unidentified stream, and so below a control stream, with - /// more urgent subscriptions and then newer groups kept longer. + /// Every group sits below an unidentified stream, and so below a control stream, and above + /// a parked one, with more urgent subscriptions and then newer groups kept longer. #[test] fn recv_group_stays_below_unidentified_streams() { for order in [Order::Descending, Order::Ascending] { @@ -619,8 +624,8 @@ mod tests { assert!(recv_group(i8::MIN, u64::MAX, order) < recv_group(-1, 0, order)); assert!(recv_group(i8::MAX, 0, order) < 0); assert!(recv_group(i8::MAX, u64::MAX, order) < 0); - assert!(recv_group(i8::MIN, 0, order) > i64::MIN); - assert!(recv_group(i8::MIN, u64::MAX, order) > i64::MIN); + assert!(recv_group(i8::MIN, 0, order) > RECV_PARKED); + assert!(recv_group(i8::MIN, u64::MAX, order) > RECV_PARKED); } // The group the subscription would deliver last is the first to go. assert!(recv_group(0, 0, Order::Descending) < recv_group(0, 1, Order::Descending)); diff --git a/rs/moq-net/src/transport.rs b/rs/moq-net/src/transport.rs index c640e1e1e8b..372fbdbf156 100644 --- a/rs/moq-net/src/transport.rs +++ b/rs/moq-net/src/transport.rs @@ -162,12 +162,13 @@ impl ReceiveWindow { } } -/// The smallest connection receive window a transport that leases may offer. +/// The smallest connection receive window a transport that leases may offer: one 1 MiB +/// storage segment. /// /// A session keeps each unit it is still assembling leased, and a unit larger than the -/// window can never complete, so the window has to fit the largest one with its framing: -/// a legacy announcement set, past any frame segment. A frame returns its credit a -/// segment at a time, so its size never matters here. +/// window can never complete, so the window has to fit the largest one with its framing. +/// A frame returns its credit a segment at a time, so its size never matters here, and a +/// legacy announcement set is capped to fit the same segment. pub const MIN_RECEIVE_WINDOW: u64 = { // A type and a size prefix, each a varint of at most 8 bytes. const PREFIXES: u64 = 16; @@ -866,12 +867,13 @@ pub mod poll { /// Call this method manually if you want to specify a code yourself. fn stop(&mut self, code: u32); - /// Poll until the stream fails without reading it: the peer resets it, or the - /// connection closes. Pending for good once the peer finished it. + /// Poll until the stream fails without reading it: the peer resets it, the receiver + /// evicts it to free a stuck window, or the connection closes. Pending for good once the + /// peer finished it. /// - /// A session that stops reading a stream to pace it (see - /// [`Session::receive_window`]) watches this instead, so a reset still ends the group - /// it carries. It takes no bytes and grants no credit. The default never completes, + /// A session that stops reading a stream, to pace it (see [`Session::receive_window`]) + /// or until a control message says how to read it, watches this instead, so a reset + /// still ends the group it carries and an eviction stops the stream. It takes no bytes and grants no credit. The default never completes, /// which suits a transport reporting no receive window, since none of its streams pause. fn poll_reset(&mut self, waiter: &kio::Waiter) -> Poll { let _ = waiter; diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index ca642eda946..e89dfec0323 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -4,6 +4,11 @@ How to update your code for each breaking change, newest first. ## Unreleased +### An evicted stream fails `received_reset` ([#7624](https://github.com/moq-dev/moq/pull/7624)) + +`recv::Stream::received_reset` returns `Err(Error::Evicted)` for a stream evicted to free a stuck receive window, until the application stops it, rather than `Err(Error::Closed)`. +Treat it as a failed read and stop the stream: a reader that watches for a reset instead of reading would otherwise leave its leased bytes holding further eviction off. + ### Early data is removed Wait for `Event::Connected` before opening streams or sending application data. diff --git a/rs/moq-quic/src/connection/mux/mod.rs b/rs/moq-quic/src/connection/mux/mod.rs index d0de1852e72..32abe827393 100644 --- a/rs/moq-quic/src/connection/mux/mod.rs +++ b/rs/moq-quic/src/connection/mux/mod.rs @@ -405,6 +405,52 @@ impl Connection { } } + /// The connection-wide receive window now in force; see [`Limits::receive_window`] + pub fn receive_window(&self) -> u64 { + self.streams.receive_window() + } + + /// The most receive credit one stream may hold: the largest per-stream window ever in force, + /// since credit already granted outlives a shrink; see [`Limits::stream_receive_window`] + pub fn stream_receive_window(&self) -> u64 { + self.streams.peak_stream_receive_window() + } + + /// Receive flow control as the receiver sees it, evictions included + pub fn credit_stats(&self) -> super::CreditStats { + self.streams.credit_stats() + } + + /// Change the windows and stream counts `limits` sets, leaving the others as they are + /// + /// Errors, changing nothing, on limits [`Limits::validate`] refuses. QMux never paces, so + /// the rates are ignored. + pub fn set_limits(&mut self, limits: Limits) -> Result<(), crate::LimitsError> { + limits.validate()?; + if let Some(window) = limits.receive_window + && self + .streams + .set_receive_window(VarInt::from_u64(window.get()).expect("validated")) + { + self.pending.max_data = true; + } + if let Some(window) = limits.stream_receive_window { + self.streams.set_stream_receive_window(window.get()); + } + if let Some(window) = limits.send_window { + self.streams.set_send_window(window.get()); + } + if let Some(streams) = limits.max_streams { + let streams = VarInt::from_u64(streams.get()).expect("validated"); + for dir in [Dir::Bi, Dir::Uni] { + self.streams.set_max_concurrent(dir, streams); + } + // A lower limit can make a credit update once deemed insignificant significant. + self.streams.queue_max_stream_id(&mut self.pending); + } + Ok(()) + } + /// The receive half of stream `id` pub fn recv_stream(&mut self, id: crate::StreamId) -> recv::Stream<'_> { recv::Stream { diff --git a/rs/moq-quic/src/connection/streams/recv.rs b/rs/moq-quic/src/connection/streams/recv.rs index 44644de5913..092f579cafc 100644 --- a/rs/moq-quic/src/connection/streams/recv.rs +++ b/rs/moq-quic/src/connection/streams/recv.rs @@ -83,7 +83,9 @@ impl Stream<'_> { /// Check whether this stream has been reset by the peer, returning the reset error code if so /// /// After returning `Ok(Some(_))` once, stream state will be discarded and all future calls will - /// fail with [`Error::Closed`]. + /// fail with [`Error::Closed`]. A stream evicted to free a stuck window fails with + /// [`Error::Evicted`] until the application stops it, so a reader that watches for a reset + /// instead of reading learns it too: bytes it took hold further eviction off until then. pub fn received_reset(&mut self) -> Result, Error> { let hash_map::Entry::Occupied(entry) = self.state.recv.entry(self.id) else { return Err(Error::Closed); @@ -92,7 +94,10 @@ impl Stream<'_> { return Ok(None); }; if s.stopped { - return Err(Error::Closed); + return Err(match s.lease.evicted { + true => Error::Evicted, + false => Error::Closed, + }); } let Some(code) = s.reset_code() else { return Ok(None); diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index fc0f9b214c9..37eff831a67 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -4097,10 +4097,19 @@ mod tests { assert_eq!(client.credit_stats().leased, 2 * WINDOW, "taken credit stays held"); assert!(matches!(wait(&mut client, ids[0], 1), Err(recv::Error::Blocked))); assert_eq!(client.credit_stats().evicted, 1, "waits for the stop"); + // A reader watching for a reset instead of reading learns of it too. + assert_eq!( + recv(&mut client, &mut pending, ids[1]).received_reset(), + Err(recv::Error::Evicted) + ); assert_eq!(wait(&mut client, ids[1], 1), Err(recv::Error::Evicted)); // The stop pays the debt alone, so eviction resumes with the other stream. let _ = stop(&mut client, ids[1], 0); + assert_eq!( + recv(&mut client, &mut pending, ids[1]).received_reset(), + Err(recv::Error::Closed) + ); assert_eq!(client.local_max_data, max_data); assert_eq!(client.credit_stats().evicted, 2); assert_eq!(readable(&mut client), [ids[0]]); diff --git a/rs/moq-relay/MIGRATE.md b/rs/moq-relay/MIGRATE.md index 87eae779068..226b006254e 100644 --- a/rs/moq-relay/MIGRATE.md +++ b/rs/moq-relay/MIGRATE.md @@ -5,6 +5,14 @@ Rolling a cluster across a release also follows the [upgrade guide](https://moq. ## Unreleased +### Smaller default receive windows ([#7624](https://github.com/moq-dev/moq/pull/7624)) + +`[quic]` defaults to a 4 MiB `receive_window`, a 1 MiB `stream_receive_window`, and a 4 MiB `send_window`, down from 128 MiB, 4 MiB, and 16 MiB, and the smallest `receive_window` accepted here or in `[limits.peer.quic]` is 1 MiB instead of about 64 MiB. +`[limits.peer.quic]` defaults to a 16 MiB `receive_window` and `send_window`, down from 640 MiB. Set the old values to keep them: a link that must carry more than about 450 Mbps at a 250 ms RTT needs a larger window. +WebSocket, `tcp://`, `tls://`, and `unix://` sessions take the `[quic]` windows and stream count instead of qmux's own 1 MiB and 256 KiB, cancel the least urgent stream when incomplete messages fill the window as QUIC does, and peers over them widen to `[limits.peer.quic]`. +An embedder that builds `web::Web` itself passes the relay's `[quic]` with `Web::with_quic`; without it, WebSocket sessions start at the `[quic]` defaults until admitted. +A lite-01/02 peer whose initial announcement set exceeds 1 MiB is refused; it needs lite-03 or later. + ### `Cluster::new` returns a driver to poll ([#7626](https://github.com/moq-dev/moq/pull/7626)) `Cluster::new` no longer spawns the origin's lifecycle work or the cache governor; it returns `(Cluster, Driver)`, and nothing runs until the driver is polled. diff --git a/rs/moq-relay/src/limits.rs b/rs/moq-relay/src/limits.rs index 07d10587fa4..561825c0ef8 100644 --- a/rs/moq-relay/src/limits.rs +++ b/rs/moq-relay/src/limits.rs @@ -96,7 +96,7 @@ pub struct Quic { #[usage( long = "limits-peer-quic-receive-window", env = "MOQ_LIMITS_PEER_QUIC_RECEIVE_WINDOW", - default = "671088640", + default = "16777216", setting = "limits.peer.quic.receive_window" )] pub receive_window: std::num::NonZeroU64, @@ -104,7 +104,7 @@ pub struct Quic { #[usage( long = "limits-peer-quic-send-window", env = "MOQ_LIMITS_PEER_QUIC_SEND_WINDOW", - default = "671088640", + default = "16777216", setting = "limits.peer.quic.send_window" )] pub send_window: std::num::NonZeroU64, @@ -112,9 +112,10 @@ pub struct Quic { impl Default for Quic { fn default() -> Self { - // Well above the `[quic]` defaults, which are sized for one viewer rather - // than a link carrying a whole cluster's traffic. - let window = std::num::NonZeroU64::new(640 << 20).expect("non-zero"); + // Above the `[quic]` defaults, which are sized for one viewer rather than a link + // carrying a whole cluster's traffic: about 450 Mbps at a 250 ms RTT, and a paused + // stream budget of half of it for the groups slow readers hold back. + let window = std::num::NonZeroU64::new(16 << 20).expect("non-zero"); Self { max_streams: std::num::NonZeroU64::new(10 * moq_tokio::quic::DEFAULT_MAX_STREAMS).expect("non-zero"), receive_window: window, diff --git a/rs/moq-relay/src/relay.rs b/rs/moq-relay/src/relay.rs index 2da85e07b91..ebdb2be1f36 100644 --- a/rs/moq-relay/src/relay.rs +++ b/rs/moq-relay/src/relay.rs @@ -368,6 +368,7 @@ impl Relay { .with_extensions(server_extensions) .with_implementation(implementation) .with_timeout(handshake_timeout) + .with_quic(&config.quic) .with_sessions(sessions.clone()) .with_tls_acceptor(server.tls_acceptor()); #[cfg(feature = "acme")] diff --git a/rs/moq-relay/src/uring.rs b/rs/moq-relay/src/uring.rs index f2a871d3cdb..211b0448ad1 100644 --- a/rs/moq-relay/src/uring.rs +++ b/rs/moq-relay/src/uring.rs @@ -1079,7 +1079,7 @@ mod tests { set.send_window = Some(32 << 20); for (quic, expected) in [ - (moq_tokio::quic::Config::default(), (128 << 20, 4 << 20, 16 << 20)), + (moq_tokio::quic::Config::default(), (4 << 20, 1 << 20, 4 << 20)), (set, (256 << 20, 8 << 20, 32 << 20)), ] { let resolved = quic.resolve(); diff --git a/rs/moq-relay/src/web.rs b/rs/moq-relay/src/web.rs index 2a445f6c0da..512e82dd05d 100644 --- a/rs/moq-relay/src/web.rs +++ b/rs/moq-relay/src/web.rs @@ -168,6 +168,8 @@ pub(crate) struct WebState { /// handshake and partial headers included), and a WebSocket wait for its MoQ /// SETUP; `None` waits forever. pub(crate) timeout: Option, + /// The `[quic]` windows and stream count each WebSocket session starts with. + pub(crate) quic: moq_tokio::quic::Resolved, } /// The bound addresses of the public web listeners. @@ -221,6 +223,7 @@ impl Web { shutdown: crate::shutdown::Observer::disabled(), sessions: crate::session::Registry::new(), timeout: moq_tokio::listen::Config::default().resolved_timeout(), + quic: Default::default(), }); Self { state, @@ -387,6 +390,15 @@ impl Web { self } + /// Start each WebSocket session at `quic`'s windows and stream count, the relay's + /// `[quic]` section, as its QUIC and `tcp://` sessions do. Defaults to the `[quic]` + /// defaults. + pub fn with_quic(mut self, quic: &moq_tokio::quic::Config) -> Self { + let state = Arc::get_mut(&mut self.state).expect("with_quic called after routes were built"); + state.quic = quic.resolve(); + self + } + /// Register WebSocket sessions in the node's live table so they can be listed /// and nudged. Without it they are served but do not appear. pub fn with_sessions(mut self, sessions: crate::session::Registry) -> Self { diff --git a/rs/moq-relay/src/websocket.rs b/rs/moq-relay/src/websocket.rs index 1d49c95500b..44bbb029085 100644 --- a/rs/moq-relay/src/websocket.rs +++ b/rs/moq-relay/src/websocket.rs @@ -73,6 +73,7 @@ pub(crate) async fn serve_ws( options.socket_stats = socket_stats.map(|Extension(stats)| stats.0); // The listener reads `None` as no deadline; `Options` reads it as the default. options.timeout = Some(state.timeout.unwrap_or_default()); + options.quic = state.quic.clone(); let upgrade = hyper::upgrade::on(&mut request); tokio::spawn(async move { diff --git a/rs/moq-tokio/MIGRATE.md b/rs/moq-tokio/MIGRATE.md index 1cabb27479f..599f9c579fa 100644 --- a/rs/moq-tokio/MIGRATE.md +++ b/rs/moq-tokio/MIGRATE.md @@ -4,6 +4,16 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Smaller default windows, which qmux shares ([#7624](https://github.com/moq-dev/moq/pull/7624)) + +`quic::DEFAULT_RECEIVE_WINDOW` is 4 MiB, `DEFAULT_STREAM_RECEIVE_WINDOW` 1 MiB, and `DEFAULT_SEND_WINDOW` 4 MiB, down from 128 MiB, 4 MiB, and 16 MiB. `Error::ReceiveWindowTooSmall` now refuses only a `receive_window` below 1 MiB. +Set `[quic]` windows explicitly to keep the old behavior, such as for a path whose bandwidth-delay product exceeds them. +Every qmux session (WebSocket, `tcp://`, `tls://`, `unix://`, WebRTC) starts at the `[quic]` windows and stream count, which a client's or server's `[quic]` overrides, and leases receive credit, so a stream can now fail as evicted. +A `Server` applies its `[quic]` to every qmux listener and to `tls_acceptor`. `websocket::accept` reads it from the new `websocket::Options::quic`, which defaults to the `[quic]` defaults, so set it to your config's `quic.resolve()`; `tcp::Listener::with_quic` and `websocket::Listener::with_quic` set it on a listener built outside a `Server`. +`server::Request::with_quic_limits` also widens a qmux session at `ok()`, except a WebRTC session's receive window, which sizes its read queue; one refusing them, such as a rate cap, which qmux cannot pace, fails `ok()` with the new `Error::QmuxLimits`. +`quic::driver::recv::Stream::received_reset`, and so `quic::web::RecvStream::poll_reset`, fail with `Evicted` for a stream evicted to free a stuck window. +`quic::web::RecvStream::received_reset` now fails with a `ReadError`, `ReadError::SessionError` for what was its `SessionError` or `ReadError::Evicted`, rather than panicking on an eviction. + ### `listen::LoadBalancer` has a `forward` field ([#7539](https://github.com/moq-dev/moq/pull/7539)) `listen::LoadBalancer` gained `forward: listen::Forward`, the `[listen.lb.forward]` settings that forward QUIC packets to the server their connection ID names. diff --git a/rs/moq-tokio/src/client.rs b/rs/moq-tokio/src/client.rs index 20288bae012..1b9b5b23c09 100644 --- a/rs/moq-tokio/src/client.rs +++ b/rs/moq-tokio/src/client.rs @@ -81,6 +81,10 @@ pub struct Client { tls: rustls::ClientConfig, #[cfg(feature = "quic")] quic: Option, + /// The settings each qmux dial starts from: the `[quic]` windows and stream count, so a + /// qmux session takes the same per-session numbers as a QUIC one. + #[cfg(any(feature = "websocket", feature = "tcp"))] + qmux: qmux::Config, #[cfg(feature = "iroh")] iroh: Option, #[cfg(feature = "iroh")] @@ -130,6 +134,8 @@ impl Client { #[cfg(any(feature = "quic", feature = "websocket"))] let tls = config.tls.build()?; + #[cfg(any(feature = "websocket", feature = "tcp"))] + let qmux = crate::qmux_config(&quic.resolve()); #[cfg(feature = "quic")] let quic = Some(crate::quic::Client::new(&config, &quic)?); @@ -178,6 +184,8 @@ impl Client { tls, #[cfg(feature = "quic")] quic, + #[cfg(any(feature = "websocket", feature = "tcp"))] + qmux, #[cfg(feature = "iroh")] iroh: None, #[cfg(feature = "iroh")] @@ -455,8 +463,14 @@ impl Client { // QUIC, which can't speak it. Use only on a trusted network. #[cfg(feature = "tcp")] if url.scheme() == "tcp" { - let session = - crate::tcp::connect(url, &self.versions.alpns(), self.failover_delay, self.resolution_delay).await?; + let session = crate::tcp::connect( + url, + &self.versions.alpns(), + &self.qmux, + self.failover_delay, + self.resolution_delay, + ) + .await?; let session = connect_session(&moq, session).await?; return Ok(Dialed::new(session, crate::Transport::Tcp)); } @@ -468,6 +482,7 @@ impl Client { url, &self.versions.alpns(), &self.tcp_tls, + &self.qmux, self.failover_delay, self.resolution_delay, ) @@ -480,7 +495,7 @@ impl Client { // authenticate us by uid/gid via SO_PEERCRED. #[cfg(all(feature = "uds", unix))] if url.scheme() == "unix" { - let session = crate::unix::connect(url, &self.versions.alpns()).await?; + let session = crate::unix::connect(url, &self.versions.alpns(), &self.qmux).await?; let session = connect_session(&moq, session).await?; return Ok(Dialed::new(session, crate::Transport::Unix)); } @@ -542,8 +557,15 @@ impl Client { #[cfg(feature = "websocket")] async fn connect_websocket(&self, moq: &moq_net::Client, addr: crate::connect::Addr) -> crate::Result { let alpns = self.versions.alpns(); - let session = - crate::websocket::connect(&self.websocket, &self.tls, self.tls_host_name.as_deref(), addr, &alpns).await?; + let session = crate::websocket::connect( + &self.websocket, + &self.tls, + self.tls_host_name.as_deref(), + addr, + &alpns, + &self.qmux, + ) + .await?; let session = connect_session(moq, session).await?; Ok(Dialed::new(session, crate::Transport::WebSocket)) } @@ -570,10 +592,18 @@ impl Client { let ws_config = self.websocket.clone(); let ws_tls = self.tls.clone(); let ws_tls_host_name = self.tls_host_name.clone(); + let ws_session = self.qmux.clone(); let websocket = async move { - crate::websocket::race_handle(&ws_config, &ws_tls, ws_tls_host_name.as_deref(), addr, &alpns) - .await - .map(|res| res.map_err(Error::from)) + crate::websocket::race_handle( + &ws_config, + &ws_tls, + ws_tls_host_name.as_deref(), + addr, + &alpns, + &ws_session, + ) + .await + .map(|res| res.map_err(Error::from)) }; match race_transport_connect(&url, quic, websocket).await? { diff --git a/rs/moq-tokio/src/error.rs b/rs/moq-tokio/src/error.rs index afc8fecb984..ea84c9e5947 100644 --- a/rs/moq-tokio/src/error.rs +++ b/rs/moq-tokio/src/error.rs @@ -269,6 +269,12 @@ pub enum Error { #[cfg(feature = "webrtc")] #[error(transparent)] WebRtc(Arc), + + /// A qmux session refused the limits [`crate::server::Request::with_quic_limits`] set, + /// such as a rate cap, which qmux cannot pace. + #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] + #[error("qmux refused the session limits: {0}")] + QmuxLimits(String), } impl Error { diff --git a/rs/moq-tokio/src/lib.rs b/rs/moq-tokio/src/lib.rs index 67f83db7216..56855434fd4 100644 --- a/rs/moq-tokio/src/lib.rs +++ b/rs/moq-tokio/src/lib.rs @@ -89,18 +89,26 @@ pub mod webrtc; #[cfg(feature = "websocket")] pub mod websocket; -/// Session settings for every qmux transport (WebSocket, `tcp://`, `tls://`, `unix://`, WebRTC). +/// Session settings for every qmux transport (WebSocket, `tcp://`, `tls://`, `unix://`, WebRTC), +/// with `quic`'s windows and stream count. /// -/// moq-lite holds a stream open per subscription, so a qmux session grants the same -/// stream count as QUIC rather than qmux's default of 100, past which a peer's opens -/// wait on credit forever. +/// A qmux session leases receive credit as QUIC does, so it takes the same per-session +/// numbers rather than qmux's own defaults. moq-lite holds a stream open per subscription, +/// past qmux's default of 100, where a peer's opens would wait on credit forever. #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] -fn qmux_config() -> qmux::Config { +fn qmux_config(quic: &quic::Resolved) -> qmux::Config { let mut config = qmux::Config::default(); - config.limits.max_streams = std::num::NonZeroU64::new(quic::DEFAULT_MAX_STREAMS); + config.limits = quic.limits(); config } +/// [`qmux_config`] at the `[quic]` defaults, for a server's qmux sessions, which widen once +/// admitted (see [`server::Request::with_quic_limits`]). +#[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] +fn qmux_default() -> qmux::Config { + qmux_config(&quic::Resolved::default()) +} + /// Run a qmux session's driver on the runtime, handing back the session. #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] fn spawn_qmux( diff --git a/rs/moq-tokio/src/quic.rs b/rs/moq-tokio/src/quic.rs index 30a55aa6515..133b4c86471 100644 --- a/rs/moq-tokio/src/quic.rs +++ b/rs/moq-tokio/src/quic.rs @@ -121,23 +121,26 @@ pub const DEFAULT_MAX_STREAMS: u64 = 10_000; /// /// Bounds how much unread data and incomplete messages one peer can make us buffer across /// all of its streams, which the backends otherwise leave unlimited. On iroh, which returns -/// credit as it reads, it bounds only the unread data. It has to be at least -/// moq-net's `transport::MIN_RECEIVE_WINDOW`, the 64 MiB announcement -/// cap plus framing, so 64 MiB would be refused; 128 MiB also holds two maximum-size frames -/// at once, and lets a relay-to-relay session sustain several Gbps at intercontinental RTTs. -pub const DEFAULT_RECEIVE_WINDOW: u64 = 128 << 20; +/// credit as it reads, it bounds only the unread data. It has to be at least moq-net's +/// `transport::MIN_RECEIVE_WINDOW`, one 1 MiB segment. 4 MiB sustains about 270 Mbps at a +/// 100 ms RTT, room for any one viewer or publisher, and lets a session pause two group +/// streams for slow readers (half the window, a stream window each). +pub const DEFAULT_RECEIVE_WINDOW: u64 = 4 << 20; #[cfg(feature = "_transport")] const _: () = assert!(DEFAULT_RECEIVE_WINDOW >= moq_net::transport::MIN_RECEIVE_WINDOW); /// Default per-stream receive window, used when [`Config::stream_receive_window`] is unset. /// /// Stream credit returns as bytes are read, whether or not their message is complete, -/// so this only paces one stream: enough for a single group stream to reach a few -/// hundred Mbps at a 100 ms RTT. -pub const DEFAULT_STREAM_RECEIVE_WINDOW: u64 = 4 << 20; +/// so this only paces one stream: enough for a single group stream to reach about +/// 67 Mbps at a 100 ms RTT. It is also what a stream paused for a slow reader holds. +pub const DEFAULT_STREAM_RECEIVE_WINDOW: u64 = 1 << 20; /// Default cap on unacknowledged outgoing data, used when [`Config::send_window`] is unset. -pub const DEFAULT_SEND_WINDOW: u64 = 16 << 20; +/// +/// Matches [`DEFAULT_RECEIVE_WINDOW`], the most a peer with the defaults takes in flight, so +/// a slow peer's connection buffers no more than it could receive. +pub const DEFAULT_SEND_WINDOW: u64 = 4 << 20; /// Default idle timeout before an inactive connection is dropped. /// @@ -241,7 +244,7 @@ pub struct Config { /// Connection-wide receive window, in bytes: how much data the peer may have /// in flight or in incomplete messages across every stream at once. Defaults to - /// 128 MiB, and must fit the largest message a session assembles, just over 64 MiB. + /// 4 MiB, and must fit the largest unit a session assembles, one 1 MiB segment. /// /// When incomplete messages fill it, the least urgent stream is cancelled. iroh /// returns credit as it reads, so there it bounds only unread data. @@ -258,7 +261,7 @@ pub struct Config { )] pub receive_window: Option, - /// Per-stream receive window, in bytes. Defaults to 4 MiB. + /// Per-stream receive window, in bytes. Defaults to 1 MiB. /// /// Stream credit returns as bytes are read, whether or not their message is /// complete, so a smaller window only adds round trips to a large frame. The @@ -273,7 +276,7 @@ pub struct Config { pub stream_receive_window: Option, /// Cap on unacknowledged outgoing data, in bytes, regardless of what the peer - /// allows. Defaults to 16 MiB. + /// allows. Defaults to 4 MiB. /// /// This bounds the transport send buffer. #[serde(skip_serializing_if = "Option::is_none")] diff --git a/rs/moq-tokio/src/quic/driver/recv.rs b/rs/moq-tokio/src/quic/driver/recv.rs index 9139ef770cd..300aeebbe06 100644 --- a/rs/moq-tokio/src/quic/driver/recv.rs +++ b/rs/moq-tokio/src/quic/driver/recv.rs @@ -223,7 +223,8 @@ impl Stream { /// Yields `Some` with the reset error code when the stream is reset by the peer. Yields `None` /// when the stream was [`finish()`](crate::quic::driver::send::Stream::finish)ed by the peer /// and all data has been received, after which it is no longer meaningful for the stream to - /// be reset. + /// be reset. Fails with [`Error::Evicted`] once the stream was evicted to free a stuck receive + /// window, until it is stopped. /// /// This operation is cancel-safe. pub async fn received_reset(&mut self) -> Result, Error> { @@ -240,6 +241,8 @@ impl Stream { } match conn.inner.recv_stream(self.id).received_reset() { + // Evicted, so its bytes are gone: nothing frees the window until it is stopped. + Err(moq_quic::recv::Error::Evicted) => Poll::Ready(Err(Error::Evicted)), Err(_) => Poll::Ready(Ok(None)), Ok(Some(error_code)) => { // Stream state has just now been freed, so the connection may need to issue new diff --git a/rs/moq-tokio/src/quic/driver/tests.rs b/rs/moq-tokio/src/quic/driver/tests.rs index 64da9a12878..8cebf6fb8c1 100755 --- a/rs/moq-tokio/src/quic/driver/tests.rs +++ b/rs/moq-tokio/src/quic/driver/tests.rs @@ -2180,6 +2180,8 @@ async fn stuck_window_evicts_the_lower_priority() { })), ); assert_eq!(leased, (Err(recv::Error::Evicted), Ok(WINDOW))); + // A reader waiting for a reset instead of reading learns of it too. + assert_eq!(low.received_reset().await, Err(recv::Error::Evicted)); // The peer hears of the eviction once the stream is stopped. Drain the survivor to FIN // instead of dropping unread data, which would stop that stream too. diff --git a/rs/moq-tokio/src/quic/web/recv.rs b/rs/moq-tokio/src/quic/web/recv.rs index af1188809c9..7ede7c7201d 100644 --- a/rs/moq-tokio/src/quic/web/recv.rs +++ b/rs/moq-tokio/src/quic/web/recv.rs @@ -8,7 +8,7 @@ use std::{ use bytes::{BufMut, Bytes}; use crate::quic::driver::recv; -use crate::quic::web::{CloseReason, ReadError, SessionError}; +use crate::quic::web::{CloseReason, ReadError}; /// A stream that can be used to receive bytes. See [`crate::quic::driver::recv::Stream`]. /// @@ -45,8 +45,9 @@ impl RecvStream { } } - /// Poll until the peer resets the stream or the session closes, without reading it; pending - /// for good once every byte arrived. See [`recv::Stream::received_reset`]. + /// Poll until the peer resets the stream, the receiver evicts it, or the session closes, + /// without reading it; pending for good once every byte arrived. See + /// [`recv::Stream::received_reset`]. pub fn poll_reset(&mut self, cx: &mut Context<'_>) -> Poll { let close = &self.close; let Some(inner) = &mut self.inner else { @@ -102,21 +103,19 @@ impl RecvStream { /// Block until the stream has been reset and return the error code. See /// [`recv::Stream::received_reset`]. /// - /// Returns the stream's reset code or its session error. - pub async fn received_reset(&mut self) -> Result, SessionError> { + /// Returns the stream's reset code, or fails with its session error or + /// [`ReadError::Evicted`] once the receiver evicted it to free a stuck window. + pub async fn received_reset(&mut self) -> Result, ReadError> { let Some(inner) = &mut self.inner else { return Ok(None); }; match inner.received_reset().await { Ok(None) => Ok(None), Ok(Some(code)) => match self.close.session_gone(code) { - Some(err) => Err(err), + Some(err) => Err(ReadError::SessionError(err)), None => Ok(self.close.decode_stream_code(code)), }, - Err(err) => match map_error(&self.close, err) { - ReadError::SessionError(err) => Err(err), - err => unreachable!("only the connection fails a reset wait: {err}"), - }, + Err(err) => Err(map_error(&self.close, err)), } } diff --git a/rs/moq-tokio/src/server.rs b/rs/moq-tokio/src/server.rs index cba979c6d71..184c1dbd0a5 100644 --- a/rs/moq-tokio/src/server.rs +++ b/rs/moq-tokio/src/server.rs @@ -314,6 +314,9 @@ impl Server { // is (the default). A stream-only server (`--listen-unix-bind` with no // `--listen`) doesn't also open UDP/443. quic.validate()?; + // Every qmux session this server accepts starts at the same `[quic]` windows. + #[cfg(any(feature = "websocket", feature = "tcp", all(feature = "uds", unix)))] + let resolved = quic.resolve(); let build_quic = parts.quic() && (config.bind.is_some() || !config.has_stream_listener()); // A stream-only server would silently ignore what only QUIC reads. A caller @@ -425,6 +428,7 @@ impl Server { stream_binds, stream_versions(&versions), timeout, + resolved.clone(), #[cfg(all(feature = "uds", unix))] unix_allow, ); @@ -478,7 +482,7 @@ impl Server { #[cfg(feature = "quic")] quic, #[cfg(feature = "websocket")] - websocket, + websocket: websocket.map(|websocket| websocket.with_quic(&resolved)), }) } @@ -549,7 +553,8 @@ impl Server { pub fn tls_acceptor(&self) -> Option { let config = self.tls_stream.clone()?; let server = self.moq.clone().with_versions(self.streams.versions.clone()); - Some(crate::tcp::TlsAcceptor::new(config, server, self.timeout)) + let session = crate::qmux_config(&self.streams.quic); + Some(crate::tcp::TlsAcceptor::new(config, session, server, self.timeout)) } /// A live handle to the certificates this server is serving. @@ -790,7 +795,7 @@ impl Server { let (request, deadline) = accept_request(&setup.server, session, deadline).await?; // Only WebTransport carries a request URL; raw QUIC puts the path in the SETUP. let transport = match url { Some(_) => Transport::WebTransport, None => Transport::Quic }; - Ok(Request { transport, url, identity, authority, link, handshake: Box::new(request), quic, window, quic_limits: Default::default(), deadline, active }) + Ok(Request { transport, url, identity, authority, link, handshake: Box::new(request), quic, window, quic_limits: Default::default(), #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] qmux: None, deadline, active }) }.boxed()).boxed()); } } @@ -824,8 +829,10 @@ impl Server { quic: None, #[cfg(feature = "quic")] window: None, - #[cfg(feature = "quic")] + #[cfg(feature = "_transport")] quic_limits: Default::default(), + #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] + qmux: None, deadline, active, }) @@ -989,6 +996,7 @@ impl Listener { }; let (session, protocol) = within(deadline, handshake).await?; + let qmux = session.clone(); let (request, deadline) = accept_request(&server.moq, session, deadline).await?; Ok(Request { transport: Transport::WebRtc, @@ -1004,8 +1012,10 @@ impl Listener { quic: None, #[cfg(feature = "quic")] window: None, - #[cfg(feature = "quic")] + #[cfg(feature = "_transport")] quic_limits: Default::default(), + #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] + qmux: Some(qmux), deadline, active, }) @@ -1134,12 +1144,24 @@ where Ok((request, deadline)) } +/// The limits [`Request::ok`] gives an accepted qmux session on `transport`. A WebRTC data +/// channel keeps its own receive window, which sizes its read queue, so a wider one would let +/// a peer honoring flow control overrun the queue and close the channel. +#[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] +fn qmux_limits(transport: Transport, mut limits: moq_quic::Limits) -> moq_quic::Limits { + if transport == Transport::WebRtc { + limits.receive_window = None; + } + limits +} + #[cfg(feature = "websocket")] pub(crate) async fn accept_websocket( session: qmux::Session, options: crate::websocket::Options, deadline: Option, ) -> crate::Result { + let qmux = session.clone(); let (request, deadline) = accept_request(&options.server, session, deadline).await?; let authority = options.url.host_str().filter(|h| !h.is_empty()).map(str::to_owned); Ok(Request { @@ -1153,8 +1175,10 @@ pub(crate) async fn accept_websocket( quic: None, #[cfg(feature = "quic")] window: None, - #[cfg(feature = "quic")] + #[cfg(feature = "_transport")] quic_limits: Default::default(), + #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] + qmux: Some(qmux), deadline, active: options.active, }) @@ -1341,6 +1365,8 @@ struct StreamListeners { health: Vec, versions: moq_net::Versions, timeout: Option, + /// The `[quic]` section each accepted session starts with. + quic: crate::quic::Resolved, #[cfg(all(feature = "uds", unix))] unix_allow: Option, /// Bound sockets whose accept loops have not started. Empty once [`Self::serve`] @@ -1359,6 +1385,7 @@ impl StreamListeners { binds: Vec, versions: moq_net::Versions, timeout: Option, + quic: crate::quic::Resolved, #[cfg(all(feature = "uds", unix))] unix_allow: Option, ) -> Self { let health = binds @@ -1372,6 +1399,7 @@ impl StreamListeners { health, versions, timeout, + quic, #[cfg(all(feature = "uds", unix))] unix_allow, pending: Vec::new(), @@ -1404,6 +1432,7 @@ impl StreamListeners { let mut listener = crate::tcp::Listener::bind(addr) .await? .with_protocols(alpns) + .with_quic(&self.quic) .with_accept_health(health); let encrypted = tls.is_some(); if let Some(tls) = tls { @@ -1420,6 +1449,7 @@ impl StreamListeners { let listener = crate::unix::Listener::bind(&path) .await? .with_protocols(alpns) + .with_quic(&self.quic) .with_accept_health(health); // Loose socket perms let workers run as a different user. The parent // directory or uid/gid/pid allowlist is the access gate. @@ -1674,6 +1704,7 @@ pub(crate) async fn accept_stream( deadline, active, } = incoming; + let qmux = session.clone(); let (request, deadline) = accept_request(server, session, deadline).await?; Ok(Request { transport, @@ -1686,8 +1717,10 @@ pub(crate) async fn accept_stream( quic: None, #[cfg(feature = "quic")] window: None, - #[cfg(feature = "quic")] + #[cfg(feature = "_transport")] quic_limits: Default::default(), + #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] + qmux: Some(qmux), deadline, active, }) @@ -1752,9 +1785,13 @@ pub struct Request { /// The receive window [`Request::ok`] opens unless [`Request::with_quic_limits`] names one. #[cfg(feature = "quic")] window: Option, - /// What [`Request::ok`] applies to the QUIC connection before answering SETUP. - #[cfg(feature = "quic")] + /// What [`Request::ok`] applies to the QUIC connection or qmux session before answering SETUP. + #[cfg(feature = "_transport")] quic_limits: moq_quic::Limits, + /// The session under a WebSocket, `tcp://`, `tls://`, `unix://`, or WebRTC request, which + /// [`Request::ok`] widens to [`Request::with_quic_limits`]. + #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] + qmux: Option, /// What remains of the handshake deadline, which also bounds [`Request::ok`]. deadline: Option, } @@ -1807,12 +1844,14 @@ impl Request { } /// Set the QUIC limits [`Self::ok`] applies before answering SETUP, replacing any set - /// before. Ignored on a transport moq-quic does not drive. + /// before. Ignored on iroh, which moq-quic does not drive. /// - /// Until then the connection holds only [`moq_net::transport::SETUP_RECEIVE_WINDOW`]; + /// Until then a QUIC connection holds only [`moq_net::transport::SETUP_RECEIVE_WINDOW`]; /// `ok` opens `receive_window`, or `[quic] receive_window` when it is unset, after the - /// other limits, so a `receive_rate` set here paces the opened window. - #[cfg(feature = "quic")] + /// other limits, so a `receive_rate` set here paces the opened window. A qmux session + /// starts at the `[quic]` defaults and takes the windows and stream count set here; it + /// never paces, so `ok` refuses a rate cap on one. + #[cfg(feature = "_transport")] pub fn with_quic_limits(mut self, limits: moq_quic::Limits) -> Result { limits.validate()?; self.quic_limits = limits; @@ -1830,6 +1869,13 @@ impl Request { crate::runtime::scope(Deadline::bound(self.deadline.as_ref(), async { Ok(work.await) })).await } + /// The receive window of the qmux session under this request. + #[cfg(all(test, any(feature = "websocket", feature = "tcp")))] + pub(crate) fn qmux_window(&self) -> moq_net::transport::ReceiveWindow { + let qmux = self.qmux.as_ref().expect("a qmux session"); + moq_net::transport::poll::Session::receive_window(qmux).expect("qmux reports its window") + } + /// Accept the session, driving it until it closes. /// /// A driver panic is reported through [`Session::closed`]. @@ -1846,6 +1892,13 @@ impl Request { quic.set_limits(limits) .expect("validated by with_quic_limits and the config"); } + #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] + if let Some(qmux) = &self.qmux + && let Err(err) = qmux.set_limits(qmux_limits(self.transport, self.quic_limits)) + { + self.handshake.close(moq_net::SessionError::Internal.into()); + return Err(crate::Error::QmuxLimits(crate::error::message(err))); + } let Request { handshake, deadline, @@ -2016,6 +2069,7 @@ mod tests { Vec::new(), Default::default(), None, + Default::default(), #[cfg(all(feature = "uds", unix))] None, ); @@ -2051,6 +2105,47 @@ mod tests { assert!(matches!(Server::new(config), Err(Error::NoBackend(_)))); } + /// The windows `[quic]` configures, smaller than its defaults. + #[cfg(feature = "tcp")] + fn small_quic() -> crate::quic::Config { + crate::quic::Config { + receive_window: Some(moq_net::transport::MIN_RECEIVE_WINDOW * 2), + stream_receive_window: Some(256 * 1024), + ..Default::default() + } + } + + /// A `tcp://` session starts at the server's `[quic]` windows rather than the defaults, + /// before anything admits it. + #[cfg(feature = "tcp")] + #[tokio::test] + async fn an_accepted_qmux_session_starts_at_the_configured_windows() { + const TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + let mut listen = crate::listen::Config::default(); + listen.tcp.bind = Some("127.0.0.1:0".parse().unwrap()); + let mut listener = listen + .init(small_quic()) + .expect("server init") + .listen() + .await + .expect("listen"); + let addr = listener.tcp_local_addr().expect("tcp listener bound"); + + let client = crate::connect::Config::default() + .init(Default::default()) + .expect("client init"); + let url: Url = format!("tcp://{addr}/").parse().unwrap(); + let _connection = client.connect(url); + let request = kio::clock::timeout(TIMEOUT, listener.accept()) + .await + .expect("accept timeout") + .expect("accept"); + + let window = request.qmux_window(); + assert_eq!(window.connection, moq_net::transport::MIN_RECEIVE_WINDOW * 2); + assert_eq!(window.stream, 256 * 1024); + } + /// A millisecond track: the frames written here carry timestamps. #[cfg(all(feature = "uds", unix))] fn timed() -> moq_net::track::Info { @@ -2683,8 +2778,10 @@ mod tests { quic: None, #[cfg(feature = "quic")] window: None, - #[cfg(feature = "quic")] + #[cfg(feature = "_transport")] quic_limits: Default::default(), + #[cfg(any(feature = "websocket", feature = "tcp", feature = "webrtc"))] + qmux: None, deadline, } .with_stats( diff --git a/rs/moq-tokio/src/tcp.rs b/rs/moq-tokio/src/tcp.rs index e0ddff55aef..03ea6e30e64 100644 --- a/rs/moq-tokio/src/tcp.rs +++ b/rs/moq-tokio/src/tcp.rs @@ -161,6 +161,7 @@ type Result = std::result::Result; pub(crate) async fn connect( url: Url, protocols: &[&str], + session: &qmux::Config, failover_delay: std::time::Duration, resolution_delay: std::time::Duration, ) -> Result { @@ -169,7 +170,7 @@ pub(crate) async fn connect( tracing::debug!(peer = %crate::connect::Endpoint(&url), "connecting via TCP"); let candidates = crate::resolve::Candidates::resolve(host, port, resolution_delay); - connect_addrs(candidates, protocols, failover_delay).await + connect_addrs(candidates, protocols, session, failover_delay).await } /// Dial a `tls://host:port` URL: qmux over TLS over TCP, negotiating one of @@ -181,6 +182,7 @@ pub(crate) async fn connect_tls( url: Url, protocols: &[&str], tls: &crate::tls::Connect, + session: &qmux::Config, failover_delay: std::time::Duration, resolution_delay: std::time::Duration, ) -> crate::Result { @@ -195,7 +197,7 @@ pub(crate) async fn connect_tls( let client = qmux::tls::Client::new(std::sync::Arc::new(tls.build()?)) .with_protocols(protocols.iter().map(|&alpn| (alpn, &[WIRE_VERSION][..]))) .require_protocol() - .with_session_config(crate::qmux_config()); + .with_session_config(session.clone()); tracing::debug!(peer = %crate::connect::Endpoint(&url), "connecting via TLS over TCP"); let candidates = crate::resolve::Candidates::resolve(host, port, resolution_delay); @@ -218,12 +220,13 @@ pub(crate) async fn connect_tls( async fn connect_addrs( candidates: crate::resolve::Candidates, protocols: &[&str], + session: &qmux::Config, failover_delay: std::time::Duration, ) -> Result { crate::failover::race(candidates, failover_delay, |addr| { let protocols: Vec = protocols.iter().map(|&p| p.to_owned()).collect(); + let mut config = session.clone(); async move { - let mut config = crate::qmux_config(); config.version = WIRE_VERSION; config.protocol = qmux::Protocol::Negotiate(protocols); qmux::tcp::connect(addr, config) @@ -253,10 +256,11 @@ pub struct TlsAcceptor { impl TlsAcceptor { pub(crate) fn new( config: std::sync::Arc, + session: qmux::Config, server: moq_net::Server, timeout: Option, ) -> Self { - let qmux = qmux::tls::Server::new(config.clone()).with_session_config(crate::qmux_config()); + let qmux = qmux::tls::Server::new(config.clone()).with_session_config(session); Self { config, qmux, @@ -324,6 +328,8 @@ pub struct Listener { listener: tokio::net::TcpListener, protocols: Vec, health: crate::accept::Health, + /// The settings each accepted qmux session starts with. + session: qmux::Config, /// Present when the listener serves TLS; it negotiates the ALPN itself. tls: Option, } @@ -337,6 +343,7 @@ impl Listener { listener, protocols: Vec::new(), health: crate::accept::Health::new("tcp"), + session: crate::qmux_default(), tls: None, }) }) @@ -346,7 +353,16 @@ impl Listener { /// Serve TLS with `config`, whose ALPN list already names each /// `qmux-01.` pair to accept. pub(crate) fn with_tls(mut self, config: std::sync::Arc) -> Self { - self.tls = Some(qmux::tls::Server::new(config).with_session_config(crate::qmux_config())); + self.tls = Some(qmux::tls::Server::new(config).with_session_config(self.session.clone())); + self + } + + /// Open each accepted session with `quic`'s windows and stream count, the `[quic]` + /// section, rather than its defaults. A session admitted with + /// [`crate::server::Request::with_quic_limits`] then takes those. + pub fn with_quic(mut self, quic: &crate::quic::Resolved) -> Self { + self.session = crate::qmux_config(quic); + self.tls = self.tls.map(|tls| tls.with_session_config(self.session.clone())); self } @@ -403,7 +419,7 @@ impl Listener { ) -> impl Future> + use<> { let (stream, addr) = self.accept_socket().await; tracing::debug!(%addr, "accepted TCP connection"); - let mut config = crate::qmux_config(); + let mut config = self.session.clone(); config.version = WIRE_VERSION; config.protocol = qmux::Protocol::Negotiate(self.protocols.clone()); let tls = self.tls.clone(); @@ -639,7 +655,12 @@ mod tests { let candidates = crate::resolve::Candidates::fixed([blackhole, addr]); let session = kio::clock::timeout( Duration::from_secs(5), - connect_addrs(candidates, &["moq-test"], Duration::from_millis(50)), + connect_addrs( + candidates, + &["moq-test"], + &crate::qmux_default(), + Duration::from_millis(50), + ), ) .await .expect("failover timed out") @@ -662,7 +683,7 @@ mod tests { let addr = listener.local_addr().expect("local addr"); let accept = tokio::spawn(async move { listener.accept().await.expect("listener gone").expect("accept") }); let candidates = crate::resolve::Candidates::fixed([addr]); - let client = connect_addrs(candidates, &["moq-test"], Duration::ZERO) + let client = connect_addrs(candidates, &["moq-test"], &crate::qmux_default(), Duration::ZERO) .await .expect("connect failed"); let server = accept.await.expect("accept task panicked"); @@ -685,7 +706,7 @@ mod tests { #[tokio::test] async fn connect_addrs_rejects_empty() { let candidates = crate::resolve::Candidates::fixed([]); - let res = connect_addrs(candidates, &["moq-test"], Duration::ZERO).await; + let res = connect_addrs(candidates, &["moq-test"], &crate::qmux_default(), Duration::ZERO).await; assert!(matches!(res, Err(Error::NoAddresses))); } } diff --git a/rs/moq-tokio/src/unix.rs b/rs/moq-tokio/src/unix.rs index 4129849cf20..9b3fb22a8d8 100644 --- a/rs/moq-tokio/src/unix.rs +++ b/rs/moq-tokio/src/unix.rs @@ -242,10 +242,10 @@ pub(crate) struct PeerCred { /// /// The path is taken from the URL path, so use a triple slash for an absolute /// path: `unix:///run/moq/internal.sock`. -pub(crate) async fn connect(url: Url, protocols: &[&str]) -> Result { +pub(crate) async fn connect(url: Url, protocols: &[&str], session: &qmux::Config) -> Result { let path = socket_path(&url).ok_or(Error::MissingPath)?; tracing::debug!(peer = %crate::connect::Endpoint(&url), "connecting via Unix socket"); - let mut config = crate::qmux_config(); + let mut config = session.clone(); config.version = WIRE_VERSION; config.protocol = qmux::Protocol::Negotiate(protocols.iter().map(|p| (*p).to_owned()).collect()); qmux::uds::connect(path, config) @@ -272,6 +272,8 @@ pub(crate) struct Listener { path: PathBuf, protocols: Vec, health: crate::accept::Health, + /// The settings each accepted qmux session starts with. + session: qmux::Config, } impl Listener { @@ -299,6 +301,7 @@ impl Listener { path, protocols: Vec::new(), health: crate::accept::Health::new("unix"), + session: crate::qmux_default(), }) } @@ -312,6 +315,13 @@ impl Listener { self } + /// Open each accepted session with `quic`'s windows and stream count, the `[quic]` + /// section, rather than its defaults. + pub(crate) fn with_quic(mut self, quic: &crate::quic::Resolved) -> Self { + self.session = crate::qmux_config(quic); + self + } + /// Advertise these application protocols (moq ALPNs) for in-band negotiation, /// in preference order. The first server entry the client also offers wins. pub(crate) fn with_protocols(mut self, protocols: I) -> Self @@ -336,7 +346,7 @@ impl Listener { ) -> impl Future> + use<> { let stream = self.accept_socket().await; let allow = allow.cloned(); - let mut config = crate::qmux_config(); + let mut config = self.session.clone(); config.version = WIRE_VERSION; config.protocol = qmux::Protocol::Negotiate(self.protocols.clone()); async move { diff --git a/rs/moq-tokio/src/webrtc.rs b/rs/moq-tokio/src/webrtc.rs index 91f95f41326..22f38b315b4 100644 --- a/rs/moq-tokio/src/webrtc.rs +++ b/rs/moq-tokio/src/webrtc.rs @@ -56,8 +56,9 @@ const SEND_WINDOW: usize = RECORD_SIZE as usize * 4; /// ignores it has the channel closed instead of growing memory without limit. const READ_SLACK: u64 = 16 * RECORD_SIZE; -/// The connection-level receive window a data channel session grants, qmux's default. -const RECEIVE_WINDOW: u64 = 1 << 20; +/// The connection-level receive window a data channel session grants, the `[quic]` default +/// every qmux session takes. +const RECEIVE_WINDOW: u64 = crate::quic::DEFAULT_RECEIVE_WINDOW; /// The default STUN port (RFC 8489 section 18.10). const STUN_PORT: u16 = 3478; @@ -502,7 +503,7 @@ pub(crate) struct Opened { /// The qmux session settings for a channel carrying `protocol`, advertising /// `max_record_size`: 16 KiB, capped by the peer's SCTP message limit. fn session_config(protocol: &str, max_record_size: u64) -> qmux::Config { - let mut config = crate::qmux_config(); + let mut config = crate::qmux_default(); config.version = qmux::Version::QMux01; config.protocol = qmux::Protocol::Negotiated(protocol.to_string()); config.max_record_size = max_record_size; diff --git a/rs/moq-tokio/src/websocket.rs b/rs/moq-tokio/src/websocket.rs index 899680d4db4..88708730cc3 100644 --- a/rs/moq-tokio/src/websocket.rs +++ b/rs/moq-tokio/src/websocket.rs @@ -123,6 +123,10 @@ pub struct Options { pub socket_stats: Option, /// The SETUP deadline, including its reply; unset uses ten seconds, zero disables it. pub timeout: Option, + /// The `[quic]` windows and stream count the session starts with, until + /// [`crate::server::Request::with_quic_limits`] widens it. Defaults to the `[quic]` + /// defaults, so a server passes its own resolved section. + pub quic: crate::quic::Resolved, } impl Options { @@ -136,6 +140,7 @@ impl Options { identity: None, socket_stats: None, timeout: None, + quic: Default::default(), } } } @@ -144,7 +149,7 @@ impl Options { /// /// Cap the socket at this before upgrading it, so a peer cannot make it buffer more. pub fn max_message_size() -> usize { - qmux::ws::max_message_size(&crate::qmux_config()) + qmux::ws::max_message_size(&crate::qmux_default()) } /// A socket config capped at [`max_message_size`], for the sockets upgraded here. @@ -177,7 +182,7 @@ where let alpn = options.link.alpn.as_deref().unwrap_or_default(); let mut upgraded = qmux::ws::Upgraded::new(socket, alpn) .with_keep_alive(qmux::ws::KeepAlive::default()) - .with_session_config(crate::qmux_config()); + .with_session_config(crate::qmux_config(&options.quic)); if let Some(stats) = &options.socket_stats { upgraded = upgraded.with_socket_stats(stats.clone()); } @@ -347,6 +352,7 @@ pub(crate) async fn race_handle( tls_host_name: Option<&str>, addr: crate::connect::Addr, alpns: &[&str], + session: &qmux::Config, ) -> Option> { if !config.resolve().enabled { return None; @@ -360,7 +366,7 @@ pub(crate) async fn race_handle( _ => return None, } - let res = connect(config, tls, tls_host_name, addr, alpns).await; + let res = connect(config, tls, tls_host_name, addr, alpns, session).await; if let Err(err) = &res { tracing::warn!(%err, "WebSocket connection failed"); } @@ -373,6 +379,7 @@ pub(crate) async fn connect( tls_host_name: Option<&str>, addr: crate::connect::Addr, alpns: &[&str], + session: &qmux::Config, ) -> Result { let mut url = addr.url().clone(); let resolved = config.resolve(); @@ -437,7 +444,7 @@ pub(crate) async fn connect( async move { let stream = tokio::net::TcpStream::connect(addr).await?; stream.set_nodelay(true)?; - connect_tls_override(tls, name, url, stream, alpns).await + connect_tls_override(tls, name, url, stream, alpns, session).await } }) .await? @@ -459,7 +466,7 @@ pub(crate) async fn connect( .with_protocols(alpns.iter().map(|&a| (a, qmux_versions_for(a)))) .with_connector(connector) .with_keep_alive(qmux::ws::KeepAlive::default()) // 5s ping / 30s deadline; TCP backs off retransmits too far for QUIC's 10s - .with_session_config(crate::qmux_config()) + .with_session_config(session.clone()) .connect(url.as_str()) .await .map(crate::spawn_qmux) @@ -480,6 +487,7 @@ async fn connect_tls_override( url: &Url, stream: tokio::net::TcpStream, alpns: &[&str], + session: &qmux::Config, ) -> Result { let original_request = url .as_str() @@ -522,7 +530,7 @@ async fn connect_tls_override( .and_then(|value| value.to_str().ok()); qmux::ws::Upgraded::new(websocket, negotiated.unwrap_or_default()) .with_keep_alive(qmux::ws::KeepAlive::default()) - .with_session_config(crate::qmux_config()) + .with_session_config(session.clone()) .connect() .map(crate::spawn_qmux) .map_err(Error::connect) @@ -606,6 +614,8 @@ pub struct Listener { listener: tokio::net::TcpListener, protocols: Vec, health: crate::accept::Health, + /// The settings each accepted qmux session starts with. + session: qmux::Config, } impl Listener { @@ -618,6 +628,7 @@ impl Listener { listener, protocols, health: crate::accept::Health::new("websocket"), + session: crate::qmux_default(), }) }) .await @@ -642,6 +653,13 @@ impl Listener { Ok(self) } + /// Open each accepted session with `quic`'s windows and stream count, the `[quic]` + /// section, rather than its defaults. A [`crate::Server`] applies its own. + pub fn with_quic(mut self, quic: &crate::quic::Resolved) -> Self { + self.session = crate::qmux_config(quic); + self + } + /// The local address the listener is bound to. pub fn local_addr(&self) -> Result { Ok(self.listener.local_addr()?) @@ -687,6 +705,7 @@ impl Listener { let accepted = Arc::new(Mutex::new(None::<(String, Url)>)); let accepted_callback = accepted.clone(); let protocols = self.protocols.clone(); + let session = self.session.clone(); #[expect( clippy::result_large_err, reason = "the error type is tungstenite's handshake response, not ours" @@ -736,7 +755,7 @@ impl Listener { .expect("successful upgrade selected a protocol"); let session = qmux::ws::Upgraded::new(websocket, &protocol) .with_keep_alive(qmux::ws::KeepAlive::default()) - .with_session_config(crate::qmux_config()) + .with_session_config(session) .accept() .map(crate::spawn_qmux) .map_err(Error::accept)?; @@ -968,15 +987,71 @@ mod tests { url.into() }; let expected_host = format!("{url_host}:{}", addr.port()); - let session = connect(&config, &client_tls, tls_name, target, moq_net::ALPNS) - .await - .unwrap(); + let session = connect( + &config, + &client_tls, + tls_name, + target, + moq_net::ALPNS, + &crate::qmux_default(), + ) + .await + .unwrap(); drop(session); let (server_name, host) = accepted.await.unwrap(); assert_eq!(server_name.as_deref(), expected_sni); assert_eq!(host.as_deref(), Some(expected_host.as_str())); } + /// A session [`accept`] upgrades, as the relay's own WebSocket listener does, starts at + /// the `[quic]` windows its [`Options`] carry rather than the defaults. + #[tokio::test] + async fn an_upgraded_session_starts_at_the_options_windows() { + use tungstenite::protocol::Role; + const TIMEOUT: time::Duration = time::Duration::from_secs(10); + + let alpn = supported_subprotocols(moq_net::ALPNS).remove(0); + let (server_io, client_io) = tokio::io::duplex(1 << 20); + let server_ws = + tokio_tungstenite::WebSocketStream::from_raw_socket(server_io, Role::Server, Some(socket_config())).await; + let client_ws = tokio_tungstenite::WebSocketStream::from_raw_socket(client_io, Role::Client, None).await; + + let quic = crate::quic::Config { + receive_window: Some(moq_net::transport::MIN_RECEIVE_WINDOW * 2), + stream_receive_window: Some(256 * 1024), + ..Default::default() + }; + let activity = moq_net::server::Activity::default(); + let mut options = Options::new( + "ws://localhost/".parse().unwrap(), + moq_net::Server::new().with_versions(moq_net::Versions::all()), + activity.enter().unwrap(), + ); + options.link.alpn = Some(alpn.clone()); + options.quic = quic.resolve(); + let server = tokio::spawn(accept(server_ws, options)); + + let (session, driver) = qmux::ws::Upgraded::new(client_ws, &alpn).connect().unwrap(); + tokio::spawn(driver); + // Runs until aborted: its driver sends SETUP, and ending it would cancel the request. + let client = tokio::spawn(async move { + let client = moq_net::Client::new().with_versions(moq_net::Versions::all()); + let (_session, driver) = client.connect(moq_net::time::Instant::now(), session).await?; + moq_net::time::run(driver).await; + moq_net::Result::Ok(()) + }); + + let request = kio::clock::timeout(TIMEOUT, server) + .await + .expect("accept timeout") + .expect("accept task") + .expect("accept"); + let window = request.qmux_window(); + assert_eq!(window.connection, moq_net::transport::MIN_RECEIVE_WINDOW * 2); + assert_eq!(window.stream, 256 * 1024); + client.abort(); + } + #[test] fn moqt_18_and_newer_pin_to_qmux01() { // The literals in `qmux_versions_for` must stay the IETF draft ALPNs; diff --git a/rs/moq-tokio/tests/integration/receive_credit.rs b/rs/moq-tokio/tests/integration/receive_credit.rs index ddfa7205186..ef04e801e67 100644 --- a/rs/moq-tokio/tests/integration/receive_credit.rs +++ b/rs/moq-tokio/tests/integration/receive_credit.rs @@ -4,8 +4,8 @@ //! The receiver runs on the raw driver so its connection window can be far smaller than //! the stack allows, which keeps the payloads small. Without recovery the high-priority //! frame never arrives: the window is spent on a low-priority frame that can never -//! complete. The Rust arm dials a stock server. The JavaScript arm serves WebTransport -//! to a Bun publisher; it needs Bun and runs in the interop workflow. +//! complete. The Rust arm dials a stock server. The JavaScript arms serve WebTransport, and +//! qmux over WebSocket, to a Bun publisher; they need Bun and run in the interop workflow. //! //! Some runs go through a bufferbloated path instead of loopback, so a sender held back by //! a slow link and a deep queue is shown not to look like a stuck window. @@ -573,6 +573,114 @@ async fn unidentified_headers_give_way() { assert_eq!(next_frame(&mut sub).await.len(), 4096); } +/// A moq-lite-07 varint below 2^28: leading one bits count the extra bytes. +fn varint(bytes: &mut Vec, value: u64) { + let [_, b, c, d] = u32::try_from(value).unwrap().to_be_bytes(); + match value { + 0..0x80 => bytes.push(d), + 0x80..0x4000 => bytes.extend_from_slice(&[0x80 | c, d]), + 0x4000..0x20_0000 => bytes.extend_from_slice(&[0xc0 | b, c, d]), + 0x20_0000..0x1000_0000 => bytes.extend_from_slice(&(0xe000_0000 | value as u32).to_be_bytes()), + _ => panic!("{value} needs a longer varint"), + } +} + +/// A moq-lite-07 GROUP stream for subscription `subscribe`, carrying one frame of `size` +/// bytes timed at zero. +fn group_stream(subscribe: u64, sequence: u64, size: usize) -> Vec { + let mut header = Vec::new(); + varint(&mut header, subscribe); + varint(&mut header, sequence); + // The first frame. + varint(&mut header, 0); + + // GROUP, then its size-prefixed header. + let mut bytes = vec![0x00]; + varint(&mut bytes, header.len() as u64); + bytes.extend_from_slice(&header); + // The zigzag timestamp delta, then the frame's size and payload. + varint(&mut bytes, 0); + varint(&mut bytes, size as u64); + bytes.resize(bytes.len() + size, 5); + bytes +} + +/// GROUP streams that beat their subscription's TRACK_INFO wait unread at the lowest +/// receive priority. When they fill the window, they give way before a more urgent track's +/// partial frame, the TRACK_INFO still arrives, and the groups that are left are then read. +#[tokio::test] +async fn groups_waiting_for_track_info_give_way_first() { + let setup = reversed("moq-lite-07-wip").await; + + // Subscription 0: an urgent track's frame, half sent and holding its credit. + let (urgent, mut urgent_sub) = setup.subscribe("urgent", 100).await; + let mut frame = urgent + .append_group() + .unwrap() + .create_frame(frame::Info { + size: (WINDOW / 2) as u64, + timestamp: Some(Timestamp::ZERO), + }) + .unwrap(); + frame.write(vec![1u8; WINDOW / 4]).unwrap(); + let mut partial = partial_frame(&mut urgent_sub).await; + read_partial(&mut partial, WINDOW / 4).await; + + // Subscription 1, sent alongside a TRACK whose TRACK_INFO the publisher holds back. + let mut requests = setup.broadcast.requests(); + let every = Subscription::default() + .with_order(moq_net::track::Order::Ascending) + .with_range(0..) + .with_max_delay(Duration::from_secs(60)); + let waiting = tokio::spawn({ + let remote = setup.remote.clone(); + async move { remote.track("parked").unwrap().subscribe(every).await } + }); + let request = kio::clock::timeout(TIMEOUT, requests.requested_track()) + .await + .expect("TRACK timed out") + .expect("TRACK"); + + // Its groups, which together with the urgent frame overflow the window: one gives way. + let groups = (0..4).map(|sequence| group_stream(1, sequence, WINDOW / 5)).collect(); + setup.raw(false, groups, 1).await; + + // The urgent frame was kept, and completes as more of the waiting groups give way. + frame.write(vec![1u8; WINDOW / 4]).unwrap(); + read_partial(&mut partial, WINDOW / 4).await; + assert!( + kio::clock::timeout(TIMEOUT, partial.read_chunk()) + .await + .expect("the urgent frame never ended") + .expect("the urgent frame failed") + .is_none(), + "the urgent frame overran its size" + ); + + // The TRACK_INFO arrives, and the groups still waiting are read. + let _track = request.accept(timed()); + let mut parked = kio::clock::timeout(TIMEOUT, waiting) + .await + .expect("subscribe timed out") + .expect("subscribe task") + .expect("subscribe"); + loop { + let mut group = next_group(&mut parked).await; + match kio::clock::timeout(TIMEOUT, group.read_frame()) + .await + .expect("a waiting group never delivered") + { + Ok(Some(frame)) => { + assert_eq!(frame.payload.len(), WINDOW / 5); + break; + } + // Stopped to make room for the urgent frame. + Err(moq_net::Error::Evicted) => continue, + other => panic!("group {} ended with {other:?}", group.sequence), + } + } +} + /// Control requests that never complete fill the window: one is stopped with its stream, /// and the session carries on. #[tokio::test] @@ -634,6 +742,51 @@ async fn ietf_largest_unit_completes_under_pressure() { the_largest_unit_completes_under_pressure("moq-transport-18").await; } +/// Drive the Bun sender through its commands against a subscriber of `origin` whose +/// connection window is `window`: a low-priority frame twice the window gives way to a +/// high-priority one, and the low track then keeps delivering. Waits for the sender to exit. +async fn javascript_exhausts( + version: moq_net::Version, + window: usize, + origin: &moq_net::origin::Producer, + mut child: tokio::process::Child, +) { + let mut commands = child.stdin.take().unwrap(); + let remote = room(origin).await; + let mut low = subscribe(&remote, "low", 1).await; + let mut high = subscribe(&remote, "high", 100).await; + + // Wait until three quarters of the window holds a frame that can never complete. + commands.write_all(&b"low\n"[..]).await.unwrap(); + let mut partial = partial_frame(&mut low).await; + read_partial(&mut partial, window * 3 / 4).await; + + // Half the window, which only fits once the low frame lets go. + commands.write_all(&b"high\n"[..]).await.unwrap(); + assert_eq!(next_frame(&mut high).await, vec![2u8; window / 2], "{version}"); + loop { + let chunk = kio::clock::timeout(TIMEOUT, partial.read_chunk()) + .await + .expect("stop timed out"); + match chunk { + Ok(Some(_)) => {} + Ok(None) => panic!("{version}: a frame larger than the window completed"), + Err(_) => break, + } + } + + // The session survives, and the low track keeps delivering what fits. + commands.write_all(&b"after\n"[..]).await.unwrap(); + assert_eq!(next_frame(&mut low).await, &b"after"[..], "{version}"); + + drop(commands); + let status = kio::clock::timeout(TIMEOUT, child.wait()) + .await + .expect("sender exit timed out") + .expect("sender"); + assert!(status.success(), "{version}: JS sender failed"); +} + /// A Bun publisher (`test/receive-credit/sender.ts`) dials a WebTransport server whose /// connection window is [`WINDOW`] and sends a low-priority frame twice that size. The /// receiver stops it, a high-priority frame completes, and a later low group arrives. @@ -665,7 +818,7 @@ async fn javascript_partial_frame_gives_way(version: &str) { let mut server = web::Server::new(endpoint); let script = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test/receive-credit/sender.ts"); - let mut child = tokio::process::Command::new("bun") + let child = tokio::process::Command::new("bun") .arg(script) .arg(format!("https://127.0.0.1:{port}/")) .arg(version.to_string()) @@ -677,7 +830,6 @@ async fn javascript_partial_frame_gives_way(version: &str) { .kill_on_drop(true) .spawn() .expect("spawn bun"); - let mut commands = child.stdin.take().unwrap(); let request = kio::clock::timeout(TIMEOUT, server.accept()) .await @@ -698,42 +850,68 @@ async fn javascript_partial_frame_gives_way(version: &str) { .await .expect("server handshake"); tokio::spawn(moq_net::time::run(driver)); - let remote = room(&origin).await; - let mut low = subscribe(&remote, "low", 1).await; - let mut high = subscribe(&remote, "high", 100).await; - - // Wait until three quarters of the window holds a frame that can never complete. - commands.write_all(&b"low\n"[..]).await.unwrap(); - let mut partial = partial_frame(&mut low).await; - read_partial(&mut partial, WINDOW * 3 / 4).await; + javascript_exhausts(version, WINDOW, &origin, child).await; + drop(session); +} - // Half the window, which only fits once the low frame lets go. - commands.write_all(&b"high\n"[..]).await.unwrap(); - assert_eq!(next_frame(&mut high).await, vec![2u8; WINDOW / 2], "{version}"); - loop { - let chunk = kio::clock::timeout(TIMEOUT, partial.read_chunk()) - .await - .expect("stop timed out"); - match chunk { - Ok(Some(_)) => {} - Ok(None) => panic!("{version}: a frame larger than the window completed"), - Err(_) => break, - } - } +/// The same exhaustion as [`javascript_partial_frame_gives_way`], over qmux on a WebSocket, +/// whose sessions lease receive credit like native QUIC. The window is moq-net's floor, the +/// smallest a qmux session takes. +async fn javascript_partial_frame_gives_way_websocket(version: &str) { + const WINDOW: usize = moq_net::transport::MIN_RECEIVE_WINDOW as usize; + let version: moq_net::Version = version.parse().expect("version"); - // The session survives, and the low track keeps delivering what fits. - commands.write_all(&b"after\n"[..]).await.unwrap(); - assert_eq!(next_frame(&mut low).await, &b"after"[..], "{version}"); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("bind"); + let port = listener.local_addr().expect("addr").port(); + let script = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../test/receive-credit/sender.ts"); + let child = tokio::process::Command::new("bun") + .arg(script) + .arg(format!("http://127.0.0.1:{port}/")) + .arg(version.to_string()) + .arg("websocket") + .arg(WINDOW.to_string()) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::inherit()) + .kill_on_drop(true) + .spawn() + .expect("spawn bun"); - drop(commands); - let status = kio::clock::timeout(TIMEOUT, child.wait()) + let (socket, _) = kio::clock::timeout(TIMEOUT, listener.accept()) .await - .expect("sender exit timed out") - .expect("sender"); - assert!(status.success(), "{version}: JS sender failed"); + .expect("connect timeout") + .expect("accept"); + socket.set_nodelay(true).expect("nodelay"); + let mut session_config = qmux::Config::default(); + session_config.limits.receive_window = std::num::NonZeroU64::new(WINDOW as u64); + let (session, driver) = qmux::ws::Server::new() + .with_protocol(version.alpn(), &[]) + .require_protocol() + .with_session_config(session_config) + .accept(socket) + .await + .expect("websocket handshake"); + tokio::spawn(driver); + let origin = moq_tokio::origin::spawn(); + let (session, driver) = moq_net::Server::new() + .with_versions(version.into()) + .with_subscriber(origin.clone()) + .accept(moq_net::time::Instant::now(), session) + .await + .expect("server handshake"); + tokio::spawn(moq_net::time::run(driver)); + javascript_exhausts(version, WINDOW, &origin, child).await; drop(session); } +#[tokio::test] +#[ignore = "requires Bun; run by just test receive-credit in the interop workflow"] +async fn javascript_partial_frame_gives_way_qmux() { + for version in ["moq-lite-06", "moq-transport-18"] { + javascript_partial_frame_gives_way_websocket(version).await; + } +} + #[tokio::test] #[ignore = "requires Bun; run by just test receive-credit in the interop workflow"] async fn javascript_partial_frame_gives_way_tokio() { @@ -743,7 +921,7 @@ async fn javascript_partial_frame_gives_way_tokio() { } /// moq-tokio's default connection receive window. -const STOCK_WINDOW: u64 = 128 << 20; +const STOCK_WINDOW: u64 = moq_tokio::quic::DEFAULT_RECEIVE_WINDOW; /// Bytes each loopback benchmark run delivers. const BENCH_BYTES: usize = 256 << 20; /// The benchmark's bottleneck, and the bytes a run delivers through it. diff --git a/rs/moq-uring/MIGRATE.md b/rs/moq-uring/MIGRATE.md index b889d2beff2..ea4b96c710b 100644 --- a/rs/moq-uring/MIGRATE.md +++ b/rs/moq-uring/MIGRATE.md @@ -4,6 +4,11 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Smaller default windows ([#7624](https://github.com/moq-dev/moq/pull/7624)) + +`quic::transport::Config::default()` takes a 4 MiB connection window, a 1 MiB stream window, and 4 MiB of unacknowledged outgoing data, down from 128 MiB, 4 MiB, and 16 MiB, matching moq-tokio's `[quic]` defaults. Set `limits` explicitly to keep the old windows. +`quic::driver::recv::Stream::received_reset`, and so `quic::web::RecvStream::poll_reset`, fail with `Error::Evicted` for a stream evicted to free a stuck window. + ### QUIC early data is disabled The shared moq-quic core no longer sends or accepts early data, including with caller-supplied rustls server configs. diff --git a/rs/moq-uring/src/quic/driver/mod.rs b/rs/moq-uring/src/quic/driver/mod.rs index 9217b33b368..2b0f92ff8b9 100644 --- a/rs/moq-uring/src/quic/driver/mod.rs +++ b/rs/moq-uring/src/quic/driver/mod.rs @@ -33,14 +33,15 @@ use crate::udp; /// Streams of each kind a peer may have open at once. const MAX_STREAMS: u64 = 1024; /// Per-stream flow control credit, which returns as bytes are read, so it only paces a stream. -const STREAM_WINDOW: u32 = 4 * 1024 * 1024; +/// The same default as moq-tokio's `[quic] stream_receive_window`. +const STREAM_WINDOW: u32 = 1024 * 1024; /// Per-connection flow control credit, which also bounds incomplete messages, so it has to fit -/// the largest one: `MIN_RECEIVE_WINDOW` is the 64 MiB announcement cap plus framing. 128 MiB -/// also holds two maximum-size frames at once. -const CONNECTION_WINDOW: u32 = 128 * 1024 * 1024; +/// the largest unit, `MIN_RECEIVE_WINDOW`: one 1 MiB segment. The same default as moq-tokio's +/// `[quic] receive_window`. +const CONNECTION_WINDOW: u32 = 4 * 1024 * 1024; const _: () = assert!(CONNECTION_WINDOW as u64 >= moq_net::transport::MIN_RECEIVE_WINDOW); /// Unacknowledged outgoing data to buffer, whatever the peer allows. -const SEND_WINDOW: u32 = 16 * 1024 * 1024; +const SEND_WINDOW: u32 = 4 * 1024 * 1024; /// Datagram bytes to buffer in each direction, about 64 full-size datagrams. const DATAGRAM_WINDOW: usize = 96 * 1024; diff --git a/rs/moq-uring/src/quic/driver/recv.rs b/rs/moq-uring/src/quic/driver/recv.rs index c205f6c7f36..b3c0f905362 100644 --- a/rs/moq-uring/src/quic/driver/recv.rs +++ b/rs/moq-uring/src/quic/driver/recv.rs @@ -211,7 +211,8 @@ impl Stream { /// /// Yields `Some` with the reset error code when the stream is reset by the peer, and `None` /// once every byte the peer finished it with has been read, after which it is no longer - /// meaningful for the stream to be reset. + /// meaningful for the stream to be reset. Fails with [`Error::Evicted`] once the stream was + /// evicted to free a stuck receive window, until it is stopped. /// /// Cancel-safe. pub async fn received_reset(&mut self) -> Result, Error> { @@ -230,6 +231,8 @@ impl Stream { } let reset = self.shared.conn.borrow_mut().recv_stream(self.id).received_reset(); match reset { + // Evicted, so its bytes are gone: nothing frees the window until it is stopped. + Err(moq_quic::recv::Error::Evicted) => Poll::Ready(Err(Error::Evicted)), Err(_) => Poll::Ready(Ok(None)), Ok(Some(code)) => { // The stream's state was just freed, so the peer may be owed stream credit, and diff --git a/rs/moq-uring/src/quic/transport.rs b/rs/moq-uring/src/quic/transport.rs index c3fc00374c6..213d658b3a8 100644 --- a/rs/moq-uring/src/quic/transport.rs +++ b/rs/moq-uring/src/quic/transport.rs @@ -20,15 +20,15 @@ pub struct Config { /// /// - 1024 streams of each kind a peer may have open at once. MoQ opens a /// stream per group, so busy endpoints want this high. - /// - A 128 MiB connection window, the unread data and incomplete messages + /// - A 4 MiB connection window, the unread data and incomplete messages /// one peer may have in flight across every stream. A moq session needs /// it to fit the largest message it assembles, /// [`MIN_RECEIVE_WINDOW`](moq_net::transport::MIN_RECEIVE_WINDOW), or a /// partial one can stall the connection. - /// - A 4 MiB stream window. Stream credit returns as bytes are read, + /// - A 1 MiB stream window. Stream credit returns as bytes are read, /// whether or not their message is complete, so this only paces one /// stream. - /// - 16 MiB of unacknowledged outgoing data, whatever the peer allows. + /// - 4 MiB of unacknowledged outgoing data, whatever the peer allows. pub limits: Limits, /// Which congestion controller to run. pub congestion: Congestion, diff --git a/rs/moq-uring/src/quic/web.rs b/rs/moq-uring/src/quic/web.rs index 751ca45747f..6592874c34e 100644 --- a/rs/moq-uring/src/quic/web.rs +++ b/rs/moq-uring/src/quic/web.rs @@ -1125,8 +1125,9 @@ impl RecvStream { } } - /// Poll until the peer resets the stream or the connection closes, without reading it; - /// pending for good once every byte arrived. See [`recv::Stream::received_reset`]. + /// Poll until the peer resets the stream, the receiver evicts it, or the connection closes, + /// without reading it; pending for good once every byte arrived. See + /// [`recv::Stream::received_reset`]. pub fn poll_reset(&mut self, cx: &mut Context<'_>) -> Poll { let web = self.web; let Some(inner) = &mut self.inner else { diff --git a/rs/qmux/MIGRATE.md b/rs/qmux/MIGRATE.md index 27861015f71..9d85216bede 100644 --- a/rs/qmux/MIGRATE.md +++ b/rs/qmux/MIGRATE.md @@ -4,6 +4,13 @@ How to update your code for each breaking change, newest first. ## Unreleased +### `RecvStream` leases its credit ([#7624](https://github.com/moq-dev/moq/pull/7624)) + +`RecvStream` keeps leased bytes' connection credit until `Lease::release`, rather than returning it as it reads through `poll::Buffered`, so `receive_window` bounds what incomplete messages hold together. When they fill it and no reader can progress, the lowest priority stream is evicted and its next lease fails with `Error::Evicted`. Its lease type is now `RecvLease`. +`Config::limits.receive_window` below moq-net's `transport::MIN_RECEIVE_WINDOW` (1 MiB) is refused with `Error::ReceiveWindowTooSmall`, and a lease past the window fails with `Error::LeaseTooLarge`. +`Session::set_limits` changes the windows and stream counts of a running session. +`RecvStream::poll_reset` completes with `Error::Evicted` once the stream is evicted, so a reader that stopped reading it still learns of it. + ### Framing belongs to session establishment Wrap byte streams with `transport::Stream::new(io)` and pass the sole `Config` to `Session::connect` or `Session::accept`; wrapping no longer starts I/O. Custom `Transport` implementations receive `&Config` in `split` and bind their framing there. diff --git a/rs/qmux/src/config.rs b/rs/qmux/src/config.rs index 197e4bcf61b..6e2deac6068 100644 --- a/rs/qmux/src/config.rs +++ b/rs/qmux/src/config.rs @@ -145,7 +145,7 @@ impl Config { return Err(crate::Error::InvalidConfig("zero rtt_interval")); } - self.limits.validate()?; + check_limits(&self.limits)?; let mut config = mux::Config::default(); config.version = self.version; @@ -168,6 +168,21 @@ impl Config { } } +/// Refuse limits a peer would refuse, and a receive window that cannot fit the largest unit a +/// moq-net session assembles: incomplete messages hold leased credit, so a smaller one could +/// stall the session. +pub(crate) fn check_limits(limits: &Limits) -> Result<(), crate::Error> { + limits.validate()?; + if let Some(window) = limits.receive_window + && window.get() < moq_net::transport::MIN_RECEIVE_WINDOW + { + return Err(crate::Error::ReceiveWindowTooSmall( + moq_net::transport::MIN_RECEIVE_WINDOW, + )); + } + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -211,6 +226,21 @@ mod tests { assert!(matches!(rtt.to_mux(), Err(crate::Error::InvalidConfig(_)))); } + /// Incomplete messages hold leased credit, so a window that cannot fit the largest unit is + /// refused rather than left to stall. + #[test] + fn a_receive_window_below_the_largest_unit_is_refused() { + let min = moq_net::transport::MIN_RECEIVE_WINDOW; + let mut config = Config::default(); + config.limits.receive_window = std::num::NonZeroU64::new(min - 1); + assert!(matches!( + config.to_mux(), + Err(crate::Error::ReceiveWindowTooSmall(got)) if got == min + )); + config.limits.receive_window = std::num::NonZeroU64::new(min); + assert!(config.to_mux().is_ok()); + } + #[test] fn limits_a_peer_would_refuse_are_refused() { let mut over = Config::default(); diff --git a/rs/qmux/src/error.rs b/rs/qmux/src/error.rs index 82a194d6035..891f01c67ef 100644 --- a/rs/qmux/src/error.rs +++ b/rs/qmux/src/error.rs @@ -110,6 +110,20 @@ pub enum Error { /// The datagram exceeds [`max_datagram_size`](moq_net::transport::poll::Session::max_datagram_size). #[error("datagram too large")] DatagramTooLarge, + + /// Incomplete messages filled the receive window and this stream ranked lowest, so its + /// buffered bytes were discarded to free it. + #[error("evicted")] + Evicted, + + /// The receive window is below the given floor, moq-net's + /// [`MIN_RECEIVE_WINDOW`](moq_net::transport::MIN_RECEIVE_WINDOW). + #[error("receive_window must be at least {0} bytes to fit the largest message")] + ReceiveWindowTooSmall(u64), + + /// A lease wanted more than the receive window can ever hold. + #[error("lease exceeds the receive window")] + LeaseTooLarge, } impl Error { @@ -168,6 +182,7 @@ impl moq_net::transport::Error for Error { match self { Self::IdleTimeout => CloseKind::IdleTimeout, Self::Closed => CloseKind::Closed, + Self::Evicted => CloseKind::Evicted, _ => CloseKind::Transport, } } diff --git a/rs/qmux/src/session.rs b/rs/qmux/src/session.rs index ba67de8989a..dfde62ae656 100644 --- a/rs/qmux/src/session.rs +++ b/rs/qmux/src/session.rs @@ -6,7 +6,7 @@ //! wakes, the way the QUIC drivers do. use std::sync::{Arc, Mutex, MutexGuard}; -use std::task::Poll; +use std::task::{Poll, ready}; use std::time::Duration; use bytes::{Buf, Bytes}; @@ -281,6 +281,25 @@ impl Session { self.inner.lock().conn.version() } + /// Change the windows and stream counts `limits` sets, leaving the others as they are, such + /// as to widen a session once its peer is known. + /// + /// Refuses, changing nothing, a rate cap (QMux never paces, so only + /// [`Rate::Unlimited`](moq_quic::Rate::Unlimited) is accepted), a receive window below + /// moq-net's [`MIN_RECEIVE_WINDOW`](net::MIN_RECEIVE_WINDOW), and limits a peer would + /// refuse. A window only grows at once: credit already granted cannot be taken back. + pub fn set_limits(&self, limits: moq_quic::Limits) -> Result<(), Error> { + crate::config::check_limits(&limits)?; + let capped = |rate: Option| matches!(rate, Some(moq_quic::Rate::BytesPerSecond(_))); + if capped(limits.receive_rate) || capped(limits.send_rate) { + return Err(Error::InvalidConfig("qmux does not pace, so it takes no rate cap")); + } + let mut state = self.inner.lock(); + state.conn.set_limits(limits)?; + state.driver.wake(); + Ok(()) + } + /// Open a send group at `priority`, for streams to join with /// [`SendStream::set_group`](poll::SendStream::set_group) and datagrams with /// [`SendOptions`](poll::SendOptions). @@ -476,18 +495,28 @@ impl poll::Session for Session { Metrics { rtt: socket.and_then(|stats| stats.rtt()).or(state.conn.rtt()), rate: socket.and_then(|stats| stats.estimated_send_rate()), + evicted: state.conn.credit_stats().evicted, } } fn wire(&self) -> Option { Some(self.inner.wire.clone()) } + + fn receive_window(&self) -> Option { + let state = self.inner.lock(); + Some(net::ReceiveWindow::new( + state.conn.receive_window(), + state.conn.stream_receive_window(), + )) + } } /// A snapshot of the session's metrics; its [`net::Wire`] holds the byte counts. struct Metrics { rtt: Option, rate: Option, + evicted: u64, } impl net::Metrics for Metrics { @@ -498,6 +527,10 @@ impl net::Metrics for Metrics { fn estimated_send_rate(&self) -> Option { self.rate } + + fn streams_evicted(&self) -> Option { + Some(self.evicted) + } } /// A send group on a [`Session`]. @@ -684,152 +717,232 @@ impl std::fmt::Debug for SendStream { } } -/// One read out of the connection's reassembly buffer. -enum Read { - /// Bytes, at most as many as were asked for. - Chunk(Bytes), - /// Every byte up to the FIN has been handed over. - Finished, - /// Nothing buffered; the reader wakes us when that changes. - Blocked, - /// The peer reset the stream with this code. - Reset(u64), -} - -/// An incoming stream. Dropping it unfinished sends STOP_SENDING with code 0. +/// An incoming stream. Dropping it sends STOP_SENDING with code 0, which also frees its state. /// -/// Reading returns the peer's connection credit as it goes, so incomplete messages are -/// bounded by the largest message each rather than by `receive_window`, and nothing is evicted. -pub struct RecvStream(poll::Buffered); +/// Leased bytes keep their connection credit until released, so `receive_window` bounds what +/// every stream's incomplete messages hold together. When they fill it and no reader can make +/// progress, the stream ranked lowest by [`set_priority`](poll::RecvStream::set_priority) is +/// evicted: its next lease fails with [`Error::Evicted`]. +pub struct RecvStream { + inner: Arc, + id: StreamId, + /// The peer reset the stream with this code. Observing it frees the connection's stream + /// state, so later calls report it from here. + reset: Option, + /// We stopped the stream; it is gone. + stopped: bool, +} impl RecvStream { fn new(inner: Arc, id: StreamId) -> Self { - Self(poll::Buffered::new(RecvChunks { + Self { inner, id, - finished: false, reset: None, stopped: false, - })) + } + } + + /// Take the connection's lease, which [`RecvLease`] wraps once this returns: the borrow of + /// `self` it holds cannot start inside a poll that may return `Pending`. + fn poll_take(&mut self, waiter: &kio::Waiter, want: usize) -> Poll> { + if let Some(code) = self.reset { + return Poll::Ready(Err(Error::StreamReset(code))); + } + if self.stopped { + return Poll::Ready(Err(Error::StreamClosed)); + } + let mut state = self.inner.lock(); + let (result, transmit) = state.conn.recv_stream(self.id).lease(want); + // Freed credit, or an eviction whose reader the driver's sweep wakes. + if transmit.should_transmit() { + state.driver.wake(); + } + match result { + Ok(lease) => Poll::Ready(Ok(lease)), + Err(recv::Error::Blocked) => { + if let Some(err) = &state.closed { + return Poll::Ready(Err(err.clone())); + } + waiter.register(state.readable.entry(self.id).or_default()); + Poll::Pending + } + Err(recv::Error::Reset(code)) => { + // Observing the reset frees the stream and the credit its lease withheld; only + // then may later calls skip it. + let _ = state.conn.recv_stream(self.id).received_reset(); + state.readable.remove(&self.id); + state.driver.wake(); + self.reset = Some(code.into_inner()); + Poll::Ready(Err(Error::StreamReset(code.into_inner()))) + } + Err(recv::Error::Evicted) => Poll::Ready(Err(Error::Evicted)), + Err(recv::Error::TooLarge) => Poll::Ready(Err(Error::LeaseTooLarge)), + // The stream is gone, which only a lost connection does to one we hold. + Err(_) => Poll::Ready(Err(state.closed.clone().unwrap_or(Error::Closed))), + } } } impl poll::RecvStream for RecvStream { type Error = Error; - type Lease<'a> = poll::BufferedLease<'a>; + type Lease<'a> = RecvLease<'a>; - fn poll_lease(&mut self, waiter: &kio::Waiter, want: usize) -> Poll, Error>> { - self.0.poll_lease(waiter, want) + fn poll_lease(&mut self, waiter: &kio::Waiter, want: usize) -> Poll, Error>> { + let lease = ready!(self.poll_take(waiter, want))?; + Poll::Ready(Ok(RecvLease { + stream: self, + lease: Some(lease), + })) } fn set_priority(&mut self, priority: i64) { - self.0.set_priority(priority); + if self.reset.is_some() || self.stopped { + return; + } + self.inner.lock().conn.recv_stream(self.id).set_priority(priority); } fn stop(&mut self, code: u32) { - self.0.stop(code); - } -} - -impl std::fmt::Debug for RecvStream { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("RecvStream").field("id", &self.0.get_ref().id).finish() + if self.stopped || self.reset.is_some() { + return; + } + let mut state = self.inner.lock(); + state.readable.remove(&self.id); + // Err means the stream is already gone, which is what we wanted. + let _ = state.conn.recv_stream(self.id).stop(VarInt::from_u32(code)); + state.driver.wake(); + self.stopped = true; } -} - -/// Reads a [`RecvStream`] out of the connection's reassembly buffer. -struct RecvChunks { - inner: Arc, - id: StreamId, - /// Every byte up to the FIN was read out of the connection. - finished: bool, - /// The peer reset the stream with this code. Reading it frees the connection's stream - /// state, so later reads report it from here. - reset: Option, - /// We stopped the stream; no more reads matter. - stopped: bool, -} -impl poll::Chunks for RecvChunks { - type Error = Error; - - fn poll_read_chunk(&mut self, waiter: &kio::Waiter, max: usize) -> Poll, Error>> { - if max == 0 { - return Poll::Ready(Ok(Some(Bytes::new()))); - } + fn poll_reset(&mut self, waiter: &kio::Waiter) -> Poll { if let Some(code) = self.reset { - return Poll::Ready(Err(Error::StreamReset(code))); + return Poll::Ready(Error::StreamReset(code)); } - if self.finished { - return Poll::Ready(Ok(None)); + if self.stopped { + return Poll::Ready(Error::StreamClosed); } let mut state = self.inner.lock(); - match read(&mut state, self.id, max) { - Read::Chunk(bytes) => Poll::Ready(Ok(Some(bytes))), - Read::Finished => { - self.finished = true; - Poll::Ready(Ok(None)) + match state.conn.recv_stream(self.id).received_reset() { + // Evicted, so its bytes are gone: nothing frees the window until it is stopped. + Err(recv::Error::Evicted) => Poll::Ready(Error::Evicted), + // Every byte arrived, so nothing is left to reset. + Err(_) => Poll::Pending, + Ok(Some(code)) => { + // Observing it freed the stream, which may owe the peer credit. + self.reset = Some(code.into_inner()); + state.driver.wake(); + Poll::Ready(Error::StreamReset(code.into_inner())) } - Read::Blocked => { + Ok(None) => { if let Some(err) = &state.closed { - return Poll::Ready(Err(err.clone())); + return Poll::Ready(err.clone()); } + // A reset notifies readers, since it is an immediate read error. waiter.register(state.readable.entry(self.id).or_default()); Poll::Pending } - Read::Reset(code) => { - self.reset = Some(code); - Poll::Ready(Err(Error::StreamReset(code))) - } } } +} - fn stop(&mut self, code: u32) { - if self.stopped || self.finished || self.reset.is_some() { +impl Drop for RecvStream { + fn drop(&mut self) { + if self.stopped || self.reset.is_some() { return; } let mut state = self.inner.lock(); - // Err means the stream is already gone, which is what we wanted. - let _ = state.conn.recv_stream(self.id).stop(VarInt::from_u32(code)); + state.readable.remove(&self.id); + // A leased stream keeps its state until stopped, even once it ended; Err means it is + // already gone. + let _ = state.conn.recv_stream(self.id).stop(VarInt::from_u32(0)); state.driver.wake(); - self.stopped = true; } } -/// Take up to `max` bytes out of the reassembly buffer. +impl std::fmt::Debug for RecvStream { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RecvStream").field("id", &self.id).finish() + } +} + +/// Unreleased bytes of a [`RecvStream`], read as a [`Buf`]. /// -/// Reading is what returns the peer's flow control credit, so a read that owes it a frame -/// kicks the writer. -fn read(state: &mut State, id: StreamId, max: usize) -> Read { - let mut recv = state.conn.recv_stream(id); - let mut chunks = match recv.read() { - Ok(chunks) => chunks, - // The stream is gone, so everything it held is already ours. - Err(_) => return Read::Finished, - }; - let read = match chunks.next(max) { - Ok(Some(chunk)) => Read::Chunk(chunk.bytes), - Ok(None) => Read::Finished, - Err(recv::Error::Reset(code)) => Read::Reset(code.into_inner()), - // Blocked, or the connection closed, which the caller reports. - Err(_) => Read::Blocked, - }; - if chunks.finalize().should_transmit() { - state.driver.wake(); +/// It owns the bytes, so the session carries on while it is held. [`release`](poll::Lease::release) +/// returns the credit of the bytes read; dropping it keeps their credit, as for a message read +/// only in part. Either way the bytes not read are leased again next time. +pub struct RecvLease<'a> { + stream: &'a mut RecvStream, + /// Taken only to hand it back. + lease: Option, +} + +impl RecvLease<'_> { + fn lease(&self) -> &moq_quic::recv::Lease { + self.lease.as_ref().expect("only taken to hand it back") + } + + fn lease_mut(&mut self) -> &mut moq_quic::recv::Lease { + self.lease.as_mut().expect("only taken to hand it back") + } +} + +impl Buf for RecvLease<'_> { + fn remaining(&self) -> usize { + self.lease().remaining() + } + + fn chunk(&self) -> &[u8] { + self.lease().chunk() + } + + fn advance(&mut self, n: usize) { + self.lease_mut().advance(n); + } + + fn copy_to_bytes(&mut self, n: usize) -> Bytes { + self.lease_mut().copy_to_bytes(n) + } +} + +impl poll::Lease for RecvLease<'_> { + fn chunks(&self) -> impl Iterator { + self.lease().chunks() + } + + fn is_fin(&self) -> bool { + self.lease().is_fin() + } + + fn release(mut self) { + let lease = self.lease.take().expect("only taken to hand it back"); + let mut state = self.stream.inner.lock(); + if state.conn.recv_stream(self.stream.id).release(lease).should_transmit() { + state.driver.wake(); + } } - read } -impl Drop for RecvChunks { +impl Drop for RecvLease<'_> { fn drop(&mut self) { - let mut state = self.inner.lock(); - state.readable.remove(&self.id); - if !self.finished && !self.stopped && self.reset.is_none() { - let _ = state.conn.recv_stream(self.id).stop(VarInt::from_u32(0)); + let Some(lease) = self.lease.take() else { + return; + }; + let mut state = self.stream.inner.lock(); + if state.conn.recv_stream(self.stream.id).restore(lease).should_transmit() { state.driver.wake(); } } } +impl std::fmt::Debug for RecvLease<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RecvLease") + .field("id", &self.stream.id) + .field("remaining", &self.remaining()) + .finish() + } +} + #[cfg(all(test, any(feature = "tcp", all(unix, feature = "uds"))))] mod tests; diff --git a/rs/qmux/src/session/tests.rs b/rs/qmux/src/session/tests.rs index 5c54f6c4691..1aca3608374 100644 --- a/rs/qmux/src/session/tests.rs +++ b/rs/qmux/src/session/tests.rs @@ -138,11 +138,11 @@ async fn every_version_establishes_and_negotiates_in_band() { async fn reading_returns_flow_control_credit() { for &version in Version::ALL { let mut small = config(version); - small.limits.receive_window = std::num::NonZeroU64::new(64 * 1024); + small.limits.receive_window = std::num::NonZeroU64::new(moq_net::transport::MIN_RECEIVE_WINDOW); small.limits.stream_receive_window = std::num::NonZeroU64::new(16 * 1024); let (client, server) = pair(config(version), small).await; - let data = payload(1 << 20, 3); + let data = payload(4 << 20, 3); let mut send = client.open_uni(SendOptions::default()).await.unwrap(); let writer = { let data = data.clone(); @@ -159,6 +159,156 @@ async fn reading_returns_flow_control_credit() { } } +/// Lease `want` bytes, returning how many were leased and handing them back unread, so they +/// keep their credit. +async fn lease(recv: &mut RecvStream, want: usize) -> Result { + kio::wait(|waiter| recv.poll_lease(waiter, want).map_ok(|lease| lease.remaining())).await +} + +/// Lease `want` bytes, then read and release them, returning their credit. +async fn read_exact(recv: &mut RecvStream, want: usize) -> Result { + kio::wait(|waiter| { + recv.poll_lease(waiter, want).map_ok(|mut lease| { + let read = lease.copy_to_bytes(want); + poll::Lease::release(lease); + read + }) + }) + .await +} + +/// Leased bytes keep their credit, so incomplete messages fill the receive window rather than +/// memory outside it. Once no reader can make progress, the lowest priority holder is evicted, +/// and the credit it held lets the others carry on. +#[tokio::test(start_paused = true)] +async fn partial_messages_evict_the_lowest_priority_stream() { + const HALF: usize = 512 * 1024; + for &version in Version::ALL { + let window = 2 * HALF as u64; + let mut server = config(version); + server.limits.receive_window = std::num::NonZeroU64::new(window); + server.limits.stream_receive_window = std::num::NonZeroU64::new(window); + let (client, server) = pair(config(version), server).await; + assert_eq!( + poll::Session::receive_window(&server), + Some(net::ReceiveWindow::new(window, window)) + ); + + let mut sends = Vec::new(); + for seed in 0..3 { + let mut send = client.open_uni(SendOptions::default()).await.unwrap(); + write_all(&mut send, payload(1, seed)).await; + sends.push(send); + } + let mut kept = server.accept_uni().await.unwrap(); + let mut evicted = server.accept_uni().await.unwrap(); + let mut waiting = server.accept_uni().await.unwrap(); + kept.set_priority(1); + waiting.set_priority(2); + + // Two incomplete messages fill the window, and a third waits for credit. + let (mut keep, mut evict, mut wait) = (sends.remove(0), sends.remove(0), sends.remove(0)); + write_all(&mut keep, payload(HALF - 1, 0)).await; + write_all(&mut evict, payload(HALF - 2, 1)).await; + let writer = tokio::spawn(async move { + write_all(&mut wait, payload(HALF - 1, 2)).await; + wait + }); + + // Every reader wants more than it holds, so the window is stuck. + let kept = tokio::spawn(async move { + let leased = lease(&mut kept, HALF + 1).await; + (kept, leased) + }); + let waiting = tokio::spawn(async move { + let read = read_exact(&mut waiting, HALF).await; + (waiting, read) + }); + let err = lease(&mut evicted, HALF).await.expect_err("evicted"); + assert!(matches!(err, Error::Evicted), "{version:?}: {err}"); + assert_eq!(net::Error::close_kind(&err), net::CloseKind::Evicted); + assert_eq!(net::Metrics::streams_evicted(&poll::Session::metrics(&server)), Some(1)); + drop(evicted); + + // The evicted stream's credit lets the waiting one through, and reading it returns + // the credit the kept stream needs to finish its message. + let (_waiting, read) = waiting.await.unwrap(); + assert_eq!(read.unwrap().len(), HALF, "{version:?}"); + let _wait = writer.await.unwrap(); + write_all(&mut keep, payload(1, 0)).await; + let (_kept, leased) = kept.await.unwrap(); + assert_eq!(leased.unwrap(), HALF + 1, "{version:?}"); + drop(evict); + } +} + +/// A running session widens its windows, and refuses a rate cap it could never pace, while an +/// explicit "no cap" is fine. +#[tokio::test(start_paused = true)] +async fn set_limits_widens_and_refuses_a_rate_cap() { + let (_client, server) = pair(config(Version::QMux02), config(Version::QMux02)).await; + let mut limits = moq_quic::Limits::default(); + limits.receive_window = std::num::NonZeroU64::new(16 << 20); + limits.receive_rate = Some(moq_quic::Rate::Unlimited); + limits.send_rate = Some(moq_quic::Rate::Unlimited); + server.set_limits(limits).expect("unlimited rates"); + assert_eq!(poll::Session::receive_window(&server).unwrap().connection, 16 << 20); + + limits.send_rate = Some(moq_quic::Rate::BytesPerSecond(std::num::NonZeroU64::new(1000).unwrap())); + assert!(matches!(server.set_limits(limits), Err(Error::InvalidConfig(_)))); + limits.send_rate = None; + limits.receive_window = std::num::NonZeroU64::new(1024); + assert!(matches!( + server.set_limits(limits), + Err(Error::ReceiveWindowTooSmall(_)) + )); +} + +/// A reset seen through a lease frees the stream while the reader still holds it, returning +/// its stream credit and the connection credit its partial message withheld. +#[tokio::test(start_paused = true)] +async fn a_reset_seen_by_a_lease_frees_the_stream() { + let mut server = config(Version::QMux02); + server.limits.max_streams = std::num::NonZeroU64::new(1); + let (client, server) = pair(config(Version::QMux02), server).await; + + let mut first = client.open_uni(SendOptions::default()).await.unwrap(); + write_all(&mut first, payload(1000, 0)).await; + let mut recv = server.accept_uni().await.unwrap(); + assert_eq!(lease(&mut recv, 1000).await.unwrap(), 1000, "a partial message, leased"); + first.reset(3); + let err = lease(&mut recv, 2000).await.expect_err("reset"); + assert!(matches!(err, Error::StreamReset(3)), "{err}"); + + // Still holding `recv`, the peer gets its stream back. + let opening = client.open_uni(SendOptions::default()); + kio::clock::timeout(Duration::from_secs(1), opening) + .await + .expect("credit returned on the reset") + .unwrap(); + drop(recv); +} + +/// A stream the session stops reading still sees a reset, without reading a byte. +#[tokio::test(start_paused = true)] +async fn a_reset_reaches_an_unread_stream() { + for &version in Version::ALL { + let (client, server) = pair(config(version), config(version)).await; + let mut send = client.open_uni(SendOptions::default()).await.unwrap(); + write_all(&mut send, payload(16, 0)).await; + let mut recv = server.accept_uni().await.unwrap(); + + let mut reset = std::pin::pin!(kio::wait(|waiter| recv.poll_reset(waiter))); + assert!( + futures_poll_once(&mut reset).await.is_none(), + "{version:?}: no reset yet" + ); + send.reset(7); + let err = reset.await; + assert!(matches!(err, Error::StreamReset(7)), "{version:?}: {err}"); + } +} + #[tokio::test(start_paused = true)] async fn bidirectional_streams_carry_both_halves_and_fins() { for &version in Version::ALL { diff --git a/test/receive-credit/sender.ts b/test/receive-credit/sender.ts index 85ea5c7ba10..7ea3575d038 100644 --- a/test/receive-credit/sender.ts +++ b/test/receive-credit/sender.ts @@ -2,7 +2,8 @@ * A JavaScript publisher that drives a native receiver into receive-credit exhaustion. * * Dials the Rust test's WebTransport server through the `@moq/web-transport` polyfill, - * pinned to its certificate, and announces broadcast `room` with tracks `low` and `high`. + * pinned to its certificate, or its qmux server over WebSocket when the certificate argument + * is `websocket`, and announces broadcast `room` with tracks `low` and `high`. * Each stdin line asks for one group, so the receiver decides when each one is written: * * - `low`: a frame twice the window on `low`, which can never complete. @@ -10,6 +11,7 @@ * - `after`: a small group on `low`, which has to arrive after that stream is stopped. * * bun sender.ts https://127.0.0.1:PORT/ VERSION CERT.pem WINDOW + * bun sender.ts http://127.0.0.1:PORT/ VERSION websocket WINDOW * * Exits 0 once stdin closes. * @@ -21,15 +23,17 @@ import { install } from "@moq/web-transport"; // stdout is unused, but keep library debug logs off it all the same. console.debug = console.error; -// Bun has no native WebTransport; @moq/net reads the global at connect time. -install(); - const [address, version, certificate, windowArg] = process.argv.slice(2); const window = Number(windowArg); if (!address || !version || !certificate || !Number.isInteger(window) || window <= 0) { - console.error("usage: sender.ts URL VERSION CERT.pem WINDOW"); + console.error("usage: sender.ts URL VERSION (CERT.pem | websocket) WINDOW"); process.exit(2); } +const websocket = certificate === "websocket"; + +// Bun has no native WebTransport; @moq/net reads the global at connect time. Without it, +// the WebSocket fallback is the only transport. +if (!websocket) install(); const origin = new Moq.Origin.Producer(); const broadcastPublication = origin.createBroadcast(Moq.Path.from("room")); @@ -42,9 +46,14 @@ const session = await Moq.Connection.connect({ url: new URL(address), publish: origin.consume(), versions: [version], - webtransport: { serverCertificate: await Bun.file(certificate).text() }, - // The point is the native QUIC receiver; a WebSocket fallback would skip it. - websocket: { enabled: false }, + // Each run tests one receiver, so only its transport may connect. Without WebTransport + // installed, the WebSocket fallback dials at once. + ...(websocket + ? {} + : { + webtransport: { serverCertificate: await Bun.file(certificate).text() }, + websocket: { enabled: false }, + }), }); // Written once and left open: the receiver has to give up on it.