From 950827b47dcf54b5cfb073c12772eb81587139d9 Mon Sep 17 00:00:00 2001 From: Frando Date: Wed, 2 Sep 2026 01:36:10 +0200 Subject: [PATCH 1/2] feat(moq-net): expose broadcast demand on the read handle `Producer::demand` is public, `Consumer::demand` is not, so the only way to ask whether anything is reading a broadcast is to own the producer for it. That is the wrong half for a relay that pulls a broadcast from upstream: the session ingesting the remote announce owns the producer, while the code that has to decide whether the upstream connection is still worth holding open has a read handle and nothing else. The handle is already what the publishers use to price an announcement (`select_route` takes a `&Demand` derived from the consumer it is forwarding), so this exposes an existing internal signal rather than adding one. It is a weak handle: it neither keeps the broadcast alive nor counts as demand, so watching a broadcast cannot make it look watched. The spliced demand test now parks on the read handle for the unused edge, which is the shape a relay watches: a route-fed front whose logical tracks outlive any one session. --- rs/moq-net/src/model/broadcast.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index bda38d1da66..a4aa5fdc037 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -1058,7 +1058,13 @@ impl Consumer { } /// A watch-only handle to the broadcast's demand. See [`Demand`]. - pub(crate) fn demand(&self) -> Demand { + /// + /// The consumer-side sibling of [`Producer::demand`], for a holder that has + /// only a read handle: a relay pulling a broadcast from upstream owns no + /// producer for it (the ingesting session does), yet the question it has to + /// answer is whether anything downstream is still reading. Holding this + /// handle, or the [`Consumer`] it came from, is not itself demand. + pub fn demand(&self) -> Demand { Demand { alive: self.alive.weak(), state: self.state.clone(), @@ -1164,7 +1170,8 @@ impl super::WeakEntry for WeakConsumer { /// A cloneable, watch-only handle to a broadcast's subscriber demand. /// -/// Obtained from [`Producer::demand`]; the broadcast-level sibling of +/// Obtained from [`Producer::demand`] or [`Consumer::demand`]; the +/// broadcast-level sibling of /// [`track::Demand`](crate::track::Demand). Demand means live interest in the /// broadcast's content: a subscribed spliced track on a route-fed broadcast, or /// a pending track request / a consumed track on an ordinary one. A publisher @@ -1302,16 +1309,23 @@ mod test { let producer = Producer::new_spliced(Info::new()); let consumer = producer.consume(); let demand = producer.demand(); + // The read handle answers the same question, which is all a relay + // holding a pulled broadcast has. + let watched = consumer.demand(); assert!(!demand.is_used()); + assert!(!watched.is_used()); let track = consumer.track("video").unwrap(); assert!(demand.is_used()); + assert!(watched.is_used()); // Dropping the only consumer wakes a parked `unused`, even though the - // logical track itself stays cached in the broadcast. - let (unused, ()) = tokio::join!(expect(demand.unused()), async { drop(track) }); + // logical track itself stays cached in the broadcast. Parked on the read + // handle: that edge is what tells a relay its pull has no readers left. + let (unused, ()) = tokio::join!(expect(watched.unused()), async { drop(track) }); unused.unwrap(); assert!(!demand.is_used()); + assert!(!watched.is_used()); // A repeat consumer for the cached track counts again. let _track = consumer.track("video").unwrap(); From ec836fe334893fa0f3d6117655396c6a1dd18c25 Mon Sep 17 00:00:00 2001 From: Frando Date: Wed, 2 Sep 2026 18:11:12 +0200 Subject: [PATCH 2/2] test(moq-net): tell a dropped producer apart from lost demand A relay watching a pulled broadcast has two endings to act on and one handle to learn both from. Readers going away means stop pulling; an upstream that vanished means the pull is over and retrying is pointless. They arrive as `unused` resolving and as `Error::Dropped`, which the docs now say on `Consumer::demand` rather than only on `Demand` itself. Co-Authored-By: Claude Opus 5 (1M context) --- rs/moq-net/src/model/broadcast.rs | 39 ++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index a4aa5fdc037..f98992a9ebf 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -1064,6 +1064,11 @@ impl Consumer { /// producer for it (the ingesting session does), yet the question it has to /// answer is whether anything downstream is still reading. Holding this /// handle, or the [`Consumer`] it came from, is not itself demand. + /// + /// Two endings a caller has to tell apart. Demand going away is + /// [`Demand::unused`] resolving, and means nobody downstream is reading. + /// The broadcast going away is [`Error::Dropped`], and here that is the + /// upstream producer, not the readers. pub fn demand(&self) -> Demand { Demand { alive: self.alive.weak(), @@ -1170,9 +1175,8 @@ impl super::WeakEntry for WeakConsumer { /// A cloneable, watch-only handle to a broadcast's subscriber demand. /// -/// Obtained from [`Producer::demand`] or [`Consumer::demand`]; the -/// broadcast-level sibling of -/// [`track::Demand`](crate::track::Demand). Demand means live interest in the +/// Obtained from [`Producer::demand`] or [`Consumer::demand`]; the broadcast-level +/// sibling of [`track::Demand`](crate::track::Demand). Demand means live interest in the /// broadcast's content: a subscribed spliced track on a route-fed broadcast, or /// a pending track request / a consumed track on an ordinary one. A publisher /// uses it to run expensive work only while someone is watching, and routing @@ -1332,6 +1336,35 @@ mod test { assert!(demand.is_used()); } + /// A read handle's demand reports the producer going away as `Dropped`, + /// distinct from its readers going away. + /// + /// The distinction a relay has to act on: no readers means stop pulling, + /// while an upstream that vanished means the pull is over. Both arrive on + /// the same handle, so the two have to be told apart by their result rather + /// than by which one resolved. + #[tokio::test] + async fn a_read_handle_reports_a_dropped_producer_apart_from_lost_demand() { + let producer = Producer::new_spliced(Info::new()); + let consumer = producer.consume(); + let watched = consumer.demand(); + + let track = consumer.track("video").unwrap(); + assert!(watched.is_used()); + + // Readers go, the broadcast stays: demand ends, and the handle keeps + // answering. + let (unused, ()) = tokio::join!(expect(watched.unused()), async { drop(track) }); + unused.unwrap(); + + // The producer goes: the same handle now refuses rather than reporting + // no demand, which is what stops a relay retrying a pull that has no + // source left. + drop(producer); + assert!(matches!(watched.used().await, Err(Error::Dropped))); + assert!(matches!(watched.unused().await, Err(Error::Dropped))); + } + /// Subscribe and assert the result hasn't resolved yet (it stays pending until /// a publisher accepts). Returns the pending subscription to resolve after accepting. macro_rules! subscribe_pending {