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 59b24a830d0..6d22a7bb1a4 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -2697,10 +2697,19 @@ 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 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) => { target.fail(err); @@ -2824,7 +2833,8 @@ where } let mut messages = super::request_stream::Messages::new(self.session.clone(), self.control.client()); - 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, &mut messages)); loop { @@ -2834,9 +2844,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 @@ -2940,9 +2954,7 @@ where } Err(err) => { tracing::debug!(%err, "subscribe response error"); - if retired { - 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()); } @@ -2950,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; } }; @@ -6953,6 +6968,144 @@ 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 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, 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(); + 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 unsubscribes = subscriber.unsubscribes(); + + 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() + ); + // 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 { + 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"); + } + } + /// 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]