From 0e06ef9a2f9849f6cfdc693e5b56088b012878ab Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 00:26:20 -0800 Subject: [PATCH 1/5] chore(quest): claim quest/m1/quic-api/poll.md Quest-Claim: e8ee455d33443ec8 From 62501a29f5db467d7c6cc4c7fadb925f195d3c58 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 00:43:33 -0800 Subject: [PATCH 2/5] refactor(quic)!: expose driver polls and remove transmit hints Co-Authored-By: GPT-6.1 Sol --- quest/m1/quic-api/README.md | 1 - quest/m1/quic-api/native-web.md | 4 - quest/m1/quic-api/poll.md | 92 -------- quest/m2/quic-ack-hook.md | 7 +- quest/m2/quic-sansio-driver.md | 6 +- rs/moq-quic/MIGRATE.md | 7 + rs/moq-quic/benches/common/mod.rs | 6 +- rs/moq-quic/benches/receive.rs | 2 +- rs/moq-quic/benches/receive_bursts.rs | 2 +- rs/moq-quic/src/connection/mod.rs | 12 +- rs/moq-quic/src/connection/mux/mod.rs | 4 +- rs/moq-quic/src/connection/mux/tests.rs | 4 +- rs/moq-quic/src/connection/streams/lease.rs | 96 ++++---- rs/moq-quic/src/connection/streams/mod.rs | 15 -- rs/moq-quic/src/connection/streams/recv.rs | 51 ++--- rs/moq-quic/src/connection/streams/state.rs | 228 ++++++++----------- rs/moq-quic/src/fuzz/connection.rs | 6 +- rs/moq-quic/src/lib.rs | 4 +- rs/moq-quic/src/tests/characterize/frames.rs | 2 +- rs/moq-quic/src/tests/characterize/limits.rs | 4 +- rs/moq-quic/src/tests/characterize/paths.rs | 2 +- rs/moq-quic/src/tests/characterize/reset.rs | 10 +- rs/moq-quic/src/tests/mod.rs | 55 +++-- rs/moq-quic/src/tests/probe.rs | 4 +- rs/moq-quic/src/tests/rate.rs | 4 +- rs/moq-quic/src/tests/receive_ts.rs | 2 +- rs/moq-quic/src/tests/reset_stream_at.rs | 4 +- rs/moq-quic/src/tests/scream.rs | 2 +- rs/moq-quic/src/tests/sim.rs | 2 +- rs/moq-quic/src/tests/sim/workloads.rs | 2 +- rs/moq-quic/src/tests/util.rs | 2 +- rs/moq-tokio/MIGRATE.md | 9 + rs/moq-tokio/src/quic/driver/connection.rs | 69 ++++-- rs/moq-tokio/src/quic/driver/endpoint.rs | 181 ++++++--------- rs/moq-tokio/src/quic/driver/recv.rs | 8 +- rs/moq-tokio/src/quic/driver/send.rs | 43 +++- rs/moq-tokio/src/quic/driver/tests.rs | 181 ++++++++++++++- rs/moq-uring/MIGRATE.md | 9 + rs/moq-uring/src/quic/driver/connection.rs | 58 ++--- rs/moq-uring/src/quic/driver/endpoint.rs | 32 +-- rs/moq-uring/src/quic/driver/recv.rs | 8 +- rs/moq-uring/src/quic/driver/send.rs | 28 ++- rs/moq-uring/src/quic/web.rs | 49 ++-- rs/moq-uring/tests/integration/credit.rs | 4 +- rs/qmux/src/session.rs | 6 +- 45 files changed, 690 insertions(+), 637 deletions(-) delete mode 100644 quest/m1/quic-api/poll.md diff --git a/quest/m1/quic-api/README.md b/quest/m1/quic-api/README.md index 25691a17e26..354aced6f89 100644 --- a/quest/m1/quic-api/README.md +++ b/quest/m1/quic-api/README.md @@ -46,7 +46,6 @@ side by side and fix any name or signature that still differs, and check ## Required -- [Poll forms](/quest/m1/quic-api/poll.md) - both drivers' waits poll as `&self, &kio::Waiter` forwards to moq-quic, the async forms wrap them, and `send::Finished` is a future - [Native WebTransport](/quest/m1/quic-api/native-web.md) - `quic::web` in both crates implements moq-net's poll traits, deleting the generic `web_transport_trait` adapters and the dependency on it ## Related diff --git a/quest/m1/quic-api/native-web.md b/quest/m1/quic-api/native-web.md index a69103078be..9ae2df7f38f 100644 --- a/quest/m1/quic-api/native-web.md +++ b/quest/m1/quic-api/native-web.md @@ -65,10 +65,6 @@ Align the two stacks while each gets its impl: Measure relay CPU per Gbps and allocations per stream before and after on the same workload. -## Required - -- [Poll forms](/quest/m1/quic-api/poll.md) - the leak-free driver polls these impls forward to - ## Related - [Relay allocations](/quest/m2/perf/relay-allocs.md) - retakes its profile after the adapter's per-stream boxing is gone diff --git a/quest/m1/quic-api/poll.md b/quest/m1/quic-api/poll.md deleted file mode 100644 index 75c25623500..00000000000 --- a/quest/m1/quic-api/poll.md +++ /dev/null @@ -1,92 +0,0 @@ -# [M] The QUIC drivers' waits poll - -## Goal - -The stream, datagram, close, and accept waits in `moq_tokio::quic::driver` -and moq-uring's driver have public `poll_*` forms taking `&self, -&kio::Waiter`: `Connection::poll_open_uni`, `poll_open_bi`, -`poll_accept_uni`, `poll_accept_bi`, `poll_recv_datagram` and `poll_closed`, -`send::Stream::poll_closed` and `send::Finished::poll_closed` (today's -`stopped`, which both share), and `Endpoint::poll_accept`. -`send::Stream::poll_acked`, which [poll_acked](/quest/m2/quic-ack-hook.md) -adds, takes the same shape. The async methods are thin `kio::wait` wrappers, -and `send::Finished` implements `Future` over its `poll_closed`, resolving -as `stopped()` does today: `Ok(None)` once the peer acknowledges the FIN and -every byte, or after a local reset, `Ok(Some(code))` if the peer stopped it first, and `Err` if the -connection is lost. -moq-tokio's WebTransport `SendStream` needs the poll form, not just the -`Future`: once finished, it answers moq-net's waiter-based `poll_closed` and -still reports the peer's stop code. No `tokio::sync::Notify` is left -in the driver. - -## Plan - -Moved to m1 into the [moq-quic API](/quest/m1/quic-api/README.md) epic -(maintainer, 2026-10-23): an async-only driver is quinn's shape, and it forces -moq-tokio's async-to-poll adapter (a boxed future per operation, a copy per -write, deferred finish and reset, an emulated `closed()`). Poll forms are the -primary surface. [Native WebTransport](/quest/m1/quic-api/native-web.md) and -[poll_acked](/quest/m2/quic-ack-hook.md) both need them. - -Decided 2026-10-25 (maintainer), replacing the 2026-10-23 shape: - -- **moq-quic owns the waits** (landed: `connection/waits.rs`). Each - connection and stream form here takes the lock and forwards to moq-quic's - `poll_*` in one line; the driver keeps no waiter table of its own. Only - `Endpoint::poll_accept` waits on driver state, the queue of handshaken - connections, and parks on a `kio::WaiterList` there. -- **`&self, &kio::Waiter`**, the `rs/AGENTS.md` convention, instead of - `&mut self, cx` with a `kio::Park` in each handle. The earlier objection, - that kio would enter the driver's public API so a kio break is a driver - break, no longer holds: moq-net already re-exports kio, and moq-quic takes - `&kio::Waiter` too. The caller owns the waiter, so a poll that is - abandoned leaves nothing behind, and handles stay `&self`. moq-net's - `transport::poll` traits take the same shape, so their native impls - forward without a `Park`. - -```rust -impl Connection { - pub fn poll_accept_uni(&self, waiter: &kio::Waiter) -> Poll>; - pub fn poll_open_uni(&self, waiter: &kio::Waiter, options: send::Options<'_>) -> Poll>; - pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll; -} -impl send::Stream { - pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll, send::Error>>; -} -impl send::Finished { - pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll, send::Error>>; -} -``` - -Keep the existing guarantees, such as draining accepted streams and -datagrams from a closed connection before its error (moq-quic's waits carry -that now). The `Connecting` handshake, `Endpoint::wait_idle`, and -`Endpoint::wait_all_draining` move off `Notify` too, but get no poll form -until a caller needs one. Report the public API: -new poll methods on `Connection`, `send::Stream`, `send::Finished` and -`Endpoint`, with the -async forms unchanged. - -Added 2026-11-01 (maintainer, follow-up planning after the 2026-11-01 -/quest-afk run), from [moq-dev/moq#7490](https://github.com/moq-dev/moq/pull/7490), -which finishes the waits quest: - -- **Remove `ShouldTransmit`.** Once moq-quic wakes the transmit waiter itself, - the `ShouldTransmit` that `Chunks::finalize` and the send lease methods - (`lease`, `poll_lease`, `restore`, `release` in - `rs/moq-quic/src/connection/streams/lease.rs`) return is ignored by every - consumer. Drop it from moq-quic's public API and its callers, as part of - this reshape, since moq-quic is unreleased. -- **Test moq-tokio's close while the socket is blocked.** moq-tokio's driver - skips `poll_transmit` while the socket holds output back, so a local close - could not re-arm the closing period's timer; #7490's round 1 fixed it by - also parking the driver on the non-consuming `poll_closed`. No driver test - has a socket that holds sends back, so nothing covers it. - Add one (a test socket whose sends return `WouldBlock` until released) and - a test that a close behind it is still bounded, mirroring qmux's - `a_close_behind_a_stalled_peer_is_bounded`. - -## Related - -- [Native WebTransport](/quest/m1/quic-api/native-web.md) - its native QUIC `poll::Session` calls these -- [poll_acked on moq-net's send stream](/quest/m2/quic-ack-hook.md) - adds `send::Stream::poll_acked` in this shape diff --git a/quest/m2/quic-ack-hook.md b/quest/m2/quic-ack-hook.md index 76775e82514..08ff75546ea 100644 --- a/quest/m2/quic-ack-hook.md +++ b/quest/m2/quic-ack-hook.md @@ -39,7 +39,7 @@ unsupported has to live in the return type rather than the error, and a consumer must treat `None` as unknown, never as delivered. Each QUIC driver, moq-tokio's `quic::driver` and moq-uring's, gets the hook -first, in the shape of [the driver poll waits](/quest/m1/quic-api/poll.md), +first, in the drivers' caller-owned waiter shape, with an async wrapper: ```rust @@ -97,8 +97,3 @@ close. Decided in the 2026-09-30 audit: moved to m2 with its consumers, the [viewer lag histogram](/quest/m2/qos/lag-histogram.md) and the [delivery-delay histogram](/quest/m2/delivery-delay.md). - -## Required - -- [QUIC driver poll waits](/quest/m1/quic-api/poll.md) - the `&self, &kio::Waiter` - driver shape `poll_acked` takes diff --git a/quest/m2/quic-sansio-driver.md b/quest/m2/quic-sansio-driver.md index c55c9a31fd8..f95ac9f13e7 100644 --- a/quest/m2/quic-sansio-driver.md +++ b/quest/m2/quic-sansio-driver.md @@ -18,7 +18,7 @@ whose C, C++, and Kotlin bindings drive it from their own loops Decided with the maintainer on 2026-10-28, from [moq-dev/rust2#215](https://github.com/moq-dev/rust2/pull/215): -- **After the waits (landed) and [poll](/quest/m1/quic-api/poll.md).** +- **The waits and driver poll forms have landed.** Once moq-quic owns its waits, a driver is I/O, timers, and a lock. This one is the timers and the lock, with the caller doing the I/O. Built earlier, it would carry the waiter tables that waits deletes. @@ -46,7 +46,3 @@ other drivers do. Public API: a new driver, its endpoint and connection types, and their `poll::Session` impl. - -## Required - -- [Poll forms](/quest/m1/quic-api/poll.md) - the drivers' poll shape this one mirrors diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index 61908a96f45..fddf21a533e 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -4,6 +4,13 @@ How to update your code for each breaking change, newest first. ## Unreleased +### Receive operations wake transmission themselves + +`ShouldTransmit` is removed. `recv::Stream::lease` returns `Result` and +`poll_lease` returns `Poll>`; remove the transmit hint from tuple +destructuring. `restore`, `release`, and `Chunks::finalize` return `()`; call them +directly. Each operation wakes the connection's transmit waiter when needed. + ### Waits replace stream and connection events Every wait is a `poll_*` taking the caller's `&kio::Waiter`, which moq-quic registers and wakes itself, so a driver keeps no waiter tables. diff --git a/rs/moq-quic/benches/common/mod.rs b/rs/moq-quic/benches/common/mod.rs index 3d0bcb793c1..7c0c06ec548 100644 --- a/rs/moq-quic/benches/common/mod.rs +++ b/rs/moq-quic/benches/common/mod.rs @@ -362,7 +362,7 @@ fn drain(conn: &mut Connection, id: StreamId, sink: Sink, received: &mut Receive Poll::Ready(Err(err)) => panic!("read failed: {err}"), } }; - let _ = chunks.finalize(); + chunks.finalize(); done } @@ -374,7 +374,7 @@ fn drain(conn: &mut Connection, id: StreamId, sink: Sink, received: &mut Receive fn lease(conn: &mut Connection, id: StreamId, want: usize, received: &mut Received, waiter: &kio::Waiter) -> bool { loop { let mut stream = conn.recv_stream(id); - let mut lease = match stream.poll_lease(waiter, want).0 { + let mut lease = match stream.poll_lease(waiter, want) { Poll::Ready(Ok(lease)) => lease, Poll::Pending => return false, Poll::Ready(Err(recv::Error::Closed)) => return true, @@ -394,7 +394,7 @@ fn lease(conn: &mut Connection, id: StreamId, want: usize, received: &mut Receiv let chunk = lease.copy_to_bytes(lease.chunk().len()); received.bytes += chunk.len() as u64; } - let _ = stream.release(lease); + stream.release(lease); if fin { received.finished += 1; let _ = stream.stop(VarInt::from_u32(0)); diff --git a/rs/moq-quic/benches/receive.rs b/rs/moq-quic/benches/receive.rs index 094d6d5910e..365366c18c7 100644 --- a/rs/moq-quic/benches/receive.rs +++ b/rs/moq-quic/benches/receive.rs @@ -297,7 +297,7 @@ fn bench_reset_debt(c: &mut Criterion) { let mut stream = conn.recv_stream(id); let mut chunks = stream.read().unwrap(); assert!(matches!(chunks.next(usize::MAX), Err(recv::Error::Reset(_)))); - let _ = chunks.finalize(); + chunks.finalize(); } let mut limits = moq_quic::Limits::default(); limits.receive_window = std::num::NonZeroU64::new(window as u64); diff --git a/rs/moq-quic/benches/receive_bursts.rs b/rs/moq-quic/benches/receive_bursts.rs index 01df7c6d5eb..ca993160719 100644 --- a/rs/moq-quic/benches/receive_bursts.rs +++ b/rs/moq-quic/benches/receive_bursts.rs @@ -169,7 +169,7 @@ fn burst(pair: &mut Pair, mode: Mode, streams: usize, packets: usize) -> isize { } } pair.server.received.finished += 1; - let _ = chunks.finalize(); + chunks.finalize(); } } pair.server.busy += start.elapsed(); diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index f04957267cf..11060e71813 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -106,7 +106,7 @@ pub use stats::{CreditStats, FrameStats, PathStats, Stats, UdpStats}; mod streams; pub(crate) use streams::StreamsState; -pub use streams::{ShouldTransmit, Streams, recv, send}; +pub use streams::{Streams, recv, send}; mod timer; @@ -1383,7 +1383,7 @@ impl Connection { .streams .receive_rate_deadline() .is_some_and(|deadline| deadline <= now) - && self.streams.grant_receive_rate(now).should_transmit() + && self.streams.grant_receive_rate(now) { self.spaces[SpaceId::Data].pending.max_data = true; } @@ -1728,7 +1728,7 @@ impl Connection { limits.validate()?; // The cap first, so a window opened with it is paced rather than granted at once. if let Some(rate) = limits.receive_rate - && self.streams.set_receive_rate(rate).should_transmit() + && self.streams.set_receive_rate(rate) { self.spaces[SpaceId::Data].pending.max_data = true; } @@ -3097,7 +3097,7 @@ impl Connection { let payload = packet.payload.freeze(); // Earned before anything this packet carries is charged against the cap, and granted now: // the refill retires the credit deadline, so a later timeout would not grant it. - if self.streams.grant_receive_rate(now).should_transmit() { + if self.streams.grant_receive_rate(now) { self.spaces[SpaceId::Data].pending.max_data = true; } let mut is_probing_packet = true; @@ -3144,7 +3144,7 @@ impl Connection { self.read_crypto(SpaceId::Data, &frame, payload_len)?; } Frame::Stream(frame) => { - if self.streams.received(frame, payload_len)?.should_transmit() { + if self.streams.received(frame, payload_len)? { self.spaces[SpaceId::Data].pending.max_data = true; } } @@ -3202,7 +3202,7 @@ impl Connection { err.frame = Some(frame::FrameType::RESET_STREAM_AT); return Err(err); } - if self.streams.received_reset(frame)?.should_transmit() { + if self.streams.received_reset(frame)? { self.spaces[SpaceId::Data].pending.max_data = true; } } diff --git a/rs/moq-quic/src/connection/mux/mod.rs b/rs/moq-quic/src/connection/mux/mod.rs index 318eb09bf03..65045e24fc1 100644 --- a/rs/moq-quic/src/connection/mux/mod.rs +++ b/rs/moq-quic/src/connection/mux/mod.rs @@ -733,7 +733,7 @@ impl Connection { err.frame = Some(frame::FrameType::RESET_STREAM_AT); return Err(err); } - if self.streams.received_reset(reset)?.should_transmit() { + if self.streams.received_reset(reset)? { self.pending.max_data = true; } } @@ -799,7 +799,7 @@ impl Connection { } fn on_stream(&mut self, stream: frame::Stream, allocation: usize) -> Result<(), TransportError> { - if self.streams.received(stream, allocation)?.should_transmit() { + if self.streams.received(stream, allocation)? { self.pending.max_data = true; } Ok(()) diff --git a/rs/moq-quic/src/connection/mux/tests.rs b/rs/moq-quic/src/connection/mux/tests.rs index 539db83c4f0..1dcc4f31479 100644 --- a/rs/moq-quic/src/connection/mux/tests.rs +++ b/rs/moq-quic/src/connection/mux/tests.rs @@ -77,7 +77,7 @@ fn read_all(conn: &mut Connection, id: StreamId) -> (Vec, bool) { Err(err) => panic!("read failed: {err}"), } } - let _ = chunks.finalize(); + chunks.finalize(); (data, fin) } @@ -444,7 +444,7 @@ fn reset_stream_at_delivers_the_prefix() { Err(err) => break err, } }; - let _ = chunks.finalize(); + chunks.finalize(); assert!(data.starts_with(b"header"), "{data:?} {err:?}"); assert_eq!(err, recv::Error::Reset(VarInt::from_u32(9))); } diff --git a/rs/moq-quic/src/connection/streams/lease.rs b/rs/moq-quic/src/connection/streams/lease.rs index 434a05b2a28..ecf377ef018 100644 --- a/rs/moq-quic/src/connection/streams/lease.rs +++ b/rs/moq-quic/src/connection/streams/lease.rs @@ -7,8 +7,8 @@ use std::task::Poll; use bytes::{Buf, BufMut, Bytes, BytesMut}; +use super::StreamId; use super::recv::{Error, Stream}; -use super::{ShouldTransmit, StreamId}; use super::{StreamsState, state::get_or_insert_recv}; use crate::VarInt; use crate::connection::assembler::Assembler; @@ -262,8 +262,7 @@ impl Stream<'_> { /// /// The bytes keep their connection credit until the [`Lease`] is handed back with /// [`Self::release()`], and those not read are leased again next time. Hand every lease - /// back before leasing again. If the returned [`ShouldTransmit::should_transmit()`] is - /// `true`, transmit soon, whatever the result. + /// back before leasing again. /// /// [`Error::Blocked`] records the `want`, so the next wake of a [`Self::poll_lease()`] on this /// stream means it may be met, and so that a connection window every reader is waiting on @@ -286,23 +285,23 @@ impl Stream<'_> { /// /// # Panics /// - when the last lease of the stream was not handed back - #[must_use = "a lease may need a transmit"] - pub fn lease(&mut self, want: usize) -> (Result, ShouldTransmit) { + pub fn lease(&mut self, want: usize) -> Result { let (result, transmit) = self.lease_inner(want); - (result, self.transmit(transmit)) + self.transmit(transmit); + result } - fn lease_inner(&mut self, want: usize) -> (Result, ShouldTransmit) { + fn lease_inner(&mut self, want: usize) -> (Result, bool) { // Bytes out in a lease do not count: a unit that outgrows the window with them is one // the stuck window evicts by rank, so it gives way like any other rather than failing // alone. let want = want as u64; if want > self.state.receive_window { - return (Err(Error::TooLarge), ShouldTransmit(false)); + return (Err(Error::TooLarge), false); } let transmit = match self.fill() { Ok(transmit) => transmit, - Err(err) => return (Err(err), ShouldTransmit(false)), + Err(err) => return (Err(err), false), }; let lease = self.state.lease_mut(self.id).expect("filled above"); if lease.len >= want || lease.is_fin() { @@ -319,7 +318,7 @@ impl Stream<'_> { match lease.end { Some(End::Fin) => unreachable!("handed out above"), Some(End::Reset(code)) => { - debug_assert!(!transmit.should_transmit(), "a reset stream takes no credit"); + debug_assert!(!transmit, "a reset stream takes no credit"); (Err(Error::Reset(code)), transmit) } None => { @@ -328,10 +327,7 @@ impl Stream<'_> { self.state.unlooked.insert(self.id); // An eviction always has a reader to wake, even with no `MAX_DATA` due yet. let evicted = self.unstick(); - ( - Err(Error::Blocked), - ShouldTransmit(transmit.should_transmit() || evicted), - ) + (Err(Error::Blocked), transmit || evicted) } } } @@ -340,15 +336,13 @@ impl Stream<'_> { /// /// Pending registers `waiter` until more arrives, the stream ends, or an eviction fails it. /// Bytes that arrived stay leasable after the connection is lost, and only then does this - /// fail with [`Error::ConnectionLost`]. As with [`Self::lease()`], transmit soon if the - /// returned [`ShouldTransmit::should_transmit()`] is `true`, whatever the result. + /// fail with [`Error::ConnectionLost`]. /// /// # Panics /// - when the last lease of the stream was not handed back - #[must_use = "a lease may need a transmit"] - pub fn poll_lease(&mut self, waiter: &kio::Waiter, want: usize) -> (Poll>, ShouldTransmit) { - let (result, transmit) = self.lease(want); - let poll = match result { + pub fn poll_lease(&mut self, waiter: &kio::Waiter, want: usize) -> Poll> { + let result = self.lease(want); + match result { // A stuck window may have evicted this very stream, waking its readers before this // one parked. Err(Error::Blocked) if self.state.lease_mut(self.id).is_some_and(|lease| lease.evicted) => { @@ -360,60 +354,55 @@ impl Stream<'_> { Poll::Pending } result => Poll::Ready(result), - }; - (poll, transmit) + } } /// Hand a lease back, keeping the connection credit of the bytes read from it /// /// They wait for a later [`Self::release()`], as the start of a message read only in part - /// does. If [`ShouldTransmit::should_transmit()`] returns `true`, transmit soon: the window - /// may have been stuck on this lease. - pub fn restore(&mut self, lease: Lease) -> ShouldTransmit { + /// does. + pub fn restore(&mut self, lease: Lease) { self.put_back(lease); // Only a stuck pass that waited reruns: a reader handing back what it looked at is not // stuck itself, and one that is waits for more with its next lease. - let transmit = ShouldTransmit(self.state.stuck_waiting && self.unstick()); - self.transmit(transmit) + let transmit = self.state.stuck_waiting && self.unstick(); + self.transmit(transmit); } /// Hand a lease back, returning the connection credit of every byte read from it or from an /// earlier one, once the units they belong to are complete - /// - /// If [`ShouldTransmit::should_transmit()`] returns `true`, transmit soon so the peer learns - /// of the larger window. - pub fn release(&mut self, lease: Lease) -> ShouldTransmit { + pub fn release(&mut self, lease: Lease) { self.put_back(lease); let transmit = self.release_credit(); - self.transmit(transmit) + self.transmit(transmit); } - /// Wake the driver when `transmit` says a frame is due, passing it on - fn transmit(&mut self, transmit: ShouldTransmit) -> ShouldTransmit { - if transmit.should_transmit() { + /// Wake the driver when a frame is due + fn transmit(&mut self, transmit: bool) { + if transmit { self.state.waits.wake_transmit(); } - transmit } /// Return the credit of every byte read out of a lease. - fn release_credit(&mut self) -> ShouldTransmit { + fn release_credit(&mut self) -> bool { let Some(lease) = self.state.lease_mut(self.id) else { - return ShouldTransmit(false); + return false; }; let freed = lease.release(); if lease.evicted { self.state.evicted_held -= freed; } let transmit = self.return_credit(freed); - ShouldTransmit(transmit.should_transmit() || self.unstick()) + transmit || self.unstick() } /// [`Self::lease()`] and hand it straight back, for tests that read through the lease's /// bookkeeping rather than a [`Lease`]. #[cfg(test)] - pub(crate) fn peek(&mut self, want: usize) -> (Result<(), Error>, ShouldTransmit) { - let (lease, transmit) = self.lease(want); + pub(crate) fn peek(&mut self, want: usize) -> (Result<(), Error>, bool) { + let (lease, transmit) = self.lease_inner(want); + self.transmit(transmit); (lease.map(|lease| self.put_back(lease)), transmit) } @@ -439,9 +428,10 @@ impl Stream<'_> { /// Return the credit of every byte read out, as [`Self::release()`] does. #[cfg(test)] - pub(crate) fn release_taken(&mut self) -> ShouldTransmit { + pub(crate) fn release_taken(&mut self) -> bool { let transmit = self.release_credit(); - self.transmit(transmit) + self.transmit(transmit); + transmit } /// Put the bytes `lease` did not read back in front of the stream's lease. @@ -475,7 +465,7 @@ impl Stream<'_> { } /// Move what has arrived in order from the stream's receive buffer into its lease. - fn fill(&mut self) -> Result { + fn fill(&mut self) -> Result { let Some(slot) = self.state.recv.get_mut(&self.id) else { return Err(Error::Closed); }; @@ -500,19 +490,19 @@ impl Stream<'_> { }; } let (_, transmit) = recv.max_stream_data(self.state.stream_receive_window); - if transmit.should_transmit() { + if transmit { self.pending.max_stream_data.insert(self.id); } Ok(transmit) } /// Return `bytes` of withheld connection credit to the peer. - fn return_credit(&mut self, bytes: u64) -> ShouldTransmit { + fn return_credit(&mut self, bytes: u64) -> bool { if bytes == 0 { - return ShouldTransmit(false); + return false; } let transmit = self.state.release(bytes); - self.pending.max_data |= transmit.should_transmit(); + self.pending.max_data |= transmit; transmit } @@ -523,7 +513,7 @@ impl Stream<'_> { let Some(transmit) = self.state.evict_if_stuck() else { return false; }; - self.pending.max_data |= transmit.should_transmit(); + self.pending.max_data |= transmit; true } } @@ -543,7 +533,7 @@ impl StreamsState { } /// Return `bytes` of credit withheld by a lease to the peer. - pub(super) fn release(&mut self, bytes: u64) -> ShouldTransmit { + pub(super) fn release(&mut self, bytes: u64) -> bool { assert!( bytes <= self.held, "released {bytes} bytes of credit with only {} held", @@ -555,7 +545,7 @@ impl StreamsState { /// Take the lease of `id`, stopped or freed, out of the eviction order and return the credit /// of the bytes it holds. - pub(super) fn discard_lease(&mut self, id: StreamId, lease: &mut Leased) -> ShouldTransmit { + pub(super) fn discard_lease(&mut self, id: StreamId, lease: &mut Leased) -> bool { if std::mem::take(&mut lease.ranked) { self.ranks.remove(&lease.rank(id)); } @@ -586,7 +576,7 @@ impl StreamsState { /// /// Returns `None` if nothing was evicted, or whether to transmit the `MAX_DATA` the evictions /// freed; each evicted stream's reader wakes. - pub(super) fn evict_if_stuck(&mut self) -> Option { + pub(super) fn evict_if_stuck(&mut self) -> Option { self.stuck_waiting = false; // The window, not the credit a rate cap paces, says whether memory is stuck: a capped // peer out of credit is waiting on time, not on a reader. @@ -663,7 +653,7 @@ impl StreamsState { /// /// The stream stays until the application stops it, which tells the peer, and bytes the /// reader took live on outside the connection until then, so their credit stays held too. - fn evict(&mut self, id: StreamId) -> ShouldTransmit { + fn evict(&mut self, id: StreamId) -> bool { tracing::debug!(%id, "evicting a stream to free a stuck receive window"); let recv = self .recv diff --git a/rs/moq-quic/src/connection/streams/mod.rs b/rs/moq-quic/src/connection/streams/mod.rs index 1110ed3c09b..76d48ad819b 100644 --- a/rs/moq-quic/src/connection/streams/mod.rs +++ b/rs/moq-quic/src/connection/streams/mod.rs @@ -183,21 +183,6 @@ impl StreamsState { } } -/// Indicates whether a frame needs to be transmitted -/// -/// This type wraps around bool and uses the `#[must_use]` attribute in order -/// to prevent accidental loss of the frame transmission requirement. -#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)] -#[must_use = "A frame might need to be enqueued"] -pub struct ShouldTransmit(bool); - -impl ShouldTransmit { - /// Returns whether a frame should be transmitted - pub fn should_transmit(self) -> bool { - self.0 - } -} - #[derive(Debug, Copy, Clone, Eq, PartialEq)] enum StreamHalf { Send, diff --git a/rs/moq-quic/src/connection/streams/recv.rs b/rs/moq-quic/src/connection/streams/recv.rs index 7bcf6c1b15e..ab5bf48b0fc 100644 --- a/rs/moq-quic/src/connection/streams/recv.rs +++ b/rs/moq-quic/src/connection/streams/recv.rs @@ -8,7 +8,7 @@ use tracing::debug; use super::lease::Leased; use super::state::get_or_insert_recv; -use super::{Retransmits, ShouldTransmit, StreamId, StreamsState}; +use super::{Retransmits, StreamId, StreamsState}; use crate::connection::assembler::Assembler; pub use crate::connection::assembler::Chunk; use crate::connection::waits::Wait; @@ -48,7 +48,7 @@ impl Stream<'_> { true => (0, stream.final_offset_unknown()), false => { let (credits, stop_sending) = stream.stop()?; - (credits, stop_sending.should_transmit()) + (credits, stop_sending) } }; let mut lease = std::mem::take(&mut stream.lease); @@ -71,7 +71,7 @@ impl Stream<'_> { let leased = self.state.discard_lease(self.id, &mut lease); let transmit = self.state.add_read_credits(read_credits); - self.pending.max_data |= leased.should_transmit() || transmit.should_transmit(); + self.pending.max_data |= leased || transmit; // The credit returned may only have paid a shrunk window's debt. self.unstick(); // A reader parked on the stream learns it is closed. @@ -112,7 +112,7 @@ impl Stream<'_> { let transmit = self .state .stream_recv_freed(self.id, recv.expect("must have recv on reset")); - self.pending.max_data |= transmit.should_transmit(); + self.pending.max_data |= transmit; self.state.queue_max_stream_id(self.pending); // The credit returned may only have paid a shrunk window's debt. self.unstick(); @@ -226,7 +226,7 @@ impl Recv { Ok((new_bytes, frame.fin && self.stopped)) } - pub(super) fn stop(&mut self) -> Result<(u64, ShouldTransmit), Error> { + pub(super) fn stop(&mut self) -> Result<(u64, bool), Error> { if self.stopped { return Err(Error::Closed); } @@ -239,7 +239,7 @@ impl Recv { let stop_sending = self.is_receiving() && !self.is_read(); self.stopped = true; self.assembler.clear(); - Ok((read_credits, ShouldTransmit(stop_sending))) + Ok((read_credits, stop_sending)) } /// The connection credit held for data the application has not read: everything below the @@ -263,7 +263,7 @@ impl Recv { /// transmission of the value is recommended. If the boolean value is /// `false` the new window should only be transmitted if a previous transmission /// had failed. - pub(super) fn max_stream_data(&mut self, stream_receive_window: u64) -> (u64, ShouldTransmit) { + pub(super) fn max_stream_data(&mut self, stream_receive_window: u64) -> (u64, bool) { // A window shrunk since the last announcement cannot take back credit already granted. let max_stream_data = (self.assembler.bytes_read() + stream_receive_window).max(self.sent_max_stream_data); @@ -276,7 +276,7 @@ impl Recv { // does not get stuck. let diff = max_stream_data - self.sent_max_stream_data; let transmit = self.can_send_flow_control() && diff >= (stream_receive_window / 8); - (max_stream_data, ShouldTransmit(transmit)) + (max_stream_data, transmit) } /// Records that a `MAX_STREAM_DATA` announcing a certain window was sent @@ -486,7 +486,7 @@ impl<'a> Chunks<'a> { ChunksState::Readable(recv) => recv, _ => unreachable!("state must be ChunkState::Readable"), }; - self.pending.max_data |= self.streams.stream_recv_freed(self.id, recv).should_transmit(); + self.pending.max_data |= self.streams.stream_recv_freed(self.id, recv); Err(Error::Reset(error_code)) } RecvState::Recv { size } => { @@ -497,7 +497,7 @@ impl<'a> Chunks<'a> { ChunksState::Readable(recv) => recv, _ => unreachable!("state must be ChunkState::Readable"), }; - self.pending.max_data |= self.streams.stream_recv_freed(self.id, recv).should_transmit(); + self.pending.max_data |= self.streams.stream_recv_freed(self.id, recv); Ok(None) } else { // We don't need a distinct `ChunksState` variant for a blocked stream because @@ -531,19 +531,15 @@ impl<'a> Chunks<'a> { /// The number of read bytes will be released from the congestion window, /// allowing the remote peer to send more data if it was previously blocked. /// - /// If [`ShouldTransmit::should_transmit()`] returns `true`, - /// a packet needs to be sent to the peer informing them that the stream is unblocked. - /// This means that you should call [`Connection::poll_transmit()`][crate::connection::Connection::poll_transmit] - /// and send the returned packet as soon as is reasonable, to unblock the remote peer. - pub fn finalize(mut self) -> ShouldTransmit { - self.finalize_inner() + pub fn finalize(mut self) { + self.finalize_inner(); } - fn finalize_inner(&mut self) -> ShouldTransmit { + fn finalize_inner(&mut self) { let state = mem::replace(&mut self.state, ChunksState::Finalized); if let ChunksState::Finalized = state { // Noop on repeated calls - return ShouldTransmit(false); + return; } // We issue additional stream ID credit after the application is notified that a previously @@ -554,8 +550,8 @@ impl<'a> Chunks<'a> { // If the stream hasn't finished, we may need to issue stream-level flow control credit if let ChunksState::Readable(mut rs) = state { let (_, max_stream_data) = rs.max_stream_data(self.streams.stream_receive_window); - should_transmit |= max_stream_data.0; - if max_stream_data.0 { + should_transmit |= max_stream_data; + if max_stream_data { self.pending.max_stream_data.insert(self.id); } // Return the stream to storage for future use @@ -564,18 +560,17 @@ impl<'a> Chunks<'a> { // Issue connection-level flow control credit for any data we read regardless of state let max_data = self.streams.add_read_credits(self.read); - self.pending.max_data |= max_data.0; - should_transmit |= max_data.0; + self.pending.max_data |= max_data; + should_transmit |= max_data; if should_transmit { self.streams.waits.wake_transmit(); } - ShouldTransmit(should_transmit) } } impl Drop for Chunks<'_> { fn drop(&mut self) { - let _ = self.finalize_inner(); + self.finalize_inner(); } } @@ -681,7 +676,7 @@ mod tests { assert!(!is_closed); let (credits, transmit) = s.stop().unwrap(); - assert!(transmit.should_transmit()); + assert!(transmit); assert_eq!( credits, INITIAL_OFFSET + INITIAL_BYTES, @@ -689,7 +684,7 @@ mod tests { ); let (max_stream_data, transmit) = s.max_stream_data(RECV_WINDOW); - assert!(!transmit.should_transmit()); + assert!(!transmit); assert_eq!( max_stream_data, RECV_WINDOW, "stream flow control credit isn't issued by stop" @@ -714,7 +709,7 @@ mod tests { assert!(!is_closed); let (max_stream_data, transmit) = s.max_stream_data(RECV_WINDOW); - assert!(!transmit.should_transmit()); + assert!(!transmit); assert_eq!( max_stream_data, RECV_WINDOW, "stream flow control credit isn't issued after stop" @@ -741,7 +736,7 @@ mod tests { assert!(!is_closed); let (max_stream_data, transmit) = s.max_stream_data(RECV_WINDOW); - assert!(!transmit.should_transmit()); + assert!(!transmit); assert_eq!( max_stream_data, RECV_WINDOW, "stream flow control credit isn't issued after stop" diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index 1f147533e1f..15763a8cc52 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -11,7 +11,7 @@ use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet}; use tracing::{debug, trace}; use super::{ - Next, Recv, Retransmits, Schedule, Send, SendState, ShouldTransmit, StreamHalf, ThinRetransmits, + Next, Recv, Retransmits, Schedule, Send, SendState, StreamHalf, ThinRetransmits, lease::Rank, send::{End, Error}, }; @@ -244,11 +244,7 @@ impl StreamsState { /// Process incoming stream frame /// /// If successful, returns whether a `MAX_DATA` frame needs to be transmitted - pub(crate) fn received( - &mut self, - frame: frame::Stream, - payload_len: usize, - ) -> Result { + pub(crate) fn received(&mut self, frame: frame::Stream, payload_len: usize) -> Result { let id = frame.id; self.validate_receive_id(id).inspect_err(|_| { debug!("received illegal STREAM frame"); @@ -262,12 +258,12 @@ impl StreamsState { .map(|opt| get_or_insert_recv(opt, self.initial_stream_receive_window)) else { trace!("dropping frame for closed stream"); - return Ok(ShouldTransmit(false)); + return Ok(false); }; if !rs.is_receiving() { trace!("dropping frame for finished stream"); - return Ok(ShouldTransmit(false)); + return Ok(false); } let (new_bytes, closed) = rs.ingest(frame, payload_len, self.data_recvd, self.local_max_data)?; @@ -281,7 +277,7 @@ impl StreamsState { self.unlooked.insert(id); // Credit released while the peer still had room may not have been worth a frame // then; it is now that the peer is about to block. - ShouldTransmit(self.max_data_due()) + self.max_data_due() } else { // Stopped streams become closed instantly on FIN, so check whether we need to clean up. // An evicted one waits for the application's stop, which still holds its lease. @@ -320,15 +316,15 @@ impl StreamsState { /// Rerun the stuck check after an arrival, which may spend the last of the window or return /// credit that only pays shrink debt, adding any `MAX_DATA` it frees to `transmit`. - fn evict_after_arrival(&mut self, transmit: ShouldTransmit) -> ShouldTransmit { + fn evict_after_arrival(&mut self, transmit: bool) -> bool { let evicted = self.evict_if_stuck(); - ShouldTransmit(transmit.should_transmit() || evicted.is_some_and(ShouldTransmit::should_transmit)) + transmit || evicted.unwrap_or(false) } /// Process incoming RESET_STREAM frame /// /// If successful, returns whether a `MAX_DATA` frame needs to be transmitted - pub(crate) fn received_reset(&mut self, frame: frame::ResetStream) -> Result { + pub(crate) fn received_reset(&mut self, frame: frame::ResetStream) -> Result { let frame::ResetStream { id, error_code, @@ -350,7 +346,7 @@ impl StreamsState { .map(|opt| get_or_insert_recv(opt, self.initial_stream_receive_window)) else { trace!("received RESET_STREAM on closed stream"); - return Ok(ShouldTransmit(false)); + return Ok(false); }; // State transition @@ -364,7 +360,7 @@ impl StreamsState { )? else { // Redundant reset - return Ok(ShouldTransmit(false)); + return Ok(false); }; let (stopped, evicted) = (rs.stopped, rs.lease.evicted); let rank = (!stopped).then(|| rs.lease.arrived(id)).flatten(); @@ -387,7 +383,7 @@ impl StreamsState { // Update connection-level flow control let transmit = match credit { - 0 => ShouldTransmit(false), + 0 => false, credit => self.add_read_credits(credit), }; if new_bytes == 0 && credit == 0 { @@ -1110,7 +1106,7 @@ impl StreamsState { /// queued, the [`Recv::record_sent_max_stream_data`] function should be called to /// suppress sending further updates until the window increases significantly /// again. - pub(super) fn add_read_credits(&mut self, credits: u64) -> ShouldTransmit { + pub(super) fn add_read_credits(&mut self, credits: u64) -> bool { if credits > self.receive_window_shrink_debt { let net_credits = credits - self.receive_window_shrink_debt; self.window_max_data = self.window_max_data.saturating_add(net_credits); @@ -1120,7 +1116,7 @@ impl StreamsState { } self.grant(); - ShouldTransmit(self.max_data_due()) + self.max_data_due() } /// Raise `local_max_data` as far as the window and any rate cap allow, returning whether @@ -1152,14 +1148,14 @@ impl StreamsState { /// Cap what the peer sends, or lift the cap, returning whether a `MAX_DATA` frame is due. /// /// A new cap starts with a full burst, which counts the credit already outstanding. - pub(crate) fn set_receive_rate(&mut self, rate: Rate) -> ShouldTransmit { + pub(crate) fn set_receive_rate(&mut self, rate: Rate) -> bool { match (rate, &mut self.receive_rate) { (Rate::Unlimited, bucket) => *bucket = None, (Rate::BytesPerSecond(rate), Some(bucket)) => bucket.set_rate(rate), (Rate::BytesPerSecond(rate), bucket @ None) => *bucket = Some(Bucket::new(rate)), } self.grant(); - ShouldTransmit(self.max_data_due()) + self.max_data_due() } /// Earn the rate cap's credit up to `now`. @@ -1203,10 +1199,10 @@ impl StreamsState { } /// Grant the rate cap's credit earned by `now`, returning whether a `MAX_DATA` frame is due. - pub(crate) fn grant_receive_rate(&mut self, now: Instant) -> ShouldTransmit { + pub(crate) fn grant_receive_rate(&mut self, now: Instant) -> bool { self.refill_receive_rate(now); self.grant(); - ShouldTransmit(self.max_data_due()) + self.max_data_due() } /// Whether the window has grown enough since the last `MAX_DATA` to be worth announcing. @@ -1296,7 +1292,7 @@ impl StreamsState { /// the instructions a burst costs (`benches/receive_bursts.rs`). /// /// Returns whether a `MAX_DATA` frame should be enqueued for that credit. - pub(super) fn stream_recv_freed(&mut self, id: StreamId, mut recv: Box) -> ShouldTransmit { + pub(super) fn stream_recv_freed(&mut self, id: StreamId, mut recv: Box) -> bool { let transmit = self.discard_lease(id, &mut recv.lease); self.unlooked.remove(&id); self.stream_freed(id, StreamHalf::Recv); @@ -1724,8 +1720,8 @@ mod tests { let id = StreamId::new(Side::Server, Dir::Uni, 0); let initial_max = client.local_max_data; const MESSAGE_SIZE: usize = 2048; - assert_eq!( - client + assert!( + !client .received( frame::Stream { id, @@ -1735,8 +1731,7 @@ mod tests { }, 2048 ) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.data_recvd, 2048); assert_eq!(client.local_max_data - initial_max, 0); @@ -1751,8 +1746,7 @@ mod tests { let mut chunks = recv.read().unwrap(); assert_eq!(chunks.next(MESSAGE_SIZE).unwrap().unwrap().bytes.len(), MESSAGE_SIZE); assert!(chunks.next(0).unwrap().is_none()); - let should_transmit = chunks.finalize(); - assert!(should_transmit.0); + chunks.finalize(); assert!(pending.max_stream_id[Dir::Uni as usize]); assert_eq!(client.local_max_data - initial_max, MESSAGE_SIZE as u64); } @@ -1770,16 +1764,15 @@ mod tests { (1024 * 1024u32).into(), ); let id = StreamId::new(Side::Server, Dir::Uni, 0); - assert_eq!( - client + assert!( + !client .received_reset(frame::ResetStream { id, error_code: 0u32.into(), final_offset: 0u32.into(), reliable_size: None, }) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); let mut pending = Retransmits::default(); @@ -1798,8 +1791,8 @@ mod tests { let mut client = make(Side::Client); let id = StreamId::new(Side::Server, Dir::Uni, 0); let initial_max = client.local_max_data; - assert_eq!( - client + assert!( + !client .received( frame::Stream { id, @@ -1809,8 +1802,7 @@ mod tests { }, 2048 ) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.data_recvd, 2048); assert_eq!(client.local_max_data - initial_max, 0); @@ -1824,18 +1816,17 @@ mod tests { let mut chunks = recv.read().unwrap(); chunks.next(1024).unwrap(); - let _ = chunks.finalize(); + chunks.finalize(); assert_eq!(client.local_max_data - initial_max, 1024); - assert_eq!( - client + assert!( + !client .received_reset(frame::ResetStream { id, error_code: 0u32.into(), final_offset: 4096u32.into(), reliable_size: None, }) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.data_recvd, 4096); @@ -1849,7 +1840,7 @@ mod tests { }; let mut chunks = recv.read().unwrap(); assert_eq!(chunks.next(1024).unwrap_err(), crate::recv::Error::Reset(0u32.into())); - let _ = chunks.finalize(); + chunks.finalize(); assert_eq!(client.data_recvd, 4096); assert_eq!(client.local_max_data - initial_max, 4096); } @@ -1859,8 +1850,8 @@ mod tests { let mut client = make(Side::Client); let id = StreamId::new(Side::Server, Dir::Uni, 0); let initial_max = client.local_max_data; - assert_eq!( - client + assert!( + !client .received( frame::Stream { id, @@ -1870,21 +1861,19 @@ mod tests { }, 0 ) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.data_recvd, 4096); assert_eq!(client.local_max_data - initial_max, 0); - assert_eq!( - client + assert!( + !client .received_reset(frame::ResetStream { id, error_code: 0u32.into(), final_offset: 4096u32.into(), reliable_size: None, }) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.data_recvd, 4096); assert_eq!(client.local_max_data - initial_max, 4096); @@ -1894,28 +1883,26 @@ mod tests { fn duplicate_reset_flow_control() { let mut client = make(Side::Client); let id = StreamId::new(Side::Server, Dir::Uni, 0); - assert_eq!( - client + assert!( + !client .received_reset(frame::ResetStream { id, error_code: 0u32.into(), final_offset: 4096u32.into(), reliable_size: None, }) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.data_recvd, 4096); - assert_eq!( - client + assert!( + !client .received_reset(frame::ResetStream { id, error_code: 0u32.into(), final_offset: 4096u32.into(), reliable_size: None, }) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.data_recvd, 4096); } @@ -1925,8 +1912,8 @@ mod tests { let mut client = make(Side::Client); let id = StreamId::new(Side::Server, Dir::Uni, 0); let initial_max = client.local_max_data; - assert_eq!( - client + assert!( + !client .received( frame::Stream { id, @@ -1936,8 +1923,7 @@ mod tests { }, 32 ) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.local_max_data, initial_max); @@ -1956,8 +1942,8 @@ mod tests { assert_eq!(recv.read().err(), Some(recv::Error::Closed)); assert_eq!(client.local_max_data - initial_max, 32); - assert_eq!( - client + assert!( + !client .received( frame::Stream { id, @@ -1967,8 +1953,7 @@ mod tests { }, 16 ) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert_eq!(client.local_max_data - initial_max, 48); assert!(!client.recv.contains_key(&id)); @@ -1981,8 +1966,8 @@ mod tests { let initial_max_data = client.local_max_data; let id = StreamId::new(Side::Server, Dir::Uni, 0); // Server opens stream - assert_eq!( - client + assert!( + !client .received( frame::Stream { id, @@ -1992,8 +1977,7 @@ mod tests { }, 32 ) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); let mut pending = Retransmits::default(); @@ -2010,16 +1994,15 @@ mod tests { // Server complies let prev_max = client.max_remote[Dir::Uni as usize]; - assert_eq!( - client + assert!( + !client .received_reset(frame::ResetStream { id, error_code: 0u32.into(), final_offset: final_offset.into(), reliable_size: None, }) - .unwrap(), - ShouldTransmit(false) + .unwrap() ); assert!(!client.recv.contains_key(&id), "stream state is freed"); assert_eq!(client.max_remote[Dir::Uni as usize], prev_max + 1); @@ -2542,7 +2525,7 @@ mod tests { }, 0 ), - Ok(ShouldTransmit(false)) + Ok(false) ); // Try to open stream 128, exceeding limit assert_eq!( @@ -2581,7 +2564,7 @@ mod tests { }, 0 ), - Ok(ShouldTransmit(false)) + Ok(false) ); } @@ -2599,7 +2582,7 @@ mod tests { }, 0 ), - Ok(ShouldTransmit(false)) + Ok(false) ); // Try to open stream 128, exceeding limit assert_eq!( @@ -2632,7 +2615,7 @@ mod tests { }, 0 ), - Ok(ShouldTransmit(false)) + Ok(false) ); } @@ -2650,7 +2633,7 @@ mod tests { }, 0 ), - Ok(ShouldTransmit(false)) + Ok(false) ); // Tighten limit by one @@ -2690,7 +2673,7 @@ mod tests { final_offset: 0u32.into(), reliable_size: None, }), - Ok(ShouldTransmit(false)) + Ok(false) ); let mut pending = Retransmits::default(); let mut stream = recv::Stream { @@ -2711,7 +2694,7 @@ mod tests { }, 0 ), - Ok(ShouldTransmit(false)) + Ok(false) ); } @@ -2761,7 +2744,7 @@ mod tests { }, 8, ), - Ok(ShouldTransmit(false)) + Ok(false) ); assert_eq!(client.next_remote[Dir::Uni as usize], 6); assert_eq!( @@ -2774,7 +2757,7 @@ mod tests { }, 4, ), - Ok(ShouldTransmit(false)) + Ok(false) ); let id = StreamId::new(Side::Server, Dir::Uni, 3); let mut pending = Retransmits::default(); @@ -2785,7 +2768,7 @@ mod tests { }; let mut chunks = recv.read().unwrap(); assert_eq!(chunks.next(4).unwrap().unwrap().bytes.len(), 4); - let _ = chunks.finalize(); + chunks.finalize(); } #[test] @@ -2803,7 +2786,7 @@ mod tests { }, 4, ), - Ok(ShouldTransmit(false)) + Ok(false) ); // Drain the stream so it's fully freed let mut pending = Retransmits::default(); @@ -2815,7 +2798,7 @@ mod tests { let mut chunks = recv.read().unwrap(); assert_eq!(chunks.next(4).unwrap().unwrap().bytes.len(), 4); assert!(chunks.next(4).unwrap().is_none()); - let _ = chunks.finalize(); + chunks.finalize(); assert!(!client.recv.contains_key(&id)); // A stray retransmit must not resurrect the stream @@ -2829,7 +2812,7 @@ mod tests { }, 4, ), - Ok(ShouldTransmit(false)) + Ok(false) ); assert!(!client.recv.contains_key(&id)); } @@ -2853,7 +2836,7 @@ mod tests { }, 1, ), - Ok(ShouldTransmit(false)) + Ok(false) ); let mut pending = Retransmits::default(); let mut recv = recv::Stream { @@ -2864,7 +2847,7 @@ mod tests { let mut chunks = recv.read().unwrap(); let _ = chunks.next(1).unwrap(); assert!(chunks.next(1).unwrap().is_none()); - let _ = chunks.finalize(); + chunks.finalize(); } assert_eq!(client.recv.len(), 0); assert_eq!(client.send.len(), 0); @@ -2886,7 +2869,7 @@ mod tests { let should_transmit = server.add_read_credits(credits); assert_eq!(server.receive_window_shrink_debt, 0); assert_eq!(server.local_max_data, prev_local_max_data + credits); - assert!(should_transmit.should_transmit()); + assert!(should_transmit); } /// A stream may still hold credit granted under a larger window, so the bound reported to @@ -2922,7 +2905,7 @@ mod tests { let should_transmit = server.add_read_credits(credits); assert_eq!(server.receive_window_shrink_debt, expected_receive_window_shrink_debt); assert_eq!(server.local_max_data, prev_local_max_data); - assert!(!should_transmit.should_transmit()); + assert!(!should_transmit); } // credit again which exceeds all remaining expected_receive_window_shrink_debt @@ -2940,7 +2923,7 @@ mod tests { let should_transmit = server.add_read_credits(credits); assert_eq!(server.receive_window_shrink_debt, 0); assert_eq!(server.local_max_data, expected_local_max_data); - assert!(should_transmit.should_transmit()); + assert!(should_transmit); } #[test] @@ -3152,7 +3135,7 @@ mod tests { Err(e) => break e, } }; - let _ = chunks.finalize(); + chunks.finalize(); (read, err) } @@ -3197,10 +3180,7 @@ mod tests { let _ = client.received_reset(reset_at(id, 7, 20, Some(4))).unwrap(); assert_eq!(client.local_max_data - initial_max, 16); // A reordered, larger Reliable Size is ignored - assert_eq!( - client.received_reset(reset_at(id, 7, 20, Some(8))), - Ok(ShouldTransmit(false)) - ); + assert_eq!(client.received_reset(reset_at(id, 7, 20, Some(8))), Ok(false)); assert_eq!(client.local_max_data - initial_max, 16); receive(&mut client, id, 0..20); assert_eq!(drain(&mut client, id), (4, crate::recv::Error::Reset(7u32.into()))); @@ -3564,11 +3544,7 @@ mod tests { } /// [`wait`], and whether it queued a frame. - fn wait_transmit( - client: &mut StreamsState, - id: StreamId, - want: usize, - ) -> (Result<(), recv::Error>, ShouldTransmit) { + fn wait_transmit(client: &mut StreamsState, id: StreamId, want: usize) -> (Result<(), recv::Error>, bool) { let mut pending = Retransmits::default(); let result = recv(client, &mut pending, id).peek(want); client.check_leases(); @@ -4177,7 +4153,7 @@ mod tests { assert_eq!(readers.woken(&mut client), [low]); assert!(!client.leased(low).unwrap().evicted); let blocked = wait_transmit(&mut client, low, 2000); - assert_eq!(blocked, (Err(recv::Error::Blocked), ShouldTransmit(true))); + assert_eq!(blocked, (Err(recv::Error::Blocked), true)); assert_eq!(readers.woken(&mut client), [low], "the evicted reader wakes"); assert_eq!(wait(&mut client, low, 1), Err(recv::Error::Evicted)); assert_eq!(client.credit_stats().leased, WINDOW); @@ -4217,9 +4193,9 @@ mod tests { // This wait spends the last byte of credit and evicts its own stream. let waiter = Counter::new(); - let (poll, transmit) = recv(&mut client, &mut pending, low).poll_lease(&waiter, 2000); + let poll = recv(&mut client, &mut pending, low).poll_lease(&waiter, 2000); assert!(matches!(poll, Poll::Ready(Err(recv::Error::Evicted)))); - assert!(transmit.should_transmit()); + assert!(pending.max_data); assert_eq!(client.credit_stats().evicted, 1); } @@ -4236,10 +4212,7 @@ mod tests { let taken = recv(&mut client, &mut pending, frame).take(usize::MAX); assert_eq!(taken.len(), WINDOW as usize); - assert_eq!( - wait_transmit(&mut client, frame, 1), - (Err(recv::Error::Blocked), ShouldTransmit(true)) - ); + assert_eq!(wait_transmit(&mut client, frame, 1), (Err(recv::Error::Blocked), true)); assert_eq!(wait(&mut client, frame, 1), Err(recv::Error::Evicted)); assert_eq!(client.local_max_data, WINDOW, "the taken bytes are still held"); drop(taken); @@ -4271,19 +4244,19 @@ mod tests { let mut pending = Retransmits::default(); let mut stream = recv(&mut client, &mut pending, id); - let lease = stream.lease(0).0.unwrap(); + let lease = stream.lease(0).unwrap(); assert_eq!(lease.remaining(), 600); assert!(!lease.is_fin()); assert_eq!(stream.state.leased(id).unwrap().len(), 0, "moved out, not copied"); - let _ = stream.restore(lease); + stream.restore(lease); assert_eq!(stream.state.credit_stats().leased, 600); - let mut lease = stream.lease(0).0.unwrap(); + let mut lease = stream.lease(0).unwrap(); assert_eq!(lease.remaining(), 600, "restored bytes are leased again"); lease.advance(250); - let _ = stream.release(lease); + stream.release(lease); assert_eq!(stream.state.credit_stats().leased, 350); - assert_eq!(stream.lease(0).0.unwrap().remaining(), 350); + assert_eq!(stream.lease(0).unwrap().remaining(), 350); } /// Leasing again before handing the last lease back would lose its bytes. @@ -4294,7 +4267,7 @@ mod tests { data(&mut client, id, 0, 100, false); let mut pending = Retransmits::default(); let mut stream = recv(&mut client, &mut pending, id); - let _lease = stream.lease(0).0.unwrap(); + let _lease = stream.lease(0).unwrap(); let _ = stream.lease(0); } @@ -4306,12 +4279,12 @@ mod tests { let (mut client, [old, new]) = stuck(WINDOW); data(&mut client, old, 0, WINDOW as usize, false); let mut pending = Retransmits::default(); - let held = recv(&mut client, &mut pending, old).lease(0).0.unwrap(); + let held = recv(&mut client, &mut pending, old).lease(0).unwrap(); data(&mut client, new, 0, WINDOW as usize, false); assert!(matches!(wait(&mut client, new, 2000), Err(recv::Error::Blocked))); assert_eq!(client.credit_stats().evicted, 0); - let _ = recv(&mut client, &mut pending, old).restore(held); + recv(&mut client, &mut pending, old).restore(held); assert_eq!(wait(&mut client, new, 1), Err(recv::Error::Evicted)); } @@ -4332,7 +4305,7 @@ mod tests { assert_eq!(lease(&mut client, fresh), WINDOW as usize); let (blocked, transmit) = wait_transmit(&mut client, fresh, 2000); assert_eq!(blocked, Err(recv::Error::Blocked)); - assert!(transmit.should_transmit()); + assert!(transmit); assert_eq!(wait(&mut client, fresh, 1), Err(recv::Error::Evicted)); assert_eq!(lease(&mut client, waiting), WINDOW as usize); } @@ -4363,7 +4336,7 @@ mod tests { let mut pending = Retransmits::default(); assert_eq!( recv(&mut client, &mut pending, waiting).peek(2 * WINDOW), - (Err(recv::Error::Blocked), ShouldTransmit(true)) + (Err(recv::Error::Blocked), true) ); assert!(client.leased(parked).unwrap().evicted); assert!(!pending.max_stream_id[Dir::Uni as usize], "its reader has yet to hear"); @@ -4401,7 +4374,7 @@ mod tests { let mut chunks = stream.read().unwrap(); assert_eq!(chunks.next(usize::MAX).unwrap().unwrap().bytes.len(), 10); assert_eq!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); assert!(client.leased(id).is_none()); recv(&mut client, &mut pending, id).set_priority(2); @@ -4542,13 +4515,10 @@ mod tests { let mut pending = Retransmits::default(); assert_eq!( recv(&mut client, &mut pending, id).peek(1001), - (Err(recv::Error::TooLarge), ShouldTransmit(false)) + (Err(recv::Error::TooLarge), false) ); assert!(pending.max_stream_data.is_empty()); - assert_eq!( - recv(&mut client, &mut pending, id).peek(0), - (Ok(()), ShouldTransmit(true)) - ); + assert_eq!(recv(&mut client, &mut pending, id).peek(0), (Ok(()), true)); assert!(pending.max_stream_data.contains(&id)); let id = StreamId::new(Side::Server, Dir::Uni, 1); @@ -4557,7 +4527,7 @@ mod tests { let mut pending = Retransmits::default(); assert_eq!( recv(&mut client, &mut pending, id).peek(11), - (Err(recv::Error::Reset(0u32.into())), ShouldTransmit(false)) + (Err(recv::Error::Reset(0u32.into())), false) ); assert!(!pending.max_stream_id[Dir::Uni as usize]); let pending = stop(&mut client, id, 0); @@ -4689,13 +4659,13 @@ mod tests { pending: &mut pending, }; if leased { - let mut lease = recv.lease(1).0.unwrap(); + let mut lease = recv.lease(1).unwrap(); std::hint::black_box(lease.copy_to_bytes(lease.remaining())); - let _ = recv.release(lease); + recv.release(lease); } else { let mut chunks = recv.read().unwrap(); std::hint::black_box(chunks.next(usize::MAX).unwrap()); - let _ = chunks.finalize(); + chunks.finalize(); } } } diff --git a/rs/moq-quic/src/fuzz/connection.rs b/rs/moq-quic/src/fuzz/connection.rs index 90ffe616bb3..3c08ec8610c 100644 --- a/rs/moq-quic/src/fuzz/connection.rs +++ b/rs/moq-quic/src/fuzz/connection.rs @@ -850,7 +850,7 @@ fn read(peer: &mut Peer, sent: &HashMap<(Side, StreamId), Sent>, id: StreamId, m } } - let _ = chunks.finalize(); + chunks.finalize(); } /// Odd streams are read through leases, since a plain read would skip what a lease holds. @@ -867,7 +867,7 @@ fn lease(peer: &mut Peer, sent: &HashMap<(Side, StreamId), Sent>, id: StreamId, let Some((_, conn)) = &mut peer.conn else { return; }; - let mut lease = match conn.recv_stream(id).lease(want).0 { + let mut lease = match conn.recv_stream(id).lease(want) { Ok(lease) => lease, Err(recv::Error::Evicted) => { conn.recv_stream(id) @@ -892,7 +892,7 @@ fn lease(peer: &mut Peer, sent: &HashMap<(Side, StreamId), Sent>, id: StreamId, } *offset += n as u64; lease.advance(n); - let _ = conn.recv_stream(id).release(lease); + conn.recv_stream(id).release(lease); } /// The byte a writer puts at `offset` of a stream, unique enough to catch misplaced data. diff --git a/rs/moq-quic/src/lib.rs b/rs/moq-quic/src/lib.rs index da03c076484..95b7f7db9c0 100644 --- a/rs/moq-quic/src/lib.rs +++ b/rs/moq-quic/src/lib.rs @@ -52,9 +52,7 @@ mod bloom_token_log; use bloom_token_log::BloomTokenLog; pub mod connection; -pub use crate::connection::{ - CreditStats, Datagrams, Event, FrameStats, PathStats, ShouldTransmit, Streams, UdpStats, recv, send, -}; +pub use crate::connection::{CreditStats, Datagrams, Event, FrameStats, PathStats, Streams, UdpStats, recv, send}; pub mod stream; #[cfg(feature = "mux")] diff --git a/rs/moq-quic/src/tests/characterize/frames.rs b/rs/moq-quic/src/tests/characterize/frames.rs index ceb9f02c7f9..4d11421ac05 100644 --- a/rs/moq-quic/src/tests/characterize/frames.rs +++ b/rs/moq-quic/src/tests/characterize/frames.rs @@ -129,7 +129,7 @@ fn zero_rtt_packets_are_dropped() { let mut chunks = recv.read().unwrap(); assert_eq!(chunks.next(usize::MAX).unwrap().unwrap().bytes, DATA); assert!(chunks.next(usize::MAX).unwrap().is_none()); - let _ = chunks.finalize(); + chunks.finalize(); } } diff --git a/rs/moq-quic/src/tests/characterize/limits.rs b/rs/moq-quic/src/tests/characterize/limits.rs index 85fae809ee3..5aa81e89b48 100644 --- a/rs/moq-quic/src/tests/characterize/limits.rs +++ b/rs/moq-quic/src/tests/characterize/limits.rs @@ -20,7 +20,7 @@ fn finish_all(pair: &mut Pair, client_ch: ConnectionHandle, server_ch: Connectio let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); while chunks.next(usize::MAX).unwrap().is_some() {} - let _ = chunks.finalize(); + chunks.finalize(); } pair.drive(); } @@ -74,7 +74,7 @@ fn read_buffered(pair: &mut Pair, ch: ConnectionHandle, streams: &mut Vec (Vec, rec Err(e) => break e, } }; - let _ = chunks.finalize(); + chunks.finalize(); (data, end) } @@ -75,7 +75,7 @@ fn received_reset_on_closed_streams() { let mut chunks = recv.read().unwrap(); assert!(chunks.next(usize::MAX).unwrap().is_some()); assert_eq!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); } assert!(pair.server_recv(server_ch, s).received_reset().is_err()); } @@ -92,7 +92,7 @@ fn chunks_end_in_reset() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_eq!(chunks.next(2).unwrap().unwrap().bytes, &b"he"[..]); - let _ = chunks.finalize(); + chunks.finalize(); } pair.client_send(client_ch, s).reset(CODE).unwrap(); pair.drive(); @@ -102,7 +102,7 @@ fn chunks_end_in_reset() { assert_eq!(chunks.next(usize::MAX), Err(recv::Error::Reset(CODE))); // The end repeats until the chunks are finalized assert_eq!(chunks.next(usize::MAX), Err(recv::Error::Reset(CODE))); - let _ = chunks.finalize(); + chunks.finalize(); assert_eq!(pair.server_recv(server_ch, s).read().err(), Some(recv::Error::Closed)); } @@ -119,7 +119,7 @@ fn chunks_end_in_finished() { assert_eq!(chunks.next(usize::MAX).unwrap().unwrap().bytes, &b"hello"[..]); assert_eq!(chunks.next(usize::MAX), Ok(None)); assert_eq!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); assert_eq!(pair.server_recv(server_ch, s).read().err(), Some(recv::Error::Closed)); } diff --git a/rs/moq-quic/src/tests/mod.rs b/rs/moq-quic/src/tests/mod.rs index c3e94f9592b..918bb4e54ba 100644 --- a/rs/moq-quic/src/tests/mod.rs +++ b/rs/moq-quic/src/tests/mod.rs @@ -948,7 +948,7 @@ fn finish_stream_simple() { Ok(Some(chunk)) if chunk.bytes == MSG ); assert_matches!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); } #[test] @@ -975,7 +975,7 @@ fn reset_stream() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_matches!(chunks.next(usize::MAX), Err(recv::Error::Reset(ERROR))); - let _ = chunks.finalize(); + chunks.finalize(); // The reset stream ends reset, never finished. assert_eq!( peek(|waiter| pair.client_send(client_ch, s).poll_end(waiter)), @@ -1499,7 +1499,7 @@ fn stream_id_limit() { Ok(Some(chunk)) if chunk.bytes == MSG ); assert_eq!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); // Lose the stream-credit grant itself, after proving the sender exhausted its budget. let grants = |pair: &mut Pair| pair.server_conn_mut(server_ch).stats().frame_tx.max_streams_uni; @@ -1541,7 +1541,7 @@ fn stream_id_limit() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_matches!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); } #[test] @@ -1638,7 +1638,7 @@ fn data_blocked() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_matches!(chunks.next(usize::MAX), Ok(Some(chunk)) if chunk.bytes.len() == 10); - let _ = chunks.finalize(); + chunks.finalize(); pair.drive(); assert_eq!( pair.client_send(client_ch, s).write(&mut &b"0123456789ab"[..]).unwrap(), @@ -1700,7 +1700,7 @@ fn stream_data_blocked() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_matches!(chunks.next(usize::MAX), Ok(Some(chunk)) if chunk.bytes.len() == 10); - let _ = chunks.finalize(); + chunks.finalize(); pair.drive(); assert_eq!( pair.client_send(client_ch, s).write(&mut &b"0123456789ab"[..]).unwrap(), @@ -1955,7 +1955,7 @@ fn key_update_reordered() { assert_matches!(&*buf1.bytes, MSG1); let buf2 = chunks.next(usize::MAX).unwrap().unwrap(); assert_eq!(buf2.bytes, MSG2); - let _ = chunks.finalize(); + chunks.finalize(); assert_eq!(pair.client_conn_mut(client_ch).stats().path.lost_packets, 0); assert_eq!(pair.server_conn_mut(server_ch).stats().path.lost_packets, 0); @@ -2354,7 +2354,7 @@ fn test_flow_control(config: transport::Config, window_size: usize) { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_eq!(chunks.next(usize::MAX).err(), Some(recv::Error::Reset(VarInt(42)))); - let _ = chunks.finalize(); + chunks.finalize(); // Happy path info!("writing"); @@ -2388,7 +2388,7 @@ fn test_flow_control(config: transport::Config, window_size: usize) { } } } - let _ = chunks.finalize(); + chunks.finalize(); info!("finished reading"); assert_eq!(cursor, window_size); @@ -2442,7 +2442,7 @@ fn test_flow_control(config: transport::Config, window_size: usize) { } } assert_eq!(cursor, window_size); - let _ = chunks.finalize(); + chunks.finalize(); info!("finished reading"); } @@ -2509,7 +2509,7 @@ fn stop_opens_bidi() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_matches!(chunks.next(usize::MAX), Err(recv::Error::Blocked)); - let _ = chunks.finalize(); + chunks.finalize(); assert_matches!( pair.server_send(server_ch, s).write(&mut &b"foo"[..]), @@ -2843,7 +2843,7 @@ fn finish_stream_flow_control_reordered() { chunks.next(usize::MAX), Ok(Some(chunk)) if chunk.bytes == MSG ); - let _ = chunks.finalize(); + chunks.finalize(); pair.server.drive(pair.time, pair.client.addr); pair.server.delay_outbound(); // Delay it @@ -2866,7 +2866,7 @@ fn finish_stream_flow_control_reordered() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_matches!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); } #[test] @@ -2917,7 +2917,7 @@ fn handshake_1rtt_handling() { chunks.next(usize::MAX), Ok(Some(chunk)) if chunk.bytes == MSG ); - let _ = chunks.finalize(); + chunks.finalize(); } #[test] @@ -3519,7 +3519,7 @@ fn finish_acked() { Ok(Some(chunk)) if chunk.bytes == MSG ); assert_matches!(chunks.next(usize::MAX), Err(recv::Error::Blocked)); - let _ = chunks.finalize(); + chunks.finalize(); // Finish before receiving data ack pair.client_send(client_ch, s).finish().unwrap(); @@ -3543,7 +3543,7 @@ fn finish_acked() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_matches!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); } #[test] @@ -3592,7 +3592,7 @@ fn finish_retransmit() { Ok(Some(chunk)) if chunk.bytes == MSG ); assert_matches!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); } /// Ensures that exchanging data on a client-initiated bidirectional stream works past the initial @@ -3634,7 +3634,7 @@ fn repeated_request_response() { ); assert_matches!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); pair.server_send(server_ch, s).write(&mut &RESPONSE[..]).unwrap(); pair.server_send(server_ch, s).finish().unwrap(); @@ -3647,7 +3647,7 @@ fn repeated_request_response() { Ok(Some(chunk)) if chunk.bytes == RESPONSE ); assert_matches!(chunks.next(usize::MAX), Ok(None)); - let _ = chunks.finalize(); + chunks.finalize(); } } @@ -4933,7 +4933,7 @@ fn stream_chunks(mut recv: recv::Stream<'_>) -> Vec { buf.extend(chunk.bytes); } - let _ = chunks.finalize(); + chunks.finalize(); buf } @@ -6730,10 +6730,10 @@ fn leased(pair: &mut Pair, server_ch: ConnectionHandle) -> usize { /// whether the peer finished the stream right after the leased bytes. fn consume(pair: &mut Pair, server_ch: ConnectionHandle, s: StreamId, n: usize) -> bool { let mut recv = pair.server_recv(server_ch, s); - let mut lease = recv.lease(0).0.unwrap(); + let mut lease = recv.lease(0).unwrap(); let fin = lease.is_fin(); lease.advance(n); - let _ = recv.release(lease); + recv.release(lease); fin } @@ -6764,8 +6764,8 @@ fn pump_leased( let before = leased(pair, server_ch); let mut recv = pair.server_recv(server_ch, s); - if let Ok(lease) = recv.lease(0).0 { - let _ = recv.restore(lease); + if let Ok(lease) = recv.lease(0) { + recv.restore(lease); } let read = leased(pair, server_ch) - before; if read == 0 && wrote == 0 { @@ -6822,10 +6822,7 @@ fn lease_spends_connection_window_only() { assert!(consume(&mut pair, server_ch, s, all), "finished after the leased bytes"); assert_eq!(leased(&mut pair, server_ch), 0); pair.server_recv(server_ch, s).stop(0u32.into()).unwrap(); - assert_eq!( - pair.server_recv(server_ch, s).lease(0).0.err(), - Some(recv::Error::Closed) - ); + assert_eq!(pair.server_recv(server_ch, s).lease(0).err(), Some(recv::Error::Closed)); } #[test] @@ -7067,7 +7064,7 @@ fn upload( while let Ok(Some(chunk)) = chunks.next(usize::MAX) { received += chunk.bytes.len(); } - let _ = chunks.finalize(); + chunks.finalize(); } if received == total || !pair.step() { diff --git a/rs/moq-quic/src/tests/probe.rs b/rs/moq-quic/src/tests/probe.rs index c785f1fc29c..4288d2ff949 100644 --- a/rs/moq-quic/src/tests/probe.rs +++ b/rs/moq-quic/src/tests/probe.rs @@ -361,7 +361,7 @@ fn run(scenario: &Scenario) -> Outcome { while let Ok(Some(chunk)) = chunks.next(usize::MAX) { outcome.competitor += chunk.bytes.len() as u64; } - let _ = chunks.finalize(); + chunks.finalize(); } } @@ -526,7 +526,7 @@ fn receive( } } } - let _ = chunks.finalize(); + chunks.finalize(); // The sender opens each group's stream in order, after any it opened before the media let frames = &groups[(id.index() - groups[0].id.index()) as usize].written; diff --git a/rs/moq-quic/src/tests/rate.rs b/rs/moq-quic/src/tests/rate.rs index 0e025e3b900..b4fdca85eaa 100644 --- a/rs/moq-quic/src/tests/rate.rs +++ b/rs/moq-quic/src/tests/rate.rs @@ -73,7 +73,7 @@ fn read_all(mut recv: recv::Stream<'_>) -> u64 { while let Ok(Some(chunk)) = chunks.next(usize::MAX) { read += chunk.bytes.len() as u64; } - let _ = chunks.finalize(); + chunks.finalize(); read } @@ -300,7 +300,7 @@ fn a_frame_larger_than_the_burst_arrives_gradually() { { written += n; } - if leased.is_none() && pair.server_recv(server_ch, s).lease(frame).0.is_ok() { + if leased.is_none() && pair.server_recv(server_ch, s).lease(frame).is_ok() { leased = Some(pair.time); } }); diff --git a/rs/moq-quic/src/tests/receive_ts.rs b/rs/moq-quic/src/tests/receive_ts.rs index 8bdab106709..a3822b0f61c 100644 --- a/rs/moq-quic/src/tests/receive_ts.rs +++ b/rs/moq-quic/src/tests/receive_ts.rs @@ -164,7 +164,7 @@ fn send( let mut stream = pair.server_recv(server_ch, recv); let mut chunks = stream.read().unwrap(); while let Ok(Some(_)) = chunks.next(usize::MAX) {} - let _ = chunks.finalize(); + chunks.finalize(); } if now >= start + end { diff --git a/rs/moq-quic/src/tests/reset_stream_at.rs b/rs/moq-quic/src/tests/reset_stream_at.rs index e92d16c285d..977acaaf3da 100644 --- a/rs/moq-quic/src/tests/reset_stream_at.rs +++ b/rs/moq-quic/src/tests/reset_stream_at.rs @@ -29,7 +29,7 @@ fn read(pair: &mut Pair, ch: ConnectionHandle, s: StreamId) -> (Vec, Result< Err(e) => break Err(e), } }; - let _ = chunks.finalize(); + chunks.finalize(); (data, end) } @@ -257,7 +257,7 @@ fn received_reset_after_prefix() { let mut recv = pair.server_recv(server_ch, s); let mut chunks = recv.read().unwrap(); assert_matches!(chunks.next(RELIABLE), Ok(Some(chunk)) if chunk.bytes == MSG[..RELIABLE]); - let _ = chunks.finalize(); + chunks.finalize(); assert_matches!( pair.server_conn_mut(server_ch).recv_stream(s).received_reset(), Ok(Some(ERROR)) diff --git a/rs/moq-quic/src/tests/scream.rs b/rs/moq-quic/src/tests/scream.rs index 5dcead58748..9aadf54eeaa 100644 --- a/rs/moq-quic/src/tests/scream.rs +++ b/rs/moq-quic/src/tests/scream.rs @@ -261,7 +261,7 @@ fn run(scenario: &Scenario) -> Outcome { *received += chunk.bytes.len() as u64; read += chunk.bytes.len() as u64; } - let _ = chunks.finalize(); + chunks.finalize(); let frames = &groups[(id.index() - groups[0].id.index()) as usize].frames; while let Some(&(written, end)) = frames.get(*completed) && end <= *received diff --git a/rs/moq-quic/src/tests/sim.rs b/rs/moq-quic/src/tests/sim.rs index 409884bf7ea..81108f442c7 100644 --- a/rs/moq-quic/src/tests/sim.rs +++ b/rs/moq-quic/src/tests/sim.rs @@ -334,7 +334,7 @@ impl Peer { Err(error) => panic!("reading stream {index}: {error}"), } } - let _ = chunks.finalize(); + chunks.finalize(); let burst = receiving.read - before; prop_assert!( diff --git a/rs/moq-quic/src/tests/sim/workloads.rs b/rs/moq-quic/src/tests/sim/workloads.rs index cd67f3a7bd9..179be0a099d 100644 --- a/rs/moq-quic/src/tests/sim/workloads.rs +++ b/rs/moq-quic/src/tests/sim/workloads.rs @@ -149,7 +149,7 @@ impl Transfer { Err(error) => return Err(TestCaseError::fail(format!("{id} read: {error}"))), } } - let _ = chunks.finalize(); + chunks.finalize(); if matches!(self.end, End::Stop) && self.read == self.cut { stream.stop(CODE).unwrap(); self.received = true; diff --git a/rs/moq-quic/src/tests/util.rs b/rs/moq-quic/src/tests/util.rs index a9d55b8d3f2..bc832f7411f 100644 --- a/rs/moq-quic/src/tests/util.rs +++ b/rs/moq-quic/src/tests/util.rs @@ -631,7 +631,7 @@ pub(super) fn poll_read( let poll = chunks .poll_next(waiter, usize::MAX) .map(|r| r.map(|chunk| chunk.map(|c| c.bytes))); - let _ = chunks.finalize(); + chunks.finalize(); poll } diff --git a/rs/moq-tokio/MIGRATE.md b/rs/moq-tokio/MIGRATE.md index 348136bc965..eb05fb732c3 100644 --- a/rs/moq-tokio/MIGRATE.md +++ b/rs/moq-tokio/MIGRATE.md @@ -4,6 +4,15 @@ How to update your code for each breaking change, newest first. ## Unreleased +### QUIC waits have caller-owned poll forms + +The QUIC driver connection's stream opens, accepts, datagram receive and close waits, +the endpoint's accept, and both send handles' close waits now expose `poll_*` methods +taking `&self, &kio::Waiter`. Existing async methods keep their behavior and wrap the +polls. `send::Finished` also implements `Future`, with the same result as `stopped()`: +`None` for acknowledged data and FIN or a local reset, `Some(code)` for a peer stop, +and an error if the connection ends first. + ### QUIC handshake data needs no downcast ([#7028](https://github.com/moq-dev/moq/pull/7028)) `quic::driver::Connecting::handshake_data` returns `crypto::HandshakeData` and `Connection::handshake_data` returns `Option`, so drop the `downcast`. diff --git a/rs/moq-tokio/src/quic/driver/connection.rs b/rs/moq-tokio/src/quic/driver/connection.rs index 785368ee0b2..03d409399a1 100644 --- a/rs/moq-tokio/src/quic/driver/connection.rs +++ b/rs/moq-tokio/src/quic/driver/connection.rs @@ -267,8 +267,13 @@ impl Connection { /// # Panics /// - when `options.group` belongs to another connection pub async fn open_uni(&self, options: send::Options<'_>) -> Result { - let (conn, id) = kio::wait(|waiter| poll_open(waiter, &self.0, options, Dir::Uni)).await?; - Ok(send::Stream::new(conn, id)) + kio::wait(|waiter| self.poll_open_uni(waiter, options)).await + } + + /// See [`Self::open_uni`]. + pub fn poll_open_uni(&self, waiter: &kio::Waiter, options: send::Options<'_>) -> Poll> { + let (conn, id) = ready!(poll_open(waiter, &self.0, options, Dir::Uni))?; + Poll::Ready(Ok(send::Stream::new(conn, id))) } /// Open a send group at `priority`, for streams and datagrams to join with [`send::Options`] @@ -289,8 +294,17 @@ impl Connection { /// # Panics /// - when `options.group` belongs to another connection pub async fn open_bi(&self, options: send::Options<'_>) -> Result<(send::Stream, recv::Stream), Error> { - let (conn, id) = kio::wait(|waiter| poll_open(waiter, &self.0, options, Dir::Bi)).await?; - Ok((send::Stream::new(conn.clone(), id), recv::Stream::new(conn, id))) + kio::wait(|waiter| self.poll_open_bi(waiter, options)).await + } + + /// See [`Self::open_bi`]. + pub fn poll_open_bi( + &self, + waiter: &kio::Waiter, + options: send::Options<'_>, + ) -> Poll> { + let (conn, id) = ready!(poll_open(waiter, &self.0, options, Dir::Bi))?; + Poll::Ready(Ok((send::Stream::new(conn.clone(), id), recv::Stream::new(conn, id)))) } /// Open a request stream using only the credit already granted by the peer. @@ -316,8 +330,13 @@ impl Connection { /// /// This method is cancellation safe. If this does not resolve, no streams were consumed. pub async fn accept_uni(&self) -> Result { - let (conn, id) = kio::wait(|waiter| poll_accept(waiter, &self.0, Dir::Uni)).await?; - Ok(recv::Stream::new(conn, id)) + kio::wait(|waiter| self.poll_accept_uni(waiter)).await + } + + /// See [`Self::accept_uni`]. + pub fn poll_accept_uni(&self, waiter: &kio::Waiter) -> Poll> { + let (conn, id) = ready!(poll_accept(waiter, &self.0, Dir::Uni))?; + Poll::Ready(Ok(recv::Stream::new(conn, id))) } /// Accept the next incoming bidirectional stream @@ -333,8 +352,13 @@ impl Connection { /// /// This method is cancellation safe. If this does not resolve, no streams were consumed. pub async fn accept_bi(&self) -> Result<(send::Stream, recv::Stream), Error> { - let (conn, id) = kio::wait(|waiter| poll_accept(waiter, &self.0, Dir::Bi)).await?; - Ok((send::Stream::new(conn.clone(), id), recv::Stream::new(conn, id))) + kio::wait(|waiter| self.poll_accept_bi(waiter)).await + } + + /// See [`Self::accept_bi`]. + pub fn poll_accept_bi(&self, waiter: &kio::Waiter) -> Poll> { + let (conn, id) = ready!(poll_accept(waiter, &self.0, Dir::Bi))?; + Poll::Ready(Ok((send::Stream::new(conn.clone(), id), recv::Stream::new(conn, id)))) } /// Receive an application datagram @@ -343,13 +367,14 @@ impl Connection { /// /// This method is cancellation safe. If this does not resolve, no datagrams were consumed. pub fn read_datagram(&self) -> impl Future> + '_ { - kio::wait(|waiter| { - let mut state = self.0.state.lock(); - // moq-quic yields the datagrams received before checking for the connection error, - // so they, which are necessarily finite, drain from a closed connection. - let datagram = ready!(state.inner.datagrams().poll_recv(waiter)); - Poll::Ready(datagram.map_err(|err| state.lost_as(err))) - }) + kio::wait(|waiter| self.poll_recv_datagram(waiter)) + } + + /// Receive the next datagram, draining what arrived before the connection closed. + pub fn poll_recv_datagram(&self, waiter: &kio::Waiter) -> Poll> { + let mut state = self.0.state.lock(); + let datagram = ready!(state.inner.datagrams().poll_recv(waiter)); + Poll::Ready(datagram.map_err(|err| state.lost_as(err))) } /// Wait for the connection to be closed for any reason @@ -358,12 +383,14 @@ impl Connection { /// application layer. Cases that might be routine include [`Error::LocallyClosed`] /// and [`Error::ApplicationClosed`]. pub async fn closed(&self) -> Error { - kio::wait(|waiter| { - let mut state = self.0.state.lock(); - let err = ready!(state.inner.poll_closed(waiter)); - Poll::Ready(state.lost_as(err)) - }) - .await + kio::wait(|waiter| self.poll_closed(waiter)).await + } + + /// Wait for the connection to close, returning why. + pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll { + let mut state = self.0.state.lock(); + let err = ready!(state.inner.poll_closed(waiter)); + Poll::Ready(state.lost_as(err)) } /// Whether the connection is closed, and why. diff --git a/rs/moq-tokio/src/quic/driver/endpoint.rs b/rs/moq-tokio/src/quic/driver/endpoint.rs index bf9cf4fceea..cefb51468ec 100644 --- a/rs/moq-tokio/src/quic/driver/endpoint.rs +++ b/rs/moq-tokio/src/quic/driver/endpoint.rs @@ -21,9 +21,8 @@ use moq_quic::{ endpoint::{ConnectionHandle, DatagramEvent, Received}, }; use moq_sock::udp::{BATCH_SIZE, RecvMeta, is_msg_size_err}; -use pin_project_lite::pin_project; use rustc_hash::{FxHashMap, FxHashSet}; -use tokio::sync::{Notify, futures::Notified, mpsc}; +use tokio::sync::mpsc; use tracing::{Instrument, Span}; pub use super::incoming::Incoming; @@ -144,10 +143,31 @@ impl Endpoint { /// Yields [`Incoming`]s, or `None` if the endpoint is [`close`](Self::close)d. /// Call [`Incoming::accept`] and await its [`Connecting`] to finish the handshake. pub fn accept(&self) -> impl Future> + '_ { - Accept { - endpoint: self, - notify: self.inner.shared.incoming.notified(), + kio::wait(|waiter| self.poll_accept(waiter)) + } + + /// Get the next incoming connection attempt, or `None` once the endpoint closes. + pub fn poll_accept(&self, waiter: &kio::Waiter) -> Poll> { + let mut endpoint = self.inner.state.lock().unwrap(); + if endpoint.driver_lost { + return Poll::Ready(None); + } + if let Some(incoming) = endpoint.recv_state.incoming.pop_front() { + // A handed-out attempt no longer holds off `wait_all_draining`, and refusing, + // ignoring, or dropping it sends no event that would wake the waiter later. + if endpoint.all_draining() { + endpoint.all_draining_waiters.wake(); + } + // Release the mutex lock on endpoint so cloning it doesn't deadlock + drop(endpoint); + let incoming = Incoming::new(incoming, self.inner.clone()); + return Poll::Ready(Some(incoming)); + } + if endpoint.recv_state.connections.close.is_some() { + return Poll::Ready(None); } + waiter.register(&mut endpoint.incoming_waiters); + Poll::Pending } /// Connect to `addr`, verifying it as `server_name` @@ -270,26 +290,24 @@ impl Endpoint { let transmit = endpoint.inner.refuse(incoming, &mut response_buffer); respond(transmit, &response_buffer, &self.inner.slot); } - self.inner.shared.incoming.notify_waiters(); + endpoint.incoming_waiters.wake(); if endpoint.all_draining() { - self.inner.shared.all_draining.notify_waiters(); + endpoint.all_draining_waiters.wake(); } } /// Wait for all connections on the endpoint to be cleanly shut down #[cfg(test)] pub(crate) async fn wait_idle(&self) { - loop { - { - let endpoint = &mut *self.inner.state.lock().unwrap(); - if endpoint.is_idle() { - break; - } - // Construct future while lock is held to avoid race - self.inner.shared.idle.notified() + kio::wait(|waiter| { + let mut endpoint = self.inner.state.lock().unwrap(); + if endpoint.is_idle() { + return Poll::Ready(()); } - .await; - } + waiter.register(&mut endpoint.idle_waiters); + Poll::Pending + }) + .await } /// Wait until no connection on the endpoint is still active @@ -299,17 +317,15 @@ impl Endpoint { /// queued for [`accept()`](Self::accept) or partway through [`Incoming::accept`] still counts /// as active. pub(crate) async fn wait_all_draining(&self) { - loop { - { - let endpoint = &mut *self.inner.state.lock().unwrap(); - if endpoint.all_draining() { - break; - } - // Construct future while lock is held to avoid race - self.inner.shared.all_draining.notified() + kio::wait(|waiter| { + let mut endpoint = self.inner.state.lock().unwrap(); + if endpoint.all_draining() { + return Poll::Ready(()); } - .await; - } + waiter.register(&mut endpoint.all_draining_waiters); + Poll::Pending + }) + .await } /// Set the client configuration [`dial`](Self::dial) uses, so tests need not carry it @@ -328,17 +344,15 @@ impl Endpoint { /// Wait until a connection attempt is queued for [`accept()`](Endpoint::accept) #[cfg(test)] pub(crate) async fn wait_queued(&self) { - loop { - { - let endpoint = self.inner.state.lock().unwrap(); - if !endpoint.recv_state.incoming.is_empty() { - break; - } - // Construct future while lock is held to avoid race - self.inner.shared.incoming.notified() + kio::wait(|waiter| { + let mut endpoint = self.inner.state.lock().unwrap(); + if !endpoint.recv_state.incoming.is_empty() { + return Poll::Ready(()); } - .await; - } + waiter.register(&mut endpoint.incoming_waiters); + Poll::Pending + }) + .await } } @@ -369,13 +383,13 @@ impl Future for EndpointDriver { let now = Instant::now(); let mut keep_going = false; keep_going |= endpoint.drive_recv(cx, &self.0.slot, now)?; - keep_going |= endpoint.handle_events(cx, &self.0.shared); + keep_going |= endpoint.handle_events(cx); if !endpoint.recv_state.incoming.is_empty() { - self.0.shared.incoming.notify_waiters(); + endpoint.incoming_waiters.wake(); } - if self.0.shared.ref_count.load(Ordering::Relaxed) == 0 && endpoint.is_idle() { + if self.0.ref_count.load(Ordering::Relaxed) == 0 && endpoint.is_idle() { Poll::Ready(Ok(())) } else { drop(endpoint); @@ -399,7 +413,7 @@ impl Drop for EndpointDriver { endpoint.prev_socket = None; endpoint.forward = None; endpoint.driver_lost = true; - self.0.shared.incoming.notify_waiters(); + endpoint.incoming_waiters.wake(); // Drop all outgoing channels, signaling the termination of the endpoint to the associated // connections. endpoint.recv_state.connections.senders.clear(); @@ -409,12 +423,12 @@ impl Drop for EndpointDriver { for incoming in endpoint.recv_state.incoming.drain(..) { endpoint.inner.ignore(incoming); } - self.0.shared.all_draining.notify_waiters(); + endpoint.all_draining_waiters.wake(); // Clearing the senders is what makes the endpoint idle, but a waiter already parked on // `idle` only re-reads that after being notified, and with the driver gone nothing else // can wake it. Same reason as the equivalent check in `EndpointInner::accept`. if endpoint.is_idle() { - self.0.shared.idle.notify_waiters(); + endpoint.idle_waiters.wake(); } } } @@ -422,7 +436,8 @@ impl Drop for EndpointDriver { #[derive(Debug)] pub(crate) struct EndpointInner { pub(crate) state: Mutex, - pub(crate) shared: Shared, + /// Number of live handles that can initiate or handle I/O, excluding the driver. + ref_count: AtomicUsize, /// The socket, which connections send through without keeping it open pub(crate) slot: Arc, /// Where the endpoint's and its connections' tasks run @@ -479,11 +494,11 @@ impl EndpointInner { // Failed accepts and accepts completed after driver loss cannot rely on a Drained event // being processed to wake idle waiters. if state.is_idle() { - self.shared.idle.notify_waiters(); + state.idle_waiters.wake(); } // The pending accept held off `wait_all_draining`, and a failed one sends no event. if state.all_draining() { - self.shared.all_draining.notify_waiters(); + state.all_draining_waiters.wake(); } result } @@ -507,6 +522,9 @@ impl EndpointInner { #[derive(Debug)] pub(crate) struct State { + incoming_waiters: kio::WaiterList, + idle_waiters: kio::WaiterList, + all_draining_waiters: kio::WaiterList, /// During an active migration, abandoned_socket receives traffic /// until the first packet arrives on the new socket. prev_socket: Option>, @@ -524,15 +542,6 @@ pub(crate) struct State { forward: Option, } -#[derive(Debug)] -pub(crate) struct Shared { - incoming: Notify, - idle: Notify, - all_draining: Notify, - /// Number of live handles that can be used to initiate or handle I/O; excludes the driver - ref_count: AtomicUsize, -} - impl State { fn drive_recv(&mut self, cx: &mut Context<'_>, slot: &Slot, now: Instant) -> Result { let get_time = Instant::now; @@ -577,7 +586,7 @@ impl State { Ok(poll_res.keep_going || forwards?) } - fn handle_events(&mut self, cx: &mut Context<'_>, shared: &Shared) -> bool { + fn handle_events(&mut self, cx: &mut Context<'_>) -> bool { for _ in 0..IO_LOOP_BOUND { let (ch, event) = match self.events.poll_recv(cx) { Poll::Ready(Some(x)) => x, @@ -595,11 +604,11 @@ impl State { connections.senders.remove(&ch); connections.draining.remove(&ch); if self.is_idle() { - shared.idle.notify_waiters(); + self.idle_waiters.wake(); } } if (event.is_draining() || event.is_drained()) && self.all_draining() { - shared.all_draining.notify_waiters(); + self.all_draining_waiters.wake(); } let Some(event) = self.inner.handle_event(ch, event) else { continue; @@ -745,48 +754,6 @@ fn ensure_ipv6(x: SocketAddr) -> SocketAddrV6 { } } -pin_project! { - /// Future produced by [`Endpoint::accept`] - struct Accept<'a> { - endpoint: &'a Endpoint, - #[pin] - notify: Notified<'a>, - } -} - -impl Future for Accept<'_> { - type Output = Option; - fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll { - let mut this = self.project(); - let mut endpoint = this.endpoint.inner.state.lock().unwrap(); - if endpoint.driver_lost { - return Poll::Ready(None); - } - if let Some(incoming) = endpoint.recv_state.incoming.pop_front() { - // A handed-out attempt no longer holds off `wait_all_draining`, and refusing, - // ignoring, or dropping it sends no event that would wake the waiter later. - if endpoint.all_draining() { - this.endpoint.inner.shared.all_draining.notify_waiters(); - } - // Release the mutex lock on endpoint so cloning it doesn't deadlock - drop(endpoint); - let incoming = Incoming::new(incoming, this.endpoint.inner.clone()); - return Poll::Ready(Some(incoming)); - } - if endpoint.recv_state.connections.close.is_some() { - return Poll::Ready(None); - } - loop { - match this.notify.as_mut().poll(ctx) { - // `state` lock ensures we didn't race with readiness - Poll::Pending => return Poll::Pending, - // Spurious wakeup, get a new future - Poll::Ready(()) => this.notify.set(this.endpoint.inner.shared.incoming.notified()), - } - } - } -} - #[derive(Debug)] pub(crate) struct EndpointRef(Arc); @@ -807,13 +774,11 @@ impl EndpointRef { runtime, #[cfg(test)] default_client: Mutex::new(None), - shared: Shared { - incoming: Notify::new(), - idle: Notify::new(), - all_draining: Notify::new(), - ref_count: AtomicUsize::new(0), - }, + ref_count: AtomicUsize::new(0), state: Mutex::new(State { + incoming_waiters: kio::WaiterList::new(), + idle_waiters: kio::WaiterList::new(), + all_draining_waiters: kio::WaiterList::new(), prev_socket: None, local_addr, inner, @@ -831,14 +796,14 @@ impl EndpointRef { impl Clone for EndpointRef { fn clone(&self) -> Self { - self.0.shared.ref_count.fetch_add(1, Ordering::Relaxed); + self.0.ref_count.fetch_add(1, Ordering::Relaxed); Self(self.0.clone()) } } impl Drop for EndpointRef { fn drop(&mut self) { - if self.shared.ref_count.fetch_sub(1, Ordering::Relaxed) > 1 { + if self.ref_count.fetch_sub(1, Ordering::Relaxed) > 1 { return; } diff --git a/rs/moq-tokio/src/quic/driver/recv.rs b/rs/moq-tokio/src/quic/driver/recv.rs index 4673f98b118..04fe958ad52 100644 --- a/rs/moq-tokio/src/quic/driver/recv.rs +++ b/rs/moq-tokio/src/quic/driver/recv.rs @@ -229,7 +229,7 @@ impl Reader { } let mut conn = self.conn.state.lock(); // moq-quic wakes the driver for any credit the lease returned. - let (result, _) = conn.inner.recv_stream(self.id).poll_lease(waiter, want); + let result = conn.inner.recv_stream(self.id).poll_lease(waiter, want); let err = match ready!(result) { Ok(lease) => return Poll::Ready(Ok(lease)), Err(err) => err, @@ -296,7 +296,7 @@ impl Reader { Ok(mut chunks) => { let status = read_fn(&mut chunks, waiter); // moq-quic wakes the driver for the credit the read returned. - let _ = chunks.finalize(); + chunks.finalize(); status } Err(err) => ReadStatus::Failed(None, err), @@ -424,7 +424,7 @@ impl Lease<'_> { pub fn release(mut self) { let lease = self.inner.take().expect("only taken to hand it back"); let mut conn = self.reader.conn.state.lock(); - let _ = conn.inner.recv_stream(self.reader.id).release(lease); + conn.inner.recv_stream(self.reader.id).release(lease); } } @@ -452,7 +452,7 @@ impl Drop for Lease<'_> { if let Some(lease) = self.inner.take() && let Some(mut conn) = self.reader.conn.state.lock_for_drop() { - let _ = conn.inner.recv_stream(self.reader.id).restore(lease); + conn.inner.recv_stream(self.reader.id).restore(lease); } } } diff --git a/rs/moq-tokio/src/quic/driver/send.rs b/rs/moq-tokio/src/quic/driver/send.rs index 70388d082af..49c59be562e 100644 --- a/rs/moq-tokio/src/quic/driver/send.rs +++ b/rs/moq-tokio/src/quic/driver/send.rs @@ -44,7 +44,10 @@ pub struct Stream { /// Dropping it leaves the stream to finish, unless the peer stopped it, in which case it is /// reset with the peer's code. #[derive(Debug)] -pub struct Finished(Half); +pub struct Finished { + half: Half, + park: kio::Park, +} /// What [`Stream`] and [`Finished`] share. #[derive(Debug)] @@ -123,7 +126,10 @@ impl Stream { /// stopped the stream first. pub fn finish(mut self) -> Finished { self.half.finish(); - Finished(self.half) + Finished { + half: self.half, + park: kio::Park::default(), + } } /// Close the send stream immediately @@ -200,6 +206,11 @@ impl Stream { self.half.stopped() } + /// Wait for the stream to end, retaining the peer's stop code; see [`Finished::stopped`]. + pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll, Error>> { + self.half.stream.poll_stopped(waiter) + } + /// The identity of this stream pub fn id(&self) -> stream::Id { self.half.stream.id @@ -209,7 +220,7 @@ impl Stream { impl Finished { /// Abandon the stream's data the peer has not acknowledged yet. See [`Stream::reset()`]. pub fn reset(mut self, error_code: VarInt) { - self.0.reset(None, error_code); + self.half.reset(None, error_code); } /// Abandon the stream's data past `offset`. See [`Stream::reset_at()`]. @@ -217,17 +228,17 @@ impl Finished { /// # Panics /// - when `offset` came from another stream pub fn reset_at(mut self, offset: Offset, error_code: VarInt) { - self.0.reset(Some(offset), error_code); + self.half.reset(Some(offset), error_code); } /// Set the priority its data is retransmitted at. See [`Stream::set_priority()`]. pub fn set_priority(&mut self, priority: i64) { - self.0.set_priority(priority); + self.half.set_priority(priority); } /// The priority of the send stream pub fn priority(&self) -> i64 { - self.0.priority() + self.half.priority() } /// Move the stream into a send group. See [`Stream::set_group()`]. @@ -235,7 +246,7 @@ impl Finished { /// # Panics /// - when `group` belongs to another connection pub fn set_group(&mut self, group: Option<&Group>) { - self.0.set_group(group); + self.half.set_group(group); } /// Completes when the peer stops the stream or acknowledges all of its data @@ -250,12 +261,26 @@ impl Finished { /// data. As such, relying on `stopped` to know when the peer has read a stream to completion /// may introduce more latency than using an application-level response of some sort. pub fn stopped(&self) -> impl Future, Error>> + Send + Sync + 'static { - self.0.stopped() + self.half.stopped() + } + + /// Wait for the stream to end, retaining the peer's stop code; see [`Finished::stopped`]. + pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll, Error>> { + self.half.stream.poll_stopped(waiter) } /// The identity of this stream pub fn id(&self) -> stream::Id { - self.0.stream.id + self.half.stream.id + } +} + +impl Future for Finished { + type Output = Result, Error>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.get_mut(); + this.half.stream.poll_stopped(this.park.hold(cx)) } } diff --git a/rs/moq-tokio/src/quic/driver/tests.rs b/rs/moq-tokio/src/quic/driver/tests.rs index 658d2adc254..e037a4c7fe2 100755 --- a/rs/moq-tokio/src/quic/driver/tests.rs +++ b/rs/moq-tokio/src/quic/driver/tests.rs @@ -879,7 +879,7 @@ async fn echo((mut send, mut recv): (send::Stream, recv::Stream)) { send.write_all(chunk).await.expect("write chunk"); } - let _ = send.finish(); + drop(send.finish()); } fn gen_data(size: usize, seed: u64) -> Vec { @@ -1400,7 +1400,7 @@ async fn forwarded_packets_reach_their_owner() { let message = format!("round {round} through server {server}"); let (mut send, mut recv) = conn.open_bi(Default::default()).await.unwrap(); send.write_all(message.as_bytes()).await.unwrap(); - let _ = send.finish(); + drop(send.finish()); let echoed = timeout(Duration::from_secs(10), recv.read_to_end(1024)) .await .expect("the owner answered") @@ -2483,3 +2483,180 @@ async fn close_during_handshake_does_not_wait_for_silent_peer() { assert!(connect.await.is_err()); assert_eq!(elapsed, Duration::ZERO, "waited {elapsed:?}"); } + +/// A socket whose output can be held independently of QUIC's flow control. +#[cfg(feature = "sim")] +#[derive(Debug)] +struct ControlledSocket { + inner: super::sim::Socket, + output: Arc>, +} + +#[cfg(feature = "sim")] +#[derive(Debug, Default)] +struct ControlledOutput { + blocked: bool, + attempts: usize, + writable: Option, + parked: kio::WaiterList, +} + +#[cfg(feature = "sim")] +impl AsyncUdpSocket for ControlledSocket { + fn poll_recv( + &self, + cx: &mut Context<'_>, + bufs: &mut [IoSliceMut<'_>], + meta: &mut [RecvMeta], + ) -> Poll> { + // Once output stalls, suppress further ingress too, so the peer's retransmits cannot + // accidentally drive the close timer for us. + if self.output.lock().unwrap().writable.is_some() { + return Poll::Pending; + } + self.inner.poll_recv(cx, bufs, meta) + } + + fn try_send(&self, transmit: &moq_sock::udp::Transmit<'_>) -> io::Result<()> { + if self.output.lock().unwrap().blocked { + return Err(io::ErrorKind::WouldBlock.into()); + } + self.inner.try_send(transmit) + } + + fn poll_writable(&self, cx: &mut Context<'_>) -> Poll> { + let mut output = self.output.lock().unwrap(); + if !output.blocked { + return Poll::Ready(Ok(())); + } + output.attempts += 1; + output.writable = Some(cx.waker().clone()); + output.parked.wake(); + Poll::Pending + } + + fn local_addr(&self) -> io::Result { + self.inner.local_addr() + } +} + +/// Output resumes on release, and closing never depends on that release. +#[cfg(feature = "sim")] +#[test] +fn a_close_behind_a_blocked_socket_is_bounded() { + for release in [false, true] { + kio::clock::sim::run(async { + let network = super::sim::Network::new(1); + let client_addr = SocketAddr::from(([10, 0, 0, 1], 4433)); + let server_addr = SocketAddr::from(([10, 0, 0, 2], 443)); + let factory = EndpointFactory::new(); + let mut transport = transport::Config::default(); + transport.idle_timeout = None; + let mut roots = RootCertStore::empty(); + roots.add(factory.cert.cert.der().clone()).unwrap(); + let mut config = client::Config::with_root_certificates(Arc::new(roots)).unwrap(); + config.transport = transport.clone(); + let output = Arc::new(Mutex::new(ControlledOutput::default())); + let socket: Box = Box::new(ControlledSocket { + inner: network.bind(server_addr), + output: output.clone(), + }); + let client = Endpoint::new(endpoint::Config::default(), network.bind(client_addr)).unwrap(); + let server = Endpoint::new( + endpoint::Config::default().with_server(factory.server_config(transport)), + socket, + ) + .unwrap(); + let (conn, peer) = tokio::try_join!(client.connect(&config, server_addr, "localhost").unwrap(), async { + server.accept().await.unwrap().accept().unwrap().await + }) + .unwrap(); + + // Settle the server's handshake flight and FIN before holding an ACK-only packet. + let warmup = peer.open_uni(Default::default()).await.unwrap().finish(); + assert_eq!(warmup.await, Ok(None)); + // Retire handshake keys on the simulated clock before blocking its next send. + sleep(Duration::from_secs(1)).await; + + output.lock().unwrap().blocked = true; + let mut stream = conn.open_uni(Default::default()).await.unwrap(); + stream.write_all(&b"held"[..]).await.unwrap(); + let finished = stream.finish(); + kio::wait(|waiter| { + let mut output = output.lock().unwrap(); + if output.writable.is_some() { + return Poll::Ready(()); + } + waiter.register(&mut output.parked); + Poll::Pending + }) + .await; + + // Consume any transmit registration left from before the socket blocked. The driver + // must park again without polling transmission, which would consume a packet. + let attempts = output.lock().unwrap().attempts; + peer.send_datagram(Bytes::from_static(b"queued"), Default::default()) + .unwrap(); + kio::wait(|waiter| { + let mut output = output.lock().unwrap(); + if output.attempts > attempts { + return Poll::Ready(()); + } + waiter.register(&mut output.parked); + Poll::Pending + }) + .await; + + if release { + let waker = { + let mut output = output.lock().unwrap(); + output.blocked = false; + output.writable.take().unwrap() + }; + waker.wake(); + let mut received = peer.accept_uni().await.unwrap(); + assert_eq!(received.read_to_end(4).await.unwrap(), b"held"); + assert_eq!(finished.await, Ok(None)); + } + + peer.close(0u32.into(), "closed while blocked"); + // With zero path latency, three PTOs take about 78 ms. Closing must use that + // deadline rather than the unrelated timer left from the handshake. + timeout(Duration::from_secs(1), async { + server.wait_all_draining().await; + server.wait_idle().await; + }) + .await + .expect("socket backpressure held the closing period open"); + if !release { + assert!(output.lock().unwrap().blocked); + } + }); + } +} + +/// A finished handle keeps the terminal outcome when driven as a future or a caller-owned poll. +#[tokio::test] +async fn finished_waits_keep_the_terminal_outcome() { + for stopped in [false, true] { + let endpoint = endpoint(); + let (conn, peer) = connect_self(&endpoint).await; + let mut stream = conn.open_uni(Default::default()).await.unwrap(); + stream.write_all(&b"data"[..]).await.unwrap(); + let received = peer.accept_uni().await.unwrap(); + let finished = stream.finish(); + if stopped { + received.stop(42u32.into()); + } else { + peer.close(5u32.into(), "connection lost before FIN was acknowledged"); + } + let polled = kio::wait(|waiter| finished.poll_closed(waiter)).await; + let awaited = finished.await; + assert_eq!(polled, awaited); + if stopped { + assert_eq!(awaited, Ok(Some(42u32.into()))); + } else { + assert!(matches!(awaited, Err(send::Error::Connection(_)))); + } + } +} diff --git a/rs/moq-uring/MIGRATE.md b/rs/moq-uring/MIGRATE.md index a3b7d16c341..ef8048662d1 100644 --- a/rs/moq-uring/MIGRATE.md +++ b/rs/moq-uring/MIGRATE.md @@ -4,6 +4,15 @@ How to update your code for each breaking change, newest first. ## Unreleased +### QUIC waits have caller-owned poll forms + +The QUIC driver connection's stream opens, accepts, datagram receive and close waits, +the endpoint's accept, and both send handles' close waits now expose `poll_*` methods +taking `&self, &kio::Waiter`. Existing async methods keep their behavior and wrap the +polls. `send::Finished` also implements `Future`, with the same result as `stopped()`: +`None` for acknowledged data and FIN or a local reset, `Some(code)` for a peer stop, +and an error if the connection ends first. + ### `peer_chain` is `peer_identity` ([#7028](https://github.com/moq-dev/moq/pull/7028)) `quic::driver::Connection::peer_chain()` is now `peer_identity()` and returns `Option>>`, mirroring moq-tokio. diff --git a/rs/moq-uring/src/quic/driver/connection.rs b/rs/moq-uring/src/quic/driver/connection.rs index 32f7cd36d16..78d69be03c3 100644 --- a/rs/moq-uring/src/quic/driver/connection.rs +++ b/rs/moq-uring/src/quic/driver/connection.rs @@ -4,7 +4,7 @@ use kio::clock::Instant; use std::cell::{Cell, RefCell}; use std::net::SocketAddr; use std::rc::{Rc, Weak}; -use std::task::{Context, Poll, ready}; +use std::task::{Poll, ready}; use bytes::{Buf, Bytes}; use moq_quic::endpoint::ConnectionHandle; @@ -183,8 +183,7 @@ impl Inner { /// Created by [`Endpoint`](super::Endpoint) (or its /// [`client::connect`](crate::quic::client::connect) / /// [`server::accept`](crate::quic::server::accept) shorthands), already -/// established. Clones share the connection; each carries its own parking so -/// concurrent pending operations don't trample each other's wakeups. Dropping +/// established. Clones share the connection. Dropping /// the last handle, streams included, closes the connection with code 0. /// /// Once the connection ends, or the worker driving it is dropped, every @@ -197,8 +196,6 @@ impl Inner { /// it, or the packet is never built and the peer idles out instead. pub struct Connection { shared: Shared, - // Retains this clone's waiter registrations across polls. - park: kio::Park, /// The negotiated ALPN, cached at establishment so `protocol()` can /// borrow from the handle. alpn: Option, @@ -274,7 +271,7 @@ impl Connection { /// Wait for the connection to close, returning why. pub async fn closed(&self) -> Error { - kio::wait(|waiter| poll_closed(&self.shared, waiter)).await + kio::wait(|waiter| self.poll_closed(waiter)).await } /// The connection's statistics. @@ -349,7 +346,6 @@ impl Clone for Connection { self.shared.hold(); Self { shared: self.shared.clone(), - park: kio::Park::default(), alpn: self.alpn.clone(), server_name: self.server_name.clone(), remote: self.remote, @@ -467,7 +463,6 @@ pub(crate) async fn establish(shared: Shared, deadline: Option) -> Resu shared.hold(); let conn = Connection { shared, - park: kio::Park::default(), alpn, server_name, remote, @@ -500,12 +495,12 @@ impl Connection { impl Connection { /// Accept the next unidirectional stream the peer opens. pub async fn accept_uni(&self) -> Result { - kio::wait(|waiter| accept_uni(&self.shared, waiter)).await + kio::wait(|waiter| self.poll_accept_uni(waiter)).await } /// Accept the next bidirectional stream the peer opens. pub async fn accept_bi(&self) -> Result<(send::Stream, recv::Stream), Error> { - kio::wait(|waiter| accept_bi(&self.shared, waiter)).await + kio::wait(|waiter| self.poll_accept_bi(waiter)).await } /// Open a unidirectional stream scheduled by `options`, once the peer's stream credit @@ -514,9 +509,7 @@ impl Connection { /// # Panics /// - when `options.group` belongs to another connection pub async fn open_uni(&self, options: send::Options<'_>) -> Result { - kio::wait(|waiter| open(&self.shared, Dir::Uni, options, waiter)) - .await - .map(|(send, _)| send) + kio::wait(|waiter| self.poll_open_uni(waiter, options)).await } /// Open a bidirectional stream scheduled by `options`, once the peer's stream credit @@ -525,53 +518,46 @@ impl Connection { /// # Panics /// - when `options.group` belongs to another connection pub async fn open_bi(&self, options: send::Options<'_>) -> Result<(send::Stream, recv::Stream), Error> { - kio::wait(|waiter| open(&self.shared, Dir::Bi, options, waiter)) - .await - .map(|(send, recv)| (send, recv.expect("bidirectional"))) + kio::wait(|waiter| self.poll_open_bi(waiter, options)).await } /// Receive the next datagram. pub async fn read_datagram(&self) -> Result { - kio::wait(|waiter| recv_datagram(&self.shared, waiter)).await + kio::wait(|waiter| self.poll_recv_datagram(waiter)).await } /// [`Self::accept_uni`], as a poll. - pub(crate) fn poll_accept_uni(&mut self, cx: &Context<'_>) -> Poll> { - accept_uni(&self.shared, self.park.hold(cx)) + pub fn poll_accept_uni(&self, waiter: &kio::Waiter) -> Poll> { + accept_uni(&self.shared, waiter) } /// [`Self::accept_bi`], as a poll. - pub(crate) fn poll_accept_bi(&mut self, cx: &Context<'_>) -> Poll> { - accept_bi(&self.shared, self.park.hold(cx)) + pub fn poll_accept_bi(&self, waiter: &kio::Waiter) -> Poll> { + accept_bi(&self.shared, waiter) } /// [`Self::open_uni`], as a poll. - pub(crate) fn poll_open_uni( - &mut self, - cx: &Context<'_>, - options: send::Options<'_>, - ) -> Poll> { - open(&self.shared, Dir::Uni, options, self.park.hold(cx)).map_ok(|(send, _)| send) + pub fn poll_open_uni(&self, waiter: &kio::Waiter, options: send::Options<'_>) -> Poll> { + open(&self.shared, Dir::Uni, options, waiter).map_ok(|(send, _)| send) } /// [`Self::open_bi`], as a poll. - pub(crate) fn poll_open_bi( - &mut self, - cx: &Context<'_>, + pub fn poll_open_bi( + &self, + waiter: &kio::Waiter, options: send::Options<'_>, ) -> Poll> { - open(&self.shared, Dir::Bi, options, self.park.hold(cx)) - .map_ok(|(send, recv)| (send, recv.expect("bidirectional"))) + open(&self.shared, Dir::Bi, options, waiter).map_ok(|(send, recv)| (send, recv.expect("bidirectional"))) } /// [`Self::read_datagram`], as a poll. - pub(crate) fn poll_recv_datagram(&mut self, cx: &Context<'_>) -> Poll> { - recv_datagram(&self.shared, self.park.hold(cx)) + pub fn poll_recv_datagram(&self, waiter: &kio::Waiter) -> Poll> { + recv_datagram(&self.shared, waiter) } /// [`Self::closed`], as a poll. - pub(crate) fn poll_closed(&mut self, cx: &Context<'_>) -> Poll { - poll_closed(&self.shared, self.park.hold(cx)) + pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll { + poll_closed(&self.shared, waiter) } /// The connection's counters in [`web_transport_trait::Stats`] shape. diff --git a/rs/moq-uring/src/quic/driver/endpoint.rs b/rs/moq-uring/src/quic/driver/endpoint.rs index 0aa8a5f7a56..1bf012e8e65 100644 --- a/rs/moq-uring/src/quic/driver/endpoint.rs +++ b/rs/moq-uring/src/quic/driver/endpoint.rs @@ -150,21 +150,23 @@ impl Endpoint { /// [`server`](Config::server) configuration, and with the socket's error /// once the endpoint has died, its worker included. pub async fn accept(&self) -> Result { - kio::wait(|waiter| { - if let Some(err) = self.inner.closed() { - return Poll::Ready(Err(err)); - } - let mut accepting = self.inner.accepting.borrow_mut(); - let Some(accepting) = accepting.as_mut() else { - return Poll::Ready(Err(Error::NotServer)); - }; - if let Some(conn) = accepting.queue.pop_front() { - return Poll::Ready(Ok(conn)); - } - waiter.register(&mut self.inner.accept_waiters.borrow_mut()); - Poll::Pending - }) - .await + kio::wait(|waiter| self.poll_accept(waiter)).await + } + + /// The next handshaken incoming connection, or the endpoint's terminal error. + pub fn poll_accept(&self, waiter: &kio::Waiter) -> Poll> { + if let Some(err) = self.inner.closed() { + return Poll::Ready(Err(err)); + } + let mut accepting = self.inner.accepting.borrow_mut(); + let Some(accepting) = accepting.as_mut() else { + return Poll::Ready(Err(Error::NotServer)); + }; + if let Some(conn) = accepting.queue.pop_front() { + return Poll::Ready(Ok(conn)); + } + waiter.register(&mut self.inner.accept_waiters.borrow_mut()); + Poll::Pending } /// Dial `remote` through this endpoint's socket, verifying it as diff --git a/rs/moq-uring/src/quic/driver/recv.rs b/rs/moq-uring/src/quic/driver/recv.rs index f4a72fe8598..0997089e980 100644 --- a/rs/moq-uring/src/quic/driver/recv.rs +++ b/rs/moq-uring/src/quic/driver/recv.rs @@ -161,7 +161,7 @@ impl Stream { if let Some(code) = self.reset { return Poll::Ready(Err(Error::Reset(code))); } - let (result, _) = self + let result = self .shared .conn .borrow_mut() @@ -296,7 +296,7 @@ impl Stream { Ok(mut chunks) => { let status = read_fn(&mut chunks, waiter); // Reading is what returns the peer's flow control credit. - let _ = chunks.finalize(); + chunks.finalize(); status } Err(err) => ReadStatus::Failed(None, err), @@ -410,7 +410,7 @@ impl Lease<'_> { pub fn release(mut self) { let lease = self.inner.take().expect("only taken to hand it back"); let stream = &*self.stream; - let _ = stream.shared.conn.borrow_mut().recv_stream(stream.id).release(lease); + stream.shared.conn.borrow_mut().recv_stream(stream.id).release(lease); } } @@ -437,7 +437,7 @@ impl Drop for Lease<'_> { fn drop(&mut self) { if let Some(lease) = self.inner.take() { let stream = &*self.stream; - let _ = stream.shared.conn.borrow_mut().recv_stream(stream.id).restore(lease); + stream.shared.conn.borrow_mut().recv_stream(stream.id).restore(lease); } } } diff --git a/rs/moq-uring/src/quic/driver/send.rs b/rs/moq-uring/src/quic/driver/send.rs index 0485329712d..22a0ce0c8e8 100644 --- a/rs/moq-uring/src/quic/driver/send.rs +++ b/rs/moq-uring/src/quic/driver/send.rs @@ -4,7 +4,8 @@ //! copy), which wakes the driver's transmit wait itself, and the handles wait through //! moq-quic's poll forms, which wake them. -use std::future::poll_fn; +use std::future::{Future, poll_fn}; +use std::pin::Pin; use std::rc::Rc; use std::task::{Context, Poll, ready}; @@ -166,12 +167,12 @@ impl Stream { /// Completes when the peer stops the stream. See [`Finished::stopped()`]. pub async fn stopped(&self) -> Result, Error> { - kio::wait(|waiter| poll_stopped(&self.0.shared, self.0.id, waiter)).await + kio::wait(|waiter| self.poll_closed(waiter)).await } - /// [`Self::stopped`], as a poll. - pub(crate) fn poll_stopped(&mut self, cx: &Context<'_>) -> Poll, Error>> { - poll_stopped(&self.0.shared, self.0.id, self.0.park.hold(cx)) + /// Wait for the stream to end, retaining the peer's stop code; see [`Self::stopped`]. + pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll, Error>> { + poll_stopped(&self.0.shared, self.0.id, waiter) } /// The identity of this stream. @@ -219,12 +220,12 @@ impl Finished { /// it). Fails only when the connection ends first, since then the caller cannot read success /// as "every byte arrived". pub async fn stopped(&self) -> Result, Error> { - kio::wait(|waiter| poll_stopped(&self.0.shared, self.0.id, waiter)).await + kio::wait(|waiter| self.poll_closed(waiter)).await } - /// [`Self::stopped`], as a poll. - pub(crate) fn poll_stopped(&mut self, cx: &Context<'_>) -> Poll, Error>> { - poll_stopped(&self.0.shared, self.0.id, self.0.park.hold(cx)) + /// Wait for the stream to end, retaining the peer's stop code; see [`Self::stopped`]. + pub fn poll_closed(&self, waiter: &kio::Waiter) -> Poll, Error>> { + poll_stopped(&self.0.shared, self.0.id, waiter) } /// The identity of this stream. @@ -233,6 +234,15 @@ impl Finished { } } +impl Future for Finished { + type Output = Result, Error>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.get_mut(); + poll_stopped(&this.0.shared, this.0.id, this.0.park.hold(cx)) + } +} + impl Half { fn finish(&mut self) { // A STOP_SENDING may have beaten us here, which `stopped` reports. diff --git a/rs/moq-uring/src/quic/web.rs b/rs/moq-uring/src/quic/web.rs index 13948d5e5e6..025d6b7c231 100644 --- a/rs/moq-uring/src/quic/web.rs +++ b/rs/moq-uring/src/quic/web.rs @@ -157,7 +157,7 @@ impl Request { } } - async fn handshake(mut conn: Connection) -> Result { + async fn handshake(conn: Connection) -> Result { // Our control stream: the SETTINGS advertising WebTransport support. let mut control = conn.open_uni(Default::default()).await?; let mut settings = proto::Settings::default(); @@ -181,7 +181,8 @@ impl Request { // never sending a control stream. let mut arrivals = 0usize; let mut peer_control = None; - std::future::poll_fn(|cx| { + kio::wait(|waiter| { + let mut cx = waiter.context(); loop { // Stop adopting past the cap, but do not give up on what is // already here: the control stream may be sitting at the head @@ -191,7 +192,7 @@ impl Request { // cancel a legitimate stream on a handshake that then succeeds. let mut over = false; while !over { - match conn.poll_accept_uni(cx) { + match conn.poll_accept_uni(waiter) { Poll::Ready(Ok(recv)) => { arrivals += 1; pending.push(PendingUni::new(recv)); @@ -207,7 +208,7 @@ impl Request { let mut progressed = false; let mut index = 0; while index < pending.len() { - let Poll::Ready(class) = pending[index].poll_classify(cx) else { + let Poll::Ready(class) = pending[index].poll_classify(&mut cx) else { index += 1; continue; }; @@ -315,15 +316,14 @@ impl Request { let mut buf = Vec::new(); response.encode(&mut buf).map_err(|err| Error::Web(err.to_string()))?; write_all(&mut self.send, &buf).await?; - let mut send = self.send.finish(); + let send = self.send.finish(); // The guard stays armed across the wait below. Cancelling this future // mid-grace would otherwise skip the deliberate close and leak the // connection, which is the very thing the guard is here to prevent. let mut deadline = self.conn.owner().after(CLOSE_GRACE); kio::wait(|waiter| { - let cx = waiter.context(); - if send.poll_stopped(&cx).is_ready() { + if send.poll_closed(waiter).is_ready() { return Poll::Ready(()); } deadline.poll(waiter) @@ -434,6 +434,7 @@ struct State { /// disabled. Clones share the session, which closes with code 0 once its last /// clone and stream are gone. pub struct Session { + park: kio::Park, handles: Rc, conn: Connection, web: Option>, @@ -493,6 +494,7 @@ impl Session { pub fn raw(conn: Connection) -> Self { let protocol = conn.protocol().map(str::to_owned); Self { + park: kio::Park::default(), handles: Handles::new(&conn, None), conn, web: None, @@ -567,6 +569,7 @@ impl Session { .spawn(async move { read_capsules(capsules, capsule_conn, recv).await }); Self { + park: kio::Park::default(), handles: Handles::new(&conn, Some(&web)), conn, web: Some(web), @@ -588,6 +591,7 @@ impl Session { impl Clone for Session { fn clone(&self) -> Self { Self { + park: kio::Park::default(), handles: self.handles.clone(), conn: self.conn.clone(), web: self.web.clone(), @@ -634,7 +638,7 @@ impl web_transport_trait::poll::Session for Session { fn poll_accept_uni(&mut self, cx: &mut Context<'_>) -> Poll> { let Some(web) = self.web.clone() else { - let inner = ready!(self.conn.poll_accept_uni(cx))?; + let inner = ready!(self.conn.poll_accept_uni(self.park.hold(cx)))?; return Poll::Ready(Ok(RecvStream::new(inner, &self.handles))); }; @@ -647,7 +651,7 @@ impl web_transport_trait::poll::Session for Session { // Each mid-header stream parks the caller in its own stream's // waiters, so progress on any of them re-polls us. let ended = loop { - match self.conn.poll_accept_uni(cx) { + match self.conn.poll_accept_uni(self.park.hold(cx)) { Poll::Ready(Ok(recv)) => web.state.borrow_mut().pending_uni.push(PendingUni::new(recv)), Poll::Ready(Err(err)) => break Some(err), Poll::Pending => break None, @@ -672,7 +676,7 @@ impl web_transport_trait::poll::Session for Session { cx: &mut Context<'_>, ) -> Poll> { let Some(web) = self.web.clone() else { - let (send, recv) = ready!(self.conn.poll_accept_bi(cx))?; + let (send, recv) = ready!(self.conn.poll_accept_bi(self.park.hold(cx)))?; return Poll::Ready(Ok(( SendStream::new(send, &self.handles), RecvStream::new(recv, &self.handles), @@ -688,7 +692,7 @@ impl web_transport_trait::poll::Session for Session { } loop { - match self.conn.poll_accept_bi(cx) { + match self.conn.poll_accept_bi(self.park.hold(cx)) { Poll::Ready(Ok((send, recv))) => web.state.borrow_mut().pending_bi.push(PendingBi::new(send, recv)), Poll::Ready(Err(err)) => return Poll::Ready(Err(unmap_err(err))), Poll::Pending => break, @@ -703,7 +707,8 @@ impl web_transport_trait::poll::Session for Session { fn poll_open_uni(&mut self, cx: &mut Context<'_>) -> Poll> { if self.opening_uni.is_none() { - let inner = ready!(self.conn.poll_open_uni(cx, Default::default())).map_err(|err| self.map_err(err))?; + let inner = ready!(self.conn.poll_open_uni(self.park.hold(cx), Default::default())) + .map_err(|err| self.map_err(err))?; let header = self.web.as_ref().map(|web| web.header_uni.clone()).unwrap_or_default(); self.opening_uni = Some((SendStream::new(inner, &self.handles), header)); } @@ -718,8 +723,8 @@ impl web_transport_trait::poll::Session for Session { cx: &mut Context<'_>, ) -> Poll> { if self.opening_bi.is_none() { - let (send, recv) = - ready!(self.conn.poll_open_bi(cx, Default::default())).map_err(|err| self.map_err(err))?; + let (send, recv) = ready!(self.conn.poll_open_bi(self.park.hold(cx), Default::default())) + .map_err(|err| self.map_err(err))?; let header = self.web.as_ref().map(|web| web.header_bi.clone()).unwrap_or_default(); self.opening_bi = Some(( SendStream::new(send, &self.handles), @@ -743,10 +748,10 @@ impl web_transport_trait::poll::Session for Session { fn poll_recv_datagram(&mut self, cx: &mut Context<'_>) -> Poll> { let Some(web) = self.web.clone() else { - return self.conn.poll_recv_datagram(cx); + return self.conn.poll_recv_datagram(self.park.hold(cx)); }; loop { - let datagram = ready!(self.conn.poll_recv_datagram(cx)).map_err(unmap_err)?; + let datagram = ready!(self.conn.poll_recv_datagram(self.park.hold(cx))).map_err(unmap_err)?; // The prefix names the session; anything else is not ours. let mut peek: &[u8] = &datagram; match decode_varint(&mut peek) { @@ -776,7 +781,7 @@ impl web_transport_trait::poll::Session for Session { } fn poll_closed(&mut self, cx: &mut Context<'_>) -> Poll { - let err = ready!(self.conn.poll_closed(cx)); + let err = ready!(self.conn.poll_closed(self.park.hold(cx))); let Some(web) = &self.web else { return Poll::Ready(err); }; @@ -841,7 +846,7 @@ fn close(conn: &Connection, web: Option<&Rc>, code: u32, reason: &str) { let mut deadline = conn.owner().after(CLOSE_GRACE); let reason = reason.to_string(); let owner = conn.owner().clone(); - let mut conn = conn.clone(); + let conn = conn.clone(); // The caller may drop every handle right after this, so the task holds // the H3 control and QPACK streams itself: RFC 9114 makes closing one a // connection error, which a browser reports instead of the capsule. @@ -856,7 +861,7 @@ fn close(conn: &Connection, web: Option<&Rc>, code: u32, reason: &str) { // Stop writing once the connection is gone; the deadline is the // only other thing that ends this. - if conn.poll_closed(&cx).is_ready() { + if conn.poll_closed(waiter).is_ready() { return Poll::Ready(()); } if let Some(stream) = &mut send { @@ -915,6 +920,7 @@ impl Drop for Handles { /// reset still delivers the header and the peer can tell which session the /// reset belongs to. pub struct SendStream { + park: kio::Park, state: SendState, /// The end of the WebTransport header on a stream we opened, for a reset /// to deliver. @@ -942,6 +948,7 @@ impl SendStream { inner.set_drop_reset(None, http3_code(proto::error_to_http3(0))); } Self { + park: kio::Park::default(), state: SendState::Open(inner), header: None, web, @@ -1061,8 +1068,8 @@ impl web_transport_trait::poll::SendStream for SendStream { fn poll_closed(&mut self, cx: &mut Context<'_>) -> Poll> { let stopped = match &mut self.state { - SendState::Open(stream) => ready!(stream.poll_stopped(cx)), - SendState::Finished(stream) => ready!(stream.poll_stopped(cx)), + SendState::Open(stream) => ready!(stream.poll_closed(self.park.hold(cx))), + SendState::Finished(stream) => ready!(stream.poll_closed(self.park.hold(cx))), SendState::Reset => return Poll::Ready(Ok(())), }; Poll::Ready(match stopped { diff --git a/rs/moq-uring/tests/integration/credit.rs b/rs/moq-uring/tests/integration/credit.rs index fb496af7c3c..8643282c70a 100644 --- a/rs/moq-uring/tests/integration/credit.rs +++ b/rs/moq-uring/tests/integration/credit.rs @@ -115,7 +115,7 @@ fn endpoints( fn write(handle: &moq_uring::Handle, mut send: quic::send::Stream, len: usize) { handle.spawn(async move { if send.write_all(Bytes::from(vec![7u8; len])).await.is_ok() { - let _ = send.finish().stopped().await; + let _ = send.finish().await; } }); } @@ -277,7 +277,7 @@ fn chunked_writes_stop_at_the_window() { handle.spawn(async move { send.write_all(buf).await.expect("write"); - let _ = send.finish().stopped().await; + let _ = send.finish().await; }); let mut recv = within(&handle, "the stream", accepted.accept_uni()) diff --git a/rs/qmux/src/session.rs b/rs/qmux/src/session.rs index c7896ebf18a..c78261d2bd1 100644 --- a/rs/qmux/src/session.rs +++ b/rs/qmux/src/session.rs @@ -642,7 +642,7 @@ impl RecvStream { let mut state = self.inner.lock(); // moq-quic wakes the driver for any credit the lease frees, and the reader of any stream // it evicts. - let (result, _) = state.conn.recv_stream(self.id).poll_lease(waiter, want); + let result = state.conn.recv_stream(self.id).poll_lease(waiter, want); match ready!(result) { Ok(lease) => Poll::Ready(Ok(lease)), Err(recv::Error::Reset(code)) => { @@ -785,7 +785,7 @@ impl poll::Lease for RecvLease<'_> { let lease = self.lease.take().expect("only taken to hand it back"); let mut state = self.stream.inner.lock(); // moq-quic wakes the driver for the credit this returns. - let _ = state.conn.recv_stream(self.stream.id).release(lease); + state.conn.recv_stream(self.stream.id).release(lease); } } @@ -796,7 +796,7 @@ impl Drop for RecvLease<'_> { }; let mut state = self.stream.inner.lock(); // moq-quic wakes the driver if the window was stuck on this lease. - let _ = state.conn.recv_stream(self.stream.id).restore(lease); + state.conn.recv_stream(self.stream.id).restore(lease); } } From 4ee591b7beba2061014d82c887ac756d16384309 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 00:52:15 -0800 Subject: [PATCH 3/5] test(quic): verify finalized reads wake transmission Co-Authored-By: GPT-6.1 Sol --- rs/moq-quic/src/connection/streams/state.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rs/moq-quic/src/connection/streams/state.rs b/rs/moq-quic/src/connection/streams/state.rs index 15763a8cc52..8e303625d0c 100644 --- a/rs/moq-quic/src/connection/streams/state.rs +++ b/rs/moq-quic/src/connection/streams/state.rs @@ -1736,6 +1736,8 @@ mod tests { assert_eq!(client.data_recvd, 2048); assert_eq!(client.local_max_data - initial_max, 0); + let transmit = Counter::new(); + client.waits.register_transmit(&transmit); let mut pending = Retransmits::default(); let mut recv = recv::Stream { id, @@ -1746,7 +1748,9 @@ mod tests { let mut chunks = recv.read().unwrap(); assert_eq!(chunks.next(MESSAGE_SIZE).unwrap().unwrap().bytes.len(), MESSAGE_SIZE); assert!(chunks.next(0).unwrap().is_none()); + assert_eq!(transmit.wakes(), 0); chunks.finalize(); + assert_eq!(transmit.wakes(), 1); assert!(pending.max_stream_id[Dir::Uni as usize]); assert_eq!(client.local_max_data - initial_max, MESSAGE_SIZE as u64); } From 26572823c190a0f7cc8250929ac9f5e30f4dc742 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 13:19:04 -0800 Subject: [PATCH 4/5] chore(moq-tokio): drop unused pin-project-lite and link MIGRATE entries The Accept future was the last pin_project! user. Link the new MIGRATE headings to the PR like their neighbors. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 - rs/moq-quic/MIGRATE.md | 2 +- rs/moq-tokio/Cargo.toml | 3 +-- rs/moq-uring/MIGRATE.md | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 155cdfa41c7..de2ef7fcfbb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5321,7 +5321,6 @@ dependencies = [ "moq-sock", "moq-tokio", "notify", - "pin-project-lite", "qmux 0.7.0", "rand 0.10.3", "rcgen 0.14.10", diff --git a/rs/moq-quic/MIGRATE.md b/rs/moq-quic/MIGRATE.md index fddf21a533e..26f7f78e92f 100644 --- a/rs/moq-quic/MIGRATE.md +++ b/rs/moq-quic/MIGRATE.md @@ -4,7 +4,7 @@ How to update your code for each breaking change, newest first. ## Unreleased -### Receive operations wake transmission themselves +### Receive operations wake transmission themselves ([#7707](https://github.com/moq-dev/moq/pull/7707)) `ShouldTransmit` is removed. `recv::Stream::lease` returns `Result` and `poll_lease` returns `Poll>`; remove the transmit hint from tuple diff --git a/rs/moq-tokio/Cargo.toml b/rs/moq-tokio/Cargo.toml index 92b6d073e40..02e20355d08 100644 --- a/rs/moq-tokio/Cargo.toml +++ b/rs/moq-tokio/Cargo.toml @@ -19,7 +19,7 @@ default = ["net", "quic", "aws-lc-rs", "websocket", "tcp", "uds"] net = ["dep:moq-net", "dep:web-transport-trait"] # The async QUIC driver alone (`quic::driver`): moq-quic on tokio, without moq-net or # the MoQ backend. Needs a crypto-provider feature. -driver = ["dep:hmac", "dep:moq-congestion", "dep:moq-quic", "dep:pin-project-lite", "dep:rustc-hash", "dep:sha2"] +driver = ["dep:hmac", "dep:moq-congestion", "dep:moq-quic", "dep:rustc-hash", "dep:sha2"] # QUIC and WebTransport over moq-quic, for `https://`, `moqt://`, and `moql://`. quic = ["_transport", "_certs", "driver", "dep:http", "dep:web-transport-proto", "dep:reqwest", "watch", "dep:dns-lookup"] # Server-side certificate handling: chains loaded from disk, self-signed generation, @@ -81,7 +81,6 @@ moq-net = { workspace = true, optional = true } moq-quic = { workspace = true, optional = true } moq-sock = { workspace = true } notify = { version = "8", optional = true } -pin-project-lite = { version = "0.2", optional = true } qmux = { workspace = true, features = ["wss", "tls", "tcp"], optional = true } rand = "0.10.1" rcgen = { workspace = true, optional = true } diff --git a/rs/moq-uring/MIGRATE.md b/rs/moq-uring/MIGRATE.md index ef8048662d1..5759bd5e8de 100644 --- a/rs/moq-uring/MIGRATE.md +++ b/rs/moq-uring/MIGRATE.md @@ -4,7 +4,7 @@ How to update your code for each breaking change, newest first. ## Unreleased -### QUIC waits have caller-owned poll forms +### QUIC waits have caller-owned poll forms ([#7707](https://github.com/moq-dev/moq/pull/7707)) The QUIC driver connection's stream opens, accepts, datagram receive and close waits, the endpoint's accept, and both send handles' close waits now expose `poll_*` methods From f2dd9c7524913113ea871d3170070c423c31df93 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 13:45:29 -0800 Subject: [PATCH 5/5] fix(moq-tokio): compile the driver-only build again recv::Stream::poll_received_reset takes a Context only for the WebTransport adapter, so a driver-only build flagged it as dead code. Gate it on quic like set_drop_stop beside it. Co-Authored-By: Claude Opus 5.5 --- rs/moq-tokio/src/quic/driver/recv.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rs/moq-tokio/src/quic/driver/recv.rs b/rs/moq-tokio/src/quic/driver/recv.rs index 04fe958ad52..40a2339ce79 100644 --- a/rs/moq-tokio/src/quic/driver/recv.rs +++ b/rs/moq-tokio/src/quic/driver/recv.rs @@ -213,6 +213,7 @@ impl Stream { /// Poll form of [`Self::received_reset`]. It takes no bytes, so a lease in progress is /// left as it was. + #[cfg(feature = "quic")] pub(crate) fn poll_received_reset(&mut self, cx: &mut Context<'_>) -> Poll, Error>> { let waiter = self.park.hold(cx); self.reader.poll_received_reset(waiter)