From 9c397c1c5c724e4da7abe677237d982453522b1c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 17:29:03 -0700 Subject: [PATCH 01/19] quest(m2): claim quic-ack-progress From 7ec8d9d79721efa0c4db0eb5cb2fb5f307d6d0b7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 17:51:59 -0700 Subject: [PATCH 02/19] feat(quic): report when a checkpoint's data is acknowledged Checkpoint::acked returns the ACK-delay-corrected instant once the peer acknowledged everything before the checkpoint, and fails once it never will. StreamEvent::Acked reports the change. Co-Authored-By: Claude Opus 5.5 --- quest/m1/quic/group-reset.md | 2 - quest/m2/README.md | 2 - quest/m2/quic-ack-hook.md | 13 +- quest/m2/quic-ack-progress.md | 51 ----- rs/moq-quic/src/connection/mod.rs | 38 ++-- rs/moq-quic/src/connection/send_buffer.rs | 209 ++++++++++++++++-- rs/moq-quic/src/connection/streams/mod.rs | 70 +++++- rs/moq-quic/src/connection/streams/send.rs | 8 +- rs/moq-quic/src/connection/streams/state.rs | 134 +++++++----- rs/moq-quic/src/tests/acked.rs | 222 ++++++++++++++++++++ rs/moq-quic/src/tests/mod.rs | 1 + rs/moq-uring/src/quic/driver/connection.rs | 2 + 12 files changed, 599 insertions(+), 153 deletions(-) delete mode 100644 quest/m2/quic-ack-progress.md create mode 100644 rs/moq-quic/src/tests/acked.rs diff --git a/quest/m1/quic/group-reset.md b/quest/m1/quic/group-reset.md index 1b851760e6a..d73b340a423 100644 --- a/quest/m1/quic/group-reset.md +++ b/quest/m1/quic/group-reset.md @@ -58,7 +58,5 @@ half did not reach. - [SUBSCRIBE_DROP accounts for every group](/quest/m1/subscribe-drop.md) - a DROP covers a reset whose header was lost; a reset that keeps its header acts as a one-group drop -- [Per-stream ACK progress](/quest/m2/quic-ack-progress.md) - adds - `Checkpoint::acked()` to the same handle - [qmux on the QUIC stream state machine](/quest/m2/quic-qmux.md) - qmux reuses `moq-quic`'s reset state diff --git a/quest/m2/README.md b/quest/m2/README.md index 813b339b87a..6315a86c25b 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -121,8 +121,6 @@ ranked just below deterministic TS legs, which it requires. - [Align BBR loss handling with draft-06](/quest/m2/quic-bbr-loss-parity.md) - losses use their own sample and the draft-05 links go, keeping the spurious-loss undo that already re-enters ProbeUp through Refill - [Measure ECN on the backbone](/quest/m2/quic-ecn-measure.md) - a written verdict on marking versus dropping, and whether Linode and OVH keep marks -- [Per-stream ACK progress](/quest/m2/quic-ack-progress.md) - `moq-quic` reports - how far a send stream has been acknowledged and when - [poll_acked on moq-net's send stream](/quest/m2/quic-ack-hook.md) - the backend-neutral hook on moq-net's own transport trait that awaits an acknowledged stream offset, implemented by the moq-tokio and moq-uring adapters diff --git a/quest/m2/quic-ack-hook.md b/quest/m2/quic-ack-hook.md index e3706528728..af97bb49196 100644 --- a/quest/m2/quic-ack-hook.md +++ b/quest/m2/quic-ack-hook.md @@ -36,7 +36,13 @@ unsupported has to live in the return type rather than the error, and a consumer must treat `None` as unknown, never as delivered. Implement it in the moq-tokio and moq-uring adapters over `moq-quic`'s -accessor. Leave the browser transport on the default; the browser's `WebTransportSendStream.getStats()` is +`Checkpoint::acked`, which returns the corrected instant once everything +before the checkpoint is acknowledged and fails once it never will be; +`StreamEvent::Acked` wakes its waiters, which moq-uring's `sweep_stream` +currently ignores. A checkpoint is taken at the current write offset, so the +adapter checkpoints each offset as it is written rather than naming an +arbitrary one later; settle whether the trait method takes an offset or +returns a handle from a `checkpoint()` like the reset one. Leave the browser transport on the default; the browser's `WebTransportSendStream.getStats()` is unimplemented in shipping Chrome and its `bytesAcknowledged` is at risk in the W3C draft. qmux over a reliable transport may treat serialization as acknowledgment only if the qmux quest decides that is honest; until then it @@ -51,11 +57,6 @@ STOP_SENDING by the receiver, and session close. Decided in the 2026-09-30 audit: moved to m2 with its only consumer, [frame-granularity starvation](/quest/m2/starvation-frames.md). -## Required - -- [Per-stream ACK progress](/quest/m2/quic-ack-progress.md) - the - `moq-quic` accessor the adapter reads - ## Related - [Starvation at frame granularity](/quest/m2/starvation-frames.md) - the diff --git a/quest/m2/quic-ack-progress.md b/quest/m2/quic-ack-progress.md deleted file mode 100644 index ac73c222784..00000000000 --- a/quest/m2/quic-ack-progress.md +++ /dev/null @@ -1,51 +0,0 @@ -# [S] moq-quic reports per-stream acknowledgment progress - -## Goal - -`moq-quic` lets a sender ask how much of a send stream the peer has -acknowledged and when each acknowledgment was received, corrected for the -peer's reported ACK delay. Nothing MoQ builds on top needs private state. - -## Plan - -The work lives in `moq-quic`. - -`moq-quic`'s `SendBuffer` (`connection/send_buffer.rs`) already tracks most -of what is needed: the acknowledged range set (`acks`), the write offset, the -unacked length, and `is_fully_acked`; the contiguous acknowledged prefix -follows from them. `Connection` also -computes the peer's `ack_delay` per ACK frame when it updates the RTT -estimator. Expose that state through the public `SendStream` handle without -copying it: - -- an accessor for the contiguous acknowledged prefix of the stream; -- an event or waker registration that fires when that prefix crosses an - offset the caller names, so a caller can await "bytes below X are - acknowledged" without polling; -- the receive instant of the ACK that advanced the prefix, minus the peer's - reported ACK delay. That subtraction removes the delayed-ACK timer (up to - `max_ack_delay`, 25 ms by default) so a delivery-latency sample sees the - network path and not the receiver's batching. State clearly that the - instant still includes the return one-way delay. - -Match Quinn semantics for partial ACKs, retransmission of a range that was -already partly acknowledged, a stream that is reset by either side, and -stream teardown: the accessor must not return a prefix that includes bytes the -peer will never acknowledge, and a waiter for an offset beyond the final -size must resolve with the reset instead of hanging. - -Decided 2026-10-16 (maintainer, #5690): the waiter is `Checkpoint::acked()` -on the send-stream checkpoint that `RESET_STREAM_AT` already uses, so an -acknowledged offset is named the same way a reliable reset's is. - -The `web-transport-moq` half of [the ACK hook](/quest/m2/quic-ack-hook.md) -is in-tree too and can land in the same PR. - -Decided in the 2026-09-30 audit: moved to m2 with its consumers, the ACK -hook and [frame-granularity starvation](/quest/m2/starvation-frames.md). - -## Related - -- [poll_acked on moq-net's send stream](/quest/m2/quic-ack-hook.md) - the first - consumer of the accessor -- [noq#808](https://github.com/n0-computer/noq/issues/808) - the acked-offset ask to n0 diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index 9cdf4ae177a..557c84c6850 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1592,6 +1592,17 @@ impl Connection { return Ok(()); } + let ack_delay = if space != SpaceId::Data { + Duration::from_micros(0) + } else { + cmp::min( + self.ack_frequency.peer_max_ack_delay, + Duration::from_micros(ack.delay << self.peer_params.ack_delay_exponent.0), + ) + }; + // When the peer received the data, plus the ACK's trip back + let acked_at = now.checked_sub(ack_delay).unwrap_or(now); + let mut ack_eliciting_acked = false; for packet in newly_acked.elts() { if let Some(info) = self.spaces[space].take(packet) { @@ -1616,7 +1627,7 @@ impl Connection { // Notify ack frequency that a packet was acked, because it might contain an ACK_FREQUENCY frame self.ack_frequency.on_acked(packet); - self.on_packet_acked(now, info, packet, space); + self.on_packet_acked(now, acked_at, info, packet, space); } } @@ -1629,14 +1640,6 @@ impl Connection { ); if new_largest && ack_eliciting_acked { - let ack_delay = if space != SpaceId::Data { - Duration::from_micros(0) - } else { - cmp::min( - self.ack_frequency.peer_max_ack_delay, - Duration::from_micros(ack.delay << self.peer_params.ack_delay_exponent.0), - ) - }; let rtt = now.saturating_duration_since(self.spaces[space].largest_acked_packet_sent); self.path.rtt.update(ack_delay, rtt); if self.path.first_packet_after_rtt_sample.is_none() { @@ -1757,7 +1760,16 @@ impl Connection { // Not timing-aware, so it's safe to call this for inferred acks, such as arise from // high-latency handshakes - fn on_packet_acked(&mut self, now: Instant, info: SentPacket, pn: u64, space: SpaceId) { + // + // `acked_at` is when the ACK was received, less the ACK delay the peer reported. + fn on_packet_acked( + &mut self, + now: Instant, + acked_at: Instant, + info: SentPacket, + pn: u64, + space: SpaceId, + ) { self.remove_in_flight(&info); if info.ack_eliciting && self.path.challenge.is_none() { // Only pass ACKs to the congestion controller if we are not validating the current @@ -1780,10 +1792,12 @@ impl Connection { } } + // An ACK delay the peer overstated cannot place the receipt before the send + let acked_at = acked_at.max(info.time_sent); for frame in info.stream_frames { let id = frame.id; // A reset held for a peer without RESET_STREAM_AT goes out once it holds the data - if let Some(reliable_size) = self.streams.received_ack_of(frame) { + if let Some(reliable_size) = self.streams.received_ack_of(frame, acked_at) { self.spaces[SpaceId::Data] .pending .reset_stream @@ -2757,7 +2771,7 @@ impl Connection { let space = &mut self.spaces[SpaceId::Initial]; if let Some(info) = space.take(0) { - self.on_packet_acked(now, info, 0, SpaceId::Initial); + self.on_packet_acked(now, now, info, 0, SpaceId::Initial); }; self.discard_space(now, SpaceId::Initial); // Make sure we clean up after any retransmitted Initials diff --git a/rs/moq-quic/src/connection/send_buffer.rs b/rs/moq-quic/src/connection/send_buffer.rs index 97efcde5c20..a92cefb6a15 100644 --- a/rs/moq-quic/src/connection/send_buffer.rs +++ b/rs/moq-quic/src/connection/send_buffer.rs @@ -1,8 +1,17 @@ -use std::{collections::VecDeque, ops::Range}; +use std::{ + collections::VecDeque, + mem, + ops::Range, + sync::{Arc, OnceLock, Weak}, +}; use bytes::{Buf, Bytes}; -use crate::{VarInt, range_set::RangeSet}; +use super::streams::ClosedStream; +use crate::{Instant, VarInt, range_set::RangeSet}; + +/// Where a [`Checkpoint`](super::Checkpoint) learns when the data before it was acknowledged +pub(super) type AckSlot = OnceLock>; /// Buffer of outgoing retransmittable stream data #[derive(Default, Debug)] @@ -25,6 +34,10 @@ pub(super) struct SendBuffer { acks: RangeSet, /// Previously transmitted ranges deemed lost retransmits: RangeSet, + /// Checkpoints waiting for the acknowledged prefix to reach them + watches: Watches, + /// When the ACK that last advanced the acknowledged prefix was received, less its ACK delay + acked_at: Option, } impl SendBuffer { @@ -40,18 +53,23 @@ impl SendBuffer { self.unacked_segments.push_back(data); } - /// Discard a range of acknowledged stream data - pub(super) fn ack(&mut self, mut range: Range) { + /// Discard a range of acknowledged stream data, acknowledged at `at` + /// + /// `at` is when the ACK was received, less the ACK delay the peer reported. + pub(super) fn ack(&mut self, mut range: Range, at: Instant) { // Clamp the range to data which is still tracked, which `truncate` may have cut short let base_offset = self.offset - self.unacked_len as u64; range.start = base_offset.max(range.start); range.end = base_offset.max(range.end).min(self.offset); - if range.is_empty() { - return; + if !range.is_empty() { + self.acks.insert(range); + self.advance(at); } + self.watches.ack(self.acked(), at); + } - self.acks.insert(range); - + /// Discard the acknowledged ranges that extend the acknowledged prefix + fn advance(&mut self, at: Instant) { while self.acks.min() == Some(self.offset - self.unacked_len as u64) { let prefix = self.acks.pop_min().unwrap(); let mut to_advance = (prefix.end - prefix.start) as usize; @@ -77,7 +95,36 @@ impl SendBuffer { to_advance = 0; } } + self.acked_at = Some(at); + } + } + + /// The end of the contiguous acknowledged prefix + fn acked(&self) -> u64 { + self.offset - self.unacked_len as u64 + } + + /// Watch for the peer to acknowledge everything written so far + pub(super) fn watch(&mut self) -> Arc { + let slot = Arc::new(AckSlot::new()); + match self.acked_at { + // The ACK that last advanced the prefix is the one that reached the end + Some(at) if self.acked() == self.offset => { + let _ = slot.set(Ok(at)); + } + _ => self.watches.push(self.offset, &slot), } + slot + } + + /// Fail every checkpoint still waiting, since the peer stopped reading + pub(super) fn abandon_watches(&mut self) { + self.watches.fail_all(); + } + + /// Whether a checkpoint somebody still holds resolved since the last call + pub(super) fn take_resolved(&mut self) -> bool { + mem::take(&mut self.watches.resolved) } /// Compute the next range to transmit on this stream and update state to account for that @@ -169,6 +216,7 @@ impl SendBuffer { if end >= self.offset { return; } + self.watches.fail_past(end); let base_offset = self.offset - self.unacked_len as u64; let mut keep = end.saturating_sub(base_offset) as usize; if keep == 0 { @@ -176,6 +224,8 @@ impl SendBuffer { *self = Self { offset: end, unsent: end, + watches: mem::take(&mut self.watches), + acked_at: self.acked_at, ..Self::default() }; return; @@ -237,9 +287,69 @@ impl SendBuffer { } } +/// Checkpoints waiting for the acknowledged prefix to reach their offset, in offset order +/// +/// A dropped [`Checkpoint`](super::Checkpoint) leaves its entry behind until the prefix reaches +/// it, so it lives no longer than the data it waits on. Dropping the stream fails the rest, so no +/// waiter hangs on a stream that is gone. +#[derive(Default, Debug)] +struct Watches { + pending: VecDeque<(u64, Weak)>, + /// Whether a checkpoint somebody still holds resolved since the stream last reported it + resolved: bool, +} + +impl Watches { + fn push(&mut self, offset: u64, slot: &Arc) { + // Checkpoints are taken at the write offset, which only grows + debug_assert!(self.pending.back().is_none_or(|(last, _)| *last <= offset)); + self.pending.push_back((offset, Arc::downgrade(slot))); + } + + /// Resolve every checkpoint at or below the acknowledged prefix `acked` + fn ack(&mut self, acked: u64, at: Instant) { + while let Some((offset, _)) = self.pending.front() + && *offset <= acked + { + let (_, slot) = self.pending.pop_front().unwrap(); + self.resolve(slot, Ok(at)); + } + } + + /// Fail every checkpoint past `end`, which the peer will never acknowledge + fn fail_past(&mut self, end: u64) { + while let Some((offset, _)) = self.pending.back() + && *offset > end + { + let (_, slot) = self.pending.pop_back().unwrap(); + self.resolve(slot, Err(ClosedStream::default())); + } + } + + fn fail_all(&mut self) { + while let Some((_, slot)) = self.pending.pop_front() { + self.resolve(slot, Err(ClosedStream::default())); + } + } + + fn resolve(&mut self, slot: Weak, result: Result) { + if let Some(slot) = slot.upgrade() { + let _ = slot.set(result); + self.resolved = true; + } + } +} + +impl Drop for Watches { + fn drop(&mut self) { + self.fail_all(); + } +} + #[cfg(test)] mod tests { use super::*; + use crate::Duration; #[test] fn fragment_with_length() { @@ -375,17 +485,17 @@ mod tests { assert_eq!(buf.poll_transmit(42), (MSG_LEN..MSG_LEN, true)); // Now drain the segments - buf.ack(0..1); + buf.ack(0..1, Instant::now()); assert_eq!(aggregate_unacked(&buf), &MSG[1..]); - buf.ack(0..3); + buf.ack(0..3, Instant::now()); assert_eq!(aggregate_unacked(&buf), &MSG[3..]); - buf.ack(3..5); + buf.ack(3..5, Instant::now()); assert_eq!(aggregate_unacked(&buf), &MSG[5..]); - buf.ack(7..9); + buf.ack(7..9, Instant::now()); assert_eq!(aggregate_unacked(&buf), &MSG[5..]); - buf.ack(4..7); + buf.ack(4..7, Instant::now()); assert_eq!(aggregate_unacked(&buf), &MSG[9..]); - buf.ack(0..MSG_LEN); + buf.ack(0..MSG_LEN, Instant::now()); assert_eq!(aggregate_unacked(&buf), &[] as &[u8]); } @@ -413,7 +523,7 @@ mod tests { const MSG: &[u8] = b"Hello, world!"; buf.write(MSG.into()); assert_eq!(buf.poll_transmit(16), (0..8, true)); - buf.ack(0..8); + buf.ack(0..8, Instant::now()); assert_eq!(aggregate_unacked(&buf), &MSG[8..]); } @@ -424,9 +534,9 @@ mod tests { buf.write(MSG.into()); assert_eq!(buf.poll_transmit(16), (0..16, false)); assert_eq!(buf.poll_transmit(16), (16..23, true)); - buf.ack(16..23); + buf.ack(16..23, Instant::now()); assert_eq!(aggregate_unacked(&buf), MSG); - buf.ack(0..16); + buf.ack(0..16, Instant::now()); assert_eq!(aggregate_unacked(&buf), &MSG[23..]); assert!(buf.acks.is_empty()); } @@ -437,7 +547,7 @@ mod tests { buf.write(Bytes::from_static(b"Hello, ")); buf.write(Bytes::from_static(b"world!")); assert_eq!(buf.poll_transmit(16), (0..8, true)); - buf.ack(0..2); + buf.ack(0..2, Instant::now()); buf.retransmit(2..8); buf.truncate(9); @@ -449,10 +559,10 @@ mod tests { assert!(!buf.has_unsent_data()); // Late acks and losses past the truncation are ignored - buf.ack(9..13); + buf.ack(9..13, Instant::now()); buf.retransmit(9..13); assert!(!buf.has_unsent_data()); - buf.ack(2..9); + buf.ack(2..9, Instant::now()); assert!(buf.is_fully_acked()); assert_eq!(buf.buffered(), 0); } @@ -462,13 +572,70 @@ mod tests { let mut buf = SendBuffer::new(); buf.write(Bytes::from_static(b"Hello, world!")); assert_eq!(buf.poll_transmit(16), (0..8, true)); - buf.ack(0..8); + buf.ack(0..8, Instant::now()); buf.truncate(4); assert!(buf.is_fully_acked()); assert!(!buf.has_unsent_data()); assert_eq!(buf.buffered(), 0); } + #[test] + fn watch_waits_for_prefix() { + let mut buf = SendBuffer::new(); + buf.write(Bytes::from_static(b"Hello, ")); + let first = buf.watch(); + buf.write(Bytes::from_static(b"world!")); + let second = buf.watch(); + assert_eq!(buf.poll_transmit(16), (0..8, true)); + assert_eq!(buf.poll_transmit(16), (8..13, true)); + + // Acknowledged past both, but not contiguously + let early = Instant::now(); + buf.ack(8..13, early); + assert_eq!(first.get(), None); + assert_eq!(second.get(), None); + assert!(!buf.take_resolved()); + + // Filling the gap in the middle of the first segment resolves both, with that ACK + let late = early + Duration::from_millis(1); + buf.ack(0..8, late); + assert_eq!(first.get(), Some(&Ok(late))); + assert_eq!(second.get(), Some(&Ok(late))); + assert!(buf.take_resolved()); + assert!(!buf.take_resolved()); + + // Everything written is acknowledged, by the ACK that got there + assert_eq!(buf.watch().get(), Some(&Ok(late))); + } + + #[test] + fn watch_fails_past_truncation() { + let mut buf = SendBuffer::new(); + buf.write(Bytes::from_static(b"Hello, ")); + let kept = buf.watch(); + buf.write(Bytes::from_static(b"world!")); + let dropped = buf.watch(); + + buf.truncate(7); + assert_eq!(dropped.get(), Some(&Err(ClosedStream::default()))); + assert_eq!(kept.get(), None); + assert!(buf.take_resolved()); + + // A stream that goes away fails what it never acknowledged + drop(buf); + assert_eq!(kept.get(), Some(&Err(ClosedStream::default()))); + } + + #[test] + fn dropped_watch_is_not_reported() { + let mut buf = SendBuffer::new(); + buf.write(Bytes::from_static(b"Hello")); + drop(buf.watch()); + assert_eq!(buf.poll_transmit(16), (0..5, true)); + buf.ack(0..5, Instant::now()); + assert!(!buf.take_resolved()); + } + fn aggregate_unacked(buf: &SendBuffer) -> Vec { let mut result = Vec::new(); for segment in buf.unacked_segments.iter() { diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index 99d96605888..7908d1fac74 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -1,6 +1,7 @@ use std::{ collections::{BinaryHeap, hash_map}, io, + sync::Arc, }; use bytes::Bytes; @@ -9,8 +10,8 @@ use tracing::trace; use super::spaces::{Retransmits, ThinRetransmits}; use crate::{ - Dir, StreamId, VarInt, - connection::{ConnectionTag, streams::state::get_or_insert_recv}, + Dir, Instant, StreamId, VarInt, + connection::{ConnectionTag, send_buffer::AckSlot, streams::state::get_or_insert_recv}, frame, }; @@ -328,21 +329,27 @@ impl<'a> SendStream<'a> { self.reset_to(error_code, 0) } - /// Mark the data written so far, so a later [`Checkpoint::reset`] still delivers it + /// Mark the data written so far, to learn when the peer acknowledges it with + /// [`Checkpoint::acked`], or to still deliver it with a later [`Checkpoint::reset`] /// /// # Panics /// - when applied to a receive stream - pub fn checkpoint(&self) -> Checkpoint { - let offset = match self.state.send.get(&self.id) { - Some(Some(stream)) => stream.offset(), - // Not written yet, or already closed, where the reset fails anyway - _ => 0, + pub fn checkpoint(&mut self) -> Checkpoint { + let max_send_data = self.state.max_send_data(self.id); + let (offset, acked) = match self.state.send.get_mut(&self.id) { + Some(stream) => { + let stream = stream.get_or_insert_with(|| Send::new(max_send_data)); + (stream.offset(), stream.pending.watch()) + } + // Already closed, where the reset fails anyway + None => (0, Arc::new(AckSlot::from(Err(ClosedStream::default())))), }; Checkpoint { id: self.id, tag: self.state.tag, generation: self.state.generation, offset, + acked, } } @@ -375,6 +382,11 @@ impl<'a> SendStream<'a> { let buffered = stream.pending.buffered(); let send = stream.reset(error_code, reliable_size, at); self.state.buffered_data -= buffered - stream.pending.buffered(); + if stream.pending.take_resolved() { + self.state + .events + .push_back(StreamEvent::Acked { id: self.id }); + } if let Some(reliable_size) = send { self.pending.reset_stream.push((self.id, reliable_size)); } @@ -415,7 +427,8 @@ impl<'a> SendStream<'a> { } } -/// The data written to a send stream up to some point, which a reset can still deliver +/// The data written to a send stream up to some point, which the peer acknowledges and a reset can +/// still deliver /// /// Taken with [`SendStream::checkpoint`], and only valid on the connection it came from. #[derive(Debug)] @@ -424,9 +437,41 @@ pub struct Checkpoint { tag: ConnectionTag, generation: u64, offset: u64, + acked: Arc, } impl Checkpoint { + /// When the peer acknowledged everything written before the checkpoint, or `None` until it has + /// + /// The instant is when the ACK that brought the stream's contiguous acknowledged prefix to the + /// checkpoint was received, less the ACK delay the peer reported, capped at its + /// `max_ack_delay`. That leaves out the peer's delayed-ACK timer, but still includes the ACK's + /// one-way trip back. It is never earlier than the data's packet was sent. A checkpoint taken + /// once everything written was already acknowledged gets the ACK that got it there, and one + /// taken before anything was written gets the stream's next ACK. + /// + /// [`StreamEvent::Acked`] reports when this resolves. Like a [`StreamEvent::Finished`] stream, + /// an acknowledgment only means the peer's QUIC stack holds the data, not that its + /// application read it. + /// + /// Fails with [`ClosedStream`] once the peer will never acknowledge it, or no longer reads + /// it: the stream was reset before the checkpoint's data was acknowledged, the peer sent + /// STOP_SENDING, rejected 0-RTT discarded the stream, or the connection closed. + /// + /// # Panics + /// If the checkpoint came from another connection. + pub fn acked(&self, conn: &crate::Connection) -> Result, ClosedStream> { + assert!( + self.tag == conn.streams.tag, + "checkpoint used on another connection" + ); + match self.acked.get() { + Some(acked) => acked.clone().map(Some), + None if conn.is_closed() => Err(ClosedStream { _private: () }), + None => Ok(None), + } + } + /// Abandon the stream, but still deliver everything written before the checkpoint /// /// Nothing past the checkpoint is sent or retransmitted. A peer that accepts RESET_STREAM_AT @@ -574,6 +619,13 @@ pub enum StreamEvent { /// Which stream has been finished id: StreamId, }, + /// A [`Checkpoint`] on a send stream was acknowledged, or will never be + /// + /// Not generated when the connection closes or rejected 0-RTT discards the stream. + Acked { + /// Which stream the checkpoint is on + id: StreamId, + }, /// The peer asked us to stop sending on an outgoing stream Stopped { /// Which stream has been stopped diff --git a/rs/moq-quic/src/connection/streams/send.rs b/rs/moq-quic/src/connection/streams/send.rs index 729e39e6811..f1489f3e59d 100644 --- a/rs/moq-quic/src/connection/streams/send.rs +++ b/rs/moq-quic/src/connection/streams/send.rs @@ -1,7 +1,7 @@ use bytes::Bytes; use thiserror::Error; -use crate::{StreamId, VarInt, connection::send_buffer::SendBuffer, frame}; +use crate::{Instant, StreamId, VarInt, connection::send_buffer::SendBuffer, frame}; #[derive(Debug)] pub(super) struct Send { @@ -228,8 +228,10 @@ impl Send { /// Returns whether the stream has been finished or reset, and all data it still owes the peer /// has been acknowledged - pub(super) fn ack(&mut self, frame: frame::StreamMeta) -> bool { - self.pending.ack(frame.offsets); + /// + /// `at` is when the ACK was received, less the ACK delay the peer reported. + pub(super) fn ack(&mut self, frame: frame::StreamMeta, at: Instant) -> bool { + self.pending.ack(frame.offsets, at); match &mut self.state { SendState::DataSent { finish_acked } => { *finish_acked |= frame.fin; diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index 60f12adfa5c..9baf37a5591 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -13,7 +13,7 @@ use super::{ StreamHalf, ThinRetransmits, }; use crate::{ - Dir, MAX_STREAM_COUNT, Side, StreamId, TransportError, VarInt, + Dir, Instant, MAX_STREAM_COUNT, Side, StreamId, TransportError, VarInt, coding::BufMutExt, connection::ConnectionTag, connection::stats::{CreditStats, FrameStats}, @@ -103,7 +103,7 @@ pub(crate) struct StreamsState { /// Streams with outgoing data queued, sorted by priority pub(super) pending: PendingStreamsQueue, - events: VecDeque, + pub(super) events: VecDeque, /// Streams blocked on connection-level flow control or stream window space /// /// Streams are only added to this list when a write fails. @@ -411,9 +411,14 @@ impl StreamsState { self.events .push_back(StreamEvent::Stopped { id, error_code }); } + // An acknowledgment no longer means the peer will read the data + stream.pending.abandon_watches(); let buffered = stream.pending.buffered(); let reset = stream.abandon_reliable(); self.buffered_data -= buffered - stream.pending.buffered(); + if stream.pending.take_resolved() { + self.events.push_back(StreamEvent::Acked { id }); + } reset } @@ -733,7 +738,9 @@ impl StreamsState { /// /// Returns the Reliable Size of a held reset to send now, once the peer holds all the data it /// waited on. - pub(crate) fn received_ack_of(&mut self, frame: frame::StreamMeta) -> Option { + /// + /// `at` is when the ACK was received, less the ACK delay the peer reported. + pub(crate) fn received_ack_of(&mut self, frame: frame::StreamMeta, at: Instant) -> Option { let mut entry = match self.send.entry(frame.id) { hash_map::Entry::Vacant(_) => return None, hash_map::Entry::Occupied(e) => e, @@ -749,8 +756,11 @@ impl StreamsState { let id = frame.id; let buffered = stream.pending.buffered(); - let finished = stream.ack(frame); + let finished = stream.ack(frame, at); self.buffered_data -= buffered - stream.pending.buffered(); + if stream.pending.take_resolved() { + self.events.push_back(StreamEvent::Acked { id }); + } if !finished { // The stream is unfinished or may still need retransmits return stream.take_held_reset(); @@ -1182,31 +1192,40 @@ mod tests { assert_eq!(send.pending.poll_transmit(16).0, 16..23); assert_eq!(send.pending.poll_transmit(16).0, 23..24); - server.received_ack_of(frame::StreamMeta { - id, - offsets: 16..23, - fin: false, - }); + server.received_ack_of( + frame::StreamMeta { + id, + offsets: 16..23, + fin: false, + }, + Instant::now(), + ); assert_eq!( server.write_limit(), 0, "acknowledged suffix still occupies storage" ); - server.received_ack_of(frame::StreamMeta { - id, - offsets: 0..16, - fin: false, - }); + server.received_ack_of( + frame::StreamMeta { + id, + offsets: 0..16, + fin: false, + }, + Instant::now(), + ); assert_eq!( server.write_limit(), 0, "partial prefix keeps the original Bytes allocation" ); - server.received_ack_of(frame::StreamMeta { - id, - offsets: 23..24, - fin: false, - }); + server.received_ack_of( + frame::StreamMeta { + id, + offsets: 23..24, + fin: false, + }, + Instant::now(), + ); assert_eq!(server.write_limit(), 24); SendStream { id, @@ -1247,11 +1266,14 @@ mod tests { let send = server.send.get_mut(&id).unwrap().as_mut().unwrap(); send.pending.poll_transmit(16); send.pending.poll_transmit(16); - server.received_ack_of(frame::StreamMeta { - id, - offsets: 16..23, - fin: false, - }); + server.received_ack_of( + frame::StreamMeta { + id, + offsets: 16..23, + fin: false, + }, + Instant::now(), + ); SendStream { id, state: &mut server, @@ -1277,11 +1299,14 @@ mod tests { offsets: 0..16, fin: false, }); - server.received_ack_of(frame::StreamMeta { - id, - offsets: 0..16, - fin: false, - }); + server.received_ack_of( + frame::StreamMeta { + id, + offsets: 0..16, + fin: false, + }, + Instant::now(), + ); assert!(!server.send[&id].as_ref().unwrap().is_pending()); assert_eq!(server.write_limit(), 24); } @@ -2505,11 +2530,14 @@ mod tests { assert_eq!(stream.state.poll(), None); // Ack the data - stream.state.received_ack_of(frame::StreamMeta { - id: stream_id, - offsets: 0..larger_send_window, - fin: false, - }); + stream.state.received_ack_of( + frame::StreamMeta { + id: stream_id, + offsets: 0..larger_send_window, + fin: false, + }, + Instant::now(), + ); assert_eq!( stream.state.poll(), @@ -2577,20 +2605,26 @@ mod tests { assert_eq!(stream.write(&data), Err(WriteError::Blocked)); // Ack some data, assert that writes are still not accepted due to outstanding sends - stream.state.received_ack_of(frame::StreamMeta { - id: stream_id, - offsets: 0..smaller_send_window, - fin: false, - }); + stream.state.received_ack_of( + frame::StreamMeta { + id: stream_id, + offsets: 0..smaller_send_window, + fin: false, + }, + Instant::now(), + ); assert_eq!(stream.write(&data), Err(WriteError::Blocked)); // Ack the rest of the data - stream.state.received_ack_of(frame::StreamMeta { - id: stream_id, - offsets: smaller_send_window..initial_send_window, - fin: false, - }); + stream.state.received_ack_of( + frame::StreamMeta { + id: stream_id, + offsets: smaller_send_window..initial_send_window, + fin: false, + }, + Instant::now(), + ); // This should generate a `Writable` event assert_eq!( @@ -2897,7 +2931,10 @@ mod tests { buf.clear(); let resent = server.write_stream_frames(&mut buf, 100, true); assert_eq!(resent[0].offsets, 0..4); - assert_eq!(server.received_ack_of(resent[0].clone()), Some(4)); + assert_eq!( + server.received_ack_of(resent[0].clone(), Instant::now()), + Some(4) + ); assert_eq!(server.buffered_data, 0); // The plain RESET_STREAM still names the full final size @@ -2965,7 +3002,10 @@ mod tests { server.reset_acked(id, 4); assert_eq!(server.received_stop_sending(id, 9u32.into()), Some(0)); - assert_eq!(server.received_ack_of(sent[0].clone()), None); + assert_eq!( + server.received_ack_of(sent[0].clone(), Instant::now()), + None + ); assert_eq!(server.send_streams, 1); server.reset_acked(id, 0); assert_eq!(server.send_streams, 0); @@ -3053,7 +3093,7 @@ mod tests { // The stream lingers until both the reset and the prefix are acknowledged server.reset_acked(id, 4); assert_eq!(server.send_streams, 1); - server.received_ack_of(resent[0].clone()); + server.received_ack_of(resent[0].clone(), Instant::now()); assert_eq!(server.send_streams, 0); assert_eq!(server.buffered_data, 0); assert_eq!(server.poll(), None); diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs new file mode 100644 index 00000000000..b069946c1f1 --- /dev/null +++ b/rs/moq-quic/src/tests/acked.rs @@ -0,0 +1,222 @@ +//! Tests for [`Checkpoint::acked`], which reports when the peer acknowledged a send stream's prefix + +use super::*; + +const LATENCY: Duration = Duration::from_millis(10); + +/// A pair with a one-way delay, whose server does or does not advertise `reset_stream_at` +fn pair(reset_stream_at: bool) -> Pair { + let mut config = server_config(); + let mut transport = cubic_transport(); + transport.reset_stream_at(reset_stream_at); + config.transport = Arc::new(transport); + let mut pair = Pair::new(Default::default(), config); + pair.latency = LATENCY; + pair +} + +/// Open a client stream that the server has accepted, with nothing acknowledged yet +fn open(pair: &mut Pair, client_ch: ConnectionHandle, server_ch: ConnectionHandle) -> StreamId { + let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + pair.client_send(client_ch, s).write(b"header").unwrap(); + pair.drive(); + assert_eq!(pair.server_streams(server_ch).accept(Dir::Uni), Some(s)); + s +} + +fn acked( + pair: &mut Pair, + ch: ConnectionHandle, + checkpoint: &Checkpoint, +) -> Result, ClosedStream> { + checkpoint.acked(pair.client_conn_mut(ch)) +} + +/// Whether the client reported a checkpoint on `s` resolving, draining its events +fn reported(pair: &mut Pair, ch: ConnectionHandle, s: StreamId) -> bool { + let mut reported = false; + while let Some(event) = pair.client_conn_mut(ch).poll() { + reported |= matches!(event, Event::Stream(StreamEvent::Acked { id }) if id == s); + } + reported +} + +/// Transmit the client's pending packets and drop them +fn lose_client_packets(pair: &mut Pair) { + pair.client.drive(pair.time, pair.server.addr); + assert!(!pair.client.outbound.is_empty()); + pair.client.outbound.clear(); +} + +/// The instant leaves out the peer's delayed-ACK timer, but keeps both one-way trips. +#[test] +fn excludes_ack_delay() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + reported(&mut pair, client_ch, s); + + // A lone packet waits out the peer's delayed-ACK timer + pair.client_send(client_ch, s).write(b"payload").unwrap(); + let checkpoint = pair.client_send(client_ch, s).checkpoint(); + let sent = pair.time; + assert_eq!(acked(&mut pair, client_ch, &checkpoint), Ok(None)); + let received = loop { + let busy = pair.step(); + if acked(&mut pair, client_ch, &checkpoint) != Ok(None) { + break pair.time; + } + assert!(busy, "never acknowledged"); + }; + let at = acked(&mut pair, client_ch, &checkpoint).unwrap().unwrap(); + + assert!(reported(&mut pair, client_ch, s)); + assert_eq!(at - sent, 2 * LATENCY); + assert!( + received - at >= Duration::from_millis(20), + "{:?}", + received - at + ); +} + +/// A checkpoint resolves only once every byte before it is acknowledged, by the ACK that fills +/// the gap, however much of the stream that ACK covers. +#[test] +fn waits_for_contiguous_prefix() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + pair.client_send(client_ch, s).write(b"lost").unwrap(); + let lost = pair.client_send(client_ch, s).checkpoint(); + lose_client_packets(&mut pair); + + // Acknowledged, but behind the lost packet: the middle of the stream, not its prefix + pair.client_send(client_ch, s).write(b"after").unwrap(); + let after = pair.client_send(client_ch, s).checkpoint(); + let acks = pair.client_conn_mut(client_ch).stats().frame_rx.acks; + while pair.client_conn_mut(client_ch).stats().frame_rx.acks == acks { + assert!(pair.step(), "never acknowledged"); + } + assert_eq!(acked(&mut pair, client_ch, &lost), Ok(None)); + assert_eq!(acked(&mut pair, client_ch, &after), Ok(None)); + + // The retransmission fills the gap, which resolves both at once + pair.drive(); + let at = acked(&mut pair, client_ch, &lost).unwrap(); + assert!(at.is_some()); + assert_eq!(acked(&mut pair, client_ch, &after), Ok(at)); + assert!(reported(&mut pair, client_ch, s)); + assert!(pair.client_conn_mut(client_ch).stats().path.lost_packets > 0); +} + +/// A checkpoint taken once everything is acknowledged resolves at once, with the ACK that got +/// there. +#[test] +fn already_acked() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + let first = pair.client_send(client_ch, s).checkpoint(); + let second = pair.client_send(client_ch, s).checkpoint(); + let at = acked(&mut pair, client_ch, &first).unwrap(); + assert!(at.is_some()); + assert_eq!(acked(&mut pair, client_ch, &second), Ok(at)); +} + +/// A finished stream is gone once acknowledged, but its checkpoints keep their answer. +#[test] +fn outlives_finished_stream() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + pair.client_send(client_ch, s).write(b"payload").unwrap(); + let checkpoint = pair.client_send(client_ch, s).checkpoint(); + pair.client_send(client_ch, s).finish().unwrap(); + pair.drive(); + + assert_eq!(pair.client_streams(client_ch).send_streams(), 0); + assert!(acked(&mut pair, client_ch, &checkpoint).unwrap().is_some()); + assert!(reported(&mut pair, client_ch, s)); +} + +/// A reset fails the checkpoints past its Reliable Size at once, while those before it still +/// resolve once the prefix arrives. +#[test] +fn reset_fails_past_reliable_size() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + pair.client_send(client_ch, s).write(b"kept").unwrap(); + let kept = pair.client_send(client_ch, s).checkpoint(); + let reset = pair.client_send(client_ch, s).checkpoint(); + pair.client_send(client_ch, s).write(b"dropped").unwrap(); + let dropped = pair.client_send(client_ch, s).checkpoint(); + lose_client_packets(&mut pair); + reported(&mut pair, client_ch, s); + + reset + .reset(pair.client_conn_mut(client_ch), VarInt(42)) + .unwrap(); + assert_eq!( + acked(&mut pair, client_ch, &dropped), + Err(ClosedStream::default()) + ); + assert_eq!(acked(&mut pair, client_ch, &kept), Ok(None)); + assert!(reported(&mut pair, client_ch, s)); + + pair.drive(); + assert_eq!(pair.client_streams(client_ch).send_streams(), 0); + assert!(acked(&mut pair, client_ch, &kept).unwrap().is_some()); +} + +/// A peer that stopped reading will not read what it acknowledges. +#[test] +fn stop_sending_fails() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + pair.client_send(client_ch, s).write(b"unread").unwrap(); + let checkpoint = pair.client_send(client_ch, s).checkpoint(); + lose_client_packets(&mut pair); + reported(&mut pair, client_ch, s); + + pair.server_recv(server_ch, s).stop(VarInt(7)).unwrap(); + pair.drive(); + assert_eq!( + acked(&mut pair, client_ch, &checkpoint), + Err(ClosedStream::default()) + ); + assert!(reported(&mut pair, client_ch, s)); +} + +/// Nothing is acknowledged on a closed connection. +#[test] +fn close_fails() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + pair.client_send(client_ch, s).write(b"unacked").unwrap(); + let checkpoint = pair.client_send(client_ch, s).checkpoint(); + lose_client_packets(&mut pair); + + let now = pair.time; + pair.client_conn_mut(client_ch) + .close(now, VarInt(0), Bytes::new()); + assert_eq!( + acked(&mut pair, client_ch, &checkpoint), + Err(ClosedStream::default()) + ); +} diff --git a/rs/moq-quic/src/tests/mod.rs b/rs/moq-quic/src/tests/mod.rs index 28ebabca77e..2c9743f129a 100644 --- a/rs/moq-quic/src/tests/mod.rs +++ b/rs/moq-quic/src/tests/mod.rs @@ -42,6 +42,7 @@ use crate::{ mod util; use util::*; +mod acked; mod reset_stream_at; mod token; diff --git a/rs/moq-uring/src/quic/driver/connection.rs b/rs/moq-uring/src/quic/driver/connection.rs index 2b7c834b89e..b16e9e70434 100644 --- a/rs/moq-uring/src/quic/driver/connection.rs +++ b/rs/moq-uring/src/quic/driver/connection.rs @@ -946,6 +946,8 @@ fn sweep_stream(state: &mut State, event: moq_quic::StreamEvent) { waiters.wake(); } } + // Nothing parks on a checkpoint's acknowledgment. + moq_quic::StreamEvent::Acked { .. } => {} } } From ede4d627e97a5af1084530a1bac11261687f032a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 19:11:58 -0700 Subject: [PATCH 03/19] docs(quic): migration notes and a checkpoint benchmark Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/MIGRATE.md | 23 ++++++++++++++ rs/moq-quic/benches/streams.rs | 57 +++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index 0bff7f133db..386a088ce80 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -14,3 +14,26 @@ config.congestion_controller_factory(Arc::new(congestion::NewRenoConfig::default // After config.congestion_controller_factory(Arc::new(congestion::CubicConfig::default())); ``` + +### `SendStream::checkpoint` takes `&mut self` + +A checkpoint now registers to learn when the peer acknowledges the data before it, read with `Checkpoint::acked`. +Callers holding a shared borrow of the `SendStream` need a mutable one. + +```rust +// Before +let checkpoint = stream.checkpoint(); // stream: &SendStream + +// After +let checkpoint = stream.checkpoint(); // stream: &mut SendStream +``` + +### `StreamEvent::Acked` + +`StreamEvent` gained `Acked { id }`, reported when a `Checkpoint` on that send stream was acknowledged or never will be. +An exhaustive `match` needs an arm for it. + +```rust +// After +StreamEvent::Acked { id } => wake_checkpoint_waiters(id), +``` diff --git a/rs/moq-quic/benches/streams.rs b/rs/moq-quic/benches/streams.rs index 35fc49536f8..a274a5d3802 100644 --- a/rs/moq-quic/benches/streams.rs +++ b/rs/moq-quic/benches/streams.rs @@ -79,9 +79,64 @@ fn bench_write_size(c: &mut Criterion) { group.finish(); } +/// Checkpoints per stream against stream count, each awaited until acknowledged, on 16 writes of +/// 1 KiB per stream. +fn bench_checkpoint(c: &mut Criterion) { + const WRITES: usize = 16; + const SIZE: usize = 1024; + let mut group = c.benchmark_group("send_checkpoint"); + for streams in [1, 64, 256] { + for checkpoints in [0, 1, WRITES] { + let mut pair = None; + group.throughput(Throughput::Bytes((streams * WRITES * SIZE) as u64)); + group.bench_function( + BenchmarkId::new( + format!("streams_{streams}"), + format!("checkpoints_{checkpoints}"), + ), + |b| { + let pair = pair.get_or_insert_with(|| Pair::new(Setup::default())); + b.iter(|| checkpoint(pair, streams, checkpoints, WRITES, SIZE)) + }, + ); + } + } + group.finish(); +} + +/// Like [`send`], taking `checkpoints` evenly spaced checkpoints on each stream and checking +/// each was acknowledged. +fn checkpoint(pair: &mut Pair, streams: usize, checkpoints: usize, writes: usize, size: usize) { + let data = payload(size); + let every = writes / checkpoints.max(1); + let target = pair.server.received.finished + streams as u64; + let mut taken = Vec::with_capacity(streams * checkpoints); + let conn = pair.client(); + for _ in 0..streams { + let id = conn.streams().open(Dir::Uni).expect("stream limit"); + let mut stream = conn.send_stream(id); + for write in 1..=writes { + assert_eq!(stream.write(&data).unwrap(), size, "write was cut short"); + if checkpoints > 0 && write % every == 0 { + taken.push(stream.checkpoint()); + } + } + stream.finish().unwrap(); + } + pair.drive_until(|pair| pair.server.received.finished >= target); + pair.settle(); + let conn = pair.client(); + for checkpoint in &taken { + assert!( + checkpoint.acked(conn).unwrap().is_some(), + "never acknowledged" + ); + } +} + criterion_group! { name = benches; config = common::config(); - targets = bench_priority, bench_write_size + targets = bench_priority, bench_write_size, bench_checkpoint } criterion_main!(benches); From 4a1d70683dcccdcf7d9cf5af9941b8e25a700bd7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 19:45:11 -0700 Subject: [PATCH 04/19] fix(quic): checkpoint instants on reordered ACKs and empty resets An ACK that repeats its largest packet only covers reordered packets, so its delay is not theirs to subtract. A reset that leaves nothing to acknowledge fails a checkpoint taken before any write, and reports it. Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/mod.rs | 9 +++++-- rs/moq-quic/src/connection/send_buffer.rs | 22 ++++++++++++++++- rs/moq-quic/src/connection/streams/mod.rs | 8 +++--- rs/moq-quic/src/tests/acked.rs | 30 +++++++++++++++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index a59ab74a3c2..96e2f2624c2 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1609,8 +1609,13 @@ impl Connection { Duration::from_micros(ack.delay << self.peer_params.ack_delay_exponent.0), ) }; - // When the peer received the data, plus the ACK's trip back - let acked_at = now.checked_sub(ack_delay).unwrap_or(now); + // When the peer received the data, plus the ACK's trip back. The delay is for the + // largest packet acknowledged, so an ACK that repeats it only newly covers packets that + // arrived since, at an unknown point within that delay. + let acked_at = match new_largest { + true => now.checked_sub(ack_delay).unwrap_or(now), + false => now, + }; let mut ack_eliciting_acked = false; for packet in newly_acked.elts() { diff --git a/rs/moq-quic/src/connection/send_buffer.rs b/rs/moq-quic/src/connection/send_buffer.rs index a92cefb6a15..5ec3a2d520f 100644 --- a/rs/moq-quic/src/connection/send_buffer.rs +++ b/rs/moq-quic/src/connection/send_buffer.rs @@ -213,10 +213,19 @@ impl SendBuffer { /// /// The buffer then ends at `end`, so the caller tracks the stream's final size. pub(super) fn truncate(&mut self, end: u64) { + self.watches.fail_past(end); + self.cut(end); + // Only a checkpoint taken before anything was acknowledged can still wait, and with + // nothing left to acknowledge, no ACK will resolve it + if self.is_fully_acked() { + self.watches.fail_all(); + } + } + + fn cut(&mut self, end: u64) { if end >= self.offset { return; } - self.watches.fail_past(end); let base_offset = self.offset - self.unacked_len as u64; let mut keep = end.saturating_sub(base_offset) as usize; if keep == 0 { @@ -626,6 +635,17 @@ mod tests { assert_eq!(kept.get(), Some(&Err(ClosedStream::default()))); } + /// A checkpoint on an empty stream fails, and is reported, once a reset leaves nothing to + /// acknowledge. + #[test] + fn watch_fails_on_empty_reset() { + let mut buf = SendBuffer::new(); + let empty = buf.watch(); + buf.truncate(0); + assert_eq!(empty.get(), Some(&Err(ClosedStream::default()))); + assert!(buf.take_resolved()); + } + #[test] fn dropped_watch_is_not_reported() { let mut buf = SendBuffer::new(); diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index 3271ace1c28..b5f22fc264b 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -482,9 +482,11 @@ impl Checkpoint { /// The instant is when the ACK that brought the stream's contiguous acknowledged prefix to the /// checkpoint was received, less the ACK delay the peer reported, capped at its /// `max_ack_delay`. That leaves out the peer's delayed-ACK timer, but still includes the ACK's - /// one-way trip back. It is never earlier than the data's packet was sent. A checkpoint taken - /// once everything written was already acknowledged gets the ACK that got it there, and one - /// taken before anything was written gets the stream's next ACK. + /// one-way trip back. The delay is only subtracted from an ACK that raises the largest + /// acknowledged packet, since it describes that packet, not reordered ones the ACK repeats it + /// for. The instant is never earlier than the data's packet was sent. A checkpoint taken once + /// everything written was already acknowledged gets the ACK that got it there, and one taken + /// before anything was written gets the stream's next ACK, or fails if a reset comes first. /// /// [`StreamEvent::Acked`] reports when this resolves. Like a [`StreamEvent::Finished`] stream, /// an acknowledgment only means the peer's QUIC stack holds the data, not that its diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index b069946c1f1..9e21b4c1c90 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -80,6 +80,36 @@ fn excludes_ack_delay() { ); } +/// A reordered packet acknowledged after a later one keeps its receive instant: the ACK's delay +/// describes the later packet, so none is subtracted. +#[test] +fn reordered_ack_keeps_its_delay() { + const HOLD: Duration = Duration::from_millis(1); + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + // The first packet is held back, so it arrives after the second + pair.client_send(client_ch, s).write(b"held").unwrap(); + let held = pair.client_send(client_ch, s).checkpoint(); + pair.client.drive(pair.time, pair.server.addr); + pair.client.delay_outbound(); + pair.client_send(client_ch, s).write(b"ahead").unwrap(); + pair.drive_client(); + + pair.time += HOLD; + let released = pair.time; + pair.client.finish_delay(); + pair.drive_client(); + pair.drive(); + + // The peer acknowledges it at once, as out of order, after it already acknowledged the + // later packet + let at = acked(&mut pair, client_ch, &held).unwrap().unwrap(); + assert_eq!(at - released, 2 * LATENCY); +} + /// A checkpoint resolves only once every byte before it is acknowledged, by the ACK that fills /// the gap, however much of the stream that ACK covers. #[test] From a867b462ac228887ba2c56b04c81f3214cee596f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 19:53:23 -0700 Subject: [PATCH 05/19] fix(quic): fail checkpoints taken on a stopped or reset stream Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/streams/mod.rs | 13 ++++-- rs/moq-quic/src/tests/acked.rs | 48 +++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index b5f22fc264b..f7bf804dd88 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -341,13 +341,19 @@ impl<'a> SendStream<'a> { /// - when applied to a receive stream pub fn checkpoint(&mut self) -> Checkpoint { let max_send_data = self.state.max_send_data(self.id); + let closed = || Arc::new(AckSlot::from(Err(ClosedStream::default()))); let (offset, acked) = match self.state.send.get_mut(&self.id) { Some(stream) => { let stream = stream.get_or_insert_with(|| Send::new(max_send_data)); - (stream.offset(), stream.pending.watch()) + // A stopped or reset stream fails its checkpoints, so a new one does too + let acked = match stream.stop_reason.is_some() || stream.is_reset() { + true => closed(), + false => stream.pending.watch(), + }; + (stream.offset(), acked) } // Already closed, where the reset fails anyway - None => (0, Arc::new(AckSlot::from(Err(ClosedStream::default())))), + None => (0, closed()), }; Checkpoint { id: self.id, @@ -494,7 +500,8 @@ impl Checkpoint { /// /// Fails with [`ClosedStream`] once the peer will never acknowledge it, or no longer reads /// it: the stream was reset before the checkpoint's data was acknowledged, the peer sent - /// STOP_SENDING, rejected 0-RTT discarded the stream, or the connection closed. + /// STOP_SENDING, rejected 0-RTT discarded the stream, or the connection closed. A checkpoint + /// taken once the stream was reset or stopped fails at once. /// /// # Panics /// If the checkpoint came from another connection. diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index 9e21b4c1c90..0993dcfc40f 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -230,6 +230,54 @@ fn stop_sending_fails() { assert!(reported(&mut pair, client_ch, s)); } +/// A checkpoint taken after the peer stopped reading fails at once, though data in flight is +/// still acknowledged. +#[test] +fn checkpoint_after_stop_sending_fails() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + pair.client_send(client_ch, s).write(b"in flight").unwrap(); + pair.server_recv(server_ch, s).stop(VarInt(7)).unwrap(); + pair.drive(); + assert_eq!( + pair.client_send(client_ch, s).stopped(), + Ok(Some(VarInt(7))) + ); + + let checkpoint = pair.client_send(client_ch, s).checkpoint(); + pair.drive(); + assert_eq!( + acked(&mut pair, client_ch, &checkpoint), + Err(ClosedStream::default()) + ); +} + +/// A checkpoint taken after a reset fails at once, though the reset still delivers its prefix. +#[test] +fn checkpoint_after_reset_fails() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + pair.client_send(client_ch, s).write(b"prefix").unwrap(); + let reset = pair.client_send(client_ch, s).checkpoint(); + lose_client_packets(&mut pair); + reset + .reset(pair.client_conn_mut(client_ch), VarInt(42)) + .unwrap(); + + let checkpoint = pair.client_send(client_ch, s).checkpoint(); + pair.drive(); + assert_eq!( + acked(&mut pair, client_ch, &checkpoint), + Err(ClosedStream::default()) + ); +} + /// Nothing is acknowledged on a closed connection. #[test] fn close_fails() { From 3da616919dc27163bf43f7041b9cc8e607f12140 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 19:59:58 -0700 Subject: [PATCH 06/19] fix(quic): prune dropped checkpoints when taking another Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/send_buffer.rs | 40 +++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/rs/moq-quic/src/connection/send_buffer.rs b/rs/moq-quic/src/connection/send_buffer.rs index 5ec3a2d520f..1b9e0973c91 100644 --- a/rs/moq-quic/src/connection/send_buffer.rs +++ b/rs/moq-quic/src/connection/send_buffer.rs @@ -299,8 +299,8 @@ impl SendBuffer { /// Checkpoints waiting for the acknowledged prefix to reach their offset, in offset order /// /// A dropped [`Checkpoint`](super::Checkpoint) leaves its entry behind until the prefix reaches -/// it, so it lives no longer than the data it waits on. Dropping the stream fails the rest, so no -/// waiter hangs on a stream that is gone. +/// it or the next checkpoint is taken behind it, so dead entries only outnumber the live ones by +/// one. Dropping the stream fails the rest, so no waiter hangs on a stream that is gone. #[derive(Default, Debug)] struct Watches { pending: VecDeque<(u64, Weak)>, @@ -312,6 +312,14 @@ impl Watches { fn push(&mut self, offset: u64, slot: &Arc) { // Checkpoints are taken at the write offset, which only grows debug_assert!(self.pending.back().is_none_or(|(last, _)| *last <= offset)); + // Each entry is popped at most once, so this stays amortized O(1) + while self + .pending + .back() + .is_some_and(|(_, slot)| slot.strong_count() == 0) + { + self.pending.pop_back(); + } self.pending.push_back((offset, Arc::downgrade(slot))); } @@ -646,6 +654,34 @@ mod tests { assert!(buf.take_resolved()); } + /// Checkpoints taken and dropped on a stream whose prefix never moves do not pile up. + #[test] + fn dropped_watches_are_pruned() { + let mut buf = SendBuffer::new(); + buf.write(Bytes::from_static(b"Hello")); + let held = buf.watch(); + for _ in 0..1000 { + drop(buf.watch()); + } + assert!( + buf.watches.pending.len() <= 2, + "{}", + buf.watches.pending.len() + ); + + // Nothing was ever written or acknowledged on this one + let mut empty = SendBuffer::new(); + for _ in 0..1000 { + drop(empty.watch()); + } + assert!(empty.watches.pending.len() <= 1); + + assert_eq!(buf.poll_transmit(16), (0..5, true)); + buf.ack(0..5, Instant::now()); + assert!(held.get().is_some()); + assert!(buf.watches.pending.is_empty()); + } + #[test] fn dropped_watch_is_not_reported() { let mut buf = SendBuffer::new(); From 98e6caef19efe8d45491041be02af1b4f8b2c24a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 20:08:09 -0700 Subject: [PATCH 07/19] fix(quic): subtract the ACK delay only for the largest packet Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/mod.rs | 15 +++++----- rs/moq-quic/src/connection/streams/mod.rs | 6 ++-- rs/moq-quic/src/tests/acked.rs | 34 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index 96e2f2624c2..adc903be4c9 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1609,17 +1609,16 @@ impl Connection { Duration::from_micros(ack.delay << self.peer_params.ack_delay_exponent.0), ) }; - // When the peer received the data, plus the ACK's trip back. The delay is for the - // largest packet acknowledged, so an ACK that repeats it only newly covers packets that - // arrived since, at an unknown point within that delay. - let acked_at = match new_largest { - true => now.checked_sub(ack_delay).unwrap_or(now), - false => now, - }; - let mut ack_eliciting_acked = false; for packet in newly_acked.elts() { if let Some(info) = self.spaces[space].take(packet) { + // When the peer received the data, plus the ACK's trip back. The delay only + // describes the largest packet; any other may have arrived at any point within + // it, such as one that filled a gap after the largest. + let acked_at = match packet == ack.largest { + true => now.checked_sub(ack_delay).unwrap_or(now), + false => now, + }; if let Some(acked) = info.largest_acked { // Assume ACKs for all packets below the largest acknowledged in `packet` have // been received. This can cause the peer to spuriously retransmit if some of diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index f7bf804dd88..6515ccc7190 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -488,9 +488,9 @@ impl Checkpoint { /// The instant is when the ACK that brought the stream's contiguous acknowledged prefix to the /// checkpoint was received, less the ACK delay the peer reported, capped at its /// `max_ack_delay`. That leaves out the peer's delayed-ACK timer, but still includes the ACK's - /// one-way trip back. The delay is only subtracted from an ACK that raises the largest - /// acknowledged packet, since it describes that packet, not reordered ones the ACK repeats it - /// for. The instant is never earlier than the data's packet was sent. A checkpoint taken once + /// one-way trip back. The delay describes only the largest packet the ACK names, so data in + /// any other packet, which may have arrived later, gets the ACK's receive time with nothing + /// subtracted. The instant is never earlier than the data's packet was sent. A checkpoint taken once /// everything written was already acknowledged gets the ACK that got it there, and one taken /// before anything was written gets the stream's next ACK, or fails if a reset comes first. /// diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index 0993dcfc40f..d6079bb2809 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -110,6 +110,40 @@ fn reordered_ack_keeps_its_delay() { assert_eq!(at - released, 2 * LATENCY); } +/// A gap filled after a later packet keeps its receive instant, even when the first ACK of the +/// later packet was lost and the gap's ACK is the first to raise the largest. +#[test] +fn gap_acked_with_largest_keeps_its_delay() { + const HOLD: Duration = Duration::from_millis(1); + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + // The first packet is held back, so it arrives after the second + pair.client_send(client_ch, s).write(b"held").unwrap(); + let held = pair.client_send(client_ch, s).checkpoint(); + pair.client.drive(pair.time, pair.server.addr); + pair.client.delay_outbound(); + pair.client_send(client_ch, s).write(b"ahead").unwrap(); + pair.drive_client(); + + // The peer's ACK of the second packet alone is lost + pair.time += LATENCY; + pair.server.drive(pair.time, pair.client.addr); + assert!(!pair.server.outbound.is_empty()); + pair.server.outbound.clear(); + + pair.time += HOLD; + let released = pair.time; + pair.client.finish_delay(); + pair.drive_client(); + pair.drive(); + + let at = acked(&mut pair, client_ch, &held).unwrap().unwrap(); + assert_eq!(at - released, 2 * LATENCY); +} + /// A checkpoint resolves only once every byte before it is acknowledged, by the ACK that fills /// the gap, however much of the stream that ACK covers. #[test] From 6fc225960540abd40bffd718c0339728a6e084bc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 16 Oct 2026 20:20:46 -0700 Subject: [PATCH 08/19] fix(quic): cap the ACK delay after confirming ACK_FREQUENCY An ACK that confirms an ACK_FREQUENCY frame raising max_ack_delay was capped at the old value, so the checkpoint instant and the RTT sample looked late. Read the cap once the acknowledged packets are processed. Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/mod.rs | 40 +++++++++++++++----------- rs/moq-quic/src/tests/acked.rs | 48 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index adc903be4c9..ddea2987987 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1601,24 +1601,9 @@ impl Connection { return Ok(()); } - let ack_delay = if space != SpaceId::Data { - Duration::from_micros(0) - } else { - cmp::min( - self.ack_frequency.peer_max_ack_delay, - Duration::from_micros(ack.delay << self.peer_params.ack_delay_exponent.0), - ) - }; let mut ack_eliciting_acked = false; for packet in newly_acked.elts() { if let Some(info) = self.spaces[space].take(packet) { - // When the peer received the data, plus the ACK's trip back. The delay only - // describes the largest packet; any other may have arrived at any point within - // it, such as one that filled a gap after the largest. - let acked_at = match packet == ack.largest { - true => now.checked_sub(ack_delay).unwrap_or(now), - false => now, - }; if let Some(acked) = info.largest_acked { // Assume ACKs for all packets below the largest acknowledged in `packet` have // been received. This can cause the peer to spuriously retransmit if some of @@ -1640,6 +1625,16 @@ impl Connection { // Notify ack frequency that a packet was acked, because it might contain an ACK_FREQUENCY frame self.ack_frequency.on_acked(packet); + // When the peer received the data, plus the ACK's trip back. The delay only + // describes the largest packet; any other may have arrived at any point within + // it, such as one that filled a gap after the largest. The largest comes last, + // so the cap reflects any ACK_FREQUENCY this ACK confirmed. + let acked_at = match packet == ack.largest { + true => now + .checked_sub(self.peer_ack_delay(space, ack.delay)) + .unwrap_or(now), + false => now, + }; self.on_packet_acked(now, acked_at, info, packet, space); } } @@ -1654,7 +1649,9 @@ impl Connection { if new_largest && ack_eliciting_acked { let rtt = now.saturating_duration_since(self.spaces[space].largest_acked_packet_sent); - self.path.rtt.update(ack_delay, rtt); + self.path + .rtt + .update(self.peer_ack_delay(space, ack.delay), rtt); if self.path.first_packet_after_rtt_sample.is_none() { self.path.first_packet_after_rtt_sample = Some((space, self.spaces[space].next_packet_number)); @@ -1700,6 +1697,17 @@ impl Connection { Ok(()) } + /// The peer's reported ACK delay, capped at the max_ack_delay it last agreed to. + fn peer_ack_delay(&self, space: SpaceId, delay: u64) -> Duration { + match space { + SpaceId::Data => cmp::min( + self.ack_frequency.peer_max_ack_delay, + Duration::from_micros(delay << self.peer_params.ack_delay_exponent.0), + ), + _ => Duration::ZERO, + } + } + fn detect_spurious_loss(&mut self, ack: &frame::Ack, space: SpaceId) -> bool { let lost_packets = &mut self.spaces[space].lost_packets; diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index d6079bb2809..ba1c7e62ee0 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -144,6 +144,54 @@ fn gap_acked_with_largest_keeps_its_delay() { assert_eq!(at - released, 2 * LATENCY); } +/// An ACK that confirms a raised max_ack_delay subtracts its delay up to the new cap, which the +/// peer already used to hold that ACK. +#[test] +fn ack_delay_cap_follows_ack_frequency() { + let _guard = subscribe(); + let (mut pair, client_ch, server_ch) = setup_ack_frequency_test(Duration::from_millis(200)); + let s = open(&mut pair, client_ch, server_ch); + let sent_ack_frequency = |pair: &mut Pair| { + pair.client_conn_mut(client_ch) + .stats() + .frame_tx + .ack_frequency + }; + let before = sent_ack_frequency(&mut pair); + + // The RTT jumps, so the client asks for a longer max_ack_delay than the default 25 ms + pair.latency *= 100; + pair.client_conn_mut(client_ch).ping(); + while sent_ack_frequency(&mut pair) == before { + assert!(pair.step(), "never sent ACK_FREQUENCY"); + } + + // The peer holds the ACK covering both packets for the new max_ack_delay + pair.client_send(client_ch, s).write(b"payload").unwrap(); + let checkpoint = pair.client_send(client_ch, s).checkpoint(); + let sent = pair.time; + let received = loop { + let busy = pair.step(); + if acked(&mut pair, client_ch, &checkpoint) != Ok(None) { + break pair.time; + } + assert!(busy, "never acknowledged"); + }; + let at = acked(&mut pair, client_ch, &checkpoint).unwrap().unwrap(); + + assert!( + received - at > Duration::from_millis(25), + "{:?}", + received - at + ); + // Pacing may hold the packet briefly after the write + let trip = at - sent; + assert!( + trip >= 2 * pair.latency && trip < 2 * pair.latency + Duration::from_millis(1), + "{trip:?}" + ); +} + /// A checkpoint resolves only once every byte before it is acknowledged, by the ACK that fills /// the gap, however much of the stream that ACK covers. #[test] From 02826ded728cbe0cd84451cf56134b38e6953dad Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 18:25:28 -0700 Subject: [PATCH 09/19] fix(quic): resolve a checkpoint at the latest ACK instant below it One ACK assigns the largest packet an earlier instant (less its ACK delay) than the packets before it, and a range can wait behind a gap. Keep each acknowledged range's instant and resolve each checkpoint at the latest instant among the ranges below it, so the prefix never completes before its last piece arrived. Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/send_buffer.rs | 72 +++++++++++++++++++---- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/rs/moq-quic/src/connection/send_buffer.rs b/rs/moq-quic/src/connection/send_buffer.rs index 1b9e0973c91..3dfd517f597 100644 --- a/rs/moq-quic/src/connection/send_buffer.rs +++ b/rs/moq-quic/src/connection/send_buffer.rs @@ -1,5 +1,5 @@ use std::{ - collections::VecDeque, + collections::{BTreeMap, VecDeque}, mem, ops::Range, sync::{Arc, OnceLock, Weak}, @@ -32,11 +32,13 @@ pub(super) struct SendBuffer { /// offset in `unacked` // TODO: Recover storage from these by compacting (#700) acks: RangeSet, + /// When each range in `acks` was acknowledged, keyed by its start; the first ACK of a start wins + ack_instants: BTreeMap, /// Previously transmitted ranges deemed lost retransmits: RangeSet, /// Checkpoints waiting for the acknowledged prefix to reach them watches: Watches, - /// When the ACK that last advanced the acknowledged prefix was received, less its ACK delay + /// When the acknowledged prefix became complete: the latest instant among its ranges acked_at: Option, } @@ -61,15 +63,24 @@ impl SendBuffer { let base_offset = self.offset - self.unacked_len as u64; range.start = base_offset.max(range.start); range.end = base_offset.max(range.end).min(self.offset); - if !range.is_empty() { - self.acks.insert(range); - self.advance(at); + if range.is_empty() { + // Nothing new, but a FIN-only ACK still reaches a checkpoint at the prefix + let at = self.acked_at.map_or(at, |acked_at| acked_at.max(at)); + self.watches.ack(self.acked(), at); + return; } - self.watches.ack(self.acked(), at); + self.acks.insert(range.clone()); + self.ack_instants.entry(range.start).or_insert(at); + self.advance(); } - /// Discard the acknowledged ranges that extend the acknowledged prefix - fn advance(&mut self, at: Instant) { + /// Discard the acknowledged ranges that extend the acknowledged prefix, resolving the + /// checkpoints they reach + /// + /// The prefix up to an offset is complete only once every range below it is acknowledged, + /// so a checkpoint takes the latest instant among them, even when one ACK covers ranges + /// with different instants or a range waited behind a gap. + fn advance(&mut self) { while self.acks.min() == Some(self.offset - self.unacked_len as u64) { let prefix = self.acks.pop_min().unwrap(); let mut to_advance = (prefix.end - prefix.start) as usize; @@ -95,8 +106,21 @@ impl SendBuffer { to_advance = 0; } } - self.acked_at = Some(at); } + + let acked = self.acked(); + let held = self.ack_instants.split_off(&acked); + let mut at = self.acked_at; + for (start, instant) in mem::replace(&mut self.ack_instants, held) { + if let Some(at) = at { + self.watches.ack(start, at); + } + at = Some(at.map_or(instant, |at| at.max(instant))); + } + if let Some(at) = at { + self.watches.ack(acked, at); + } + self.acked_at = at; } /// The end of the contiguous acknowledged prefix @@ -108,7 +132,7 @@ impl SendBuffer { pub(super) fn watch(&mut self) -> Arc { let slot = Arc::new(AckSlot::new()); match self.acked_at { - // The ACK that last advanced the prefix is the one that reached the end + // Everything written is already acknowledged Some(at) if self.acked() == self.offset => { let _ = slot.set(Ok(at)); } @@ -253,6 +277,7 @@ impl SendBuffer { self.offset = end; self.unsent = self.unsent.min(end); self.acks.remove(end..u64::MAX); + self.ack_instants.split_off(&end); self.retransmits.remove(end..u64::MAX); } @@ -625,6 +650,33 @@ mod tests { assert_eq!(buf.watch().get(), Some(&Ok(late))); } + #[test] + fn watch_takes_the_latest_instant_below_it() { + let mut buf = SendBuffer::new(); + buf.write(Bytes::from_static(b"Hello, ")); + let first = buf.watch(); + buf.write(Bytes::from_static(b"world!")); + let second = buf.watch(); + assert_eq!(buf.poll_transmit(16), (0..8, true)); + assert_eq!(buf.poll_transmit(16), (8..13, true)); + + // One ACK, in packet order: the held range came in a packet before the largest, so it + // keeps the receive time, while the largest fills the gap less its ACK delay + let early = Instant::now(); + let late = early + Duration::from_millis(1); + buf.ack(8..13, late); + buf.ack(0..8, early); + assert_eq!(first.get(), Some(&Ok(early))); + assert_eq!(second.get(), Some(&Ok(late))); + + // A later range acknowledged at an earlier instant can't complete the prefix before it + buf.write(Bytes::from_static(b"!")); + let third = buf.watch(); + assert_eq!(buf.poll_transmit(16), (13..14, true)); + buf.ack(13..14, early); + assert_eq!(third.get(), Some(&Ok(late))); + } + #[test] fn watch_fails_past_truncation() { let mut buf = SendBuffer::new(); From b54745d2efb8cd1c225c735b088a655532d6fe8e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 18:28:44 -0700 Subject: [PATCH 10/19] fix(quic): sweep checkpoints replaced before they drop `last = stream.checkpoint()` drops the old checkpoint after pushing the new one, so pruning the back never saw it and dead entries piled up while nothing was acknowledged. Sweep the queue whenever it doubles past the live entries the last sweep kept. Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/send_buffer.rs | 34 +++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/rs/moq-quic/src/connection/send_buffer.rs b/rs/moq-quic/src/connection/send_buffer.rs index 3dfd517f597..38f37c619a4 100644 --- a/rs/moq-quic/src/connection/send_buffer.rs +++ b/rs/moq-quic/src/connection/send_buffer.rs @@ -324,11 +324,14 @@ impl SendBuffer { /// Checkpoints waiting for the acknowledged prefix to reach their offset, in offset order /// /// A dropped [`Checkpoint`](super::Checkpoint) leaves its entry behind until the prefix reaches -/// it or the next checkpoint is taken behind it, so dead entries only outnumber the live ones by -/// one. Dropping the stream fails the rest, so no waiter hangs on a stream that is gone. +/// it, the next checkpoint is taken behind it, or the queue doubles past the live entries the +/// last sweep kept, so it holds at most about twice the live checkpoints. Dropping the stream +/// fails the rest, so no waiter hangs on a stream that is gone. #[derive(Default, Debug)] struct Watches { pending: VecDeque<(u64, Weak)>, + /// The length at which `push` next sweeps out dead entries + sweep_at: usize, /// Whether a checkpoint somebody still holds resolved since the stream last reported it resolved: bool, } @@ -345,6 +348,12 @@ impl Watches { { self.pending.pop_back(); } + // A checkpoint replaced by the next one dies after it is pushed, behind a live entry + if self.pending.len() >= self.sweep_at { + self.pending.retain(|(_, slot)| slot.strong_count() > 0); + // Doubling keeps each sweep's cost covered by the pushes since the last one + self.sweep_at = 2 * self.pending.len() + 1; + } self.pending.push_back((offset, Arc::downgrade(slot))); } @@ -734,6 +743,27 @@ mod tests { assert!(buf.watches.pending.is_empty()); } + #[test] + fn replaced_watches_are_pruned() { + // `last = stream.checkpoint()` takes the new checkpoint before dropping the old one, so + // each dead entry sits behind a live one while nothing is acknowledged + let mut buf = SendBuffer::new(); + let mut last = buf.watch(); + for _ in 0..1000 { + buf.write(Bytes::from_static(b"x")); + last = buf.watch(); + } + assert!( + buf.watches.pending.len() <= 3, + "{}", + buf.watches.pending.len() + ); + + assert_eq!(buf.poll_transmit(2000), (0..1000, true)); + buf.ack(0..1000, Instant::now()); + assert!(last.get().is_some()); + } + #[test] fn dropped_watch_is_not_reported() { let mut buf = SendBuffer::new(); From 663cea616c3b47da51b3ba94973524261bc83b47 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 18:32:51 -0700 Subject: [PATCH 11/19] fix(quic): time only the bytes an ACK newly covers Keying an ACK's instant by its range start kept a short retransmission's instant for a longer frame from the same offset, so a checkpoint past the extension resolved before its last bytes were acknowledged. Record the instant for each newly covered sub-range instead. Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/send_buffer.rs | 33 ++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/rs/moq-quic/src/connection/send_buffer.rs b/rs/moq-quic/src/connection/send_buffer.rs index 38f37c619a4..4aed7076daa 100644 --- a/rs/moq-quic/src/connection/send_buffer.rs +++ b/rs/moq-quic/src/connection/send_buffer.rs @@ -32,7 +32,8 @@ pub(super) struct SendBuffer { /// offset in `unacked` // TODO: Recover storage from these by compacting (#700) acks: RangeSet, - /// When each range in `acks` was acknowledged, keyed by its start; the first ACK of a start wins + /// When the bytes from each key up to the next key (or the end of their range in `acks`) were + /// first acknowledged ack_instants: BTreeMap, /// Previously transmitted ranges deemed lost retransmits: RangeSet, @@ -69,8 +70,17 @@ impl SendBuffer { self.watches.ack(self.acked(), at); return; } - self.acks.insert(range.clone()); - self.ack_instants.entry(range.start).or_insert(at); + // Only the bytes this ACK newly covers take its instant; `replace` yields the rest in order + let mut next = range.start; + for overlap in self.acks.replace(range.clone()) { + if overlap.start > next { + self.ack_instants.insert(next, at); + } + next = overlap.end; + } + if next < range.end { + self.ack_instants.insert(next, at); + } self.advance(); } @@ -686,6 +696,23 @@ mod tests { assert_eq!(third.get(), Some(&Ok(late))); } + #[test] + fn watch_takes_the_instant_of_an_extension() { + let mut buf = SendBuffer::new(); + buf.write(Bytes::from_static(b"Hello, world!")); + let end = buf.watch(); + assert_eq!(buf.poll_transmit(16), (0..8, true)); + assert_eq!(buf.poll_transmit(16), (8..13, true)); + + // A short retransmission is acknowledged before a longer frame from the same offset + let early = Instant::now(); + let late = early + Duration::from_millis(1); + buf.ack(8..10, early); + buf.ack(8..13, late); + buf.ack(0..8, early); + assert_eq!(end.get(), Some(&Ok(late))); + } + #[test] fn watch_fails_past_truncation() { let mut buf = SendBuffer::new(); From 3a271247669a3a08e92e954615a58d46f89947c2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 18:35:37 -0700 Subject: [PATCH 12/19] test(quic): a redundant ACK leaves a checkpoint's instant alone Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/src/connection/send_buffer.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rs/moq-quic/src/connection/send_buffer.rs b/rs/moq-quic/src/connection/send_buffer.rs index 4aed7076daa..7f4e728a738 100644 --- a/rs/moq-quic/src/connection/send_buffer.rs +++ b/rs/moq-quic/src/connection/send_buffer.rs @@ -713,6 +713,23 @@ mod tests { assert_eq!(end.get(), Some(&Ok(late))); } + #[test] + fn watch_ignores_a_redundant_ack() { + let mut buf = SendBuffer::new(); + buf.write(Bytes::from_static(b"Hello, world!")); + let end = buf.watch(); + assert_eq!(buf.poll_transmit(16), (0..8, true)); + assert_eq!(buf.poll_transmit(16), (8..13, true)); + + // Repeating a held range adds no bytes, so its later instant doesn't count + let early = Instant::now(); + let late = early + Duration::from_millis(1); + buf.ack(8..13, early); + buf.ack(8..13, late); + buf.ack(0..8, early); + assert_eq!(end.get(), Some(&Ok(early))); + } + #[test] fn watch_fails_past_truncation() { let mut buf = SendBuffer::new(); From 00a43a581c0386e37f2113a956743e7cfee22317 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 18:40:23 -0700 Subject: [PATCH 13/19] docs(quic): list ACK checkpoints as a carried change Co-Authored-By: Claude Opus 5.5 --- rs/moq-quic/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index de35c22ad9c..7d45eb0f2a6 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -70,6 +70,7 @@ Changes on top of the upstream commit, besides the renames: - **Send groups** (`streams/schedule.rs`, `Connection::send_groups`): streams outside every group go first, in quinn's order; groups in one scheduling domain are served strictly by priority, with equal priorities sharing bytes by deficit round robin; domains share bytes the same way. A stream's priority is an `i64` ordering it only within its group. This replaces quinn's single pending-stream heap, so a cherry-pick touching `PendingStreamsQueue` has to be redone against the schedule. moq-tokio's `quic::driver` and moq-uring's driver wrap a group in a shared `SendGroup` handle that closes on drop. - **Scheduled datagrams** (`Datagrams::send`): a datagram is queued in a send group (or ungrouped) at an `i64` order and scheduled with the group's streams, winning a tie with a stream of equal order and spending the group's byte credit, instead of quinn's datagram-first FIFO in `populate_packet`. Sending never blocks: `datagram_send_buffer_size` is shared, and an overflow evicts the oldest datagram of the lowest-priority group in the domain holding the most datagram bytes. `SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone, and a cherry-pick touching the `// DATAGRAM` loop has to be redone against `StreamsState::write_frames`. - **`RESET_STREAM_AT`** ([draft-ietf-quic-reliable-stream-reset](https://datatracker.ietf.org/doc/draft-ietf-quic-reliable-stream-reset/)): the `reset_stream_at` transport parameter is always advertised, `Checkpoint::reset` (from `SendStream::checkpoint`) keeps the data written before the checkpoint reliable, holding a plain `RESET_STREAM` until that data is acknowledged when the peer lacks the extension, and a received reset surfaces only once the reliable prefix has been read. Stopping a stream after it was reset no longer credits its unread data twice. +- **ACK checkpoints** (`Checkpoint::acked`, `StreamEvent::Acked`): a checkpoint resolves when the peer has acknowledged every byte before it, at the latest instant among the ACKs that covered them; only the largest packet of an ACK subtracts the peer's ACK delay. `SendBuffer` keeps each newly acknowledged range's instant until the prefix reaches it, so a cherry-pick touching `SendBuffer::ack` or the ACK loop in `Connection::on_ack_received` has to be redone against it. Not offered upstream: quinn has no API for when sent data was acknowledged. - **Fuzzing** runs through the hidden `fuzz` module behind a `fuzz` feature, with one-line shims in [`fuzz/`](fuzz/README.md): upstream's `packet`, `params`, `streamid`, and `streams` targets, plus `frame` and a connection op sequence. Upstream's `cfg(fuzzing)` `fuzzing` module and `arbitrary` feature are gone, so its fuzz patches port by hand. - **Draining endpoint event**: `EndpointEvent::is_draining` fires once per connection when it stops being active, before the drained event, so the driver's `Endpoint::wait_all_draining` can end a shutdown without sitting out every closing period. - **Late stream limits**: peer transport parameters that raise a stream limit after an open was refused emit `StreamEvent::Available`, as `MAX_STREAMS` does. A server accepted before the rest of a multi-packet ClientHello (a post-quantum key share is enough) otherwise leaves a 0.5-RTT `open_uni` parked until the idle timeout. Not offered upstream yet. From 77cb57d2581875adfd7d14703ff8fe7723404e92 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 17 Oct 2026 20:45:10 -0700 Subject: [PATCH 14/19] refactor(quic): make a checkpoint plain data, registering a waiter only when polled `Checkpoint` is back to a stream id and offset. `Checkpoint::acked(&mut Connection)` returns whether the stream's contiguous acknowledged prefix reached it, and a `false` stores the lowest polled offset on the stream, so the ACK that crosses it reports `StreamEvent::Acked { id, offset }`. This replaces the per-checkpoint `Arc` slots, the `Weak` watch queue and its sweeping, and the per-range ACK instants. The ACK-delay-corrected instant is dropped: the caller records the time when woken. `SendStream::checkpoint` takes `&self` again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013bQzzUiJPvi9kFCtMqbtqC --- quest/m2/quic-ack-hook.md | 25 +- quest/m2/starvation-frames.md | 13 +- rs/moq-quic/MIGRATE.md | 17 +- rs/moq-quic/README.md | 2 +- rs/moq-quic/benches/streams.rs | 57 +-- rs/moq-quic/src/connection/mod.rs | 52 +-- rs/moq-quic/src/connection/send_buffer.rs | 392 ++---------------- rs/moq-quic/src/connection/streams/mod.rs | 83 ++-- .../src/connection/streams/saturated.rs | 41 +- rs/moq-quic/src/connection/streams/send.rs | 37 +- rs/moq-quic/src/connection/streams/state.rs | 135 +++--- rs/moq-quic/src/fuzz/connection.rs | 14 + rs/moq-quic/src/tests/acked.rs | 316 ++++---------- 13 files changed, 296 insertions(+), 888 deletions(-) diff --git a/quest/m2/quic-ack-hook.md b/quest/m2/quic-ack-hook.md index af97bb49196..4d60107d9c5 100644 --- a/quest/m2/quic-ack-hook.md +++ b/quest/m2/quic-ack-hook.md @@ -22,11 +22,14 @@ wakeups a latency sample needs. Add one method to `SendStream`, in the trait's poll style: ```rust -fn poll_acked(&mut self, cx: &mut Context, offset: u64) -> Poll, Self::Error>>; +fn poll_acked(&mut self, cx: &mut Context, offset: u64) -> Poll, Self::Error>>; ``` It is ready with `Some` once every byte below `offset` has been acknowledged. -`Acked` carries the ACK-delay-corrected receive instant from `moq-quic`. It resolves +It carries no instant: the caller records one when woken, which includes the +peer's delayed-ACK timer (up to `max_ack_delay`) as well as the return trip. +Decided 2026-10-17 on #5817, which dropped `moq-quic`'s ACK-delay-corrected +instant until a consumer shows it matters. It resolves with an error once the stream is reset by either side or the session closes, so a waiter never hangs on bytes the peer will never acknowledge. The default implementation is ready with `Ok(None)` immediately: `None` means the backend @@ -36,13 +39,17 @@ unsupported has to live in the return type rather than the error, and a consumer must treat `None` as unknown, never as delivered. Implement it in the moq-tokio and moq-uring adapters over `moq-quic`'s -`Checkpoint::acked`, which returns the corrected instant once everything -before the checkpoint is acknowledged and fails once it never will be; -`StreamEvent::Acked` wakes its waiters, which moq-uring's `sweep_stream` -currently ignores. A checkpoint is taken at the current write offset, so the -adapter checkpoints each offset as it is written rather than naming an -arbitrary one later; settle whether the trait method takes an offset or -returns a handle from a `checkpoint()` like the reset one. Leave the browser transport on the default; the browser's `WebTransportSendStream.getStats()` is +`Checkpoint::acked`, a plain stream id and offset that returns whether +everything before it is acknowledged and fails once it never will be. A +`false` asks `moq-quic` to report `StreamEvent::Acked { id, offset }` when +the stream's acknowledged prefix reaches it, so the adapter wakes waiters at +or below that offset; moq-uring's `sweep_stream` currently ignores the event. +`acked` fails once the stream is freed, after `StreamEvent::Finished`, which +means everything was acknowledged, so the adapter answers from that. A +checkpoint is taken at the current write offset, so the adapter checkpoints +each offset as it is written rather than naming an arbitrary one later; +settle whether the trait method takes an offset or returns a handle from a +`checkpoint()` like the reset one. Leave the browser transport on the default; the browser's `WebTransportSendStream.getStats()` is unimplemented in shipping Chrome and its `bytesAcknowledged` is at risk in the W3C draft. qmux over a reliable transport may treat serialization as acknowledgment only if the qmux quest decides that is honest; until then it diff --git a/quest/m2/starvation-frames.md b/quest/m2/starvation-frames.md index 4923e8c7794..4760ae8f2ab 100644 --- a/quest/m2/starvation-frames.md +++ b/quest/m2/starvation-frames.md @@ -31,10 +31,11 @@ span from the newest acknowledged frame to the newest written one, which is the exact media the viewer lost. Add a second byte-weighted cumulative histogram of delivery delay: -`acked.received - written_at` per frame, where `received` is the -ACK-delay-corrected instant the hook returns. Its low edge approaches the -path RTT and its spread is the jitter. Document that the value still includes -the return one-way delay and that a backend without the hook leaves the +`acked_at - written_at` per frame, where `acked_at` is when the serve task is +woken by `poll_acked`. Its low edge approaches the path RTT and its spread is +the jitter. Document that the value still includes the return one-way delay +and the peer's delayed-ACK timer, since the hook reports no corrected instant +(decided 2026-10-17 on #5817), and that a backend without the hook leaves the histogram absent, never zero. Backends that return unsupported from `poll_acked` keep the group-granularity @@ -52,8 +53,8 @@ is Required. Tests: the frontier tracking frame ends under a peer that acknowledges in bursts, with interval samples landing one bucket lower than group-granularity tracking of the same run; a reset mid-group attributing only the -unacknowledged span to `dropped_duration`; delivery delay under an injected ACK delay staying inside one bucket of the -true RTT; and fallback to group-granularity frontier tracking when the hook is +unacknowledged span to `dropped_duration`; delivery delay staying inside one bucket of the true RTT plus the peer's +`max_ack_delay`; and fallback to group-granularity frontier tracking when the hook is unsupported. ## Required diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index f66419864c8..08d7494c4a2 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -4,27 +4,14 @@ How to update your code for each breaking change, newest first. ## Unreleased -### `SendStream::checkpoint` takes `&mut self` - -A checkpoint now registers to learn when the peer acknowledges the data before it, read with `Checkpoint::acked`. -Callers holding a shared borrow of the `SendStream` need a mutable one. - -```rust -// Before -let checkpoint = stream.checkpoint(); // stream: &SendStream - -// After -let checkpoint = stream.checkpoint(); // stream: &mut SendStream -``` - ### `StreamEvent::Acked` -`StreamEvent` gained `Acked { id }`, reported when a `Checkpoint` on that send stream was acknowledged or never will be. +`StreamEvent` gained `Acked { id, offset }`, reported when a send stream's acknowledged prefix reaches a `Checkpoint` that `Checkpoint::acked` was polled on. An exhaustive `match` needs an arm for it. ```rust // After -StreamEvent::Acked { id } => wake_checkpoint_waiters(id), +StreamEvent::Acked { id, offset } => wake_checkpoint_waiters(id, offset), ``` ### `Datagrams::send` takes a send group and order (#5770) diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index 7d45eb0f2a6..a9a60eb4484 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -70,7 +70,7 @@ Changes on top of the upstream commit, besides the renames: - **Send groups** (`streams/schedule.rs`, `Connection::send_groups`): streams outside every group go first, in quinn's order; groups in one scheduling domain are served strictly by priority, with equal priorities sharing bytes by deficit round robin; domains share bytes the same way. A stream's priority is an `i64` ordering it only within its group. This replaces quinn's single pending-stream heap, so a cherry-pick touching `PendingStreamsQueue` has to be redone against the schedule. moq-tokio's `quic::driver` and moq-uring's driver wrap a group in a shared `SendGroup` handle that closes on drop. - **Scheduled datagrams** (`Datagrams::send`): a datagram is queued in a send group (or ungrouped) at an `i64` order and scheduled with the group's streams, winning a tie with a stream of equal order and spending the group's byte credit, instead of quinn's datagram-first FIFO in `populate_packet`. Sending never blocks: `datagram_send_buffer_size` is shared, and an overflow evicts the oldest datagram of the lowest-priority group in the domain holding the most datagram bytes. `SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone, and a cherry-pick touching the `// DATAGRAM` loop has to be redone against `StreamsState::write_frames`. - **`RESET_STREAM_AT`** ([draft-ietf-quic-reliable-stream-reset](https://datatracker.ietf.org/doc/draft-ietf-quic-reliable-stream-reset/)): the `reset_stream_at` transport parameter is always advertised, `Checkpoint::reset` (from `SendStream::checkpoint`) keeps the data written before the checkpoint reliable, holding a plain `RESET_STREAM` until that data is acknowledged when the peer lacks the extension, and a received reset surfaces only once the reliable prefix has been read. Stopping a stream after it was reset no longer credits its unread data twice. -- **ACK checkpoints** (`Checkpoint::acked`, `StreamEvent::Acked`): a checkpoint resolves when the peer has acknowledged every byte before it, at the latest instant among the ACKs that covered them; only the largest packet of an ACK subtracts the peer's ACK delay. `SendBuffer` keeps each newly acknowledged range's instant until the prefix reaches it, so a cherry-pick touching `SendBuffer::ack` or the ACK loop in `Connection::on_ack_received` has to be redone against it. Not offered upstream: quinn has no API for when sent data was acknowledged. +- **ACK checkpoints** (`Checkpoint::acked`, `StreamEvent::Acked`): a checkpoint reports whether the peer acknowledged every byte before it, by comparing against the stream's contiguous acknowledged prefix. A `false` stores the lowest polled offset on the stream, and the ACK that advances the prefix past it reports `StreamEvent::Acked` with the new prefix, so a cherry-pick touching `StreamsState::received_ack_of` has to keep that report. Not offered upstream: quinn has no API for when sent data was acknowledged. - **Fuzzing** runs through the hidden `fuzz` module behind a `fuzz` feature, with one-line shims in [`fuzz/`](fuzz/README.md): upstream's `packet`, `params`, `streamid`, and `streams` targets, plus `frame` and a connection op sequence. Upstream's `cfg(fuzzing)` `fuzzing` module and `arbitrary` feature are gone, so its fuzz patches port by hand. - **Draining endpoint event**: `EndpointEvent::is_draining` fires once per connection when it stops being active, before the drained event, so the driver's `Endpoint::wait_all_draining` can end a shutdown without sitting out every closing period. - **Late stream limits**: peer transport parameters that raise a stream limit after an open was refused emit `StreamEvent::Available`, as `MAX_STREAMS` does. A server accepted before the rest of a multi-packet ClientHello (a post-quantum key share is enough) otherwise leaves a 0.5-RTT `open_uni` parked until the idle timeout. Not offered upstream yet. diff --git a/rs/moq-quic/benches/streams.rs b/rs/moq-quic/benches/streams.rs index a274a5d3802..35fc49536f8 100644 --- a/rs/moq-quic/benches/streams.rs +++ b/rs/moq-quic/benches/streams.rs @@ -79,64 +79,9 @@ fn bench_write_size(c: &mut Criterion) { group.finish(); } -/// Checkpoints per stream against stream count, each awaited until acknowledged, on 16 writes of -/// 1 KiB per stream. -fn bench_checkpoint(c: &mut Criterion) { - const WRITES: usize = 16; - const SIZE: usize = 1024; - let mut group = c.benchmark_group("send_checkpoint"); - for streams in [1, 64, 256] { - for checkpoints in [0, 1, WRITES] { - let mut pair = None; - group.throughput(Throughput::Bytes((streams * WRITES * SIZE) as u64)); - group.bench_function( - BenchmarkId::new( - format!("streams_{streams}"), - format!("checkpoints_{checkpoints}"), - ), - |b| { - let pair = pair.get_or_insert_with(|| Pair::new(Setup::default())); - b.iter(|| checkpoint(pair, streams, checkpoints, WRITES, SIZE)) - }, - ); - } - } - group.finish(); -} - -/// Like [`send`], taking `checkpoints` evenly spaced checkpoints on each stream and checking -/// each was acknowledged. -fn checkpoint(pair: &mut Pair, streams: usize, checkpoints: usize, writes: usize, size: usize) { - let data = payload(size); - let every = writes / checkpoints.max(1); - let target = pair.server.received.finished + streams as u64; - let mut taken = Vec::with_capacity(streams * checkpoints); - let conn = pair.client(); - for _ in 0..streams { - let id = conn.streams().open(Dir::Uni).expect("stream limit"); - let mut stream = conn.send_stream(id); - for write in 1..=writes { - assert_eq!(stream.write(&data).unwrap(), size, "write was cut short"); - if checkpoints > 0 && write % every == 0 { - taken.push(stream.checkpoint()); - } - } - stream.finish().unwrap(); - } - pair.drive_until(|pair| pair.server.received.finished >= target); - pair.settle(); - let conn = pair.client(); - for checkpoint in &taken { - assert!( - checkpoint.acked(conn).unwrap().is_some(), - "never acknowledged" - ); - } -} - criterion_group! { name = benches; config = common::config(); - targets = bench_priority, bench_write_size, bench_checkpoint + targets = bench_priority, bench_write_size } criterion_main!(benches); diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index d2b3d7f3559..8b06a3e3c45 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1656,17 +1656,7 @@ impl Connection { // Notify ack frequency that a packet was acked, because it might contain an ACK_FREQUENCY frame self.ack_frequency.on_acked(packet); - // When the peer received the data, plus the ACK's trip back. The delay only - // describes the largest packet; any other may have arrived at any point within - // it, such as one that filled a gap after the largest. The largest comes last, - // so the cap reflects any ACK_FREQUENCY this ACK confirmed. - let acked_at = match packet == ack.largest { - true => now - .checked_sub(self.peer_ack_delay(space, ack.delay)) - .unwrap_or(now), - false => now, - }; - self.on_packet_acked(now, acked_at, info, packet, space); + self.on_packet_acked(now, info, packet, space); } } @@ -1679,10 +1669,16 @@ impl Connection { ); if new_largest && ack_eliciting_acked { + let ack_delay = if space != SpaceId::Data { + Duration::from_micros(0) + } else { + cmp::min( + self.ack_frequency.peer_max_ack_delay, + Duration::from_micros(ack.delay << self.peer_params.ack_delay_exponent.0), + ) + }; let rtt = now.saturating_duration_since(self.spaces[space].largest_acked_packet_sent); - self.path - .rtt - .update(self.peer_ack_delay(space, ack.delay), rtt); + self.path.rtt.update(ack_delay, rtt); if self.path.first_packet_after_rtt_sample.is_none() { self.path.first_packet_after_rtt_sample = Some((space, self.spaces[space].next_packet_number)); @@ -1728,17 +1724,6 @@ impl Connection { Ok(()) } - /// The peer's reported ACK delay, capped at the max_ack_delay it last agreed to. - fn peer_ack_delay(&self, space: SpaceId, delay: u64) -> Duration { - match space { - SpaceId::Data => cmp::min( - self.ack_frequency.peer_max_ack_delay, - Duration::from_micros(delay << self.peer_params.ack_delay_exponent.0), - ), - _ => Duration::ZERO, - } - } - fn detect_spurious_loss(&mut self, ack: &frame::Ack, space: SpaceId) -> bool { let lost_packets = &mut self.spaces[space].lost_packets; @@ -1812,16 +1797,7 @@ impl Connection { // Not timing-aware, so it's safe to call this for inferred acks, such as arise from // high-latency handshakes - // - // `acked_at` is when the ACK was received, less the ACK delay the peer reported. - fn on_packet_acked( - &mut self, - now: Instant, - acked_at: Instant, - info: SentPacket, - pn: u64, - space: SpaceId, - ) { + fn on_packet_acked(&mut self, now: Instant, info: SentPacket, pn: u64, space: SpaceId) { self.remove_in_flight(&info); if info.ack_eliciting && self.path.challenge.is_none() { // Only pass ACKs to the congestion controller if we are not validating the current @@ -1844,12 +1820,10 @@ impl Connection { } } - // An ACK delay the peer overstated cannot place the receipt before the send - let acked_at = acked_at.max(info.time_sent); for frame in info.stream_frames { let id = frame.id; // A reset held for a peer without RESET_STREAM_AT goes out once it holds the data - if let Some(reliable_size) = self.streams.received_ack_of(frame, acked_at) { + if let Some(reliable_size) = self.streams.received_ack_of(frame) { self.spaces[SpaceId::Data] .pending .reset_stream @@ -2823,7 +2797,7 @@ impl Connection { let space = &mut self.spaces[SpaceId::Initial]; if let Some(info) = space.take(0) { - self.on_packet_acked(now, now, info, 0, SpaceId::Initial); + self.on_packet_acked(now, info, 0, SpaceId::Initial); }; self.discard_space(now, SpaceId::Initial); // Make sure we clean up after any retransmitted Initials diff --git a/rs/moq-quic/src/connection/send_buffer.rs b/rs/moq-quic/src/connection/send_buffer.rs index 7f4e728a738..9828a3c4a84 100644 --- a/rs/moq-quic/src/connection/send_buffer.rs +++ b/rs/moq-quic/src/connection/send_buffer.rs @@ -1,17 +1,8 @@ -use std::{ - collections::{BTreeMap, VecDeque}, - mem, - ops::Range, - sync::{Arc, OnceLock, Weak}, -}; +use std::{collections::VecDeque, ops::Range}; use bytes::{Buf, Bytes}; -use super::streams::ClosedStream; -use crate::{Instant, VarInt, range_set::RangeSet}; - -/// Where a [`Checkpoint`](super::Checkpoint) learns when the data before it was acknowledged -pub(super) type AckSlot = OnceLock>; +use crate::{VarInt, range_set::RangeSet}; /// Buffer of outgoing retransmittable stream data #[derive(Default, Debug)] @@ -32,15 +23,8 @@ pub(super) struct SendBuffer { /// offset in `unacked` // TODO: Recover storage from these by compacting (#700) acks: RangeSet, - /// When the bytes from each key up to the next key (or the end of their range in `acks`) were - /// first acknowledged - ack_instants: BTreeMap, /// Previously transmitted ranges deemed lost retransmits: RangeSet, - /// Checkpoints waiting for the acknowledged prefix to reach them - watches: Watches, - /// When the acknowledged prefix became complete: the latest instant among its ranges - acked_at: Option, } impl SendBuffer { @@ -56,41 +40,18 @@ impl SendBuffer { self.unacked_segments.push_back(data); } - /// Discard a range of acknowledged stream data, acknowledged at `at` - /// - /// `at` is when the ACK was received, less the ACK delay the peer reported. - pub(super) fn ack(&mut self, mut range: Range, at: Instant) { + /// Discard a range of acknowledged stream data + pub(super) fn ack(&mut self, mut range: Range) { // Clamp the range to data which is still tracked, which `truncate` may have cut short let base_offset = self.offset - self.unacked_len as u64; range.start = base_offset.max(range.start); range.end = base_offset.max(range.end).min(self.offset); if range.is_empty() { - // Nothing new, but a FIN-only ACK still reaches a checkpoint at the prefix - let at = self.acked_at.map_or(at, |acked_at| acked_at.max(at)); - self.watches.ack(self.acked(), at); return; } - // Only the bytes this ACK newly covers take its instant; `replace` yields the rest in order - let mut next = range.start; - for overlap in self.acks.replace(range.clone()) { - if overlap.start > next { - self.ack_instants.insert(next, at); - } - next = overlap.end; - } - if next < range.end { - self.ack_instants.insert(next, at); - } - self.advance(); - } - /// Discard the acknowledged ranges that extend the acknowledged prefix, resolving the - /// checkpoints they reach - /// - /// The prefix up to an offset is complete only once every range below it is acknowledged, - /// so a checkpoint takes the latest instant among them, even when one ACK covers ranges - /// with different instants or a range waited behind a gap. - fn advance(&mut self) { + self.acks.insert(range); + while self.acks.min() == Some(self.offset - self.unacked_len as u64) { let prefix = self.acks.pop_min().unwrap(); let mut to_advance = (prefix.end - prefix.start) as usize; @@ -117,48 +78,6 @@ impl SendBuffer { } } } - - let acked = self.acked(); - let held = self.ack_instants.split_off(&acked); - let mut at = self.acked_at; - for (start, instant) in mem::replace(&mut self.ack_instants, held) { - if let Some(at) = at { - self.watches.ack(start, at); - } - at = Some(at.map_or(instant, |at| at.max(instant))); - } - if let Some(at) = at { - self.watches.ack(acked, at); - } - self.acked_at = at; - } - - /// The end of the contiguous acknowledged prefix - fn acked(&self) -> u64 { - self.offset - self.unacked_len as u64 - } - - /// Watch for the peer to acknowledge everything written so far - pub(super) fn watch(&mut self) -> Arc { - let slot = Arc::new(AckSlot::new()); - match self.acked_at { - // Everything written is already acknowledged - Some(at) if self.acked() == self.offset => { - let _ = slot.set(Ok(at)); - } - _ => self.watches.push(self.offset, &slot), - } - slot - } - - /// Fail every checkpoint still waiting, since the peer stopped reading - pub(super) fn abandon_watches(&mut self) { - self.watches.fail_all(); - } - - /// Whether a checkpoint somebody still holds resolved since the last call - pub(super) fn take_resolved(&mut self) -> bool { - mem::take(&mut self.watches.resolved) } /// Compute the next range to transmit on this stream and update state to account for that @@ -247,16 +166,6 @@ impl SendBuffer { /// /// The buffer then ends at `end`, so the caller tracks the stream's final size. pub(super) fn truncate(&mut self, end: u64) { - self.watches.fail_past(end); - self.cut(end); - // Only a checkpoint taken before anything was acknowledged can still wait, and with - // nothing left to acknowledge, no ACK will resolve it - if self.is_fully_acked() { - self.watches.fail_all(); - } - } - - fn cut(&mut self, end: u64) { if end >= self.offset { return; } @@ -267,8 +176,6 @@ impl SendBuffer { *self = Self { offset: end, unsent: end, - watches: mem::take(&mut self.watches), - acked_at: self.acked_at, ..Self::default() }; return; @@ -287,7 +194,6 @@ impl SendBuffer { self.offset = end; self.unsent = self.unsent.min(end); self.acks.remove(end..u64::MAX); - self.ack_instants.split_off(&end); self.retransmits.remove(end..u64::MAX); } @@ -313,6 +219,11 @@ impl SendBuffer { self.offset } + /// The end of the contiguous acknowledged prefix + pub(super) fn acked(&self) -> u64 { + self.offset - self.unacked_len as u64 + } + /// Whether all sent data has been acknowledged pub(super) fn is_fully_acked(&self) -> bool { self.unacked_len == 0 @@ -331,86 +242,9 @@ impl SendBuffer { } } -/// Checkpoints waiting for the acknowledged prefix to reach their offset, in offset order -/// -/// A dropped [`Checkpoint`](super::Checkpoint) leaves its entry behind until the prefix reaches -/// it, the next checkpoint is taken behind it, or the queue doubles past the live entries the -/// last sweep kept, so it holds at most about twice the live checkpoints. Dropping the stream -/// fails the rest, so no waiter hangs on a stream that is gone. -#[derive(Default, Debug)] -struct Watches { - pending: VecDeque<(u64, Weak)>, - /// The length at which `push` next sweeps out dead entries - sweep_at: usize, - /// Whether a checkpoint somebody still holds resolved since the stream last reported it - resolved: bool, -} - -impl Watches { - fn push(&mut self, offset: u64, slot: &Arc) { - // Checkpoints are taken at the write offset, which only grows - debug_assert!(self.pending.back().is_none_or(|(last, _)| *last <= offset)); - // Each entry is popped at most once, so this stays amortized O(1) - while self - .pending - .back() - .is_some_and(|(_, slot)| slot.strong_count() == 0) - { - self.pending.pop_back(); - } - // A checkpoint replaced by the next one dies after it is pushed, behind a live entry - if self.pending.len() >= self.sweep_at { - self.pending.retain(|(_, slot)| slot.strong_count() > 0); - // Doubling keeps each sweep's cost covered by the pushes since the last one - self.sweep_at = 2 * self.pending.len() + 1; - } - self.pending.push_back((offset, Arc::downgrade(slot))); - } - - /// Resolve every checkpoint at or below the acknowledged prefix `acked` - fn ack(&mut self, acked: u64, at: Instant) { - while let Some((offset, _)) = self.pending.front() - && *offset <= acked - { - let (_, slot) = self.pending.pop_front().unwrap(); - self.resolve(slot, Ok(at)); - } - } - - /// Fail every checkpoint past `end`, which the peer will never acknowledge - fn fail_past(&mut self, end: u64) { - while let Some((offset, _)) = self.pending.back() - && *offset > end - { - let (_, slot) = self.pending.pop_back().unwrap(); - self.resolve(slot, Err(ClosedStream::default())); - } - } - - fn fail_all(&mut self) { - while let Some((_, slot)) = self.pending.pop_front() { - self.resolve(slot, Err(ClosedStream::default())); - } - } - - fn resolve(&mut self, slot: Weak, result: Result) { - if let Some(slot) = slot.upgrade() { - let _ = slot.set(result); - self.resolved = true; - } - } -} - -impl Drop for Watches { - fn drop(&mut self) { - self.fail_all(); - } -} - #[cfg(test)] mod tests { use super::*; - use crate::Duration; #[test] fn fragment_with_length() { @@ -546,17 +380,17 @@ mod tests { assert_eq!(buf.poll_transmit(42), (MSG_LEN..MSG_LEN, true)); // Now drain the segments - buf.ack(0..1, Instant::now()); + buf.ack(0..1); assert_eq!(aggregate_unacked(&buf), &MSG[1..]); - buf.ack(0..3, Instant::now()); + buf.ack(0..3); assert_eq!(aggregate_unacked(&buf), &MSG[3..]); - buf.ack(3..5, Instant::now()); + buf.ack(3..5); assert_eq!(aggregate_unacked(&buf), &MSG[5..]); - buf.ack(7..9, Instant::now()); + buf.ack(7..9); assert_eq!(aggregate_unacked(&buf), &MSG[5..]); - buf.ack(4..7, Instant::now()); + buf.ack(4..7); assert_eq!(aggregate_unacked(&buf), &MSG[9..]); - buf.ack(0..MSG_LEN, Instant::now()); + buf.ack(0..MSG_LEN); assert_eq!(aggregate_unacked(&buf), &[] as &[u8]); } @@ -584,7 +418,7 @@ mod tests { const MSG: &[u8] = b"Hello, world!"; buf.write(MSG.into()); assert_eq!(buf.poll_transmit(16), (0..8, true)); - buf.ack(0..8, Instant::now()); + buf.ack(0..8); assert_eq!(aggregate_unacked(&buf), &MSG[8..]); } @@ -595,9 +429,9 @@ mod tests { buf.write(MSG.into()); assert_eq!(buf.poll_transmit(16), (0..16, false)); assert_eq!(buf.poll_transmit(16), (16..23, true)); - buf.ack(16..23, Instant::now()); + buf.ack(16..23); assert_eq!(aggregate_unacked(&buf), MSG); - buf.ack(0..16, Instant::now()); + buf.ack(0..16); assert_eq!(aggregate_unacked(&buf), &MSG[23..]); assert!(buf.acks.is_empty()); } @@ -608,7 +442,7 @@ mod tests { buf.write(Bytes::from_static(b"Hello, ")); buf.write(Bytes::from_static(b"world!")); assert_eq!(buf.poll_transmit(16), (0..8, true)); - buf.ack(0..2, Instant::now()); + buf.ack(0..2); buf.retransmit(2..8); buf.truncate(9); @@ -620,10 +454,10 @@ mod tests { assert!(!buf.has_unsent_data()); // Late acks and losses past the truncation are ignored - buf.ack(9..13, Instant::now()); + buf.ack(9..13); buf.retransmit(9..13); assert!(!buf.has_unsent_data()); - buf.ack(2..9, Instant::now()); + buf.ack(2..9); assert!(buf.is_fully_acked()); assert_eq!(buf.buffered(), 0); } @@ -633,191 +467,13 @@ mod tests { let mut buf = SendBuffer::new(); buf.write(Bytes::from_static(b"Hello, world!")); assert_eq!(buf.poll_transmit(16), (0..8, true)); - buf.ack(0..8, Instant::now()); + buf.ack(0..8); buf.truncate(4); assert!(buf.is_fully_acked()); assert!(!buf.has_unsent_data()); assert_eq!(buf.buffered(), 0); } - #[test] - fn watch_waits_for_prefix() { - let mut buf = SendBuffer::new(); - buf.write(Bytes::from_static(b"Hello, ")); - let first = buf.watch(); - buf.write(Bytes::from_static(b"world!")); - let second = buf.watch(); - assert_eq!(buf.poll_transmit(16), (0..8, true)); - assert_eq!(buf.poll_transmit(16), (8..13, true)); - - // Acknowledged past both, but not contiguously - let early = Instant::now(); - buf.ack(8..13, early); - assert_eq!(first.get(), None); - assert_eq!(second.get(), None); - assert!(!buf.take_resolved()); - - // Filling the gap in the middle of the first segment resolves both, with that ACK - let late = early + Duration::from_millis(1); - buf.ack(0..8, late); - assert_eq!(first.get(), Some(&Ok(late))); - assert_eq!(second.get(), Some(&Ok(late))); - assert!(buf.take_resolved()); - assert!(!buf.take_resolved()); - - // Everything written is acknowledged, by the ACK that got there - assert_eq!(buf.watch().get(), Some(&Ok(late))); - } - - #[test] - fn watch_takes_the_latest_instant_below_it() { - let mut buf = SendBuffer::new(); - buf.write(Bytes::from_static(b"Hello, ")); - let first = buf.watch(); - buf.write(Bytes::from_static(b"world!")); - let second = buf.watch(); - assert_eq!(buf.poll_transmit(16), (0..8, true)); - assert_eq!(buf.poll_transmit(16), (8..13, true)); - - // One ACK, in packet order: the held range came in a packet before the largest, so it - // keeps the receive time, while the largest fills the gap less its ACK delay - let early = Instant::now(); - let late = early + Duration::from_millis(1); - buf.ack(8..13, late); - buf.ack(0..8, early); - assert_eq!(first.get(), Some(&Ok(early))); - assert_eq!(second.get(), Some(&Ok(late))); - - // A later range acknowledged at an earlier instant can't complete the prefix before it - buf.write(Bytes::from_static(b"!")); - let third = buf.watch(); - assert_eq!(buf.poll_transmit(16), (13..14, true)); - buf.ack(13..14, early); - assert_eq!(third.get(), Some(&Ok(late))); - } - - #[test] - fn watch_takes_the_instant_of_an_extension() { - let mut buf = SendBuffer::new(); - buf.write(Bytes::from_static(b"Hello, world!")); - let end = buf.watch(); - assert_eq!(buf.poll_transmit(16), (0..8, true)); - assert_eq!(buf.poll_transmit(16), (8..13, true)); - - // A short retransmission is acknowledged before a longer frame from the same offset - let early = Instant::now(); - let late = early + Duration::from_millis(1); - buf.ack(8..10, early); - buf.ack(8..13, late); - buf.ack(0..8, early); - assert_eq!(end.get(), Some(&Ok(late))); - } - - #[test] - fn watch_ignores_a_redundant_ack() { - let mut buf = SendBuffer::new(); - buf.write(Bytes::from_static(b"Hello, world!")); - let end = buf.watch(); - assert_eq!(buf.poll_transmit(16), (0..8, true)); - assert_eq!(buf.poll_transmit(16), (8..13, true)); - - // Repeating a held range adds no bytes, so its later instant doesn't count - let early = Instant::now(); - let late = early + Duration::from_millis(1); - buf.ack(8..13, early); - buf.ack(8..13, late); - buf.ack(0..8, early); - assert_eq!(end.get(), Some(&Ok(early))); - } - - #[test] - fn watch_fails_past_truncation() { - let mut buf = SendBuffer::new(); - buf.write(Bytes::from_static(b"Hello, ")); - let kept = buf.watch(); - buf.write(Bytes::from_static(b"world!")); - let dropped = buf.watch(); - - buf.truncate(7); - assert_eq!(dropped.get(), Some(&Err(ClosedStream::default()))); - assert_eq!(kept.get(), None); - assert!(buf.take_resolved()); - - // A stream that goes away fails what it never acknowledged - drop(buf); - assert_eq!(kept.get(), Some(&Err(ClosedStream::default()))); - } - - /// A checkpoint on an empty stream fails, and is reported, once a reset leaves nothing to - /// acknowledge. - #[test] - fn watch_fails_on_empty_reset() { - let mut buf = SendBuffer::new(); - let empty = buf.watch(); - buf.truncate(0); - assert_eq!(empty.get(), Some(&Err(ClosedStream::default()))); - assert!(buf.take_resolved()); - } - - /// Checkpoints taken and dropped on a stream whose prefix never moves do not pile up. - #[test] - fn dropped_watches_are_pruned() { - let mut buf = SendBuffer::new(); - buf.write(Bytes::from_static(b"Hello")); - let held = buf.watch(); - for _ in 0..1000 { - drop(buf.watch()); - } - assert!( - buf.watches.pending.len() <= 2, - "{}", - buf.watches.pending.len() - ); - - // Nothing was ever written or acknowledged on this one - let mut empty = SendBuffer::new(); - for _ in 0..1000 { - drop(empty.watch()); - } - assert!(empty.watches.pending.len() <= 1); - - assert_eq!(buf.poll_transmit(16), (0..5, true)); - buf.ack(0..5, Instant::now()); - assert!(held.get().is_some()); - assert!(buf.watches.pending.is_empty()); - } - - #[test] - fn replaced_watches_are_pruned() { - // `last = stream.checkpoint()` takes the new checkpoint before dropping the old one, so - // each dead entry sits behind a live one while nothing is acknowledged - let mut buf = SendBuffer::new(); - let mut last = buf.watch(); - for _ in 0..1000 { - buf.write(Bytes::from_static(b"x")); - last = buf.watch(); - } - assert!( - buf.watches.pending.len() <= 3, - "{}", - buf.watches.pending.len() - ); - - assert_eq!(buf.poll_transmit(2000), (0..1000, true)); - buf.ack(0..1000, Instant::now()); - assert!(last.get().is_some()); - } - - #[test] - fn dropped_watch_is_not_reported() { - let mut buf = SendBuffer::new(); - buf.write(Bytes::from_static(b"Hello")); - drop(buf.watch()); - assert_eq!(buf.poll_transmit(16), (0..5, true)); - buf.ack(0..5, Instant::now()); - assert!(!buf.take_resolved()); - } - fn aggregate_unacked(buf: &SendBuffer) -> Vec { let mut result = Vec::new(); for segment in buf.unacked_segments.iter() { diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index 23ba67a19d8..e645d37e974 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -1,4 +1,4 @@ -use std::{collections::hash_map, io, sync::Arc}; +use std::{collections::hash_map, io}; use bytes::Bytes; use thiserror::Error; @@ -6,8 +6,8 @@ use tracing::trace; use super::spaces::{Retransmits, ThinRetransmits}; use crate::{ - Dir, Instant, StreamId, VarInt, - connection::{ConnectionTag, send_buffer::AckSlot, streams::state::get_or_insert_recv}, + Dir, StreamId, VarInt, + connection::{ConnectionTag, streams::state::get_or_insert_recv}, frame, }; @@ -358,28 +358,17 @@ impl<'a> SendStream<'a> { /// /// # Panics /// - when applied to a receive stream - pub fn checkpoint(&mut self) -> Checkpoint { - let max_send_data = self.state.max_send_data(self.id); - let closed = || Arc::new(AckSlot::from(Err(ClosedStream::default()))); - let (offset, acked) = match self.state.send.get_mut(&self.id) { - Some(stream) => { - let stream = stream.get_or_insert_with(|| Send::new(max_send_data)); - // A stopped or reset stream fails its checkpoints, so a new one does too - let acked = match stream.stop_reason.is_some() || stream.is_reset() { - true => closed(), - false => stream.pending.watch(), - }; - (stream.offset(), acked) - } - // Already closed, where the reset fails anyway - None => (0, closed()), + pub fn checkpoint(&self) -> Checkpoint { + let offset = match self.state.send.get(&self.id) { + Some(Some(stream)) => stream.offset(), + // Not written yet, or already closed, where the reset fails anyway + _ => 0, }; Checkpoint { id: self.id, tag: self.state.tag, generation: self.state.generation, offset, - acked, } } @@ -412,11 +401,6 @@ impl<'a> SendStream<'a> { let buffered = stream.pending.buffered(); let send = stream.reset(error_code, reliable_size, at); self.state.buffered_data -= buffered - stream.pending.buffered(); - if stream.pending.take_resolved() { - self.state - .events - .push_back(StreamEvent::Acked { id: self.id }); - } // Only data below the Reliable Size is left to send if !stream.is_pending() { self.state.pending.remove(&mut stream.slot); @@ -498,41 +482,38 @@ pub struct Checkpoint { tag: ConnectionTag, generation: u64, offset: u64, - acked: Arc, } impl Checkpoint { - /// When the peer acknowledged everything written before the checkpoint, or `None` until it has - /// - /// The instant is when the ACK that brought the stream's contiguous acknowledged prefix to the - /// checkpoint was received, less the ACK delay the peer reported, capped at its - /// `max_ack_delay`. That leaves out the peer's delayed-ACK timer, but still includes the ACK's - /// one-way trip back. The delay describes only the largest packet the ACK names, so data in - /// any other packet, which may have arrived later, gets the ACK's receive time with nothing - /// subtracted. The instant is never earlier than the data's packet was sent. A checkpoint taken once - /// everything written was already acknowledged gets the ACK that got it there, and one taken - /// before anything was written gets the stream's next ACK, or fails if a reset comes first. + /// Whether the peer acknowledged everything written before the checkpoint /// - /// [`StreamEvent::Acked`] reports when this resolves. Like a [`StreamEvent::Finished`] stream, - /// an acknowledgment only means the peer's QUIC stack holds the data, not that its - /// application read it. + /// A `false` notes to report a [`StreamEvent::Acked`] once the stream's contiguous + /// acknowledged prefix reaches the checkpoint, so poll again then. One event answers every + /// checkpoint at or below the offset it carries. Like a [`StreamEvent::Finished`] stream, an + /// acknowledgment only means the peer's QUIC stack holds the data, not that its application + /// read it. /// /// Fails with [`ClosedStream`] once the peer will never acknowledge it, or no longer reads - /// it: the stream was reset before the checkpoint's data was acknowledged, the peer sent - /// STOP_SENDING, rejected 0-RTT discarded the stream, or the connection closed. A checkpoint - /// taken once the stream was reset or stopped fails at once. + /// it: a reset dropped the checkpoint's data, the peer sent STOP_SENDING, rejected 0-RTT + /// discarded the stream, or the connection closed. It also fails once the stream is freed, + /// after its [`StreamEvent::Finished`], which means every checkpoint on it was acknowledged. /// /// # Panics /// If the checkpoint came from another connection. - pub fn acked(&self, conn: &crate::Connection) -> Result, ClosedStream> { + pub fn acked(&self, conn: &mut crate::Connection) -> Result { assert!( self.tag == conn.streams.tag, "checkpoint used on another connection" ); - match self.acked.get() { - Some(acked) => acked.clone().map(Some), - None if conn.is_closed() => Err(ClosedStream { _private: () }), - None => Ok(None), + // Rejected 0-RTT discarded the stream, and a new one may have its id + if conn.is_closed() || self.generation != conn.streams.generation { + return Err(ClosedStream { _private: () }); + } + match conn.streams.send.get_mut(&self.id) { + Some(Some(stream)) => stream.acked(self.offset), + // Nothing was written yet, so nothing is left to acknowledge + Some(None) => Ok(true), + None => Err(ClosedStream { _private: () }), } } @@ -742,12 +723,16 @@ pub enum StreamEvent { /// Which stream has been finished id: StreamId, }, - /// A [`Checkpoint`] on a send stream was acknowledged, or will never be + /// A send stream's contiguous acknowledged prefix reached a [`Checkpoint`] being waited on /// - /// Not generated when the connection closes or rejected 0-RTT discards the stream. + /// Reported before a [`StreamEvent::Finished`] from the same ACK. Not reported after + /// STOP_SENDING, a reset that drops the checkpoint's data, or the connection closing, which + /// fail [`Checkpoint::acked`] instead. Acked { - /// Which stream the checkpoint is on + /// Which stream was acknowledged id: StreamId, + /// The end of the stream's contiguous acknowledged prefix + offset: u64, }, /// The peer asked us to stop sending on an outgoing stream Stopped { diff --git a/rs/moq-quic/src/connection/streams/saturated.rs b/rs/moq-quic/src/connection/streams/saturated.rs index 7f6b6edbc90..0ae52976672 100644 --- a/rs/moq-quic/src/connection/streams/saturated.rs +++ b/rs/moq-quic/src/connection/streams/saturated.rs @@ -9,7 +9,7 @@ use bytes::{BufMut, Bytes, BytesMut}; use super::state::StreamsState; use super::{SendDomain, SendGroup, SendGroups, SendStream, Streams}; use crate::{ - Dir, Instant, Side, StreamId, VarInt, + Dir, Side, StreamId, VarInt, connection::{State as ConnState, spaces::Retransmits}, frame::{self, Frame, StreamMeta}, transport_parameters::TransportParameters, @@ -505,14 +505,11 @@ fn reset_frees_a_released_group() { assert!(!sender.state.can_send_stream_data()); assert!(sender.drain(1).is_empty()); - sender.state.received_ack_of( - StreamMeta { - id: finished, - offsets: 0..100, - fin: true, - }, - Instant::now(), - ); + sender.state.received_ack_of(StreamMeta { + id: finished, + offsets: 0..100, + fin: true, + }); assert!(!sender.state.pending.is_clean()); sender.state.reset_acked(reset, 0); assert!(sender.state.pending.is_clean()); @@ -536,22 +533,16 @@ fn reliable_reset_sends_its_prefix_then_frees_its_group() { assert_eq!(sender.bytes(&[finished]), 10_000); assert!(!sender.state.can_send_stream_data()); - sender.state.received_ack_of( - StreamMeta { - id: finished, - offsets: 0..10_000, - fin: true, - }, - Instant::now(), - ); - sender.state.received_ack_of( - StreamMeta { - id: reset, - offsets: 0..3_000, - fin: false, - }, - Instant::now(), - ); + sender.state.received_ack_of(StreamMeta { + id: finished, + offsets: 0..10_000, + fin: true, + }); + sender.state.received_ack_of(StreamMeta { + id: reset, + offsets: 0..3_000, + fin: false, + }); assert!( !sender.state.pending.is_clean(), "the reset is unacknowledged" diff --git a/rs/moq-quic/src/connection/streams/send.rs b/rs/moq-quic/src/connection/streams/send.rs index eed004bfee8..cff5114de8e 100644 --- a/rs/moq-quic/src/connection/streams/send.rs +++ b/rs/moq-quic/src/connection/streams/send.rs @@ -1,8 +1,8 @@ use bytes::Bytes; use thiserror::Error; -use super::schedule::Slot; -use crate::{Instant, StreamId, VarInt, connection::send_buffer::SendBuffer, frame}; +use super::{ClosedStream, schedule::Slot}; +use crate::{StreamId, VarInt, connection::send_buffer::SendBuffer, frame}; #[derive(Debug)] pub(super) struct Send { @@ -21,6 +21,8 @@ pub(super) struct Send { pub(super) data_blocked_limit: Option, /// The reason the peer wants us to stop, if `STOP_SENDING` was received pub(super) stop_reason: Option, + /// The lowest offset a [`Checkpoint::acked`](super::Checkpoint::acked) call is waiting for + pub(super) notify: Option, } impl Send { @@ -35,6 +37,7 @@ impl Send { connection_blocked: false, data_blocked_limit: None, stop_reason: None, + notify: None, }) } @@ -231,10 +234,8 @@ impl Send { /// Returns whether the stream has been finished or reset, and all data it still owes the peer /// has been acknowledged - /// - /// `at` is when the ACK was received, less the ACK delay the peer reported. - pub(super) fn ack(&mut self, frame: frame::StreamMeta, at: Instant) -> bool { - self.pending.ack(frame.offsets, at); + pub(super) fn ack(&mut self, frame: frame::StreamMeta) -> bool { + self.pending.ack(frame.offsets); match &mut self.state { SendState::DataSent { finish_acked } => { *finish_acked |= frame.fin; @@ -245,6 +246,30 @@ impl Send { } } + /// Whether the peer acknowledged everything before `offset`, noting to report it if not + pub(super) fn acked(&mut self, offset: u64) -> Result { + // An acknowledgment no longer means the peer reads the data, and a reset dropped the rest + if self.stop_reason.is_some() || offset > self.pending.offset() { + return Err(ClosedStream::default()); + } + if offset <= self.pending.acked() { + return Ok(true); + } + self.notify = Some(self.notify.map_or(offset, |notify| notify.min(offset))); + Ok(false) + } + + /// The acknowledged prefix to report, once it reaches the offset a waiter is waiting for + pub(super) fn take_acked(&mut self) -> Option { + let acked = self.pending.acked(); + // A stopped stream fails its waiters instead + if self.stop_reason.is_some() || self.notify.is_none_or(|notify| acked < notify) { + return None; + } + self.notify = None; + Some(acked) + } + /// Handle increase to stream-level flow control limit /// /// Returns whether the stream was unblocked diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index 816b0929d21..77b551d81e4 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -14,7 +14,7 @@ use super::{ StreamHalf, ThinRetransmits, }; use crate::{ - Dir, Instant, MAX_STREAM_COUNT, Side, StreamId, TransportError, VarInt, + Dir, MAX_STREAM_COUNT, Side, StreamId, TransportError, VarInt, coding::BufMutExt, connection::ConnectionTag, connection::stats::{CreditStats, FrameStats}, @@ -113,7 +113,7 @@ pub(crate) struct StreamsState { /// Streams with outgoing data queued and outgoing datagrams, in the order to send them pub(super) pending: Schedule, - pub(super) events: VecDeque, + events: VecDeque, /// Streams blocked on connection-level flow control or stream window space /// /// Streams are only added to this list when a write fails. @@ -422,14 +422,9 @@ impl StreamsState { self.events .push_back(StreamEvent::Stopped { id, error_code }); } - // An acknowledgment no longer means the peer will read the data - stream.pending.abandon_watches(); let buffered = stream.pending.buffered(); let reset = stream.abandon_reliable(); self.buffered_data -= buffered - stream.pending.buffered(); - if stream.pending.take_resolved() { - self.events.push_back(StreamEvent::Acked { id }); - } if !stream.is_pending() { self.pending.remove(&mut stream.slot); } @@ -759,9 +754,7 @@ impl StreamsState { /// /// Returns the Reliable Size of a held reset to send now, once the peer holds all the data it /// waited on. - /// - /// `at` is when the ACK was received, less the ACK delay the peer reported. - pub(crate) fn received_ack_of(&mut self, frame: frame::StreamMeta, at: Instant) -> Option { + pub(crate) fn received_ack_of(&mut self, frame: frame::StreamMeta) -> Option { let mut entry = match self.send.entry(frame.id) { hash_map::Entry::Vacant(_) => return None, hash_map::Entry::Occupied(e) => e, @@ -777,10 +770,10 @@ impl StreamsState { let id = frame.id; let buffered = stream.pending.buffered(); - let finished = stream.ack(frame, at); + let finished = stream.ack(frame); self.buffered_data -= buffered - stream.pending.buffered(); - if stream.pending.take_resolved() { - self.events.push_back(StreamEvent::Acked { id }); + if let Some(offset) = stream.take_acked() { + self.events.push_back(StreamEvent::Acked { id, offset }); } if !finished { // The stream is unfinished or may still need retransmits @@ -1216,40 +1209,31 @@ mod tests { assert_eq!(send.pending.poll_transmit(16).0, 16..23); assert_eq!(send.pending.poll_transmit(16).0, 23..24); - server.received_ack_of( - frame::StreamMeta { - id, - offsets: 16..23, - fin: false, - }, - Instant::now(), - ); + server.received_ack_of(frame::StreamMeta { + id, + offsets: 16..23, + fin: false, + }); assert_eq!( server.write_limit(), 0, "acknowledged suffix still occupies storage" ); - server.received_ack_of( - frame::StreamMeta { - id, - offsets: 0..16, - fin: false, - }, - Instant::now(), - ); + server.received_ack_of(frame::StreamMeta { + id, + offsets: 0..16, + fin: false, + }); assert_eq!( server.write_limit(), 0, "partial prefix keeps the original Bytes allocation" ); - server.received_ack_of( - frame::StreamMeta { - id, - offsets: 23..24, - fin: false, - }, - Instant::now(), - ); + server.received_ack_of(frame::StreamMeta { + id, + offsets: 23..24, + fin: false, + }); assert_eq!(server.write_limit(), 24); SendStream { id, @@ -1290,14 +1274,11 @@ mod tests { let send = server.send.get_mut(&id).unwrap().as_mut().unwrap(); send.pending.poll_transmit(16); send.pending.poll_transmit(16); - server.received_ack_of( - frame::StreamMeta { - id, - offsets: 16..23, - fin: false, - }, - Instant::now(), - ); + server.received_ack_of(frame::StreamMeta { + id, + offsets: 16..23, + fin: false, + }); SendStream { id, state: &mut server, @@ -1323,14 +1304,11 @@ mod tests { offsets: 0..16, fin: false, }); - server.received_ack_of( - frame::StreamMeta { - id, - offsets: 0..16, - fin: false, - }, - Instant::now(), - ); + server.received_ack_of(frame::StreamMeta { + id, + offsets: 0..16, + fin: false, + }); assert!(!server.send[&id].as_ref().unwrap().is_pending()); assert_eq!(server.write_limit(), 24); } @@ -2560,14 +2538,11 @@ mod tests { assert_eq!(stream.state.poll(), None); // Ack the data - stream.state.received_ack_of( - frame::StreamMeta { - id: stream_id, - offsets: 0..larger_send_window, - fin: false, - }, - Instant::now(), - ); + stream.state.received_ack_of(frame::StreamMeta { + id: stream_id, + offsets: 0..larger_send_window, + fin: false, + }); assert_eq!( stream.state.poll(), @@ -2635,26 +2610,20 @@ mod tests { assert_eq!(stream.write(&data), Err(WriteError::Blocked)); // Ack some data, assert that writes are still not accepted due to outstanding sends - stream.state.received_ack_of( - frame::StreamMeta { - id: stream_id, - offsets: 0..smaller_send_window, - fin: false, - }, - Instant::now(), - ); + stream.state.received_ack_of(frame::StreamMeta { + id: stream_id, + offsets: 0..smaller_send_window, + fin: false, + }); assert_eq!(stream.write(&data), Err(WriteError::Blocked)); // Ack the rest of the data - stream.state.received_ack_of( - frame::StreamMeta { - id: stream_id, - offsets: smaller_send_window..initial_send_window, - fin: false, - }, - Instant::now(), - ); + stream.state.received_ack_of(frame::StreamMeta { + id: stream_id, + offsets: smaller_send_window..initial_send_window, + fin: false, + }); // This should generate a `Writable` event assert_eq!( @@ -2961,10 +2930,7 @@ mod tests { buf.clear(); let resent = server.write_frames(&mut buf, 0..100, true).streams; assert_eq!(resent[0].offsets, 0..4); - assert_eq!( - server.received_ack_of(resent[0].clone(), Instant::now()), - Some(4) - ); + assert_eq!(server.received_ack_of(resent[0].clone()), Some(4)); assert_eq!(server.buffered_data, 0); // The plain RESET_STREAM still names the full final size @@ -3032,10 +2998,7 @@ mod tests { server.reset_acked(id, 4); assert_eq!(server.received_stop_sending(id, 9u32.into()), Some(0)); - assert_eq!( - server.received_ack_of(sent[0].clone(), Instant::now()), - None - ); + assert_eq!(server.received_ack_of(sent[0].clone()), None); assert_eq!(server.send_streams, 1); server.reset_acked(id, 0); assert_eq!(server.send_streams, 0); @@ -3123,7 +3086,7 @@ mod tests { // The stream lingers until both the reset and the prefix are acknowledged server.reset_acked(id, 4); assert_eq!(server.send_streams, 1); - server.received_ack_of(resent[0].clone(), Instant::now()); + server.received_ack_of(resent[0].clone()); assert_eq!(server.send_streams, 0); assert_eq!(server.buffered_data, 0); assert_eq!(server.poll(), None); diff --git a/rs/moq-quic/src/fuzz/connection.rs b/rs/moq-quic/src/fuzz/connection.rs index 6c419ad0bc8..de0e900f64c 100644 --- a/rs/moq-quic/src/fuzz/connection.rs +++ b/rs/moq-quic/src/fuzz/connection.rs @@ -103,6 +103,10 @@ enum Op { checkpoint: u8, code: u32, }, + AckedCheckpoint { + who: Who, + checkpoint: u8, + }, Priority { who: Who, stream: u8, @@ -500,6 +504,16 @@ impl Pair { let _ = checkpoint.reset(conn, code.into()); } } + Op::AckedCheckpoint { who, checkpoint } => { + let peer = self.peer(who); + if peer.checkpoints.is_empty() { + return; + } + let index = checkpoint as usize % peer.checkpoints.len(); + if let Some((_, conn)) = peer.conn.as_mut() { + let _ = peer.checkpoints[index].acked(conn); + } + } Op::Priority { who, stream, diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index ba1c7e62ee0..a213a2ebc45 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -1,4 +1,4 @@ -//! Tests for [`Checkpoint::acked`], which reports when the peer acknowledged a send stream's prefix +//! Tests for [`Checkpoint::acked`], which reports whether the peer acknowledged a send stream's prefix use super::*; @@ -28,15 +28,19 @@ fn acked( pair: &mut Pair, ch: ConnectionHandle, checkpoint: &Checkpoint, -) -> Result, ClosedStream> { +) -> Result { checkpoint.acked(pair.client_conn_mut(ch)) } -/// Whether the client reported a checkpoint on `s` resolving, draining its events -fn reported(pair: &mut Pair, ch: ConnectionHandle, s: StreamId) -> bool { - let mut reported = false; +/// The prefix the client last reported acknowledged on `s`, draining its events +fn reported(pair: &mut Pair, ch: ConnectionHandle, s: StreamId) -> Option { + let mut reported = None; while let Some(event) = pair.client_conn_mut(ch).poll() { - reported |= matches!(event, Event::Stream(StreamEvent::Acked { id }) if id == s); + if let Event::Stream(StreamEvent::Acked { id, offset }) = event + && id == s + { + reported = Some(offset); + } } reported } @@ -48,158 +52,16 @@ fn lose_client_packets(pair: &mut Pair) { pair.client.outbound.clear(); } -/// The instant leaves out the peer's delayed-ACK timer, but keeps both one-way trips. -#[test] -fn excludes_ack_delay() { - let _guard = subscribe(); - let mut pair = pair(true); - let (client_ch, server_ch) = pair.connect(); - let s = open(&mut pair, client_ch, server_ch); - reported(&mut pair, client_ch, s); - - // A lone packet waits out the peer's delayed-ACK timer - pair.client_send(client_ch, s).write(b"payload").unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); - let sent = pair.time; - assert_eq!(acked(&mut pair, client_ch, &checkpoint), Ok(None)); - let received = loop { - let busy = pair.step(); - if acked(&mut pair, client_ch, &checkpoint) != Ok(None) { - break pair.time; - } - assert!(busy, "never acknowledged"); - }; - let at = acked(&mut pair, client_ch, &checkpoint).unwrap().unwrap(); - - assert!(reported(&mut pair, client_ch, s)); - assert_eq!(at - sent, 2 * LATENCY); - assert!( - received - at >= Duration::from_millis(20), - "{:?}", - received - at - ); -} - -/// A reordered packet acknowledged after a later one keeps its receive instant: the ACK's delay -/// describes the later packet, so none is subtracted. -#[test] -fn reordered_ack_keeps_its_delay() { - const HOLD: Duration = Duration::from_millis(1); - let _guard = subscribe(); - let mut pair = pair(true); - let (client_ch, server_ch) = pair.connect(); - let s = open(&mut pair, client_ch, server_ch); - - // The first packet is held back, so it arrives after the second - pair.client_send(client_ch, s).write(b"held").unwrap(); - let held = pair.client_send(client_ch, s).checkpoint(); - pair.client.drive(pair.time, pair.server.addr); - pair.client.delay_outbound(); - pair.client_send(client_ch, s).write(b"ahead").unwrap(); - pair.drive_client(); - - pair.time += HOLD; - let released = pair.time; - pair.client.finish_delay(); - pair.drive_client(); - pair.drive(); - - // The peer acknowledges it at once, as out of order, after it already acknowledged the - // later packet - let at = acked(&mut pair, client_ch, &held).unwrap().unwrap(); - assert_eq!(at - released, 2 * LATENCY); -} - -/// A gap filled after a later packet keeps its receive instant, even when the first ACK of the -/// later packet was lost and the gap's ACK is the first to raise the largest. -#[test] -fn gap_acked_with_largest_keeps_its_delay() { - const HOLD: Duration = Duration::from_millis(1); - let _guard = subscribe(); - let mut pair = pair(true); - let (client_ch, server_ch) = pair.connect(); - let s = open(&mut pair, client_ch, server_ch); - - // The first packet is held back, so it arrives after the second - pair.client_send(client_ch, s).write(b"held").unwrap(); - let held = pair.client_send(client_ch, s).checkpoint(); - pair.client.drive(pair.time, pair.server.addr); - pair.client.delay_outbound(); - pair.client_send(client_ch, s).write(b"ahead").unwrap(); - pair.drive_client(); - - // The peer's ACK of the second packet alone is lost - pair.time += LATENCY; - pair.server.drive(pair.time, pair.client.addr); - assert!(!pair.server.outbound.is_empty()); - pair.server.outbound.clear(); - - pair.time += HOLD; - let released = pair.time; - pair.client.finish_delay(); - pair.drive_client(); - pair.drive(); - - let at = acked(&mut pair, client_ch, &held).unwrap().unwrap(); - assert_eq!(at - released, 2 * LATENCY); -} - -/// An ACK that confirms a raised max_ack_delay subtracts its delay up to the new cap, which the -/// peer already used to hold that ACK. -#[test] -fn ack_delay_cap_follows_ack_frequency() { - let _guard = subscribe(); - let (mut pair, client_ch, server_ch) = setup_ack_frequency_test(Duration::from_millis(200)); - let s = open(&mut pair, client_ch, server_ch); - let sent_ack_frequency = |pair: &mut Pair| { - pair.client_conn_mut(client_ch) - .stats() - .frame_tx - .ack_frequency - }; - let before = sent_ack_frequency(&mut pair); - - // The RTT jumps, so the client asks for a longer max_ack_delay than the default 25 ms - pair.latency *= 100; - pair.client_conn_mut(client_ch).ping(); - while sent_ack_frequency(&mut pair) == before { - assert!(pair.step(), "never sent ACK_FREQUENCY"); - } - - // The peer holds the ACK covering both packets for the new max_ack_delay - pair.client_send(client_ch, s).write(b"payload").unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); - let sent = pair.time; - let received = loop { - let busy = pair.step(); - if acked(&mut pair, client_ch, &checkpoint) != Ok(None) { - break pair.time; - } - assert!(busy, "never acknowledged"); - }; - let at = acked(&mut pair, client_ch, &checkpoint).unwrap().unwrap(); - - assert!( - received - at > Duration::from_millis(25), - "{:?}", - received - at - ); - // Pacing may hold the packet briefly after the write - let trip = at - sent; - assert!( - trip >= 2 * pair.latency && trip < 2 * pair.latency + Duration::from_millis(1), - "{trip:?}" - ); -} - -/// A checkpoint resolves only once every byte before it is acknowledged, by the ACK that fills -/// the gap, however much of the stream that ACK covers. +/// A checkpoint is acknowledged only once every byte before it is, and the event that reports it +/// fires only for a checkpoint someone polled. #[test] fn waits_for_contiguous_prefix() { let _guard = subscribe(); let mut pair = pair(true); let (client_ch, server_ch) = pair.connect(); let s = open(&mut pair, client_ch, server_ch); + pair.drive(); + reported(&mut pair, client_ch, s); pair.client_send(client_ch, s).write(b"lost").unwrap(); let lost = pair.client_send(client_ch, s).checkpoint(); @@ -212,54 +74,89 @@ fn waits_for_contiguous_prefix() { while pair.client_conn_mut(client_ch).stats().frame_rx.acks == acks { assert!(pair.step(), "never acknowledged"); } - assert_eq!(acked(&mut pair, client_ch, &lost), Ok(None)); - assert_eq!(acked(&mut pair, client_ch, &after), Ok(None)); + assert_eq!(acked(&mut pair, client_ch, &after), Ok(false)); - // The retransmission fills the gap, which resolves both at once + // The retransmission fills the gap, which acknowledges both at once pair.drive(); - let at = acked(&mut pair, client_ch, &lost).unwrap(); - assert!(at.is_some()); - assert_eq!(acked(&mut pair, client_ch, &after), Ok(at)); - assert!(reported(&mut pair, client_ch, s)); + assert_eq!( + reported(&mut pair, client_ch, s), + Some(b"headerlostafter".len() as u64) + ); + assert_eq!(acked(&mut pair, client_ch, &lost), Ok(true)); + assert_eq!(acked(&mut pair, client_ch, &after), Ok(true)); assert!(pair.client_conn_mut(client_ch).stats().path.lost_packets > 0); } -/// A checkpoint taken once everything is acknowledged resolves at once, with the ACK that got -/// there. +/// Without a poll waiting on it, an acknowledgment is not reported. #[test] -fn already_acked() { +fn unpolled_is_not_reported() { let _guard = subscribe(); let mut pair = pair(true); let (client_ch, server_ch) = pair.connect(); let s = open(&mut pair, client_ch, server_ch); - let first = pair.client_send(client_ch, s).checkpoint(); - let second = pair.client_send(client_ch, s).checkpoint(); - let at = acked(&mut pair, client_ch, &first).unwrap(); - assert!(at.is_some()); - assert_eq!(acked(&mut pair, client_ch, &second), Ok(at)); + pair.client_send(client_ch, s).write(b"payload").unwrap(); + let checkpoint = pair.client_send(client_ch, s).checkpoint(); + pair.drive(); + assert_eq!(reported(&mut pair, client_ch, s), None); + assert_eq!(acked(&mut pair, client_ch, &checkpoint), Ok(true)); +} + +/// A checkpoint taken before anything was written, or once everything was acknowledged, is +/// acknowledged at once. +#[test] +fn already_acked() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, _) = pair.connect(); + let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); + let empty = pair.client_send(client_ch, s).checkpoint(); + assert_eq!(acked(&mut pair, client_ch, &empty), Ok(true)); + + pair.client_send(client_ch, s).write(b"payload").unwrap(); + pair.drive(); + let all = pair.client_send(client_ch, s).checkpoint(); + assert_eq!(acked(&mut pair, client_ch, &all), Ok(true)); } -/// A finished stream is gone once acknowledged, but its checkpoints keep their answer. +/// A finished stream is freed by the ACK that completes it, which reports a waiting checkpoint +/// before the stream finishes. #[test] -fn outlives_finished_stream() { +fn finished_stream_reports_before_freed() { let _guard = subscribe(); let mut pair = pair(true); let (client_ch, server_ch) = pair.connect(); let s = open(&mut pair, client_ch, server_ch); + pair.drive(); + reported(&mut pair, client_ch, s); pair.client_send(client_ch, s).write(b"payload").unwrap(); let checkpoint = pair.client_send(client_ch, s).checkpoint(); + assert_eq!(acked(&mut pair, client_ch, &checkpoint), Ok(false)); pair.client_send(client_ch, s).finish().unwrap(); pair.drive(); assert_eq!(pair.client_streams(client_ch).send_streams(), 0); - assert!(acked(&mut pair, client_ch, &checkpoint).unwrap().is_some()); - assert!(reported(&mut pair, client_ch, s)); + let mut events = Vec::new(); + while let Some(Event::Stream(event)) = pair.client_conn_mut(client_ch).poll() { + events.push(event); + } + let len = b"headerpayload".len() as u64; + assert_eq!( + events, + [ + StreamEvent::Acked { id: s, offset: len }, + StreamEvent::Finished { id: s } + ] + ); + assert_eq!( + acked(&mut pair, client_ch, &checkpoint), + Err(ClosedStream::default()) + ); } -/// A reset fails the checkpoints past its Reliable Size at once, while those before it still -/// resolve once the prefix arrives. +/// A reset fails the checkpoints past its Reliable Size at once, while those before it are +/// still acknowledged once the prefix arrives. #[test] fn reset_fails_past_reliable_size() { let _guard = subscribe(); @@ -282,15 +179,19 @@ fn reset_fails_past_reliable_size() { acked(&mut pair, client_ch, &dropped), Err(ClosedStream::default()) ); - assert_eq!(acked(&mut pair, client_ch, &kept), Ok(None)); - assert!(reported(&mut pair, client_ch, s)); + assert_eq!(acked(&mut pair, client_ch, &kept), Ok(false)); + // The retransmitted prefix is acknowledged before the reset frees the stream pair.drive(); assert_eq!(pair.client_streams(client_ch).send_streams(), 0); - assert!(acked(&mut pair, client_ch, &kept).unwrap().is_some()); + assert_eq!( + reported(&mut pair, client_ch, s), + Some(b"headerkept".len() as u64) + ); } -/// A peer that stopped reading will not read what it acknowledges. +/// A peer that stopped reading will not read what it acknowledges, before or after the +/// checkpoint was taken. #[test] fn stop_sending_fails() { let _guard = subscribe(); @@ -299,65 +200,24 @@ fn stop_sending_fails() { let s = open(&mut pair, client_ch, server_ch); pair.client_send(client_ch, s).write(b"unread").unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); + let before = pair.client_send(client_ch, s).checkpoint(); lose_client_packets(&mut pair); - reported(&mut pair, client_ch, s); - - pair.server_recv(server_ch, s).stop(VarInt(7)).unwrap(); - pair.drive(); - assert_eq!( - acked(&mut pair, client_ch, &checkpoint), - Err(ClosedStream::default()) - ); - assert!(reported(&mut pair, client_ch, s)); -} - -/// A checkpoint taken after the peer stopped reading fails at once, though data in flight is -/// still acknowledged. -#[test] -fn checkpoint_after_stop_sending_fails() { - let _guard = subscribe(); - let mut pair = pair(true); - let (client_ch, server_ch) = pair.connect(); - let s = open(&mut pair, client_ch, server_ch); + assert_eq!(acked(&mut pair, client_ch, &before), Ok(false)); - pair.client_send(client_ch, s).write(b"in flight").unwrap(); pair.server_recv(server_ch, s).stop(VarInt(7)).unwrap(); pair.drive(); assert_eq!( pair.client_send(client_ch, s).stopped(), Ok(Some(VarInt(7))) ); - - let checkpoint = pair.client_send(client_ch, s).checkpoint(); - pair.drive(); - assert_eq!( - acked(&mut pair, client_ch, &checkpoint), - Err(ClosedStream::default()) - ); -} - -/// A checkpoint taken after a reset fails at once, though the reset still delivers its prefix. -#[test] -fn checkpoint_after_reset_fails() { - let _guard = subscribe(); - let mut pair = pair(true); - let (client_ch, server_ch) = pair.connect(); - let s = open(&mut pair, client_ch, server_ch); - - pair.client_send(client_ch, s).write(b"prefix").unwrap(); - let reset = pair.client_send(client_ch, s).checkpoint(); - lose_client_packets(&mut pair); - reset - .reset(pair.client_conn_mut(client_ch), VarInt(42)) - .unwrap(); - - let checkpoint = pair.client_send(client_ch, s).checkpoint(); - pair.drive(); - assert_eq!( - acked(&mut pair, client_ch, &checkpoint), - Err(ClosedStream::default()) - ); + let after = pair.client_send(client_ch, s).checkpoint(); + assert_eq!(reported(&mut pair, client_ch, s), None); + for checkpoint in [&before, &after] { + assert_eq!( + acked(&mut pair, client_ch, checkpoint), + Err(ClosedStream::default()) + ); + } } /// Nothing is acknowledged on a closed connection. From e2a4a02e5fe50bcfe55f87d656d8b2326c09c613 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:19:06 -0700 Subject: [PATCH 15/19] refactor(quic)!: replace Checkpoint with byte offsets on SendStream `SendStream::offset()` returns how much was written, `SendStream::acked(offset)` reports whether the peer acknowledged everything before it, and `SendStream::reset_at(offset, code)` replaces `Checkpoint::reset`. An offset past the written data panics. moq-tokio's `Checkpoint` wrapper becomes `SendStream::reset_at`, and both WebTransport adapters keep their header's end as a `u64`. The 0-RTT generation guard goes with `Checkpoint`: every other stream op is already keyed by a bare id, and the adapters check 0-RTT rejection themselves. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013bQzzUiJPvi9kFCtMqbtqC --- quest/m1/quic/group-reset.md | 13 +- quest/m2/quic-ack-hook.md | 20 +- rs/moq-quic/MIGRATE.md | 4 +- rs/moq-quic/README.md | 4 +- rs/moq-quic/fuzz/README.md | 2 +- rs/moq-quic/src/connection/mod.rs | 5 +- rs/moq-quic/src/connection/streams/mod.rs | 156 +++++-------- .../src/connection/streams/saturated.rs | 4 +- rs/moq-quic/src/connection/streams/send.rs | 7 +- rs/moq-quic/src/connection/streams/state.rs | 17 +- rs/moq-quic/src/fuzz/connection.rs | 59 ++--- rs/moq-quic/src/lib.rs | 8 +- rs/moq-quic/src/tests/acked.rs | 99 +++++---- rs/moq-quic/src/tests/reset_stream_at.rs | 208 +++++++----------- rs/moq-tokio/src/quic/driver/mod.rs | 2 +- rs/moq-tokio/src/quic/driver/send_stream.rs | 54 ++--- rs/moq-tokio/src/quic/web/send.rs | 12 +- rs/moq-tokio/src/quic/web/session.rs | 4 +- rs/moq-uring/src/quic/driver/connection.rs | 2 +- rs/moq-uring/src/quic/driver/stream.rs | 16 +- rs/moq-uring/src/quic/web.rs | 8 +- 21 files changed, 307 insertions(+), 397 deletions(-) diff --git a/quest/m1/quic/group-reset.md b/quest/m1/quic/group-reset.md index d73b340a423..2754f19bdea 100644 --- a/quest/m1/quic/group-reset.md +++ b/quest/m1/quic/group-reset.md @@ -14,9 +14,8 @@ browser, keep the grace. Split from the transport half, which landed in `moq-quic`, `moq_tokio::quic::web`, and moq-uring's WebTransport adapter (#5690). There, -`SendStream::checkpoint()` marks the data written so far and -`Checkpoint::reset(code)` sends `RESET_STREAM_AT` with that offset as the -Reliable Size. A peer without the extension instead gets a plain +`SendStream::reset_at(offset, code)` sends `RESET_STREAM_AT` with that +offset as the Reliable Size. A peer without the extension instead gets a plain `RESET_STREAM` once it has acknowledged the data before the checkpoint, a round trip later. The WebTransport adapters already keep their own stream header this way. @@ -29,9 +28,11 @@ read by the application, so check whether Chrome hands it to JS when a plain `RESET_STREAM` follows the ACK, and treat a peer without `reset_stream_at` as unable to keep the header unless it does. -- Give moq-net's `transport::poll::SendStream` a reset that keeps a - checkpoint, using `Checkpoint` rather than a byte count, so a size past the - written data cannot be asked for. Implement it in the moq-tokio and +- Give moq-net's `transport::poll::SendStream` a reset that keeps the first + `offset` bytes, matching `moq-quic`'s `reset_at(offset, code)`. Decided + 2026-10-17 on #5817: a byte offset rather than a `Checkpoint` handle; an + offset past the written data panics instead of being unrepresentable. + Implement it in the moq-tokio and moq-uring adapters. Backends that cannot do it (browser, qmux, iroh) report that rather than silently sending a plain reset. - The lite publisher checkpoints right after writing a group header and diff --git a/quest/m2/quic-ack-hook.md b/quest/m2/quic-ack-hook.md index 4d60107d9c5..9ca0cba8a9a 100644 --- a/quest/m2/quic-ack-hook.md +++ b/quest/m2/quic-ack-hook.md @@ -39,17 +39,15 @@ unsupported has to live in the return type rather than the error, and a consumer must treat `None` as unknown, never as delivered. Implement it in the moq-tokio and moq-uring adapters over `moq-quic`'s -`Checkpoint::acked`, a plain stream id and offset that returns whether -everything before it is acknowledged and fails once it never will be. A -`false` asks `moq-quic` to report `StreamEvent::Acked { id, offset }` when -the stream's acknowledged prefix reaches it, so the adapter wakes waiters at -or below that offset; moq-uring's `sweep_stream` currently ignores the event. -`acked` fails once the stream is freed, after `StreamEvent::Finished`, which -means everything was acknowledged, so the adapter answers from that. A -checkpoint is taken at the current write offset, so the adapter checkpoints -each offset as it is written rather than naming an arbitrary one later; -settle whether the trait method takes an offset or returns a handle from a -`checkpoint()` like the reset one. Leave the browser transport on the default; the browser's `WebTransportSendStream.getStats()` is +`SendStream::acked(offset)`, which returns whether every byte before the +offset is acknowledged and fails once it never will be. A `false` asks +`moq-quic` to report `StreamEvent::Acked { id, offset }` when the stream's +acknowledged prefix reaches it, so the adapter wakes waiters at or below that +offset; moq-uring's `sweep_stream` currently ignores the event. `acked` fails +once the stream is freed, after `StreamEvent::Finished`, which means +everything was acknowledged, so the adapter answers from that. The trait +method takes a byte offset, like `moq-quic` (decided 2026-10-17 on #5817); +the starvation quest's writer counts the bytes it writes. Leave the browser transport on the default; the browser's `WebTransportSendStream.getStats()` is unimplemented in shipping Chrome and its `bytesAcknowledged` is at risk in the W3C draft. qmux over a reliable transport may treat serialization as acknowledgment only if the qmux quest decides that is honest; until then it diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index 08d7494c4a2..edbeb49dee4 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -6,12 +6,12 @@ How to update your code for each breaking change, newest first. ### `StreamEvent::Acked` -`StreamEvent` gained `Acked { id, offset }`, reported when a send stream's acknowledged prefix reaches a `Checkpoint` that `Checkpoint::acked` was polled on. +`StreamEvent` gained `Acked { id, offset }`, reported when a send stream's acknowledged prefix reaches an offset that `SendStream::acked` was polled on. An exhaustive `match` needs an arm for it. ```rust // After -StreamEvent::Acked { id, offset } => wake_checkpoint_waiters(id, offset), +StreamEvent::Acked { id, offset } => wake_ack_waiters(id, offset), ``` ### `Datagrams::send` takes a send group and order (#5770) diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index a9a60eb4484..2a13c3e1376 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -69,8 +69,8 @@ Changes on top of the upstream commit, besides the renames: - **Instants on [moq-clock](../moq-clock)**: the crate's `Instant` is `moq_clock::Instant`, so one clock steps every timer, tokio's paused clock and the simulator included. qlog stamps events relative to `QlogConfig::start_time` on that clock rather than the streamer's own `std::time::Instant`. - **Send groups** (`streams/schedule.rs`, `Connection::send_groups`): streams outside every group go first, in quinn's order; groups in one scheduling domain are served strictly by priority, with equal priorities sharing bytes by deficit round robin; domains share bytes the same way. A stream's priority is an `i64` ordering it only within its group. This replaces quinn's single pending-stream heap, so a cherry-pick touching `PendingStreamsQueue` has to be redone against the schedule. moq-tokio's `quic::driver` and moq-uring's driver wrap a group in a shared `SendGroup` handle that closes on drop. - **Scheduled datagrams** (`Datagrams::send`): a datagram is queued in a send group (or ungrouped) at an `i64` order and scheduled with the group's streams, winning a tie with a stream of equal order and spending the group's byte credit, instead of quinn's datagram-first FIFO in `populate_packet`. Sending never blocks: `datagram_send_buffer_size` is shared, and an overflow evicts the oldest datagram of the lowest-priority group in the domain holding the most datagram bytes. `SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone, and a cherry-pick touching the `// DATAGRAM` loop has to be redone against `StreamsState::write_frames`. -- **`RESET_STREAM_AT`** ([draft-ietf-quic-reliable-stream-reset](https://datatracker.ietf.org/doc/draft-ietf-quic-reliable-stream-reset/)): the `reset_stream_at` transport parameter is always advertised, `Checkpoint::reset` (from `SendStream::checkpoint`) keeps the data written before the checkpoint reliable, holding a plain `RESET_STREAM` until that data is acknowledged when the peer lacks the extension, and a received reset surfaces only once the reliable prefix has been read. Stopping a stream after it was reset no longer credits its unread data twice. -- **ACK checkpoints** (`Checkpoint::acked`, `StreamEvent::Acked`): a checkpoint reports whether the peer acknowledged every byte before it, by comparing against the stream's contiguous acknowledged prefix. A `false` stores the lowest polled offset on the stream, and the ACK that advances the prefix past it reports `StreamEvent::Acked` with the new prefix, so a cherry-pick touching `StreamsState::received_ack_of` has to keep that report. Not offered upstream: quinn has no API for when sent data was acknowledged. +- **`RESET_STREAM_AT`** ([draft-ietf-quic-reliable-stream-reset](https://datatracker.ietf.org/doc/draft-ietf-quic-reliable-stream-reset/)): the `reset_stream_at` transport parameter is always advertised, `SendStream::reset_at` keeps the data written before an offset reliable, holding a plain `RESET_STREAM` until that data is acknowledged when the peer lacks the extension, and a received reset surfaces only once the reliable prefix has been read. Stopping a stream after it was reset no longer credits its unread data twice. +- **ACK progress** (`SendStream::offset`, `SendStream::acked`, `StreamEvent::Acked`): `acked` reports whether the peer acknowledged every byte before an offset, by comparing against the stream's contiguous acknowledged prefix. A `false` stores the lowest polled offset on the stream, and the ACK that advances the prefix past it reports `StreamEvent::Acked` with the new prefix, so a cherry-pick touching `StreamsState::received_ack_of` has to keep that report. Not offered upstream: quinn has no API for when sent data was acknowledged. - **Fuzzing** runs through the hidden `fuzz` module behind a `fuzz` feature, with one-line shims in [`fuzz/`](fuzz/README.md): upstream's `packet`, `params`, `streamid`, and `streams` targets, plus `frame` and a connection op sequence. Upstream's `cfg(fuzzing)` `fuzzing` module and `arbitrary` feature are gone, so its fuzz patches port by hand. - **Draining endpoint event**: `EndpointEvent::is_draining` fires once per connection when it stops being active, before the drained event, so the driver's `Endpoint::wait_all_draining` can end a shutdown without sitting out every closing period. - **Late stream limits**: peer transport parameters that raise a stream limit after an open was refused emit `StreamEvent::Available`, as `MAX_STREAMS` does. A server accepted before the rest of a multi-packet ClientHello (a post-quantum key share is enough) otherwise leaves a 0.5-RTT `open_uni` parked until the idle timeout. Not offered upstream yet. diff --git a/rs/moq-quic/fuzz/README.md b/rs/moq-quic/fuzz/README.md index 0b5e608809a..a24c0489b21 100644 --- a/rs/moq-quic/fuzz/README.md +++ b/rs/moq-quic/fuzz/README.md @@ -48,7 +48,7 @@ symbol stripping. Prefix direct calls with - A stream ID keeps the initiator, direction, and index it was built from. - ACK ranges walk downward without overlapping. - `connection` handshakes a client and server over TLS, then applies opens, accepts, - writes, finishes, resets (plain and from a checkpoint), stops, reads that hold or + writes, finishes, resets (plain and at an offset), acknowledgment queries, stops, reads that hold or release credit, runtime limit and window changes, datagrams, key updates, and time jumps, while dropping, duplicating, reordering, corrupting, and injecting datagrams. Every stream read is in order, never past what was written, byte-for-byte what was diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index 8b06a3e3c45..5f9da9d8e52 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -93,9 +93,8 @@ pub use stats::{ConnectionStats, CreditStats, FrameStats, PathStats, UdpStats}; mod streams; pub(crate) use streams::StreamsState; pub use streams::{ - Checkpoint, Chunks, ClosedStream, Credit, FinishError, ReadError, ReadableError, RecvStream, - SendDomain, SendGroup, SendGroups, SendStream, ShouldTransmit, StreamEvent, Streams, - WriteError, Written, + Chunks, ClosedStream, Credit, FinishError, ReadError, ReadableError, RecvStream, SendDomain, + SendGroup, SendGroups, SendStream, ShouldTransmit, StreamEvent, Streams, WriteError, Written, }; mod timer; diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index e645d37e974..c2246c879fb 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -350,30 +350,68 @@ impl<'a> SendStream<'a> { /// # Panics /// - when applied to a receive stream pub fn reset(&mut self, error_code: VarInt) -> Result<(), ClosedStream> { - self.reset_to(error_code, 0) + self.reset_at(0, error_code) } - /// Mark the data written so far, to learn when the peer acknowledges it with - /// [`Checkpoint::acked`], or to still deliver it with a later [`Checkpoint::reset`] + /// How many bytes were written to the stream so far, the offset the next write starts at + /// + /// Zero before the first write, or once the stream is gone. /// /// # Panics /// - when applied to a receive stream - pub fn checkpoint(&self) -> Checkpoint { - let offset = match self.state.send.get(&self.id) { + pub fn offset(&self) -> u64 { + match self.state.send.get(&self.id) { Some(Some(stream)) => stream.offset(), - // Not written yet, or already closed, where the reset fails anyway _ => 0, - }; - Checkpoint { - id: self.id, - tag: self.state.tag, - generation: self.state.generation, - offset, } } - /// Reset the stream, still delivering its first `reliable_size` bytes - fn reset_to(&mut self, error_code: VarInt, reliable_size: u64) -> Result<(), ClosedStream> { + /// Whether the peer acknowledged every byte before `offset` + /// + /// A `false` notes to report a [`StreamEvent::Acked`] once the stream's contiguous + /// acknowledged prefix reaches `offset`, so poll again then. One event answers every offset + /// at or below the one it carries. Like a [`StreamEvent::Finished`] stream, an + /// acknowledgment only means the peer's QUIC stack holds the data, not that its application + /// read it. + /// + /// Fails with [`ClosedStream`] once the peer will never acknowledge it, or no longer reads + /// it: a reset dropped the data before `offset`, the peer sent STOP_SENDING, or the + /// connection closed. It also fails once the stream is freed, after its + /// [`StreamEvent::Finished`], which means every byte was acknowledged. + /// + /// # Panics + /// - when applied to a receive stream + /// - when `offset` is past the data written to a stream that was not reset + pub fn acked(&mut self, offset: u64) -> Result { + if self.conn_state.is_closed() { + return Err(ClosedStream { _private: () }); + } + match self.state.send.get_mut(&self.id) { + Some(Some(stream)) => stream.acked(offset), + Some(None) => { + assert_eq!(offset, 0, "acked past the data written"); + Ok(true) + } + None => Err(ClosedStream { _private: () }), + } + } + + /// Abandon the stream, but still deliver its first `offset` bytes + /// + /// Nothing past `offset` is sent or retransmitted. A peer that accepts RESET_STREAM_AT gets + /// it at once, and reads the data before the error. Otherwise the data keeps being + /// retransmitted, and a plain RESET_STREAM follows once the peer has acknowledged all of it, a + /// round trip later. An acknowledgment only means the peer's QUIC stack holds the data: + /// RFC 9000 lets it discard data its application has not read yet on RESET_STREAM, so this + /// only reliably delivers data the peer parses on arrival, like a WebTransport stream header. + /// A peer that asked us to stop sending gets the reset at once, without the data. + /// + /// Fails with [`ClosedStream`] if the stream was already reset or is gone. + /// + /// # Panics + /// - when applied to a receive stream + /// - when `offset` is past the data written + pub fn reset_at(&mut self, offset: u64, error_code: VarInt) -> Result<(), ClosedStream> { let max_send_data = self.state.max_send_data(self.id); let at = self.state.peer_reset_stream_at; let stream = self @@ -387,12 +425,11 @@ impl<'a> SendStream<'a> { // Redundant reset call return Err(ClosedStream { _private: () }); } - // A checkpoint names its stream's connection and generation, so it never runs past the data - debug_assert!(reliable_size <= stream.offset()); + assert!(offset <= stream.offset(), "reset past the data written"); // The peer that sent STOP_SENDING will not read the prefix let reliable_size = match stream.stop_reason { Some(_) => 0, - None => reliable_size, + None => offset, }; // Restore the portion of the send window consumed by the data that we aren't about to @@ -472,84 +509,6 @@ impl<'a> SendStream<'a> { } } -/// The data written to a send stream up to some point, which the peer acknowledges and a reset can -/// still deliver -/// -/// Taken with [`SendStream::checkpoint`], and only valid on the connection it came from. -#[derive(Debug)] -pub struct Checkpoint { - id: StreamId, - tag: ConnectionTag, - generation: u64, - offset: u64, -} - -impl Checkpoint { - /// Whether the peer acknowledged everything written before the checkpoint - /// - /// A `false` notes to report a [`StreamEvent::Acked`] once the stream's contiguous - /// acknowledged prefix reaches the checkpoint, so poll again then. One event answers every - /// checkpoint at or below the offset it carries. Like a [`StreamEvent::Finished`] stream, an - /// acknowledgment only means the peer's QUIC stack holds the data, not that its application - /// read it. - /// - /// Fails with [`ClosedStream`] once the peer will never acknowledge it, or no longer reads - /// it: a reset dropped the checkpoint's data, the peer sent STOP_SENDING, rejected 0-RTT - /// discarded the stream, or the connection closed. It also fails once the stream is freed, - /// after its [`StreamEvent::Finished`], which means every checkpoint on it was acknowledged. - /// - /// # Panics - /// If the checkpoint came from another connection. - pub fn acked(&self, conn: &mut crate::Connection) -> Result { - assert!( - self.tag == conn.streams.tag, - "checkpoint used on another connection" - ); - // Rejected 0-RTT discarded the stream, and a new one may have its id - if conn.is_closed() || self.generation != conn.streams.generation { - return Err(ClosedStream { _private: () }); - } - match conn.streams.send.get_mut(&self.id) { - Some(Some(stream)) => stream.acked(self.offset), - // Nothing was written yet, so nothing is left to acknowledge - Some(None) => Ok(true), - None => Err(ClosedStream { _private: () }), - } - } - - /// Abandon the stream, but still deliver everything written before the checkpoint - /// - /// Nothing past the checkpoint is sent or retransmitted. A peer that accepts RESET_STREAM_AT - /// gets it at once, and reads the data before the error. Otherwise the data keeps being - /// retransmitted, and a plain RESET_STREAM follows once the peer has acknowledged all of it, a - /// round trip later. An acknowledgment only means the peer's QUIC stack holds the data: - /// RFC 9000 lets it discard data its application has not read yet on RESET_STREAM, so this - /// only reliably delivers data the peer parses on arrival, like a WebTransport stream header. - /// A peer that asked us to stop sending gets the reset at once, without the data. - /// - /// Fails with [`ClosedStream`] if the stream was already reset or is gone, including one - /// rejected 0-RTT discarded, even if a new stream has since taken its id. - /// - /// # Panics - /// If the checkpoint came from another connection. - pub fn reset( - self, - conn: &mut crate::Connection, - error_code: VarInt, - ) -> Result<(), ClosedStream> { - let mut stream = conn.send_stream(self.id); - assert!( - self.tag == stream.state.tag, - "checkpoint used on another connection" - ); - // Rejected 0-RTT discarded the stream, and a new one may have its id - if self.generation != stream.state.generation { - return Err(ClosedStream { _private: () }); - } - stream.reset_to(error_code, self.offset) - } -} - /// Scheduling of outgoing streams between send groups /// /// A send group shares bytes with the other groups of its domain: a higher priority group @@ -723,11 +682,12 @@ pub enum StreamEvent { /// Which stream has been finished id: StreamId, }, - /// A send stream's contiguous acknowledged prefix reached a [`Checkpoint`] being waited on + /// A send stream's contiguous acknowledged prefix reached an offset [`SendStream::acked`] is + /// waiting for /// /// Reported before a [`StreamEvent::Finished`] from the same ACK. Not reported after - /// STOP_SENDING, a reset that drops the checkpoint's data, or the connection closing, which - /// fail [`Checkpoint::acked`] instead. + /// STOP_SENDING, a reset that drops the data, or the connection closing, which fail + /// [`SendStream::acked`] instead. Acked { /// Which stream was acknowledged id: StreamId, diff --git a/rs/moq-quic/src/connection/streams/saturated.rs b/rs/moq-quic/src/connection/streams/saturated.rs index 0ae52976672..2bbe5b804e1 100644 --- a/rs/moq-quic/src/connection/streams/saturated.rs +++ b/rs/moq-quic/src/connection/streams/saturated.rs @@ -526,7 +526,7 @@ fn reliable_reset_sends_its_prefix_then_frees_its_group() { sender.send(finished).finish().unwrap(); sender.groups().close(group); - sender.send(reset).reset_to(0u32.into(), 3_000).unwrap(); + sender.send(reset).reset_at(3_000, 0u32.into()).unwrap(); assert!(sender.state.can_send_stream_data()); sender.drain(100); assert_eq!(sender.bytes(&[reset]), 3_000, "only the reliable prefix"); @@ -558,7 +558,7 @@ fn stop_sending_drops_a_reliable_prefix_from_the_schedule() { let mut sender = Sender::new(); let group = sender.group(0); let reset = sender.open(Some(&group), 0, 10_000); - sender.send(reset).reset_to(0u32.into(), 3_000).unwrap(); + sender.send(reset).reset_at(3_000, 0u32.into()).unwrap(); assert!(sender.state.can_send_stream_data()); assert_eq!( diff --git a/rs/moq-quic/src/connection/streams/send.rs b/rs/moq-quic/src/connection/streams/send.rs index cff5114de8e..b388cc64d83 100644 --- a/rs/moq-quic/src/connection/streams/send.rs +++ b/rs/moq-quic/src/connection/streams/send.rs @@ -21,7 +21,7 @@ pub(super) struct Send { pub(super) data_blocked_limit: Option, /// The reason the peer wants us to stop, if `STOP_SENDING` was received pub(super) stop_reason: Option, - /// The lowest offset a [`Checkpoint::acked`](super::Checkpoint::acked) call is waiting for + /// The lowest offset a [`SendStream::acked`](super::SendStream::acked) call is waiting for pub(super) notify: Option, } @@ -248,6 +248,11 @@ impl Send { /// Whether the peer acknowledged everything before `offset`, noting to report it if not pub(super) fn acked(&mut self, offset: u64) -> Result { + // A reset cuts the buffer short, so only an unreset stream knows how much was written + assert!( + self.is_reset() || offset <= self.pending.offset(), + "acked past the data written" + ); // An acknowledgment no longer means the peer reads the data, and a reset dropped the rest if self.stop_reason.is_some() || offset > self.pending.offset() { return Err(ClosedStream::default()); diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index 77b551d81e4..14284869544 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -151,9 +151,6 @@ pub(crate) struct StreamsState { initial_max_stream_data_bidi_remote: VarInt, /// Whether the peer accepts RESET_STREAM_AT pub(super) peer_reset_stream_at: bool, - /// Bumped when rejected 0-RTT discards our streams and their ids are opened again, so a - /// [`Checkpoint`](super::Checkpoint) from before cannot reach the new stream of the same id - pub(super) generation: u64, /// The shrink to be applied to local_max_data when receive_window is shrunk receive_window_shrink_debt: u64, @@ -210,7 +207,6 @@ impl StreamsState { initial_max_stream_data_bidi_local: 0u32.into(), initial_max_stream_data_bidi_remote: 0u32.into(), peer_reset_stream_at: false, - generation: 0, receive_window_shrink_debt: 0, streams_blocked: [false, false], open_refused: [false, false], @@ -278,7 +274,6 @@ impl StreamsState { } } - self.generation += 1; self.send_streams = 0; self.data_sent = 0; self.buffered_data = 0; @@ -2921,7 +2916,7 @@ mod tests { stream.write(b"hello world").unwrap(); let mut buf = Vec::new(); let sent = stream.state.write_frames(&mut buf, 0..100, true).streams; - stream.reset_to(7u32.into(), 4).unwrap(); + stream.reset_at(4, 7u32.into()).unwrap(); assert!(pending.reset_stream.is_empty()); assert_eq!(server.buffered_data, 4); @@ -2963,7 +2958,7 @@ mod tests { conn_state: &state, }; stream.write(b"hello world").unwrap(); - stream.reset_to(7u32.into(), 4).unwrap(); + stream.reset_at(4, 7u32.into()).unwrap(); assert_eq!(pending.reset_stream, &[(id, 4)]); assert_eq!(server.received_stop_sending(id, 9u32.into()), Some(0)); @@ -2994,7 +2989,7 @@ mod tests { stream.write(b"hello world").unwrap(); let mut buf = Vec::new(); let sent = stream.state.write_frames(&mut buf, 0..100, true).streams; - stream.reset_to(7u32.into(), 4).unwrap(); + stream.reset_at(4, 7u32.into()).unwrap(); server.reset_acked(id, 4); assert_eq!(server.received_stop_sending(id, 9u32.into()), Some(0)); @@ -3035,7 +3030,7 @@ mod tests { conn_state: &state, }; stream.write(b"hello world").unwrap(); - stream.reset_to(7u32.into(), 4).unwrap(); + stream.reset_at(4, 7u32.into()).unwrap(); assert_eq!(pending.reset_stream.is_empty(), !reset_stream_at); assert_eq!(server.received_stop_sending(id, 9u32.into()), Some(0)); @@ -3071,7 +3066,7 @@ mod tests { let mut buf = Vec::new(); let sent = stream.state.write_frames(&mut buf, 0..100, true).streams; assert_eq!(sent[0].offsets, 0..11); - stream.reset_to(7u32.into(), 4).unwrap(); + stream.reset_at(4, 7u32.into()).unwrap(); assert_eq!(server.buffered_data, 4); // Only the lost prefix is retransmitted, without the FIN of a stream that was never finished @@ -3110,7 +3105,7 @@ mod tests { }; stream.write(b"hello world").unwrap(); stream.state.received_stop_sending(id, 9u32.into()); - stream.reset_to(7u32.into(), 4).unwrap(); + stream.reset_at(4, 7u32.into()).unwrap(); let send = server.send[&id].as_ref().unwrap(); let frame = send.reset_frame(id, true).unwrap(); diff --git a/rs/moq-quic/src/fuzz/connection.rs b/rs/moq-quic/src/fuzz/connection.rs index de0e900f64c..0fb5dc66e3a 100644 --- a/rs/moq-quic/src/fuzz/connection.rs +++ b/rs/moq-quic/src/fuzz/connection.rs @@ -69,7 +69,7 @@ enum Who { Server, } -/// One step of a run. Stream and checkpoint indexes wrap around what that side has. +/// One step of a run. Stream indexes wrap around what that side has. #[derive(Arbitrary, Debug)] enum Op { Open { @@ -94,18 +94,18 @@ enum Op { stream: u8, code: u32, }, - Checkpoint { + /// Reset a stream, still delivering a prefix of what was written + ResetAt { who: Who, stream: u8, - }, - ResetCheckpoint { - who: Who, - checkpoint: u8, + offset: u16, code: u32, }, - AckedCheckpoint { + /// Ask whether a prefix of what was written is acknowledged + Acked { who: Who, - checkpoint: u8, + stream: u8, + offset: u16, }, Priority { who: Who, @@ -202,7 +202,6 @@ struct Peer { recv: Vec, /// How far each stream has been read, in order. read: HashMap, - checkpoints: Vec, held: Vec, /// The length of each datagram this side sent, indexed by the sequence number it carries. datagrams_sent: Vec, @@ -227,7 +226,6 @@ impl Peer { send: Vec::new(), recv: Vec::new(), read: HashMap::new(), - checkpoints: Vec::new(), held: Vec::new(), datagrams_sent: Vec::new(), datagrams_received: HashSet::new(), @@ -482,36 +480,27 @@ impl Pair { let _ = conn.send_stream(id).reset(code.into()); } } - Op::Checkpoint { who, stream } => { - let peer = self.peer(who); - if let Some((id, conn)) = send_stream(peer, stream) { - let checkpoint = conn.send_stream(id).checkpoint(); - peer.checkpoints.push(checkpoint); - } - } - Op::ResetCheckpoint { + Op::ResetAt { who, - checkpoint, + stream, + offset, code, } => { - let peer = self.peer(who); - if peer.checkpoints.is_empty() { - return; - } - let index = checkpoint as usize % peer.checkpoints.len(); - let checkpoint = peer.checkpoints.swap_remove(index); - if let Some(conn) = peer.conn() { - let _ = checkpoint.reset(conn, code.into()); + if let Some((id, conn)) = send_stream(self.peer(who), stream) { + let mut stream = conn.send_stream(id); + let offset = u64::from(offset) % (stream.offset() + 1); + let _ = stream.reset_at(offset, code.into()); } } - Op::AckedCheckpoint { who, checkpoint } => { - let peer = self.peer(who); - if peer.checkpoints.is_empty() { - return; - } - let index = checkpoint as usize % peer.checkpoints.len(); - if let Some((_, conn)) = peer.conn.as_mut() { - let _ = peer.checkpoints[index].acked(conn); + Op::Acked { + who, + stream, + offset, + } => { + if let Some((id, conn)) = send_stream(self.peer(who), stream) { + let mut stream = conn.send_stream(id); + let offset = u64::from(offset) % (stream.offset() + 1); + let _ = stream.acked(offset); } } Op::Priority { diff --git a/rs/moq-quic/src/lib.rs b/rs/moq-quic/src/lib.rs index 1bbf7520997..43a025363dc 100644 --- a/rs/moq-quic/src/lib.rs +++ b/rs/moq-quic/src/lib.rs @@ -53,10 +53,10 @@ pub use bloom_token_log::BloomTokenLog; mod connection; pub use crate::connection::{ - Checkpoint, Chunk, Chunks, ClosedStream, Connection, ConnectionError, ConnectionStats, Credit, - CreditStats, Datagrams, Event, FinishError, FrameStats, PathStats, ReadError, ReadableError, - RecvStream, RttEstimator, SendDatagramError, SendDomain, SendGroup, SendGroups, SendStream, - ShouldTransmit, StreamEvent, Streams, UdpStats, WriteError, Written, + Chunk, Chunks, ClosedStream, Connection, ConnectionError, ConnectionStats, Credit, CreditStats, + Datagrams, Event, FinishError, FrameStats, PathStats, ReadError, ReadableError, RecvStream, + RttEstimator, SendDatagramError, SendDomain, SendGroup, SendGroups, SendStream, ShouldTransmit, + StreamEvent, Streams, UdpStats, WriteError, Written, }; #[cfg(feature = "qlog")] pub use connection::qlog::QlogStream; diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index a213a2ebc45..0a432a083d4 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -1,4 +1,4 @@ -//! Tests for [`Checkpoint::acked`], which reports whether the peer acknowledged a send stream's prefix +//! Tests for [`SendStream::acked`], which reports whether the peer acknowledged a send stream's prefix use super::*; @@ -27,9 +27,10 @@ fn open(pair: &mut Pair, client_ch: ConnectionHandle, server_ch: ConnectionHandl fn acked( pair: &mut Pair, ch: ConnectionHandle, - checkpoint: &Checkpoint, + s: StreamId, + offset: u64, ) -> Result { - checkpoint.acked(pair.client_conn_mut(ch)) + pair.client_send(ch, s).acked(offset) } /// The prefix the client last reported acknowledged on `s`, draining its events @@ -52,8 +53,8 @@ fn lose_client_packets(pair: &mut Pair) { pair.client.outbound.clear(); } -/// A checkpoint is acknowledged only once every byte before it is, and the event that reports it -/// fires only for a checkpoint someone polled. +/// An offset is acknowledged only once every byte before it is, and the event that reports it +/// fires only for an offset someone polled. #[test] fn waits_for_contiguous_prefix() { let _guard = subscribe(); @@ -64,17 +65,17 @@ fn waits_for_contiguous_prefix() { reported(&mut pair, client_ch, s); pair.client_send(client_ch, s).write(b"lost").unwrap(); - let lost = pair.client_send(client_ch, s).checkpoint(); + let lost = pair.client_send(client_ch, s).offset(); lose_client_packets(&mut pair); // Acknowledged, but behind the lost packet: the middle of the stream, not its prefix pair.client_send(client_ch, s).write(b"after").unwrap(); - let after = pair.client_send(client_ch, s).checkpoint(); + let after = pair.client_send(client_ch, s).offset(); let acks = pair.client_conn_mut(client_ch).stats().frame_rx.acks; while pair.client_conn_mut(client_ch).stats().frame_rx.acks == acks { assert!(pair.step(), "never acknowledged"); } - assert_eq!(acked(&mut pair, client_ch, &after), Ok(false)); + assert_eq!(acked(&mut pair, client_ch, s, after), Ok(false)); // The retransmission fills the gap, which acknowledges both at once pair.drive(); @@ -82,8 +83,8 @@ fn waits_for_contiguous_prefix() { reported(&mut pair, client_ch, s), Some(b"headerlostafter".len() as u64) ); - assert_eq!(acked(&mut pair, client_ch, &lost), Ok(true)); - assert_eq!(acked(&mut pair, client_ch, &after), Ok(true)); + assert_eq!(acked(&mut pair, client_ch, s, lost), Ok(true)); + assert_eq!(acked(&mut pair, client_ch, s, after), Ok(true)); assert!(pair.client_conn_mut(client_ch).stats().path.lost_packets > 0); } @@ -96,13 +97,13 @@ fn unpolled_is_not_reported() { let s = open(&mut pair, client_ch, server_ch); pair.client_send(client_ch, s).write(b"payload").unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); + let offset = pair.client_send(client_ch, s).offset(); pair.drive(); assert_eq!(reported(&mut pair, client_ch, s), None); - assert_eq!(acked(&mut pair, client_ch, &checkpoint), Ok(true)); + assert_eq!(acked(&mut pair, client_ch, s, offset), Ok(true)); } -/// A checkpoint taken before anything was written, or once everything was acknowledged, is +/// An offset taken before anything was written, or once everything was acknowledged, is /// acknowledged at once. #[test] fn already_acked() { @@ -110,16 +111,16 @@ fn already_acked() { let mut pair = pair(true); let (client_ch, _) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let empty = pair.client_send(client_ch, s).checkpoint(); - assert_eq!(acked(&mut pair, client_ch, &empty), Ok(true)); + let empty = pair.client_send(client_ch, s).offset(); + assert_eq!(acked(&mut pair, client_ch, s, empty), Ok(true)); pair.client_send(client_ch, s).write(b"payload").unwrap(); pair.drive(); - let all = pair.client_send(client_ch, s).checkpoint(); - assert_eq!(acked(&mut pair, client_ch, &all), Ok(true)); + let all = pair.client_send(client_ch, s).offset(); + assert_eq!(acked(&mut pair, client_ch, s, all), Ok(true)); } -/// A finished stream is freed by the ACK that completes it, which reports a waiting checkpoint +/// A finished stream is freed by the ACK that completes it, which reports a waiting offset /// before the stream finishes. #[test] fn finished_stream_reports_before_freed() { @@ -131,8 +132,8 @@ fn finished_stream_reports_before_freed() { reported(&mut pair, client_ch, s); pair.client_send(client_ch, s).write(b"payload").unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); - assert_eq!(acked(&mut pair, client_ch, &checkpoint), Ok(false)); + let offset = pair.client_send(client_ch, s).offset(); + assert_eq!(acked(&mut pair, client_ch, s, offset), Ok(false)); pair.client_send(client_ch, s).finish().unwrap(); pair.drive(); @@ -150,12 +151,12 @@ fn finished_stream_reports_before_freed() { ] ); assert_eq!( - acked(&mut pair, client_ch, &checkpoint), + acked(&mut pair, client_ch, s, offset), Err(ClosedStream::default()) ); } -/// A reset fails the checkpoints past its Reliable Size at once, while those before it are +/// A reset fails the offsets past its Reliable Size at once, while those before it are /// still acknowledged once the prefix arrives. #[test] fn reset_fails_past_reliable_size() { @@ -165,21 +166,21 @@ fn reset_fails_past_reliable_size() { let s = open(&mut pair, client_ch, server_ch); pair.client_send(client_ch, s).write(b"kept").unwrap(); - let kept = pair.client_send(client_ch, s).checkpoint(); - let reset = pair.client_send(client_ch, s).checkpoint(); + let kept = pair.client_send(client_ch, s).offset(); + let reset = pair.client_send(client_ch, s).offset(); pair.client_send(client_ch, s).write(b"dropped").unwrap(); - let dropped = pair.client_send(client_ch, s).checkpoint(); + let dropped = pair.client_send(client_ch, s).offset(); lose_client_packets(&mut pair); reported(&mut pair, client_ch, s); - reset - .reset(pair.client_conn_mut(client_ch), VarInt(42)) + pair.client_send(client_ch, s) + .reset_at(reset, VarInt(42)) .unwrap(); assert_eq!( - acked(&mut pair, client_ch, &dropped), + acked(&mut pair, client_ch, s, dropped), Err(ClosedStream::default()) ); - assert_eq!(acked(&mut pair, client_ch, &kept), Ok(false)); + assert_eq!(acked(&mut pair, client_ch, s, kept), Ok(false)); // The retransmitted prefix is acknowledged before the reset frees the stream pair.drive(); @@ -191,7 +192,7 @@ fn reset_fails_past_reliable_size() { } /// A peer that stopped reading will not read what it acknowledges, before or after the -/// checkpoint was taken. +/// offset was taken. #[test] fn stop_sending_fails() { let _guard = subscribe(); @@ -200,9 +201,9 @@ fn stop_sending_fails() { let s = open(&mut pair, client_ch, server_ch); pair.client_send(client_ch, s).write(b"unread").unwrap(); - let before = pair.client_send(client_ch, s).checkpoint(); + let before = pair.client_send(client_ch, s).offset(); lose_client_packets(&mut pair); - assert_eq!(acked(&mut pair, client_ch, &before), Ok(false)); + assert_eq!(acked(&mut pair, client_ch, s, before), Ok(false)); pair.server_recv(server_ch, s).stop(VarInt(7)).unwrap(); pair.drive(); @@ -210,11 +211,11 @@ fn stop_sending_fails() { pair.client_send(client_ch, s).stopped(), Ok(Some(VarInt(7))) ); - let after = pair.client_send(client_ch, s).checkpoint(); + let after = pair.client_send(client_ch, s).offset(); assert_eq!(reported(&mut pair, client_ch, s), None); - for checkpoint in [&before, &after] { + for offset in [before, after] { assert_eq!( - acked(&mut pair, client_ch, checkpoint), + acked(&mut pair, client_ch, s, offset), Err(ClosedStream::default()) ); } @@ -229,14 +230,38 @@ fn close_fails() { let s = open(&mut pair, client_ch, server_ch); pair.client_send(client_ch, s).write(b"unacked").unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); + let offset = pair.client_send(client_ch, s).offset(); lose_client_packets(&mut pair); let now = pair.time; pair.client_conn_mut(client_ch) .close(now, VarInt(0), Bytes::new()); assert_eq!( - acked(&mut pair, client_ch, &checkpoint), + acked(&mut pair, client_ch, s, offset), Err(ClosedStream::default()) ); } + +/// Asking about data never written is a caller bug. +#[test] +#[should_panic(expected = "acked past the data written")] +fn acked_past_written_panics() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + let past = pair.client_send(client_ch, s).offset() + 1; + let _ = acked(&mut pair, client_ch, s, past); +} + +/// Resetting past the data written is a caller bug. +#[test] +#[should_panic(expected = "reset past the data written")] +fn reset_past_written_panics() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + let past = pair.client_send(client_ch, s).offset() + 1; + let _ = pair.client_send(client_ch, s).reset_at(past, VarInt(42)); +} diff --git a/rs/moq-quic/src/tests/reset_stream_at.rs b/rs/moq-quic/src/tests/reset_stream_at.rs index 4144d3a4974..864937344ab 100644 --- a/rs/moq-quic/src/tests/reset_stream_at.rs +++ b/rs/moq-quic/src/tests/reset_stream_at.rs @@ -36,21 +36,12 @@ fn accept(pair: &mut Pair, ch: ConnectionHandle) -> StreamId { pair.server_streams(ch).accept(Dir::Uni).unwrap() } -/// Write `MSG` to a client stream, with a checkpoint after its first `RELIABLE` bytes -fn write(pair: &mut Pair, ch: ConnectionHandle, s: StreamId) -> Checkpoint { +/// Write `MSG` to a client stream, returning the offset after its first `RELIABLE` bytes +fn write(pair: &mut Pair, ch: ConnectionHandle, s: StreamId) -> u64 { pair.client_send(ch, s).write(&MSG[..RELIABLE]).unwrap(); - let checkpoint = pair.client_send(ch, s).checkpoint(); + let offset = pair.client_send(ch, s).offset(); pair.client_send(ch, s).write(&MSG[RELIABLE..]).unwrap(); - checkpoint -} - -/// Reset a client stream, still delivering what was written before `checkpoint` -fn reset( - pair: &mut Pair, - ch: ConnectionHandle, - checkpoint: Checkpoint, -) -> Result<(), ClosedStream> { - checkpoint.reset(pair.client_conn_mut(ch), ERROR) + offset } /// Drive the pair to idle while the server reads stream `s` as its data arrives, returning the @@ -110,12 +101,14 @@ fn stop_sending_abandons_prefix(reset_stream_at: bool) { pair.client_send(client_ch, s) .write(&MSG[1..RELIABLE]) .unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); + let offset = pair.client_send(client_ch, s).offset(); pair.client_send(client_ch, s) .write(&MSG[RELIABLE..]) .unwrap(); lose_client_packets(&mut pair); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.client.drive(pair.time, pair.server.addr); pair.client.outbound.clear(); let resets = resets_sent(&mut pair, client_ch); @@ -149,8 +142,10 @@ fn delivers_prefix_before_error() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); - reset(&mut pair, client_ch, checkpoint).unwrap(); + let offset = write(&mut pair, client_ch, s); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); @@ -178,8 +173,10 @@ fn delivers_unsent_prefix() { pair.client_send(client_ch, s) .write(&MSG[..RELIABLE]) .unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); - reset(&mut pair, client_ch, checkpoint).unwrap(); + let offset = pair.client_send(client_ch, s).offset(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); @@ -196,9 +193,11 @@ fn retransmits_lost_prefix() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); + let offset = write(&mut pair, client_ch, s); lose_client_packets(&mut pair); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); // The reset arrives first, and withholds the error until the prefix is retransmitted pair.drive_client(); @@ -230,9 +229,11 @@ fn retransmits_lost_reset() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); + let offset = write(&mut pair, client_ch, s); pair.drive(); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); lose_client_packets(&mut pair); pair.drive(); @@ -252,8 +253,10 @@ fn received_reset_after_prefix() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); - reset(&mut pair, client_ch, checkpoint).unwrap(); + let offset = write(&mut pair, client_ch, s); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); @@ -283,12 +286,14 @@ fn after_finish() { for reliable in [RELIABLE, MSG.len()] { let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let mut checkpoint = write(&mut pair, client_ch, s); + let mut offset = write(&mut pair, client_ch, s); if reliable == MSG.len() { - checkpoint = pair.client_send(client_ch, s).checkpoint(); + offset = pair.client_send(client_ch, s).offset(); } pair.client_send(client_ch, s).finish().unwrap(); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); @@ -315,7 +320,7 @@ fn flow_control_blocked() { pair.client_send(client_ch, s) .write(&MSG[..RELIABLE]) .unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); + let offset = pair.client_send(client_ch, s).offset(); assert_eq!( pair.client_send(client_ch, s).write(&MSG[RELIABLE..]), Ok(10 - RELIABLE) @@ -324,7 +329,9 @@ fn flow_control_blocked() { pair.client_send(client_ch, s).write(MSG), Err(WriteError::Blocked) ); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); @@ -336,7 +343,7 @@ fn flow_control_blocked() { } /// Without RESET_STREAM_AT, the plain RESET_STREAM waits until the peer has acknowledged the data -/// before the checkpoint, so the peer reads that data before the error. +/// before the offset, so the peer reads that data before the error. #[test] fn unsupported_holds_reset_until_acked() { let _guard = subscribe(); @@ -344,10 +351,12 @@ fn unsupported_holds_reset_until_acked() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); - reset(&mut pair, client_ch, checkpoint).unwrap(); + let offset = write(&mut pair, client_ch, s); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); - // The first flight carries only the data before the checkpoint + // The first flight carries only the data before the offset pair.drive_client(); assert_eq!(resets_sent(&mut pair, client_ch), 0); assert_eq!( @@ -366,9 +375,11 @@ fn unsupported_retransmits_lost_prefix() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); + let offset = write(&mut pair, client_ch, s); lose_client_packets(&mut pair); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); assert_eq!( drive_reading(&mut pair, server_ch, s), @@ -387,7 +398,7 @@ fn unsupported_resets_at_once_when_acked() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); + let offset = write(&mut pair, client_ch, s); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); assert_eq!( @@ -395,7 +406,9 @@ fn unsupported_resets_at_once_when_acked() { (MSG.to_vec(), Err(ReadError::Blocked)) ); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.drive_client(); assert_eq!(resets_sent(&mut pair, client_ch), 1); pair.drive(); @@ -413,9 +426,11 @@ fn unsupported_after_finish() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); + let offset = write(&mut pair, client_ch, s); pair.client_send(client_ch, s).finish().unwrap(); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); assert_eq!( pair.client_send(client_ch, s).finish(), Err(FinishError::ClosedStream) @@ -446,12 +461,14 @@ fn unsupported_repeated_reset_is_closed() { let (client_ch, _) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); - let again = pair.client_send(client_ch, s).checkpoint(); + let offset = write(&mut pair, client_ch, s); + let again = pair.client_send(client_ch, s).offset(); lose_client_packets(&mut pair); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); assert_eq!( - reset(&mut pair, client_ch, again), + pair.client_send(client_ch, s).reset_at(again, ERROR), Err(ClosedStream::default()) ); assert!(pair.client_send(client_ch, s).reset(ERROR).is_err()); @@ -465,9 +482,11 @@ fn unsupported_close_while_held() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); + let offset = write(&mut pair, client_ch, s); lose_client_packets(&mut pair); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); let now = pair.time; pair.client_conn_mut(client_ch) .close(now, VarInt(0), Bytes::new()); @@ -478,7 +497,7 @@ fn unsupported_close_while_held() { assert!(pair.server_conn_mut(server_ch).is_closed()); } -/// The data abandoned past the checkpoint still counts against connection flow control until +/// The data abandoned past the offset still counts against connection flow control until /// the peer learns the final size, and then returns. #[test] fn unsupported_returns_flow_control() { @@ -492,8 +511,10 @@ fn unsupported_returns_flow_control() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); - reset(&mut pair, client_ch, checkpoint).unwrap(); + let offset = write(&mut pair, client_ch, s); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); let next = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); assert_eq!( pair.client_send(client_ch, next).write(MSG), @@ -516,9 +537,11 @@ fn unsupported_allows_zero() { let (client_ch, server_ch) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = pair.client_send(client_ch, s).checkpoint(); + let offset = pair.client_send(client_ch, s).offset(); pair.client_send(client_ch, s).write(MSG).unwrap(); - reset(&mut pair, client_ch, checkpoint).unwrap(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.drive(); assert_eq!(accept(&mut pair, server_ch), s); @@ -535,83 +558,18 @@ fn repeated_reset_is_closed() { let (client_ch, _) = pair.connect(); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); - let again = pair.client_send(client_ch, s).checkpoint(); - reset(&mut pair, client_ch, checkpoint).unwrap(); + let offset = write(&mut pair, client_ch, s); + let again = pair.client_send(client_ch, s).offset(); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); assert_eq!( - reset(&mut pair, client_ch, again), + pair.client_send(client_ch, s).reset_at(again, ERROR), Err(ClosedStream::default()) ); assert!(pair.client_send(client_ch, s).reset(ERROR).is_err()); } -/// A checkpoint taken in 0-RTT that the server rejects names a discarded stream, so it stays -/// closed even once the same stream id is opened again. -#[test] -fn zero_rtt_rejected_checkpoint_is_closed() { - let _guard = subscribe(); - let server_config = ServerConfig::with_crypto(Arc::new(server_crypto_with_alpn(vec![ - "foo".into(), - "bar".into(), - ]))); - let mut pair = Pair::new(Arc::new(EndpointConfig::default()), server_config); - let mut client_crypto = Arc::new(client_crypto_with_alpn(vec!["foo".into()])); - - let client_ch = pair.begin_connect(ClientConfig::new(client_crypto.clone())); - pair.drive(); - pair.server.assert_accept(); - let now = pair.time; - pair.client_conn_mut(client_ch) - .close(now, VarInt(0), Bytes::new()); - pair.drive(); - pair.client.connections.clear(); - pair.server.connections.clear(); - - // A changed ALPN makes the server reject the resumed session's 0-RTT - let this = Arc::get_mut(&mut client_crypto).expect("QuicClientConfig is shared"); - let inner = Arc::get_mut(&mut this.inner).expect("QuicClientConfig.inner is shared"); - inner.alpn_protocols = vec!["bar".into()]; - let client_ch = pair.begin_connect(ClientConfig::new(client_crypto)); - assert!(pair.client_conn_mut(client_ch).has_0rtt()); - let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); - pair.drive(); - assert!(!pair.client_conn_mut(client_ch).accepted_0rtt()); - - let reopened = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - assert_eq!(reopened, s); - pair.client_send(client_ch, reopened).write(MSG).unwrap(); - assert_eq!( - reset(&mut pair, client_ch, checkpoint), - Err(ClosedStream::default()) - ); - pair.client_send(client_ch, reopened).finish().unwrap(); - pair.drive(); - - let server_ch = pair.server.assert_accept(); - assert_eq!(accept(&mut pair, server_ch), s); - assert_eq!(read(&mut pair, server_ch, s), (MSG.to_vec(), Ok(()))); -} - -/// A checkpoint from another connection panics, even where that connection has a stream of the -/// same id holding at least as much data. -#[test] -#[should_panic(expected = "checkpoint used on another connection")] -fn checkpoint_from_another_connection() { - let _guard = subscribe(); - let mut first = pair(true); - let (first_ch, _) = first.connect(); - let mut second = pair(true); - let (second_ch, _) = second.connect(); - - let s = first.client_streams(first_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut first, first_ch, s); - let other = second.client_streams(second_ch).open(Dir::Uni).unwrap(); - assert_eq!(other, s); - second.client_send(second_ch, other).write(MSG).unwrap(); - let _ = reset(&mut second, second_ch, checkpoint); -} - /// A client remembers the server's support for 0-RTT, and may use it there. #[test] fn zero_rtt() { @@ -634,8 +592,10 @@ fn zero_rtt() { let client_ch = pair.begin_connect(config); assert!(pair.client_conn_mut(client_ch).has_0rtt()); let s = pair.client_streams(client_ch).open(Dir::Uni).unwrap(); - let checkpoint = write(&mut pair, client_ch, s); - reset(&mut pair, client_ch, checkpoint).unwrap(); + let offset = write(&mut pair, client_ch, s); + pair.client_send(client_ch, s) + .reset_at(offset, ERROR) + .unwrap(); pair.drive(); assert!(pair.client_conn_mut(client_ch).accepted_0rtt()); diff --git a/rs/moq-tokio/src/quic/driver/mod.rs b/rs/moq-tokio/src/quic/driver/mod.rs index 38e5588f6ed..a93829a5030 100644 --- a/rs/moq-tokio/src/quic/driver/mod.rs +++ b/rs/moq-tokio/src/quic/driver/mod.rs @@ -81,7 +81,7 @@ pub(crate) use crate::quic::driver::runtime::send_failed; pub(crate) use crate::quic::driver::runtime::wrap_udp_socket; pub use crate::quic::driver::runtime::{AsyncUdpSocket, UdpSender}; pub use crate::quic::driver::send_group::SendGroup; -pub use crate::quic::driver::send_stream::{Checkpoint, SendStream, StoppedError, WriteError}; +pub use crate::quic::driver::send_stream::{SendStream, StoppedError, WriteError}; #[cfg(test)] mod tests; diff --git a/rs/moq-tokio/src/quic/driver/send_stream.rs b/rs/moq-tokio/src/quic/driver/send_stream.rs index 63401220104..44010e1e1d5 100644 --- a/rs/moq-tokio/src/quic/driver/send_stream.rs +++ b/rs/moq-tokio/src/quic/driver/send_stream.rs @@ -214,15 +214,25 @@ impl SendStream { Ok(()) } - /// Mark the data written so far, so a later [`Checkpoint::reset()`] still delivers it. - pub fn checkpoint(&self) -> Checkpoint { - let mut conn = self.conn.state.lock("SendStream::checkpoint"); - let inner = conn.inner.send_stream(self.stream).checkpoint(); - Checkpoint { - conn: self.conn.clone(), - inner, - is_0rtt: self.is_0rtt, + /// Close the send stream immediately, but still deliver its first `offset` bytes. + /// + /// Like [`SendStream::reset()`], except the data before `offset` still reaches the peer. + /// A peer that accepts RESET_STREAM_AT reads it before the error. Otherwise the reset waits a + /// round trip until the peer acknowledges the data, and the peer's application may never read + /// it: see [`moq_quic::SendStream::reset_at`]. + /// + /// # Panics + /// When `offset` is past the data written. + pub fn reset_at(&mut self, offset: u64, error_code: VarInt) -> Result<(), ClosedStream> { + let mut conn = self.conn.state.lock("SendStream::reset_at"); + if self.is_0rtt && conn.check_0rtt().is_err() { + return Ok(()); } + conn.inner + .send_stream(self.stream) + .reset_at(offset, error_code)?; + conn.wake(); + Ok(()) } /// Set the priority within the stream's send group, or session-wide when ungrouped @@ -355,34 +365,6 @@ impl tokio::io::AsyncWrite for SendStream { } } -/// The data written to a [`SendStream`] up to some point, which a reset can still deliver -/// -/// Taken with [`SendStream::checkpoint()`]. It keeps the connection open, like the stream does. -#[derive(Debug)] -pub struct Checkpoint { - conn: ConnectionRef, - inner: moq_quic::Checkpoint, - is_0rtt: bool, -} - -impl Checkpoint { - /// Close the send stream immediately, but still deliver everything written before the checkpoint. - /// - /// Like [`SendStream::reset()`], except the data before the checkpoint still reaches the peer. - /// A peer that accepts RESET_STREAM_AT reads it before the error. Otherwise the reset waits a - /// round trip until the peer acknowledges the data, and the peer's application may never read - /// it: see [`moq_quic::Checkpoint::reset`]. - pub fn reset(self, error_code: VarInt) -> Result<(), ClosedStream> { - let mut conn = self.conn.state.lock("Checkpoint::reset"); - if self.is_0rtt && conn.check_0rtt().is_err() { - return Ok(()); - } - self.inner.reset(&mut conn.inner, error_code)?; - conn.wake(); - Ok(()) - } -} - impl Drop for SendStream { fn drop(&mut self) { let mut conn = self.conn.state.lock("SendStream::drop"); diff --git a/rs/moq-tokio/src/quic/web/send.rs b/rs/moq-tokio/src/quic/web/send.rs index 3c807c0fc7a..30aae19ba57 100644 --- a/rs/moq-tokio/src/quic/web/send.rs +++ b/rs/moq-tokio/src/quic/web/send.rs @@ -17,16 +17,12 @@ use crate::quic::web::{CloseReason, ClosedStream, SessionError, WriteError}; pub struct SendStream { stream: crate::quic::driver::SendStream, close: Arc, - /// Taken after the WebTransport header this side wrote first, which a reset must still deliver. - header: Option, + /// The end of the WebTransport header this side wrote first, which a reset must still deliver. + header: Option, } impl SendStream { - pub(crate) fn new( - stream: crate::quic::driver::SendStream, - close: Arc, - header: Option, - ) -> Self { + pub(crate) fn new(stream: crate::quic::driver::SendStream, close: Arc, header: Option) -> Self { Self { stream, close, header } } @@ -53,7 +49,7 @@ impl SendStream { pub fn reset(&mut self, code: u32) -> Result<(), ClosedStream> { let code = self.close.encode_stream_code(code); match self.header.take() { - Some(header) => header.reset(code).map_err(Into::into), + Some(header) => self.stream.reset_at(header, code).map_err(Into::into), None => self.stream.reset(code).map_err(Into::into), } } diff --git a/rs/moq-tokio/src/quic/web/session.rs b/rs/moq-tokio/src/quic/web/session.rs index 3e6f84a0711..4f88901bbc2 100644 --- a/rs/moq-tokio/src/quic/web/session.rs +++ b/rs/moq-tokio/src/quic/web/session.rs @@ -276,7 +276,7 @@ impl Session { // Reset the stream priority back to the default of 0. send.set_priority(0).ok(); - let header = send.checkpoint(); + let header = self.header_uni.len() as u64; Ok(SendStream::new(send, self.close.clone(), Some(header))) } @@ -295,7 +295,7 @@ impl Session { // Reset the stream priority back to the default of 0. send.set_priority(0).ok(); - let header = send.checkpoint(); + let header = self.header_bi.len() as u64; Ok(( SendStream::new(send, self.close.clone(), Some(header)), RecvStream::new(recv, self.close.clone()), diff --git a/rs/moq-uring/src/quic/driver/connection.rs b/rs/moq-uring/src/quic/driver/connection.rs index 27fbd45db6d..c3fe1b4a545 100644 --- a/rs/moq-uring/src/quic/driver/connection.rs +++ b/rs/moq-uring/src/quic/driver/connection.rs @@ -956,7 +956,7 @@ fn sweep_stream(state: &mut State, event: moq_quic::StreamEvent) { waiters.wake(); } } - // Nothing parks on a checkpoint's acknowledgment. + // Nothing waits on a send stream's acknowledgment yet. moq_quic::StreamEvent::Acked { .. } => {} } } diff --git a/rs/moq-uring/src/quic/driver/stream.rs b/rs/moq-uring/src/quic/driver/stream.rs index 36157e1cee6..b00ec6d9c34 100644 --- a/rs/moq-uring/src/quic/driver/stream.rs +++ b/rs/moq-uring/src/quic/driver/stream.rs @@ -8,7 +8,7 @@ use std::task::{Context, Poll, ready}; use bytes::{Buf, Bytes, BytesMut}; -use moq_quic::{Checkpoint, StreamId, VarInt}; +use moq_quic::{StreamId, VarInt}; use super::super::Error; use super::{End, Shared}; @@ -51,9 +51,9 @@ impl SendStream { self.fin || self.reset } - /// Mark the data written so far, for a later [`reset_code`](Self::reset_code) to deliver. - pub(crate) fn checkpoint(&self) -> Checkpoint { - self.shared.conn.borrow_mut().send_stream(self.id).checkpoint() + /// How many bytes were written so far, for a later [`reset_code`](Self::reset_code) to deliver. + pub(crate) fn offset(&self) -> u64 { + self.shared.conn.borrow_mut().send_stream(self.id).offset() } /// Set the priority within the stream's send group, or session-wide when ungrouped. @@ -80,16 +80,16 @@ impl SendStream { /// [`reset`](web_transport_trait::poll::SendStream::reset) with a /// full-width code, for the WebTransport HTTP/3 error mapping, that still - /// delivers the data before `checkpoint`, one this stream took. - pub(crate) fn reset_code(&mut self, code: u64, checkpoint: Option) { + /// delivers the first `offset` bytes. + pub(crate) fn reset_code(&mut self, code: u64, offset: Option) { if self.reset { return; } let code = VarInt::from_u64(code).unwrap_or(VarInt::MAX); let mut conn = self.shared.conn.borrow_mut(); // Err means the stream is already gone, which is what we wanted. - let _ = match checkpoint { - Some(checkpoint) => checkpoint.reset(&mut conn, code), + let _ = match offset { + Some(offset) => conn.send_stream(self.id).reset_at(offset, code), None => conn.send_stream(self.id).reset(code), }; drop(conn); diff --git a/rs/moq-uring/src/quic/web.rs b/rs/moq-uring/src/quic/web.rs index 6d91056b923..61e14189280 100644 --- a/rs/moq-uring/src/quic/web.rs +++ b/rs/moq-uring/src/quic/web.rs @@ -842,7 +842,7 @@ pub struct SendStream { inner: super::SendStream, /// The end of the WebTransport header on a stream we opened, for a reset /// to deliver. - header: Option, + header: Option, web: bool, } @@ -873,8 +873,8 @@ impl SendStream { self.inner.set_group(group); } - /// Write the WebTransport header of a stream we opened, checkpointing it - /// once it is all written. + /// Write the WebTransport header of a stream we opened, noting where it + /// ends once it is all written. fn poll_header(&mut self, cx: &mut Context<'_>, header: &mut Bytes) -> Poll> { if !self.web { return Poll::Ready(Ok(())); @@ -888,7 +888,7 @@ impl SendStream { .map_err(|err| self.map(err))?; header.advance(n); } - self.header = Some(self.inner.checkpoint()); + self.header = Some(self.inner.offset()); Poll::Ready(Ok(())) } From bfc9223d5d18f5e099326f3fb9ac1bb594cb7326 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:36:18 -0700 Subject: [PATCH 16/19] fix(quic): report a waiting offset that a reset or STOP_SENDING fails A waiter registered by `SendStream::acked` slept forever when a reset dropped its offset or the peer sent STOP_SENDING, since only ACKs reported `StreamEvent::Acked`. Both now report it, and the docs say every waiter on the stream polls again on the event, since the stream holds only the lowest waiting offset. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013bQzzUiJPvi9kFCtMqbtqC --- rs/moq-quic/MIGRATE.md | 2 +- rs/moq-quic/README.md | 2 +- rs/moq-quic/src/connection/streams/mod.rs | 25 +++++--- rs/moq-quic/src/connection/streams/send.rs | 7 ++- rs/moq-quic/src/connection/streams/state.rs | 5 +- rs/moq-quic/src/tests/acked.rs | 68 ++++++++++++++++++++- 6 files changed, 91 insertions(+), 18 deletions(-) diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index 3947d2d20f8..25d2c925708 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -8,7 +8,7 @@ Each entry is a change from `quinn-proto`. ### `StreamEvent::Acked` (#5817) -`StreamEvent` gained `Acked { id, offset }`, reported when a send stream's acknowledged prefix reaches an offset that `SendStream::acked` was polled on. +`StreamEvent` gained `Acked { id, offset }`, reported when the answer changes for the lowest offset a `SendStream::acked` call is waiting for: the stream's acknowledged prefix reached it, or STOP_SENDING or a reset made it fail. An exhaustive `match` needs an arm for it. ```rust diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index 8a3693cd899..a120b510ac0 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -71,7 +71,7 @@ Changes on top of the upstream commit, besides the renames: - **Scheduled datagrams** (`Datagrams::send`): a datagram is queued by its `SendOptions`, in a send group (or ungrouped) at an `i64` priority, and scheduled with the group's streams, winning a tie with a stream of equal priority and spending the group's byte credit, instead of quinn's datagram-first FIFO in `populate_packet`. Sending never blocks: `datagram_send_buffer_size` is shared, and an overflow evicts the oldest datagram of the lowest-priority group in the domain holding the most datagram bytes. `SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone, and a cherry-pick touching the `// DATAGRAM` loop has to be redone against `StreamsState::write_frames`. - **Sharded connection IDs** (`RandomConnectionIdGenerator::set_shard`): every CID names shard `index` of a `count`-endpoint group in its first byte, modulo `count`, and sets a marker in its fourth byte, so a `SO_REUSEPORT` steering filter delivers a packet to the endpoint that issued its CID. `validate` refuses a CID without this shard's prefix and marker, so a misrouted packet is dropped instead of answered with a stateless reset. Not offered upstream; it serves moq-sock's per-core sharding. - **`RESET_STREAM_AT`** ([draft-ietf-quic-reliable-stream-reset](https://datatracker.ietf.org/doc/draft-ietf-quic-reliable-stream-reset/)): the `reset_stream_at` transport parameter is always advertised, `SendStream::reset_at` keeps the data written before an offset reliable, holding a plain `RESET_STREAM` until that data is acknowledged when the peer lacks the extension, and a received reset surfaces only once the reliable prefix has been read. Stopping a stream after it was reset no longer credits its unread data twice. -- **ACK progress** (`SendStream::offset`, `SendStream::acked`, `StreamEvent::Acked`): `acked` reports whether the peer acknowledged every byte before an offset, by comparing against the stream's contiguous acknowledged prefix. A `false` stores the lowest polled offset on the stream, and the ACK that advances the prefix past it reports `StreamEvent::Acked` with the new prefix, so a cherry-pick touching `StreamsState::received_ack_of` has to keep that report. Not offered upstream: quinn has no API for when sent data was acknowledged. +- **ACK progress** (`SendStream::offset`, `SendStream::acked`, `StreamEvent::Acked`): `acked` reports whether the peer acknowledged every byte before an offset, by comparing against the stream's contiguous acknowledged prefix. A `false` stores the lowest polled offset on the stream, and the ACK that advances the prefix past it, or a reset or STOP_SENDING that makes it fail, reports `StreamEvent::Acked` with the prefix, so a cherry-pick touching `StreamsState::received_ack_of`, `received_stop_sending`, or the reset path has to keep that report. Not offered upstream: quinn has no API for when sent data was acknowledged. - **Fuzzing** runs through the hidden `fuzz` module behind a `fuzz` feature, with one-line shims in [`fuzz/`](fuzz/README.md): upstream's `packet`, `params`, `streamid`, and `streams` targets, plus `frame` and a connection op sequence. Upstream's `cfg(fuzzing)` `fuzzing` module and `arbitrary` feature are gone, so its fuzz patches port by hand. - **Draining endpoint event**: `EndpointEvent::is_draining` fires once per connection when it stops being active, before the drained event, so the driver's `Endpoint::wait_all_draining` can end a shutdown without sitting out every closing period. - **Late stream limits**: peer transport parameters that raise a stream limit after an open was refused emit `StreamEvent::Available`, as `MAX_STREAMS` does. A server accepted before the rest of a multi-packet ClientHello (a post-quantum key share is enough) otherwise leaves a 0.5-RTT `open_uni` parked until the idle timeout. Not offered upstream yet. diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index f48c44ffb3d..a7a33175832 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -387,11 +387,10 @@ impl<'a> SendStream<'a> { /// Whether the peer acknowledged every byte before `offset` /// - /// A `false` notes to report a [`StreamEvent::Acked`] once the stream's contiguous - /// acknowledged prefix reaches `offset`, so poll again then. One event answers every offset - /// at or below the one it carries. Like a [`StreamEvent::Finished`] stream, an - /// acknowledgment only means the peer's QUIC stack holds the data, not that its application - /// read it. + /// A `false` notes to report a [`StreamEvent::Acked`] once the answer changes, so poll again + /// then. The stream holds only its lowest waiting offset, so every waiter on it polls again + /// on that event. Like a [`StreamEvent::Finished`] stream, an acknowledgment only means the + /// peer's QUIC stack holds the data, not that its application read it. /// /// Fails with [`ClosedStream`] once the peer will never acknowledge it, or no longer reads /// it: a reset dropped the data before `offset`, the peer sent STOP_SENDING, or the @@ -457,6 +456,12 @@ impl<'a> SendStream<'a> { let buffered = stream.pending.buffered(); let send = stream.reset(error_code, reliable_size, at); self.state.buffered_data -= buffered - stream.pending.buffered(); + if let Some(offset) = stream.take_acked() { + self.state.events.push_back(StreamEvent::Acked { + id: self.id, + offset, + }); + } // Only data below the Reliable Size is left to send if !stream.is_pending() { self.state.pending.remove(&mut stream.slot); @@ -744,12 +749,12 @@ pub enum StreamEvent { /// Which stream has been finished id: StreamId, }, - /// A send stream's contiguous acknowledged prefix reached an offset [`SendStream::acked`] is - /// waiting for + /// The answer changed for the lowest offset a [`SendStream::acked`] call is waiting for /// - /// Reported before a [`StreamEvent::Finished`] from the same ACK. Not reported after - /// STOP_SENDING, a reset that drops the data, or the connection closing, which fail - /// [`SendStream::acked`] instead. + /// The stream's contiguous acknowledged prefix reached it, or STOP_SENDING or a reset made it + /// fail. Every waiter on the stream polls again: one at or below `offset` is acknowledged, and + /// one past it registers again. Reported before a [`StreamEvent::Finished`] from the same ACK. + /// Not reported when the connection closes, which fails [`SendStream::acked`] instead. Acked { /// Which stream was acknowledged id: StreamId, diff --git a/rs/moq-quic/src/connection/streams/send.rs b/rs/moq-quic/src/connection/streams/send.rs index b388cc64d83..aa96871bc97 100644 --- a/rs/moq-quic/src/connection/streams/send.rs +++ b/rs/moq-quic/src/connection/streams/send.rs @@ -264,11 +264,12 @@ impl Send { Ok(false) } - /// The acknowledged prefix to report, once it reaches the offset a waiter is waiting for + /// The acknowledged prefix to report once the answer for the offset a waiter is waiting for + /// changes: the prefix reached it, or STOP_SENDING or a reset made it fail pub(super) fn take_acked(&mut self) -> Option { + let notify = self.notify?; let acked = self.pending.acked(); - // A stopped stream fails its waiters instead - if self.stop_reason.is_some() || self.notify.is_none_or(|notify| acked < notify) { + if acked < notify && self.stop_reason.is_none() && notify <= self.pending.offset() { return None; } self.notify = None; diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index dbc05c6bcab..46c49cdef3b 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -113,7 +113,7 @@ pub(crate) struct StreamsState { /// Streams with outgoing data queued and outgoing datagrams, in the order to send them pub(super) pending: Schedule, - events: VecDeque, + pub(super) events: VecDeque, /// Streams blocked on connection-level flow control or stream window space /// /// Streams are only added to this list when a write fails. @@ -420,6 +420,9 @@ impl StreamsState { let buffered = stream.pending.buffered(); let reset = stream.abandon_reliable(); self.buffered_data -= buffered - stream.pending.buffered(); + if let Some(offset) = stream.take_acked() { + self.events.push_back(StreamEvent::Acked { id, offset }); + } if !stream.is_pending() { self.pending.remove(&mut stream.slot); } diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index 1b4e28aae4b..da36d65a65f 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -91,6 +91,41 @@ fn waits_for_contiguous_prefix() { assert!(pair.client_conn_mut(client_ch).stats().path.lost_packets > 0); } +/// A waiter past the offset an event reports polls again and registers anew, so it learns of +/// a later ACK too. +#[test] +fn later_waiter_registers_again() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + pair.drive(); + reported(&mut pair, client_ch, s); + + // The first packet is held, and the second lost + pair.client_send(client_ch, s).write(b"first").unwrap(); + let first = pair.client_send(client_ch, s).offset(); + pair.client.drive(pair.time, pair.server.addr); + pair.client.delay_outbound(); + pair.client_send(client_ch, s).write(b"second").unwrap(); + let second = pair.client_send(client_ch, s).offset(); + lose_client_packets(&mut pair); + assert_eq!(acked(&mut pair, client_ch, s, second), Ok(false)); + assert_eq!(acked(&mut pair, client_ch, s, first), Ok(false)); + + // Only the first arrives at first + pair.client.finish_delay(); + while acked(&mut pair, client_ch, s, first) != Ok(true) { + assert!(pair.step(), "never acknowledged"); + } + assert_eq!(reported(&mut pair, client_ch, s), Some(first)); + assert_eq!(acked(&mut pair, client_ch, s, second), Ok(false)); + + // The retransmission reports the second + pair.drive(); + assert_eq!(reported(&mut pair, client_ch, s), Some(second)); +} + /// Without a poll waiting on it, an acknowledgment is not reported. #[test] fn unpolled_is_not_reported() { @@ -187,6 +222,7 @@ fn reset_fails_past_reliable_size() { Err(ClosedStream::default()) ); assert_eq!(acked(&mut pair, client_ch, s, kept), Ok(false)); + assert_eq!(reported(&mut pair, client_ch, s), None); // The retransmitted prefix is acknowledged before the reset frees the stream pair.drive(); @@ -197,8 +233,33 @@ fn reset_fails_past_reliable_size() { ); } +/// A waiter whose offset a later reset drops learns so from the event. +#[test] +fn reset_reports_a_dropped_waiter() { + let _guard = subscribe(); + let mut pair = pair(true); + let (client_ch, server_ch) = pair.connect(); + let s = open(&mut pair, client_ch, server_ch); + + let kept = pair.client_send(client_ch, s).offset(); + pair.client_send(client_ch, s).write(b"dropped").unwrap(); + let dropped = pair.client_send(client_ch, s).offset(); + lose_client_packets(&mut pair); + reported(&mut pair, client_ch, s); + assert_eq!(acked(&mut pair, client_ch, s, dropped), Ok(false)); + + pair.client_send(client_ch, s) + .reset_at(kept, VarInt(42)) + .unwrap(); + assert!(reported(&mut pair, client_ch, s).is_some()); + assert_eq!( + acked(&mut pair, client_ch, s, dropped), + Err(ClosedStream::default()) + ); +} + /// A peer that stopped reading will not read what it acknowledges, before or after the -/// offset was taken. +/// offset was taken, and a waiter learns so from the event. #[test] fn stop_sending_fails() { let _guard = subscribe(); @@ -218,7 +279,10 @@ fn stop_sending_fails() { Ok(Some(VarInt(7))) ); let after = pair.client_send(client_ch, s).offset(); - assert_eq!(reported(&mut pair, client_ch, s), None); + assert_eq!( + reported(&mut pair, client_ch, s), + Some(b"header".len() as u64) + ); for offset in [before, after] { assert_eq!( acked(&mut pair, client_ch, s, offset), From f58a1c51aea883e427f182546ab725a42ec06536 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:43:47 -0700 Subject: [PATCH 17/19] fix(quic): wake every acked waiter on a reset or STOP_SENDING The stream holds only its lowest waiting offset, so a reset that keeps it but drops a higher one reported nothing until the lower one was acknowledged. A reset or STOP_SENDING now always reports to the stream's waiters. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013bQzzUiJPvi9kFCtMqbtqC --- rs/moq-quic/src/connection/streams/mod.rs | 8 ++++---- rs/moq-quic/src/connection/streams/send.rs | 13 +++++++++---- rs/moq-quic/src/connection/streams/state.rs | 2 +- rs/moq-quic/src/tests/acked.rs | 6 +++++- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index a7a33175832..f67a89543f7 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -456,7 +456,7 @@ impl<'a> SendStream<'a> { let buffered = stream.pending.buffered(); let send = stream.reset(error_code, reliable_size, at); self.state.buffered_data -= buffered - stream.pending.buffered(); - if let Some(offset) = stream.take_acked() { + if let Some(offset) = stream.take_waiting() { self.state.events.push_back(StreamEvent::Acked { id: self.id, offset, @@ -751,9 +751,9 @@ pub enum StreamEvent { }, /// The answer changed for the lowest offset a [`SendStream::acked`] call is waiting for /// - /// The stream's contiguous acknowledged prefix reached it, or STOP_SENDING or a reset made it - /// fail. Every waiter on the stream polls again: one at or below `offset` is acknowledged, and - /// one past it registers again. Reported before a [`StreamEvent::Finished`] from the same ACK. + /// The stream's contiguous acknowledged prefix reached it, or a reset or STOP_SENDING may have + /// failed any waiter. Every waiter on the stream polls again: one at or below `offset` is + /// acknowledged, and one past it learns its error or registers again. Reported before a [`StreamEvent::Finished`] from the same ACK. /// Not reported when the connection closes, which fails [`SendStream::acked`] instead. Acked { /// Which stream was acknowledged diff --git a/rs/moq-quic/src/connection/streams/send.rs b/rs/moq-quic/src/connection/streams/send.rs index aa96871bc97..58ab5c77204 100644 --- a/rs/moq-quic/src/connection/streams/send.rs +++ b/rs/moq-quic/src/connection/streams/send.rs @@ -264,18 +264,23 @@ impl Send { Ok(false) } - /// The acknowledged prefix to report once the answer for the offset a waiter is waiting for - /// changes: the prefix reached it, or STOP_SENDING or a reset made it fail + /// The acknowledged prefix to report once it reaches the offset a waiter is waiting for pub(super) fn take_acked(&mut self) -> Option { - let notify = self.notify?; let acked = self.pending.acked(); - if acked < notify && self.stop_reason.is_none() && notify <= self.pending.offset() { + if self.notify.is_none_or(|notify| acked < notify) { return None; } self.notify = None; Some(acked) } + /// The acknowledged prefix to report to every waiter after a reset or STOP_SENDING, which may + /// fail any of them + pub(super) fn take_waiting(&mut self) -> Option { + self.notify.take()?; + Some(self.pending.acked()) + } + /// Handle increase to stream-level flow control limit /// /// Returns whether the stream was unblocked diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index 46c49cdef3b..d0e8bddbd41 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -420,7 +420,7 @@ impl StreamsState { let buffered = stream.pending.buffered(); let reset = stream.abandon_reliable(); self.buffered_data -= buffered - stream.pending.buffered(); - if let Some(offset) = stream.take_acked() { + if let Some(offset) = stream.take_waiting() { self.events.push_back(StreamEvent::Acked { id, offset }); } if !stream.is_pending() { diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index da36d65a65f..6ef2a900dca 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -233,7 +233,8 @@ fn reset_fails_past_reliable_size() { ); } -/// A waiter whose offset a later reset drops learns so from the event. +/// A waiter whose offset a later reset drops learns so from the event, even beside a lower +/// waiter the reset keeps. #[test] fn reset_reports_a_dropped_waiter() { let _guard = subscribe(); @@ -241,11 +242,14 @@ fn reset_reports_a_dropped_waiter() { let (client_ch, server_ch) = pair.connect(); let s = open(&mut pair, client_ch, server_ch); + // A lower waiter the reset keeps doesn't hide the one it drops + pair.client_send(client_ch, s).write(b"kept").unwrap(); let kept = pair.client_send(client_ch, s).offset(); pair.client_send(client_ch, s).write(b"dropped").unwrap(); let dropped = pair.client_send(client_ch, s).offset(); lose_client_packets(&mut pair); reported(&mut pair, client_ch, s); + assert_eq!(acked(&mut pair, client_ch, s, kept), Ok(false)); assert_eq!(acked(&mut pair, client_ch, s, dropped), Ok(false)); pair.client_send(client_ch, s) From c58877510f74b8ab07de8a06597df74162ea0e29 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:49:55 -0700 Subject: [PATCH 18/19] docs(quic): say what SendStream::offset reports after a reset Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013bQzzUiJPvi9kFCtMqbtqC --- rs/moq-quic/src/connection/streams/mod.rs | 3 ++- rs/moq-quic/src/tests/acked.rs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index f67a89543f7..9143c0946b5 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -374,7 +374,8 @@ impl<'a> SendStream<'a> { /// How many bytes were written to the stream so far, the offset the next write starts at /// - /// Zero before the first write, or once the stream is gone. + /// After a reset, the bytes the reset still delivers. Zero before the first write, or once + /// the stream is gone. /// /// # Panics /// - when applied to a receive stream diff --git a/rs/moq-quic/src/tests/acked.rs b/rs/moq-quic/src/tests/acked.rs index 6ef2a900dca..0b03c6a4e85 100644 --- a/rs/moq-quic/src/tests/acked.rs +++ b/rs/moq-quic/src/tests/acked.rs @@ -255,6 +255,7 @@ fn reset_reports_a_dropped_waiter() { pair.client_send(client_ch, s) .reset_at(kept, VarInt(42)) .unwrap(); + assert_eq!(pair.client_send(client_ch, s).offset(), kept); assert!(reported(&mut pair, client_ch, s).is_some()); assert_eq!( acked(&mut pair, client_ch, s, dropped), From 2794e60823eb2ce384048b00dca24d0f037b2087 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Sat, 17 Oct 2026 22:13:22 -0700 Subject: [PATCH 19/19] docs(quic): format the ACK progress notes Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013bQzzUiJPvi9kFCtMqbtqC --- rs/moq-quic/MIGRATE.md | 2 +- rs/moq-quic/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index 25d2c925708..fd3da11282d 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -8,7 +8,7 @@ Each entry is a change from `quinn-proto`. ### `StreamEvent::Acked` (#5817) -`StreamEvent` gained `Acked { id, offset }`, reported when the answer changes for the lowest offset a `SendStream::acked` call is waiting for: the stream's acknowledged prefix reached it, or STOP_SENDING or a reset made it fail. +`StreamEvent` gained `Acked { id, offset }`, reported when the answer changes for the lowest offset a `SendStream::acked` call is waiting for: the stream's acknowledged prefix reached it, or `STOP_SENDING` or a reset made it fail. An exhaustive `match` needs an arm for it. ```rust diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index a120b510ac0..ba9ec36b6d0 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -71,7 +71,7 @@ Changes on top of the upstream commit, besides the renames: - **Scheduled datagrams** (`Datagrams::send`): a datagram is queued by its `SendOptions`, in a send group (or ungrouped) at an `i64` priority, and scheduled with the group's streams, winning a tie with a stream of equal priority and spending the group's byte credit, instead of quinn's datagram-first FIFO in `populate_packet`. Sending never blocks: `datagram_send_buffer_size` is shared, and an overflow evicts the oldest datagram of the lowest-priority group in the domain holding the most datagram bytes. `SendDatagramError::Blocked` and `Event::DatagramsUnblocked` are gone, and a cherry-pick touching the `// DATAGRAM` loop has to be redone against `StreamsState::write_frames`. - **Sharded connection IDs** (`RandomConnectionIdGenerator::set_shard`): every CID names shard `index` of a `count`-endpoint group in its first byte, modulo `count`, and sets a marker in its fourth byte, so a `SO_REUSEPORT` steering filter delivers a packet to the endpoint that issued its CID. `validate` refuses a CID without this shard's prefix and marker, so a misrouted packet is dropped instead of answered with a stateless reset. Not offered upstream; it serves moq-sock's per-core sharding. - **`RESET_STREAM_AT`** ([draft-ietf-quic-reliable-stream-reset](https://datatracker.ietf.org/doc/draft-ietf-quic-reliable-stream-reset/)): the `reset_stream_at` transport parameter is always advertised, `SendStream::reset_at` keeps the data written before an offset reliable, holding a plain `RESET_STREAM` until that data is acknowledged when the peer lacks the extension, and a received reset surfaces only once the reliable prefix has been read. Stopping a stream after it was reset no longer credits its unread data twice. -- **ACK progress** (`SendStream::offset`, `SendStream::acked`, `StreamEvent::Acked`): `acked` reports whether the peer acknowledged every byte before an offset, by comparing against the stream's contiguous acknowledged prefix. A `false` stores the lowest polled offset on the stream, and the ACK that advances the prefix past it, or a reset or STOP_SENDING that makes it fail, reports `StreamEvent::Acked` with the prefix, so a cherry-pick touching `StreamsState::received_ack_of`, `received_stop_sending`, or the reset path has to keep that report. Not offered upstream: quinn has no API for when sent data was acknowledged. +- **ACK progress** (`SendStream::offset`, `SendStream::acked`, `StreamEvent::Acked`): `acked` reports whether the peer acknowledged every byte before an offset, by comparing against the stream's contiguous acknowledged prefix. A `false` stores the lowest polled offset on the stream, and the ACK that advances the prefix past it, or a reset or `STOP_SENDING` that makes it fail, reports `StreamEvent::Acked` with the prefix, so a cherry-pick touching `StreamsState::received_ack_of`, `received_stop_sending`, or the reset path has to keep that report. Not offered upstream: quinn has no API for when sent data was acknowledged. - **Fuzzing** runs through the hidden `fuzz` module behind a `fuzz` feature, with one-line shims in [`fuzz/`](fuzz/README.md): upstream's `packet`, `params`, `streamid`, and `streams` targets, plus `frame` and a connection op sequence. Upstream's `cfg(fuzzing)` `fuzzing` module and `arbitrary` feature are gone, so its fuzz patches port by hand. - **Draining endpoint event**: `EndpointEvent::is_draining` fires once per connection when it stops being active, before the drained event, so the driver's `Endpoint::wait_all_draining` can end a shutdown without sitting out every closing period. - **Late stream limits**: peer transport parameters that raise a stream limit after an open was refused emit `StreamEvent::Available`, as `MAX_STREAMS` does. A server accepted before the rest of a multi-packet ClientHello (a post-quantum key share is enough) otherwise leaves a 0.5-RTT `open_uni` parked until the idle timeout. Not offered upstream yet.