From c906cb275f9c8bb7546eb077a706e68cf5cc1226 Mon Sep 17 00:00:00 2001 From: Melanie Riise Date: Thu, 15 Oct 2026 17:19:07 -0700 Subject: [PATCH 1/3] fix(net): refuse a revoked subscription while its SUBSCRIBE is still pending Co-authored-by: Claude Fable 5.1 --- rs/moq-net/src/ietf/subscriber.rs | 173 ++++++++++++++++++++++++++++-- 1 file changed, 166 insertions(+), 7 deletions(-) diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 38e8325b1df..6b15d763252 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -2671,10 +2671,21 @@ where } }; - // Retirement cancels only the wait for an ID. On drafts 14-16 the peer grants - // MAX_REQUEST_ID credit back only for requests it receives, so an allocated ID is - // always sent; the response wait below cancels it if the source retired meanwhile. - let request_id = match validity.run(self.control.next_request_id(&self.runtime)).await { + // Retirement and revocation cancel only the wait for an ID. On drafts 14-16 the peer + // grants MAX_REQUEST_ID credit back only for requests it receives, so an allocated ID + // is always sent; the response wait below cancels it if either landed meanwhile. + let request_id = { + let mut credit = std::pin::pin!(self.control.next_request_id(&self.runtime)); + kio::wait(|waiter| { + if gate.poll_denied(waiter).is_ready() { + return Poll::Ready(Err(Error::Unauthorized)); + } + validity.check(waiter)?; + waiter.poll_future(credit.as_mut()) + }) + .await + }; + let request_id = match request_id { Ok(id) => id, Err(err) => { target.fail(err); @@ -2797,7 +2808,8 @@ where Gone, } - let mut retired = false; + // A local terminal ended the wait, so the peer still holds the request to cancel. + let mut unanswered = false; let setup = { let mut response = std::pin::pin!(self.read_subscribe_response(&mut stream)); loop { @@ -2807,9 +2819,13 @@ where return Poll::Ready(Setup::Response(Err(err))); } if let Err(err) = validity.check(waiter) { - retired = true; + unanswered = true; return Poll::Ready(Setup::Response(Err(err))); } + if gate.poll_denied(waiter).is_ready() { + unanswered = true; + return Poll::Ready(Setup::Response(Err(Error::Unauthorized))); + } // An answer that has already arrived wins over the local terminals. Both can // be ready in one poll, and taking abandonment there would discard a response // the publisher has already sent: if it was a rejection, the request is gone @@ -2913,7 +2929,7 @@ where } Err(err) => { tracing::debug!(%err, "subscribe response error"); - if retired { + if unanswered { self.cancel_subscribe(stream, request_id).await; } if let Some(pending) = self.take_pending(request_id) { @@ -6885,6 +6901,149 @@ mod tests { ); } + /// A subscriber whose grant from the peer covers everything until `auth.ended(0, ..)` + /// revokes it, with the token handle to hold meanwhile. + fn revocable_auth() -> (crate::auth::Handle, crate::auth::Auth) { + let auth = crate::auth::Handle::new(true); + let cred = auth.present(bytes::Bytes::from_static(b"cred"), true).unwrap(); + auth.granted(0, crate::auth::Grant::all()); + (auth, cred) + } + + /// A grant revoked while the SUBSCRIBE waits for request-ID credit refuses the request + /// at once, not after the ten-second give-up. + #[moq_clock::test] + async fn a_revocation_never_waits_behind_subscribe_credit() { + for version in [Version::Draft14, Version::Draft15, Version::Draft16] { + // MAX_REQUEST_ID admits nothing, so the SUBSCRIBE waits for credit. + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + let log = session.log.clone(); + let (tasks, _task_set) = crate::util::TaskSet::new(); + let (auth, _cred) = revocable_auth(); + let mut subscriber = Subscriber::new( + crate::time::sim(), + session, + crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + Control::new(Some(RequestId(1)), false), + None, + peer::PeerSetup::declared(peer::Peer::default()), + crate::Hop::new(1).unwrap(), + None, + version, + tasks, + Default::default(), + ) + .with_auth(auth.clone()); + + let producer = crate::broadcast::Info::default().produce(); + let mut dynamic = producer.dynamic(); + let consumer = producer.consume(); + let track = consumer.track("video").unwrap(); + let subscription = track.subscribe(None); + let request = dynamic.requested_track().await.expect("no track requested"); + + let serving = moq_clock::sim::spawn(async move { + subscriber + .run_subscribe(Path::new("broadcast"), dynamic, None, request) + .await; + }); + settle().await; + assert!( + log.writes.lock().unwrap().is_empty(), + "{version}: a SUBSCRIBE went out without credit" + ); + + auth.ended(0, Error::Session(SessionError::Unauthorized)); + let result = moq_clock::timeout(Duration::from_secs(1), subscription) + .await + .unwrap_or_else(|_| panic!("{version}: the revocation waited behind the credit")); + assert!( + matches!(result, Err(Error::Unauthorized)), + "{version}: the request outlived its grant: {:?}", + result.err() + ); + moq_clock::timeout(Duration::from_secs(1), serving) + .await + .unwrap_or_else(|_| panic!("{version}: run_subscribe outlived its grant")) + .unwrap(); + assert!( + log.writes.lock().unwrap().is_empty(), + "{version}: nothing was sent without credit" + ); + assert!(log.closes().is_empty(), "{version}: the session stays up"); + } + } + + /// A grant revoked while the SUBSCRIBE waits for its answer refuses the request and + /// cancels the subscribe at once, not once the peer answers. + #[moq_clock::test] + async fn a_revocation_never_waits_behind_subscribe_ok() { + for version in [Version::Draft14, Version::Draft16, Version::Draft18, Version::Draft22] { + // A peer that accepts the stream and then says nothing at all. + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + let log = session.log.clone(); + let (tasks, _task_set) = crate::util::TaskSet::new(); + let (auth, _cred) = revocable_auth(); + let mut subscriber = Subscriber::new( + crate::time::sim(), + session, + crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + Control::new(None, false), + None, + peer::PeerSetup::declared(peer::Peer::default()), + crate::Hop::new(1).unwrap(), + None, + version, + tasks, + Default::default(), + ) + .with_auth(auth.clone()); + + let producer = crate::broadcast::Info::default().produce(); + let mut dynamic = producer.dynamic(); + let consumer = producer.consume(); + let track = consumer.track("video").unwrap(); + let subscription = track.subscribe(None); + let request = dynamic.requested_track().await.expect("no track requested"); + + let serving = moq_clock::sim::spawn(async move { + subscriber + .run_subscribe(Path::new("broadcast"), dynamic, None, request) + .await; + }); + // Let the SUBSCRIBE go out. No SUBSCRIBE_OK is coming. + settle().await; + assert!(occurrences(&log, &[ietf::Subscribe::ID as u8]) > 0, "{version}"); + + auth.ended(0, Error::Session(SessionError::Unauthorized)); + let result = moq_clock::timeout(Duration::from_secs(1), subscription) + .await + .unwrap_or_else(|_| panic!("{version}: the revocation waited behind SUBSCRIBE_OK")); + assert!( + matches!(result, Err(Error::Unauthorized)), + "{version}: the request outlived its grant: {:?}", + result.err() + ); + moq_clock::timeout(Duration::from_secs(1), serving) + .await + .unwrap_or_else(|_| panic!("{version}: run_subscribe outlived its grant")) + .unwrap(); + // A subscribe revoked before SUBSCRIBE_OK must still be cancelled: by UNSUBSCRIBE + // where the draft has one, and by stopping the publisher's direction on every draft. + if subscriber_unsubscribes(version) { + assert!(occurrences(&log, &[ietf::Unsubscribe::ID as u8]) > 0, "{version}"); + } + assert_eq!(log.stops(), vec![crate::ietf::error::CANCELLED], "{version}"); + assert!(log.closes().is_empty(), "{version}: the session stays up"); + } + } + + /// Whether `version` cancels a subscription with an UNSUBSCRIBE message; draft-17 + /// removed it. + fn subscriber_unsubscribes(version: Version) -> bool { + matches!(version, Version::Draft14 | Version::Draft15 | Version::Draft16) + } + /// An unanswered epochless request cannot prove which instance the peer resolved. /// Retiring its source cancels it even while the caller keeps its demand. #[moq_clock::test] From 02b33e7afbedbfad28e0869e63e267120e4e8ade Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 20 Oct 2026 13:17:43 -0700 Subject: [PATCH 2/3] test(net): reuse the subscriber's own UNSUBSCRIBE check Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/ietf/subscriber.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 6b1ca931865..d54f82d6d18 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -7073,6 +7073,7 @@ mod tests { let track = consumer.track("video").unwrap(); let subscription = track.subscribe(None); let request = dynamic.requested_track().await.expect("no track requested"); + let unsubscribes = subscriber.unsubscribes(); let serving = moq_clock::sim::spawn(async move { subscriber @@ -7098,7 +7099,7 @@ mod tests { .unwrap(); // A subscribe revoked before SUBSCRIBE_OK must still be cancelled: by UNSUBSCRIBE // where the draft has one, and by stopping the publisher's direction on every draft. - if subscriber_unsubscribes(version) { + if unsubscribes { assert!(occurrences(&log, &[ietf::Unsubscribe::ID as u8]) > 0, "{version}"); } assert_eq!(log.stops(), vec![crate::ietf::error::CANCELLED], "{version}"); @@ -7106,12 +7107,6 @@ mod tests { } } - /// Whether `version` cancels a subscription with an UNSUBSCRIBE message; draft-17 - /// removed it. - fn subscriber_unsubscribes(version: Version) -> bool { - matches!(version, Version::Draft14 | Version::Draft15 | Version::Draft16) - } - /// An unanswered epochless request cannot prove which instance the peer resolved. /// Retiring its source cancels it even while the caller keeps its demand. #[moq_clock::test] From 626734be4fb1ff069e2284236c407a64adc95f06 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 20 Oct 2026 13:42:12 -0700 Subject: [PATCH 3/3] fix(net): refuse a revoked subscribe before its wire cleanup A peer that never acknowledges the cancellation no longer holds the refusal or the resumed track. The SUBSCRIBE's credit wait now composes a poll-native Control::poll_credit instead of pinning the async wrapper. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/ietf/control.rs | 39 ++++++++++++++++++----------- rs/moq-net/src/ietf/subscriber.rs | 41 +++++++++++++++---------------- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 5698a3b05b4..a65f54cc712 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -123,6 +123,9 @@ impl Drop for RequestSend { } impl Control { + /// How long a request waits for MAX_REQUEST_ID credit before giving up. + pub const REQUEST_ID_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + pub fn new(request_id_max: Option, client: bool) -> Self { Self { state: kio::Shared::new(ControlState { @@ -286,26 +289,32 @@ impl Control { Poll::Ready(state.request_id_next.increment()) } + /// Allocate the next request_id once credit and the committed header allow it, or give + /// up with [`Error::Cancel`] once `timeout` passes. + /// + /// Arm `timeout` with [`Self::REQUEST_ID_TIMEOUT`] on the caller's clock. + pub fn poll_credit( + &self, + waiter: &kio::Waiter, + timeout: &mut crate::time::Deadline, + ) -> Poll> { + if let Poll::Ready(id) = self.poll_next_request_id(waiter) { + return Poll::Ready(Ok(id)); + } + if timeout.poll(waiter).is_ready() { + tracing::warn!("timed out waiting for MAX_REQUEST_ID"); + return Poll::Ready(Err(Error::Cancel)); + } + Poll::Pending + } + /// Allocate the next request_id once credit and the committed header allow it. /// /// `runtime` arms the give-up timer, so the wait cannot outlive the caller's /// clock. pub async fn next_request_id(&self, runtime: &crate::time::Deadlines) -> Result { - let mut timeout = crate::time::Deadline::after(runtime, std::time::Duration::from_secs(10)); - - kio::wait(|waiter| { - if let Poll::Ready(id) = self.poll_next_request_id(waiter) { - return Poll::Ready(Ok(id)); - } - - if timeout.poll(waiter).is_ready() { - tracing::warn!("timed out waiting for MAX_REQUEST_ID"); - return Poll::Ready(Err(Error::Cancel)); - } - - Poll::Pending - }) - .await + let mut timeout = crate::time::Deadline::after(runtime, Self::REQUEST_ID_TIMEOUT); + kio::wait(|waiter| self.poll_credit(waiter, &mut timeout)).await } } diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index d54f82d6d18..6d22a7bb1a4 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -2700,17 +2700,15 @@ where // Retirement and revocation cancel only the wait for an ID. On drafts 14-16 the peer // grants MAX_REQUEST_ID credit back only for requests it receives, so an allocated ID // is always sent; the response wait below cancels it if either landed meanwhile. - let request_id = { - let mut credit = std::pin::pin!(self.control.next_request_id(&self.runtime)); - kio::wait(|waiter| { - if gate.poll_denied(waiter).is_ready() { - return Poll::Ready(Err(Error::Unauthorized)); - } - validity.check(waiter)?; - waiter.poll_future(credit.as_mut()) - }) - .await - }; + let mut timeout = crate::time::Deadline::after(&self.runtime, Control::REQUEST_ID_TIMEOUT); + let request_id = kio::wait(|waiter| { + if gate.poll_denied(waiter).is_ready() { + return Poll::Ready(Err(Error::Unauthorized)); + } + validity.check(waiter)?; + self.control.poll_credit(waiter, &mut timeout) + }) + .await; let request_id = match request_id { Ok(id) => id, Err(err) => { @@ -2956,9 +2954,7 @@ where } Err(err) => { tracing::debug!(%err, "subscribe response error"); - if unanswered { - self.cancel_subscribe(stream, request_id).await; - } + // Refuse before waiting on the peer to acknowledge the cancellation. if let Some(pending) = self.take_pending(request_id) { pending.reject(err.clone()); } @@ -2966,6 +2962,9 @@ where if let Some(idle) = resumed { let _ = idle.track.abort(err); } + if unanswered { + self.cancel_subscribe(stream, request_id).await; + } return None; } }; @@ -7043,12 +7042,13 @@ mod tests { } /// A grant revoked while the SUBSCRIBE waits for its answer refuses the request and - /// cancels the subscribe at once, not once the peer answers. + /// cancels the subscribe at once, not once the peer answers or acknowledges the cancel. #[moq_clock::test] async fn a_revocation_never_waits_behind_subscribe_ok() { for version in [Version::Draft14, Version::Draft16, Version::Draft18, Version::Draft22] { - // A peer that accepts the stream and then says nothing at all. - let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + // A peer that accepts the stream and then says nothing at all, not even + // acknowledging the cancellation's FIN. + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()).with_unacked_fin(); let log = session.log.clone(); let (tasks, _task_set) = crate::util::TaskSet::new(); let (auth, _cred) = revocable_auth(); @@ -7093,10 +7093,9 @@ mod tests { "{version}: the request outlived its grant: {:?}", result.err() ); - moq_clock::timeout(Duration::from_secs(1), serving) - .await - .unwrap_or_else(|_| panic!("{version}: run_subscribe outlived its grant")) - .unwrap(); + // Only the wire cleanup still waits on the peer's acknowledgement. + settle().await; + drop(serving); // A subscribe revoked before SUBSCRIBE_OK must still be cancelled: by UNSUBSCRIBE // where the draft has one, and by stopping the publisher's direction on every draft. if unsubscribes {