diff --git a/doc/bin/relay/config.md b/doc/bin/relay/config.md index 1f63a38afdd..32818ad9826 100644 --- a/doc/bin/relay/config.md +++ b/doc/bin/relay/config.md @@ -138,12 +138,12 @@ not throttled by `[quic]` limits sized for one viewer, for example their transport's own limits. These are the defaults. Each subscription and FETCH holds a stream on moq-lite -and on moq-transport draft 17 and later, so `[quic]` or `[limits.peer.quic]` -`max_streams` bounds them there, and a peer past it waits rather than fails. +and on moq-transport draft 17 and later, as does each PUBLISH a peer's track +listing gets, so `[quic]` or `[limits.peer.quic]` `max_streams` bounds them +there, and a peer past it waits rather than fails. On drafts 14 to 16, `announces` and `requests` together size the request window instead, which every request counts against, so very small values can -prevent other requests from progressing. On later drafts `requests` bounds the -PUBLISHes a peer's track listings hold; the rest are skipped. Size peer limits +prevent other requests from progressing. Size peer limits for the combined traffic they carry, and use session peak counters to check announcement and subscription headroom. Exceeding `announces` closes the session with `TOO_MANY_REQUESTS`; reconnecting with the same load will exceed it again. diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 917e0db5cc0..24c56defda0 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -26,7 +26,7 @@ maps everything else to "not supported" or a harmless equivalent. The subset drops. What a peer actually observes against this implementation: - **Pull.** Subscribers ask. Single-track `PUBLISH` offers are declined, unless they answer our `SUBSCRIBE_TRACKS` (below); announce a namespace and serve the resulting subscriptions. Announcements go out unsolicited, and we also ask for every prefix we may discover. The solicit `SETUP` option makes us wait to be asked. Without it, a draft 16+ peer also gets each match as a `NAMESPACE` on its `SUBSCRIBE_NAMESPACE` stream, so it hears the namespace twice. We send `PUBLISH` only to answer a `SUBSCRIBE_TRACKS` (below): on drafts 16 and 17 a `SUBSCRIBE_NAMESPACE` asking only for `PUBLISH` is refused, one asking for both gets only `NAMESPACE`, and any other Subscribe Options value closes the session. -- **`SUBSCRIBE_TRACKS` lists, it never pushes.** On draft 18 and later, it lists every track of every broadcast under its prefix, later ones included. With `FORWARD=0`, each track gets a paused `PUBLISH` carrying its Track Properties, and an error `PUBLISH_DONE` once the track is no longer listed, never `TRACK_ENDED`, since the track may go on elsewhere. A `REQUEST_UPDATE` with `FORWARD=1` resumes one, served from the cache like a `SUBSCRIBE` with the update's filter and answered with `LARGEST_OBJECT`; `FORWARD=0` pauses it again. Each paused `PUBLISH` holds a subscription slot, and past the cap a track gets `PUBLISH_SKIPPED` (`PUBLISH_BLOCKED` on draft 18) instead. A listing that omits `FORWARD` or sets it to 1 asks for every track's objects: from draft 19 each track gets `PUBLISH_SKIPPED`, and the subscriber `SUBSCRIBE`s to what it wants; draft 18 has no such skip, so the request is refused with `NOT_SUPPORTED`. A prefix overlapping another listing gets `PREFIX_OVERLAP`, a `TRACK_PROPERTY_FILTER` gets `NOT_SUPPORTED`, and a `REQUEST_UPDATE` on the listing itself ends it. On draft 18, a `SUBSCRIBE` to a track we `PUBLISH`ed gets `DUPLICATE_SUBSCRIPTION`. Cancelling the listing stops new `PUBLISH`es, while those sent live on. A broadcast is listed only while our publish grant covers it, from scratch once it does. Rust sends it upstream for a listing (below); JavaScript does not. +- **`SUBSCRIBE_TRACKS` lists, it never pushes.** On draft 18 and later, it lists every track of every broadcast under its prefix, later ones included. With `FORWARD=0`, each track gets a paused `PUBLISH` carrying its Track Properties, and an error `PUBLISH_DONE` once the track is no longer listed, never `TRACK_ENDED`, since the track may go on elsewhere. A `REQUEST_UPDATE` with `FORWARD=1` resumes one, served from the cache like a `SUBSCRIBE` with the update's filter and answered with `LARGEST_OBJECT`; `FORWARD=0` pauses it again. Rust opens each paused `PUBLISH` only as the peer's stream credit admits it, so a peer that grants none holds none; JavaScript caps them, and past the cap a track gets `PUBLISH_SKIPPED` (`PUBLISH_BLOCKED` on draft 18) instead. A listing that omits `FORWARD` or sets it to 1 asks for every track's objects: from draft 19 each track gets `PUBLISH_SKIPPED`, and the subscriber `SUBSCRIBE`s to what it wants; draft 18 has no such skip, so the request is refused with `NOT_SUPPORTED`. A prefix overlapping another listing gets `PREFIX_OVERLAP`, a `TRACK_PROPERTY_FILTER` gets `NOT_SUPPORTED`, and a `REQUEST_UPDATE` on the listing itself ends it. On draft 18, a `SUBSCRIBE` to a track we `PUBLISH`ed gets `DUPLICATE_SUBSCRIPTION`. Cancelling the listing stops new `PUBLISH`es, while those sent live on. A broadcast is listed only while our publish grant covers it, from scratch once it does. Rust sends it upstream for a listing (below); JavaScript does not. - **A Rust TRACKS listing sends `SUBSCRIBE_TRACKS` upstream, only on demand.** Listing a broadcast from a moq-transport peer on draft 18 and later sends `SUBSCRIBE_TRACKS` with `FORWARD=0` while someone lists it, never on connect, announce, or subscribe, so a peer that refuses, resets, or ignores it affects only that listing. Before draft 18 the listing is refused as unsupported. Each listed track's paused `PUBLISH` is its upstream copy, which a reader resumes rather than subscribing again. Only a `PUBLISH_DONE` with `TRACK_ENDED` removes a listed track, and it lists as finished; any other end, including the error our own answerer sends for a track that left unfinished, fails the listing, and readers list again. - **History is one group.** In Rust, Object ID gaps must be marked by Prior Object ID Gap properties for a group to be fetchable. A `FETCH` returns one group from the cache, or on drafts 14 through 19 the saved prefix of the group a new subscription just joined. A range of groups is refused, as is a draft 20+ `LOCATION_FILTER` that ends at Largest Object rather than an absolute End Location. On drafts 14 through 19, `FETCH_OK` End Object 0 covers the entire End Group, matching the request; Rust also accepts the legacy answer naming the next group with Object 0. On draft 20 and later, `FETCH_OK`'s End Location is the requested end, capped at Largest Object: a range holding no objects gets an empty fetch stream, and only a start past Largest Object is refused, with `INVALID_RANGE`. A relay's copy with no upstream subscription can't know Largest Object short of the track's end, so it echoes the requested end instead. JavaScript publishing refuses every `FETCH`. Datagrams are never fetchable. A Rust reader that only fetches learns the track from `TRACK_STATUS` instead of subscribing, so a finished track stays fetchable; draft 17 still subscribes, since its `TRACK_STATUS` answer cannot say whether the track is timed, and so does a reader whose publisher refuses `TRACK_STATUS` as `NOT_SUPPORTED`. - **Compression.** Rust peers on draft 17 and later exchange compressed tracks when both offer the [Flate extension](/draft/moq-flate). Older peers receive one shared inflated copy. A publisher that seals the compression declaration makes that fallback unavailable: an incompatible peer is refused, preserving the sealed metadata. JavaScript does not offer the IETF extension. diff --git a/quest/m1/README.md b/quest/m1/README.md index b94fb4ecc84..cfc88702342 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -86,7 +86,7 @@ audit's receive-memory, priority, and lease bugs rank ahead of that reshape. - [moq-congestion](/quest/m1/congestion/README.md) - one QUIC congestion-control crate with a transport-independent trait, run by `moq-quic` and published for noq - [Release audit findings](/quest/m1/release-audit/README.md) - resolve newly identified resource, parser, playback, and interoperability risks before the cut -- [SUBSCRIBE_TRACKS follows credit](/quest/m1/listing-credit.md) - a listing holds state in proportion to what the peer reads, not to the catalog +- [JS SUBSCRIBE_TRACKS follows credit](/quest/m1/js-listing-credit.md) - a `@moq/net` listing opens PUBLISHes only as stream credit admits, like moq-net - [Cancelled moq-transport requests stop](/quest/m1/ietf-request-cancel.md) - a cancelled request releases its state even when the peer withholds credit - [SUBSCRIBE setup sees a cancel](/quest/m1/ietf-setup-cancel.md) - a subscriber that cancels mid-setup releases upstream demand and its slot at once - [JS qmux stream credit](/quest/m1/js-qmux-credit.md) - credit returns when both halves close, so `@moq/net` drops its caps diff --git a/quest/m1/js-listing-credit.md b/quest/m1/js-listing-credit.md new file mode 100644 index 00000000000..d4334d6784b --- /dev/null +++ b/quest/m1/js-listing-credit.md @@ -0,0 +1,26 @@ +# [S] JS SUBSCRIBE_TRACKS listings follow stream credit + +## Goal + +A `@moq/net` draft-18+ SUBSCRIBE_TRACKS listing holds state in proportion to +what the peer reads, not to the catalog, as moq-net's does. Each listing +PUBLISH opens only as the peer's stream credit admits it, a FORWARD=1 listing +reads its next track only as its skips go out, and the listing no longer uses +the `Limits` request cap. + +## Plan + +`#listBroadcast` in `js/net/src/ietf/publisher.ts` starts a PUBLISH for every +listed track up to the `Limits` cap and skips the rest, and queues every +FORWARD=1 skip at once. Mirror Rust's `run_listed_broadcast` in +`rs/moq-net/src/ietf/publisher/tracks.rs`: stage one track per broadcast, open +its stream only as credit admits, keep reading the listing for removals +meanwhile, since an unlisted track's PUBLISH_DONE is what returns credit, and +skip a track before its stream opens. Stream credit bounds open streams only +once `@moq/qmux` returns it when both halves close +([moq-dev/moq#6644](https://github.com/moq-dev/moq/pull/6644)). + +Test with a peer that withholds credit and reads nothing, swept over catalog +size. + +Public API: none expected. Wire: none. diff --git a/quest/m1/listing-credit.md b/quest/m1/listing-credit.md deleted file mode 100644 index f2803ab4cde..00000000000 --- a/quest/m1/listing-credit.md +++ /dev/null @@ -1,24 +0,0 @@ -# [M] SUBSCRIBE_TRACKS listings follow stream credit - -## Goal - -A draft-17+ SUBSCRIBE_TRACKS listing holds state in proportion to what the -peer reads, not to the catalog. Each listing PUBLISH opens only as the -peer's stream credit admits it, and a peer that stops reading stops the -enumeration. - -## Plan - -In `rs/moq-net/src/ietf/publisher/tracks.rs`, every FORWARD=1 or unfittable -track goes into `request.skips` as the listing is enumerated, and -`poll_write_skips` paces only the writes. A peer that stops reading makes the -session hold state proportional to the whole catalog, multiplied across -peers. After [moq-dev/moq#6467](https://github.com/moq-dev/moq/pull/6467), the -listing PUBLISHes are bounded only by `Limits::requests`, a runtime cap. - -Decided 2026-10-23 (maintainer): replace the cap with flow control. Couple -enumeration to write backpressure, open each PUBLISH as stream credit admits -it, and drop the listing's use of `Limits::requests`. Test with a peer that -withholds credit and reads nothing. - -Public API: none expected. Wire: none. diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 5444a78a2ab..56cf6572f10 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -695,7 +695,7 @@ pub(super) struct Publisher { auth: crate::auth::Handle, // Dispatched finite serves, including those not yet polled. pub(super) owed: Arc, - // Subscriptions the peer holds, PUBLISHes for its listings capped by `session::Limits::requests`. + // Subscriptions the peer holds, PUBLISHes for its listings included, counted for the peak. pub(super) subscriptions: crate::session::Slots, // Ranks every subscription's group streams against each other for the transport. priority: PriorityQueue, diff --git a/rs/moq-net/src/ietf/publisher/tracks.rs b/rs/moq-net/src/ietf/publisher/tracks.rs index 3527a6d9769..8aba90df202 100644 --- a/rs/moq-net/src/ietf/publisher/tracks.rs +++ b/rs/moq-net/src/ietf/publisher/tracks.rs @@ -139,8 +139,9 @@ struct Params { } /// The listed tracks a request answers with PUBLISH_SKIPPED and has not sent yet, keyed by -/// (suffix, name) in listing order. A track removed before its skip went out cancels it, -/// so a peer that stops reading holds at most one per listed track. +/// (suffix, name) in listing order. A track removed before its skip went out cancels it. +/// A FORWARD=1 listing adds one only once this is empty, so a peer that stops reading +/// leaves the rest unread in each broadcast's listing. type Skips = kio::Shared), ()>>; /// What a request's listing shares with the tasks listing each broadcast. @@ -159,8 +160,68 @@ struct Request { live: kio::Consumer<()>, } +/// One listed broadcast's tracks, by how far each got toward the peer. +#[derive(Default)] +struct Entries { + /// The tracks waiting their turn for a PUBLISH, in listing order. + pending: LinkedHashMap, track::Info>, + /// The next track to PUBLISH, waiting on the peer's stream credit. + staged: Option, + /// Each sent PUBLISH's entry: dropping it says the track is no longer listed. + listed: HashMap, kio::Producer>, + /// The tracks with a skip queued, so the broadcast's end cancels only its own. + skipped: HashSet>, +} + +impl Entries { + fn skip(&mut self, skips: &Skips, suffix: &crate::PathOwned, name: Arc) { + skips.lock().insert((suffix.clone(), name.clone()), ()); + self.skipped.insert(name); + } + + /// `name` is no longer listed: drop whatever it got to, ending its PUBLISH with + /// TRACK_ENDED if it `finished`. + fn remove(&mut self, skips: &Skips, suffix: &crate::PathOwned, name: Arc, finished: bool) { + if self.staged.as_ref().is_some_and(|staged| staged.name == name) { + self.staged = None; + } + self.pending.remove(&name); + if self.skipped.remove(&name) { + skips.lock().remove(&(suffix.clone(), name.clone())); + } + if let Some(entry) = self.listed.remove(&name) + && finished + && let Ok(mut done) = entry.write() + { + *done = true; + } + } + + /// Every track is no longer listed. + fn unlist(&mut self, skips: &Skips, suffix: &crate::PathOwned) { + self.pending.clear(); + self.staged = None; + self.listed.clear(); + let mut skips = skips.lock(); + for name in self.skipped.drain() { + skips.remove(&(suffix.clone(), name)); + } + } +} + +/// The next track to PUBLISH, ready but for its stream. +struct Staged { + name: Arc, + track: track::Consumer, + properties: ietf::Properties, + /// On draft-18, claims the track's one subscription from now on. + subscribed: Option, +} + /// One listed track, handed to its PUBLISH task. struct Listed { + /// The PUBLISH's stream, opened once the peer's credit admitted it. + stream: Stream, track: track::Consumer, /// The track's full namespace, as the PUBLISH names it. namespace: crate::PathOwned, @@ -170,7 +231,7 @@ struct Listed { listed: kio::Consumer, /// Closes once the request is cancelled, which a PUBLISH not yet sent heeds. live: kio::Consumer<()>, - /// The subscription slot the PUBLISH holds for as long as it lives. + /// Counts the PUBLISH among the session's subscriptions for as long as it lives. slot: crate::session::Slot, /// On draft-18, claims the track's one subscription for as long as the PUBLISH lives. subscribed: Option, @@ -504,6 +565,13 @@ impl Publisher { /// List one broadcast under a SUBSCRIBE_TRACKS: PUBLISH or skip each of its tracks until /// its route goes or the broadcast ends, then let its PUBLISHes run to their end. + /// + /// A PUBLISH starts only once the peer's stream credit opens its stream, one staged at a + /// time, so the PUBLISHes a peer holds are bounded by the credit it grants. The listing is + /// still read meanwhile, since an unlisted track's PUBLISH_DONE is what returns that + /// credit; the tracks read but not yet PUBLISHed wait in `pending`, moved off the + /// listing's own queue rather than copied. A FORWARD=1 listing has nothing to unlist, so + /// it reads its next track only once the request stream has taken the last skip. async fn run_listed_broadcast( self, request: Request, @@ -534,12 +602,9 @@ impl Publisher { } None => return, }; - // Each PUBLISH watches its track's entry here: dropping it says the track is no - // longer listed. - let mut listed: HashMap, kio::Producer> = HashMap::new(); - // The tracks this broadcast queued a skip for, so its end touches only its own. - let mut skipped: HashSet> = HashSet::new(); + let mut entries = Entries::default(); let mut publishes = TaskSet::owned(); + let mut session = self.session.clone(); // The listing ended, or the request did: nothing more to add. let mut adding = true; // The broadcast's listing, held only while our publish permit covers it: a track @@ -559,6 +624,8 @@ impl Publisher { enum Event { Track(crate::broadcast::TracksEvent), + /// The staged track's PUBLISH stream opened. + Opened(Result, Error>), /// Our publish permit may have changed. Regrant(crate::auth::Permit), /// The broadcast ended or its route went: every track it listed is gone. @@ -568,6 +635,43 @@ impl Publisher { } loop { + // Stage the next pending track that will be PUBLISHed, skipping those that will + // not, so a stream is opened only for a PUBLISH that goes out. + while adding + && entries.staged.is_none() + && let Some((name, info)) = entries.pending.pop_front() + { + // A track this draft cannot describe is skipped rather than PUBLISHed with + // its properties rewritten. + let Ok(properties) = track_properties(&info, request.properties_wanted, self.version, flate) else { + entries.skip(&request.skips, &suffix, name); + continue; + }; + let Ok(track) = broadcast.track(&name) else { + continue; + }; + // Draft-18 allows one subscription per track (section 5.1), so a track the + // peer is subscribed to, by its SUBSCRIBE or a PUBLISH still alive, is + // skipped. Held from now, so a SUBSCRIBE cannot slip in before the PUBLISH + // goes out. + let subscribed = match self.version { + Version::Draft18 => match Subscribed::publish(&self.listings, namespace.clone(), name.clone()) { + Some(subscribed) => Some(subscribed), + None => { + entries.skip(&request.skips, &suffix, name); + continue; + } + }, + _ => None, + }; + entries.staged = Some(Staged { + name, + track, + properties, + subscribed, + }); + } + let event = kio::wait(|waiter| { let drained = publishes.poll(waiter).is_ready(); if !adding && drained { @@ -585,9 +689,25 @@ impl Publisher { { return Poll::Ready(Some(Event::Regrant(permit))); } + if entries.staged.is_some() + && let Poll::Ready(opened) = Stream::poll_open(&mut session, self.version, &mut waiter.context()) + { + return Poll::Ready(Some(Event::Opened(opened))); + } let Some(tracks) = tracks.as_mut() else { return Poll::Pending; }; + if request.forward + && request + .skips + .poll(waiter, |skips| match skips.is_empty() { + true => Poll::Ready(()), + false => Poll::Pending, + }) + .is_pending() + { + return Poll::Pending; + } match tracks.poll_next(waiter) { Poll::Ready(Ok(Some(event))) => Poll::Ready(Some(Event::Track(event))), Poll::Ready(Ok(None)) => Poll::Ready(Some(Event::Ended)), @@ -603,16 +723,9 @@ impl Publisher { let Some(event) = event else { return; }; - let unlist = |listed: &mut HashMap, kio::Producer>, skipped: &mut HashSet>| { - listed.clear(); - let mut skips = request.skips.lock(); - for name in skipped.drain() { - skips.remove(&(suffix.clone(), name)); - } - }; match event { Event::Ended => { - unlist(&mut listed, &mut skipped); + entries.unlist(&request.skips, &suffix); // What it already PUBLISHed ends now; wait for that to go out. kio::wait(|waiter| publishes.poll(waiter)).await; return; @@ -622,13 +735,13 @@ impl Publisher { // cancels the skips not yet sent. (false, true) => { tracks = None; - unlist(&mut listed, &mut skipped); + entries.unlist(&request.skips, &suffix); } (true, false) if adding => match broadcast.tracks() { Ok(listing) => tracks = Some(listing), Err(err) => { tracing::debug!(broadcast = %absolute, %err, "listed broadcast ended"); - unlist(&mut listed, &mut skipped); + entries.unlist(&request.skips, &suffix); kio::wait(|waiter| publishes.poll(waiter)).await; return; } @@ -637,79 +750,41 @@ impl Publisher { }, // The PUBLISHes already sent still end when their track is unlisted, so keep // reading the listing for them, adding nothing. - Event::Cancelled => adding = false, - Event::Track(crate::broadcast::TracksEvent::Synced) => {} - Event::Track( - crate::broadcast::TracksEvent::Removed { name } | crate::broadcast::TracksEvent::Finished { name }, - ) if skipped.remove(&name) => { - request.skips.lock().remove(&(suffix.clone(), name)); - } - Event::Track(crate::broadcast::TracksEvent::Removed { name }) => { - listed.remove(&name); + Event::Cancelled => { + adding = false; + entries.pending.clear(); + entries.staged = None; } - // Its PUBLISH ends with TRACK_ENDED rather than in error. - Event::Track(crate::broadcast::TracksEvent::Finished { name }) => { - if let Some(entry) = listed.remove(&name) - && let Ok(mut finished) = entry.write() - { - *finished = true; - } + Event::Opened(Err(err)) => { + tracing::debug!(broadcast = %absolute, %err, "publish stream did not open"); + entries.unlist(&request.skips, &suffix); + kio::wait(|waiter| publishes.poll(waiter)).await; + return; } - Event::Track(crate::broadcast::TracksEvent::Added { .. }) if !adding => {} - Event::Track(crate::broadcast::TracksEvent::Added { name, info }) => { - let mut skip = |name: Arc| { - request.skips.lock().insert((suffix.clone(), name.clone()), ()); - skipped.insert(name); - }; - if request.forward { - skip(name); - continue; - } - // A track this draft cannot describe is skipped rather than PUBLISHed with - // its properties rewritten. - let Ok(properties) = track_properties(&info, request.properties_wanted, self.version, flate) else { - skip(name); - continue; - }; - let Ok(track) = broadcast.track(&name) else { - continue; - }; - // Each paused PUBLISH holds a subscription slot. Nothing else bounds how many - // a peer's listings hold, so past `Limits::requests` the peer is told the - // track exists rather than left without it. - let Ok(slot) = self.subscriptions.acquire() else { - skip(name); + Event::Opened(Ok(stream)) => { + let Some(Staged { + name, + track, + properties, + subscribed, + }) = entries.staged.take() + else { continue; }; - // Draft-18 allows one subscription per track (section 5.1), so a track the - // peer is subscribed to, by its SUBSCRIBE or a PUBLISH still alive, is - // skipped. Held from now, so a SUBSCRIBE cannot slip in before the PUBLISH - // goes out. - let subscribed = match self.version { - Version::Draft18 => { - match Subscribed::publish(&self.listings, namespace.clone(), name.clone()) { - Some(subscribed) => Some(subscribed), - None => { - skip(name); - continue; - } - } - } - _ => None, - }; let entry = kio::Producer::new(false); let task = Listed { + stream, track, namespace: namespace.clone(), name: name.clone(), properties, listed: entry.consume(), live: request.live.clone(), - slot, + slot: self.subscriptions.hold(), subscribed, params: request.params, }; - listed.insert(name.clone(), entry); + entries.listed.insert(name, entry); let this = self.clone(); publishes.push(async move { if let Err(err) = this.run_publish(task).await { @@ -717,6 +792,21 @@ impl Publisher { } }); } + Event::Track(crate::broadcast::TracksEvent::Synced) => {} + Event::Track(crate::broadcast::TracksEvent::Removed { name }) => { + entries.remove(&request.skips, &suffix, name, false) + } + // Its PUBLISH ends with TRACK_ENDED rather than in error. + Event::Track(crate::broadcast::TracksEvent::Finished { name }) => { + entries.remove(&request.skips, &suffix, name, true) + } + Event::Track(crate::broadcast::TracksEvent::Added { .. }) if !adding => {} + Event::Track(crate::broadcast::TracksEvent::Added { name, .. }) if request.forward => { + entries.skip(&request.skips, &suffix, name); + } + Event::Track(crate::broadcast::TracksEvent::Added { name, info }) => { + entries.pending.insert(name, info); + } } } } @@ -725,6 +815,7 @@ impl Publisher { /// subscriber cancels or refuses it, or, once resumed, the track ends. async fn run_publish(self, task: Listed) -> Result<(), Error> { let Listed { + stream, track, namespace, name, @@ -742,25 +833,11 @@ impl Publisher { let dynamic = properties.dynamic_groups; let absolute = self.origin.absolute(&namespace).to_owned(); - // Every wait on the peer's credit, for the stream, a Request ID, or the PUBLISH's - // bytes, lasts only while the track is listed and the request stands; a peer that - // grants none holds only this task and its slot. A Request ID abandoned unsent leaves - // a gap, which the drafts with SUBSCRIBE_TRACKS allow: none has MAX_REQUEST_ID. + // Every wait on the peer, for a Request ID or the PUBLISH's bytes, lasts only while + // the track is listed and the request stands. A Request ID abandoned unsent leaves a + // gap, which the drafts with SUBSCRIBE_TRACKS allow: none has MAX_REQUEST_ID. let mut session = self.session.clone(); - let opened = { - let mut opening = std::pin::pin!(Stream::open(&mut session, self.version)); - kio::wait(|waiter| { - if listed.poll_closed(waiter).is_ready() || live.poll_closed(waiter).is_ready() { - return Poll::Ready(None); - } - waiter.poll_future(opening.as_mut()).map(Some) - }) - .await - }; - let Some(opened) = opened else { - return Ok(()); - }; - let mut stream = super::super::request_stream::with_stats(opened?, self.stats(), self.version); + let mut stream = super::super::request_stream::with_stats(stream, self.stats(), self.version); let allocated = kio::wait(|waiter| { if listed.poll_closed(waiter).is_ready() || live.poll_closed(waiter).is_ready() { return Poll::Ready(None); @@ -1701,20 +1778,99 @@ mod tests { } } - /// Each paused PUBLISH holds a subscription slot; past `Limits::requests` the track is - /// skipped (PUBLISH_BLOCKED, the same message, on draft-18) rather than held. + /// Listed broadcast sizes to sweep: what a listing holds must not grow with them. + const CATALOGS: [usize; 3] = [4, 64, 512]; + + /// Each PUBLISH opens only as the peer's stream credit admits it: a peer that withholds + /// credit and reads nothing holds as many PUBLISHes as it grants, however many tracks are + /// listed, and each track is PUBLISHed once credit returns. #[moq_clock::test] - async fn past_the_request_limit_a_track_is_skipped() { + async fn a_listing_publishes_only_as_credit_admits() { + const CREDIT: usize = 2; for version in DRAFTS { - let mut h = harness(version); - h.publisher.subscriptions = crate::session::Slots::new(1); - let mut request = h.request(&listing(false)).await; - assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); - let (_stream, _, published, _) = h.publish().await; - let (id, body) = next(&mut request).await; - assert_eq!(id, ietf::PublishSkipped::ID, "{version}"); - let skip: ietf::PublishSkipped = body.message(version).unwrap(); - assert_ne!(skip.track_name, published, "{version}"); + for catalog in CATALOGS { + let config = memory::Config { + max_concurrent_bi_streams: Some(CREDIT), + ..Default::default() + }; + let mut h = harness_with(version, config, peer::Peer::default()); + let _extra: Vec<_> = (h.tracks.len()..catalog) + .map(|i| { + h.broadcast + .create_track(format!("t{i}"), crate::track::Info::timed()) + .unwrap() + }) + .collect(); + let held = |h: &Harness| usize::MAX - h.publisher.subscriptions.available(); + + let mut request = h.request(&listing(false)).await; + assert_eq!(next(&mut request).await.0, ietf::RequestOk::ID, "{version}"); + moq_clock::sleep(Duration::from_millis(10)).await; + assert_eq!(held(&h), CREDIT, "{version} catalog={catalog}"); + + // Refusing each PUBLISH returns its credit, which opens the next. + let mut published = HashSet::new(); + while published.len() < catalog { + let (stream, _, name, _) = h.publish().await; + assert!(published.insert(name), "{version}: PUBLISHed a track twice"); + drop(stream); + assert!(held(&h) <= CREDIT, "{version} catalog={catalog}: {} held", held(&h)); + } + } + } + } + + /// A FORWARD=1 listing reads its next track only once the request stream has taken the + /// last skip, so a peer that stops reading leaves one skip queued, however many tracks + /// are listed. + #[moq_clock::test] + async fn a_forwarding_listing_reads_only_as_skips_go_out() { + for version in [Version::Draft19, Version::Draft22] { + for catalog in CATALOGS { + let h = harness(version); + let _extra: Vec<_> = (h.tracks.len()..catalog) + .map(|i| { + h.broadcast + .create_track(format!("t{i}"), crate::track::Info::timed()) + .unwrap() + }) + .collect(); + let skips = Skips::default(); + let live = kio::Producer::new(()); + let route = kio::Producer::new(()); + let request = Request { + origin: h._origin.consume(), + prefix: crate::Path::new("").to_owned(), + forward: true, + params: Params { + priority: crate::ietf::priority::DEFAULT_WIRE, + order: None, + filter: Filter::Unfiltered, + max_age: None, + new_group: None, + }, + properties_wanted: true, + skips: skips.clone(), + live: live.consume(), + }; + moq_clock::sim::spawn(h.publisher.clone().run_listed_broadcast( + request, + crate::Path::new("room/cam").to_owned(), + None, + route.consume(), + )); + + let mut skipped = HashSet::new(); + while skipped.len() < catalog { + moq_clock::sleep(Duration::from_millis(10)).await; + let mut queued = skips.lock(); + assert_eq!(queued.len(), 1, "{version} catalog={catalog}"); + let ((_, name), ()) = queued.pop_front().unwrap(); + assert!(skipped.insert(name), "{version}: skipped a track twice"); + } + moq_clock::sleep(Duration::from_millis(10)).await; + assert!(skips.read().is_empty(), "{version}: skipped past the listing"); + } } } diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index 6f291fc05a1..67ca5ccc6f6 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -215,8 +215,9 @@ pub fn start(config: Config) -> Result<(Driver, crate::goaway::Handle), Error> { .with_stats(stats.clone()) }); let subscribe = subscribe.unwrap_or_else(|| origin::Producer::empty(Hop::random()).with_stats(stats.clone())); + // Counted for the peak only: stream credit bounds them. let subscriptions = - crate::session::Slots::new(limits.requests).with_stats(publish.stats(), crate::stats::Cap::Subscriptions); + crate::session::Slots::default().with_stats(publish.stats(), crate::stats::Cap::Subscriptions); let announces = crate::session::Slots::new(limits.announces).with_stats(subscribe.stats(), crate::stats::Cap::Announces); diff --git a/rs/moq-net/src/session.rs b/rs/moq-net/src/session.rs index 45e2b810b7d..99df51c060f 100644 --- a/rs/moq-net/src/session.rs +++ b/rs/moq-net/src/session.rs @@ -820,9 +820,8 @@ pub struct Limits { /// Other moq-transport requests the peer may hold at once, such as SUBSCRIBE and FETCH. /// /// On drafts 14 to 16 this and `announces` size the request window, which every request - /// counts against. Later drafts bound the PUBLISHes a peer's track listings hold with it, - /// skipping the rest, and still refuse a reused request ID, so a peer may leave at most - /// twice the sum of both runs of skipped IDs; one more closes the session with + /// counts against. Later drafts still refuse a reused request ID, so a peer may leave at + /// most twice the sum of both runs of skipped IDs; one more closes the session with /// `INTERNAL_ERROR` rather than forgetting any. moq-lite ignores it. pub requests: usize, } @@ -880,8 +879,7 @@ impl Slots { /// Take one slot until the returned guard drops, or [`Error::TooManyRequests`] at the cap. /// - /// A peer whose own request runs past its limits loses the session. A subscription we - /// would initiate, such as a listed track's PUBLISH, is skipped instead. + /// A peer whose own request runs past its limits loses the session. pub(crate) fn acquire(&self) -> Result { use std::sync::atomic::Ordering; let held = self