From c85605022a22acd689dc73cb6c713413169b580c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 5 Oct 2026 23:45:10 -0700 Subject: [PATCH 1/4] quest: claim 2991 coalesce dynamic tracks Co-Authored-By: Claude Opus 5.5 From 3c1365d7fcc48c1fa8d9f1eb6a95ae906a845df1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 5 Oct 2026 23:56:09 -0700 Subject: [PATCH 2/4] quest: re-check #2991's Rust stall against copy-based resume Co-Authored-By: Claude Opus 5.5 --- ...ic-tracks-and-preserve-sequences-across.md | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md b/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md index 4c2d00c09fd..cda8f707f57 100644 --- a/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md +++ b/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md @@ -14,27 +14,26 @@ but the Rust and JavaScript models violate different parts of that invariant. ### Rust resets sequences when a dynamic producer is replaced -A closed dynamic track is removed from the broadcast's weak cache. The next -subscription creates a fresh `track::Request` (`rs/moq-net/src/model/track.rs`), -and `Request::new` creates a fresh `TrackState`. Because its `max_sequence` -is empty, both `append_group` and `append_datagram` restart at sequence 0. - -That conflicts with the relay's logical track. The pump this was written -against is gone: #4741 resumes route changes by reading the routes' copies -(`rs/moq-net/src/model/front.rs`, `resume.rs`, and `origin.rs`). Re-check -first that a replacement restarting at sequence 0 still stalls under -copy-based resume, the way groups from a restarted producer were skipped -until its counter caught up (the playback stall fixed for JavaScript in -#2953), and rewrite this section against what the code does now. With #4741 this is the general hazard of restarting at 0 under one -name, which [broadcast epochs](/quest/m0/broadcast-epoch/README.md) fix for a -restarted publisher. This quest covers what an epoch does not: one dynamic -track replaced inside a live broadcast. - -The route-change tests (`rs/moq-net/tests/route_change.rs`) are the ones to -extend: none replaces a producer through `append_group()`, which would -create group 0 and, if the stall still holds, leave the subscriber stalled. Explicit group or datagram -writes can raise the old producer's shared sequence edge further, making the -catch-up window longer. +Rust already coalesces: `broadcast::Consumer::track` joins a queued request or +a live cached track, so one name has one pending or live producer. What it +loses is the sequence namespace. A closed track is dropped from the +broadcast's weak cache, the next lookup builds a fresh `track::Request` +(`rs/moq-net/src/model/track.rs`) with a fresh `TrackState`, and with +`max_sequence` empty both `append_group` and `append_datagram` restart at 0. + +Copy-based resume (#4741) still stalls on that. When the serving copy dies +after delivering, the front (`rs/moq-net/src/model/front.rs`) re-queries the +same source and splices a new copy. Each reader (`resume.rs`) subscribes to it +with its newest handed-out group as the start floor and skips sequences it +already delivered, so a replacement's groups below that floor never arrive: +the reader stalls until the replacement's counter passes the old edge. Verified +by a `route_change.rs` case that aborts a dynamic producer behind a relay and +appends from the replacement: every version hangs. Explicit group or datagram +writes that raised the old producer's edge lengthen the stall. + +[Broadcast epochs](/quest/m0/broadcast-epoch/README.md) cover a restarted +publisher; this quest covers one dynamic track replaced inside a live +broadcast. ### JavaScript permits concurrent same-name dynamic producers From 4b182099b9e5824d93386e9210916c8e26e4de28 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 6 Oct 2026 00:17:26 -0700 Subject: [PATCH 3/4] fix(net): one dynamic track per name, sequences continue across replacements Rust keeps each track name's sequence namespace on the broadcast, so a replacement producer appends past everything an earlier one wrote instead of restarting at 0, which copy-based resume skipped until the counter caught up. JS publishing-side subscriptions and info lookups now coalesce onto one request per name, like the consuming side. Co-Authored-By: Claude Opus 5.5 --- doc/lib/js/net.md | 1 + doc/lib/rs/moq-net.md | 2 +- js/net/src/broadcast.test.ts | 113 +++++------------- js/net/src/broadcast.ts | 86 +++++-------- js/net/src/internal.ts | 2 +- js/net/src/track.ts | 4 +- quest/m0/broadcast-epoch/origin.md | 2 - ...ic-tracks-and-preserve-sequences-across.md | 87 -------------- quest/m1/README.md | 1 - quest/m1/subscribe-ranges/js.md | 1 - rs/moq-net/src/model/broadcast.rs | 85 ++++++++++++- rs/moq-net/src/model/track.rs | 84 +++++++++++-- rs/moq-net/tests/route_change.rs | 87 ++++++++++++++ 13 files changed, 305 insertions(+), 250 deletions(-) delete mode 100644 quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md diff --git a/doc/lib/js/net.md b/doc/lib/js/net.md index aae825f85f4..5e1243ec2e9 100644 --- a/doc/lib/js/net.md +++ b/doc/lib/js/net.md @@ -53,6 +53,7 @@ for (;;) { - **Connections** race WebTransport against WebSocket. `new Connection({ url })` pools one connection per relay URL and reconnects with backoff, which the elements use. Supplying WebTransport/WebSocket options, discovery, delay, `goaway`, or a caller-owned origin selects a private loop; explicit `share: true` refuses those options. On a relay's GOAWAY the connection dials the replacement at once while the old session keeps serving its groups until it closes or the `goaway.handover` cap (default 10s, lowered to the relay's deadline) passes; the handle and its origin stay the same. An empty GOAWAY redials the same URL through a fresh DNS resolve. A redirect is followed onto the same host by default (`goaway.redirect: "follow"` lets the relay pick the host, `"ignore"` never moves), and moves the pooled entry to the new URL; a malformed or refused one, including a host change under a pinned certificate, ends the connection with `Error.RefusedRedirect` instead of reconnecting. `closed` settles when the handle is released (`null` on a clean close); the failure that stopped retrying the current URL is `error`, and a new URL recovers the same handle. A connection owns one send-rate sampler and one `Bandwidth.Allocator`; publishers reserve against it so their encoder targets sum to the estimate instead of each matching it. - **Bandwidth** (`Bandwidth.Allocator`) divides the connection's send-rate estimate by track priority, max-min fair within a tier. An idle track claims nothing. The receive side is untouched. - **Discovery** by any pattern scope (`origin.announced(scope)`, such as `room/*/chat`; default everything). A route event's `kind` says whether the route started, updated, or ended; its `prefix` is the covered prefix relative to the origin, and `captures` reports what the scope's wildcards matched when the prefix pins them. One `{ kind: "live" }` marker, with no prefix, follows the routes live at subscribe time. The consumer is an async iterable. `origin.broadcasts(scope)` is a live `Getter>` of the same covered prefixes for UIs that need the current set. A borrowed `Connection.origin` also exposes `dynamic(prefix, route)` for serving paths on demand. +- **Tracks**: a broadcast serves one track per name. Concurrent subscriptions and info lookups share one request and producer, and a producer that replaces an ended one continues the name's group and datagram sequences, which only a new broadcast restarts. - **Publisher retention** is optional `Track.Info.maxAge`: omission sets no limit, while zero keeps the live edge. No implicit five-second window is applied. - **Subscriptions** carry a priority, a `Time.Milli` max age, and optional `groups` bounds. Groups arrive out of order and are read frame by frame, with `Error.TooFarBehind` when a reader asks for a frame the group never held and `Error.GroupTooLarge` when a write exceeds the cache budget and aborts the group. - **Track ends**: `close()` ends a track at its live edge, while `finishAt(n)` declares the exclusive end ahead of it and still accepts the groups below. A subscriber reads the end with `final()` or awaits `finished()`. Group and datagram readers end once the live edge reaches the declared boundary, even while missing lower groups wait out the grace. The `closed` signal settles separately once every group below the end has arrived or was dropped; one reset before its header arrived is skipped after the subscription's max age on moq-lite (one second without one), or after one second on IETF. diff --git a/doc/lib/rs/moq-net.md b/doc/lib/rs/moq-net.md index 5fa5089f8b7..5c7c79f7b8e 100644 --- a/doc/lib/rs/moq-net.md +++ b/doc/lib/rs/moq-net.md @@ -19,7 +19,7 @@ above ([hang](/lib/rs/hang)); relays and CDNs implement only this. - **Broadcasts** are created unannounced and invisible to everyone, then announced as an exact route, or served below a prefix with `dynamic`. A consumer of the same origin sees exactly what a peer sees. Discovery accepts pattern unions; events carry the advertised prefix and captures for a complete match. - **Epochs** identify publisher instances with a canonical UUIDv7 and explicit path helpers; see [publisher epochs](/concept/moq-lite#publisher-epochs). - **Patterns** (`Pattern`, `Patterns`) are re-exported from [`moq-pattern`](https://docs.rs/moq-pattern). Literal `Path` stays a coordinate. -- **Tracks** carry groups with a priority, an optional publisher retention window (`Info::max_age`), and a timescale. Subscribers set their own priority and max age and can change them live. +- **Tracks** carry groups with a priority, an optional publisher retention window (`Info::max_age`), and a timescale. Subscribers set their own priority and max age and can change them live. A broadcast serves one track per name: concurrent subscriptions share one request and producer, and a producer that replaces an ended one continues the name's group and datagram sequences, which only a new broadcast restarts. - **Groups** are written frame by frame and delivered on independent streams. Old groups are cached for fetch-by-sequence; stale groups are skipped per the subscriber's budget. - **Track ends**: `finish()` ends a track at its live edge, while `finish_at(n)` declares the exclusive end ahead of it and still accepts the groups below. A subscriber awaits it with `finished()`. A remote track ends only once every group below its end has arrived or was dropped; one reset before its header arrived is skipped after the subscription's max age on moq-lite (one second without one), or after one second on IETF. - **Datagrams** send a single small frame unreliably on moq-lite 05+ and moq-transport. diff --git a/js/net/src/broadcast.test.ts b/js/net/src/broadcast.test.ts index 1895c3365ed..da57f8bf710 100644 --- a/js/net/src/broadcast.test.ts +++ b/js/net/src/broadcast.test.ts @@ -88,98 +88,45 @@ test("dynamic track sequences continue across producer replacements", async () = nextGeneration.close(); }); -test("concurrent dynamic producers share a sequence namespace", async () => { +test("publishing-side subscriptions coalesce onto one request", async () => { const broadcast = new BroadcastProducer(); - const firstSubscriber = broadcast.track("media").subscribe(); - const secondSubscriber = broadcast.track("media").subscribe(); - const firstRequest = await wireOf(broadcast).requested(); - const secondRequest = await wireOf(broadcast).requested(); - if (!firstRequest || !secondRequest) throw new Error("expected requests"); - const firstProducer = firstRequest.accept(); - const secondProducer = secondRequest.accept(); - - expect(firstProducer.appendGroup().sequence).toBe(0); - expect(secondProducer.appendGroup().sequence).toBe(1); - expect(firstProducer.appendGroup().sequence).toBe(2); - - firstSubscriber.close(); - secondSubscriber.close(); - firstProducer.close(); - secondProducer.close(); - broadcast.close(); -}); - -test("a sibling producer's groups do not settle an aborted end", async () => { - const broadcast = new BroadcastProducer(); - const firstSubscriber = broadcast.track("media").subscribe(); - const secondSubscriber = broadcast.track("media").subscribe(); - const firstRequest = await wireOf(broadcast).requested(); - const secondRequest = await wireOf(broadcast).requested(); - if (!firstRequest || !secondRequest) throw new Error("expected requests"); - const firstProducer = firstRequest.accept(); - const secondProducer = secondRequest.accept(); - - const reader = firstProducer.subscribe(); - firstProducer.finishAt(3); - for (let i = 0; i < 3; i++) secondProducer.appendGroup().close(); - const boom = new Error("boom"); - firstProducer.close(boom); - - // The first producer never received the groups its end promised, so it was cut off. - expect(firstProducer.closed.peek()).toBe(boom); - await expect(reader.recvGroup()).rejects.toBe(boom); - - firstSubscriber.close(); - secondSubscriber.close(); - secondProducer.close(); - broadcast.close(); -}); - -test("a clean close ends at the track's own last group, not a sibling's", async () => { - const broadcast = new BroadcastProducer(); - const firstSubscriber = broadcast.track("media").subscribe(); - const secondSubscriber = broadcast.track("media").subscribe(); - const firstRequest = await wireOf(broadcast).requested(); - const secondRequest = await wireOf(broadcast).requested(); - if (!firstRequest || !secondRequest) throw new Error("expected requests"); - const firstProducer = firstRequest.accept(); - const secondProducer = secondRequest.accept(); + const a = broadcast.track("media").subscribe().ordered(); + const b = broadcast.track("media").subscribe().ordered(); + const info = broadcast.track("media").info(); - const reader = firstProducer.subscribe(); - firstProducer.appendGroup().close(); - for (let i = 0; i < 3; i++) secondProducer.appendGroup().close(); - firstProducer.close(); + // One request serves every subscriber and the info lookup. + const request = await pendingRequest(broadcast); + expect(request?.name).toBe("media"); + expect(await pendingRequest(broadcast)).toBeUndefined(); - // The sibling took 1..3, which the first producer will never send. - expect(reader.final()).toBe(1); + if (!request) throw new Error("expected request"); + const producer = request.accept(); + producer.writeString("hello"); + expect(await a.readString()).toBe("hello"); + expect(await b.readString()).toBe("hello"); + await info; + // The lookup leaves the shared producer to its subscribers. + expect(producer.closed.peek()).toBeUndefined(); - firstSubscriber.close(); - secondSubscriber.close(); - secondProducer.close(); + a.close(); + b.close(); + producer.close(); broadcast.close(); }); -test("finishAt accepts an end below a sibling's groups", async () => { +test("an info lookup releases a request nobody subscribed to", async () => { const broadcast = new BroadcastProducer(); - const firstSubscriber = broadcast.track("media").subscribe(); - const secondSubscriber = broadcast.track("media").subscribe(); - const firstRequest = await wireOf(broadcast).requested(); - const secondRequest = await wireOf(broadcast).requested(); - if (!firstRequest || !secondRequest) throw new Error("expected requests"); - const firstProducer = firstRequest.accept(); - const secondProducer = secondRequest.accept(); - - const reader = firstProducer.subscribe(); - firstProducer.appendGroup().close(); - for (let i = 0; i < 3; i++) secondProducer.appendGroup().close(); - expect(() => firstProducer.finishAt(0)).toThrow("track end 0 is below the next sequence 1"); - firstProducer.finishAt(2); - expect(reader.final()).toBe(2); + const info = broadcast.track("media").info(); + const request = await pendingRequest(broadcast); + if (!request) throw new Error("expected request"); + const producer = request.accept(); + await info; + expect(producer.closed.peek()).toBeDefined(); - firstSubscriber.close(); - secondSubscriber.close(); - firstProducer.close(); - secondProducer.close(); + // The next subscription asks again. + const subscriber = broadcast.track("media").subscribe(); + expect((await pendingRequest(broadcast))?.name).toBe("media"); + subscriber.close(); broadcast.close(); }); diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index 99efcd09c33..78c8811518d 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -11,7 +11,7 @@ import { hooks, type TrackSequence } from "./internal.ts"; import * as Path from "./path.ts"; import * as track from "./track.ts"; import { untilAborted } from "./util/abort.ts"; -import { registerWire, trackOf, type Broadcast as Wire } from "./wire.ts"; +import { registerWire, trackOf } from "./wire.ts"; /** The origin callback a created broadcast uses to advertise its exact path. @internal */ export interface Announcer { @@ -94,69 +94,47 @@ function closeState(state: BroadcastState) { }); } -// `register` is set on the subscribing (consumer) side: the fresh producer is cached in -// `state.tracks` so repeat subscriptions to the same track fan out from one upstream subscription -// instead of opening a new one, mirroring the Rust `broadcast::Consumer::track` weak-dedup. The -// consumer wire watches the producer's demand ({@link track.Demand.used}) and tears the upstream -// down once its last subscriber leaves, closing the producer, which evicts the cache entry below. -// The publishing side leaves `register` false: `state.tracks` there holds only the tracks the app -// inserted, and a dynamic serve stays one request per peer subscription. -function subscribe( - state: BroadcastState, - name: string, - options: track.Subscription = {}, - register = false, -): track.Subscriber { +// The live or pending producer for `name`, or a new one with a request queued for it. +// +// A broadcast has one logical track per name, mirroring the Rust `broadcast::Consumer::track` +// weak-dedup: the producer is cached in `state.tracks` so every subscription and info lookup fans +// out from one request instead of opening another. Whoever serves it closes it once unused (the +// consuming wire tears the upstream down when its last subscriber leaves), which evicts the entry +// so a later subscribe requests the track again, continuing its sequences. +function logical(state: BroadcastState, name: string): { producer: track.Producer; requested: boolean } { if (state.closed.peek() !== undefined) { throw new Error("broadcast is closed"); } const existing = state.tracks.get(name); - if (existing) { - if (existing.closed.peek() === undefined) return existing.subscribe(options); - state.tracks.delete(name); - } + if (existing && existing.closed.peek() === undefined) return { producer: existing, requested: false }; const producer = new track.Producer(name); watchDemand(state, producer); - const subscriber = producer.subscribe(options); - - if (register) { - state.tracks.set(name, producer); - // Drop the cache entry once the upstream closes (the wire tears it down when its last - // subscriber leaves), so a later subscribe re-opens it. - void producer.closed.then(() => { - if (state.tracks.get(name) === producer) state.tracks.delete(name); - }); - } + state.tracks.set(name, producer); + void producer.closed.then(() => { + if (state.tracks.get(name) === producer) state.tracks.delete(name); + }); state.requested.mutate((requested) => { requested.push(hooks.makeRequest({ name, producer, sequences: state.sequences, pending: state.pending })); }); - return subscriber; + return { producer, requested: true }; } -async function resolveTrackInfo(state: BroadcastState, name: string): Promise { - const existing = state.tracks.get(name); - if (existing && existing.closed.peek() === undefined) { - return existing.info(); - } - - if (state.closed.peek() !== undefined) { - return Promise.reject(new Error("broadcast is closed")); - } - - const producer = new track.Producer(name); - watchDemand(state, producer); - state.requested.mutate((requested) => { - requested.push(hooks.makeRequest({ name, producer, sequences: state.sequences, pending: state.pending })); - }); +function subscribe(state: BroadcastState, name: string, options: track.Subscription = {}): track.Subscriber { + return logical(state, name).producer.subscribe(options); +} +async function resolveTrackInfo(state: BroadcastState, name: string): Promise { + const { producer, requested } = logical(state, name); try { return await producer.info(); } finally { - producer.close(); + // A lookup alone is no demand: let go of a request it opened that nobody subscribed to + // meanwhile. + if (requested && !producer.demand().used.peek()) producer.close(); } } @@ -235,7 +213,12 @@ export class Producer { #path = Path.empty(); constructor() { - registerWire(this, this.#wire(false)); + registerWire(this, { + subscribe: (name, options) => subscribe(this.#state, name, options), + resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), + fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), + requested: () => this.#requested(), + }); } static { @@ -315,15 +298,6 @@ export class Producer { return trackOf(name, this); } - #wire(register: boolean): Wire { - return { - subscribe: (name, options) => subscribe(this.#state, name, options, register), - resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), - fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), - requested: () => this.#requested(), - }; - } - /** * Advertise this broadcast's exact path, or re-price a standing advertisement in place. * @@ -387,7 +361,7 @@ export class Consumer { this.#path = shared?.path ?? Path.empty(); this.#state.consumers++; registerWire(this, { - subscribe: (name, options) => subscribe(this.#state, name, options, true), + subscribe: (name, options) => subscribe(this.#state, name, options), resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => this.#requested(), diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index db88b3b90db..6b4d069f33a 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -113,7 +113,7 @@ export interface ReadGroupFrame { complete(): void; } -/** The next group or datagram sequence shared by dynamic producers of one broadcast track. */ +/** The next group or datagram sequence, shared by every dynamic producer that serves one broadcast track in turn. */ export interface TrackSequence { next: number; } diff --git a/js/net/src/track.ts b/js/net/src/track.ts index d5fec5672bf..4ba8e8974c3 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -495,8 +495,8 @@ export class Producer { #state = new TrackState(); #sequence: TrackSequence = { next: 0 }; // One past the highest group or datagram this producer received, like the Rust - // `max_sequence`. The shared counter above can run ahead of it: sibling producers of - // the same track advance it too. + // `max_sequence`. The shared counter above can run ahead of it: earlier producers of + // the same broadcast track advanced it too. #received = 0; // Recently written source groups, retained for replay to late subscribers and diff --git a/quest/m0/broadcast-epoch/origin.md b/quest/m0/broadcast-epoch/origin.md index 9906345af6f..d75edad3429 100644 --- a/quest/m0/broadcast-epoch/origin.md +++ b/quest/m0/broadcast-epoch/origin.md @@ -49,5 +49,3 @@ Wire: none. ## Related - [Catalog track alias](/quest/m1/catalog-track-alias.md) - cross-broadcast catalog references, which must pick an epoch - -- [#2991](/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md) - a new epoch starts each track at sequence 0 diff --git a/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md b/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md deleted file mode 100644 index cda8f707f57..00000000000 --- a/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md +++ /dev/null @@ -1,87 +0,0 @@ -# [M] net: coalesce dynamic tracks and preserve sequences across replacements - -## Goal - -A broadcast has one logical dynamic track per name in both languages: at most -one pending or live producer, one on-demand request that every subscriber fans -out from, and a group and datagram sequence namespace that survives the -producer being replaced. Behavior only, so it ships on main. - -## Plan - -Dynamic tracks should have one logical identity per broadcast and track name, -but the Rust and JavaScript models violate different parts of that invariant. - -### Rust resets sequences when a dynamic producer is replaced - -Rust already coalesces: `broadcast::Consumer::track` joins a queued request or -a live cached track, so one name has one pending or live producer. What it -loses is the sequence namespace. A closed track is dropped from the -broadcast's weak cache, the next lookup builds a fresh `track::Request` -(`rs/moq-net/src/model/track.rs`) with a fresh `TrackState`, and with -`max_sequence` empty both `append_group` and `append_datagram` restart at 0. - -Copy-based resume (#4741) still stalls on that. When the serving copy dies -after delivering, the front (`rs/moq-net/src/model/front.rs`) re-queries the -same source and splices a new copy. Each reader (`resume.rs`) subscribes to it -with its newest handed-out group as the start floor and skips sequences it -already delivered, so a replacement's groups below that floor never arrive: -the reader stalls until the replacement's counter passes the old edge. Verified -by a `route_change.rs` case that aborts a dynamic producer behind a relay and -appends from the replacement: every version hangs. Explicit group or datagram -writes that raised the old producer's edge lengthen the stall. - -[Broadcast epochs](/quest/m0/broadcast-epoch/README.md) cover a restarted -publisher; this quest covers one dynamic track replaced inside a live -broadcast. - -### JavaScript permits concurrent same-name dynamic producers - -`BroadcastProducer.subscribe()` calls the internal `subscribe` with -`register = false` (`js/net/src/broadcast.ts`). Multiple publishing-side -subscriptions for the same name therefore enqueue independent requests and -create independent `track.Producer` instances. - -#2953 made those concurrent producers share a sequence allocator. That -prevents duplicate sequence allocation, but concurrent producers are the wrong -model. Publishing-side subscriptions should coalesce like -`BroadcastConsumer.subscribe()` and Rust's `broadcast::Consumer::track`: one -pending or live producer per broadcast and name, one on-demand request, and -multiple subscribers fanning out from it. - -### Desired behavior - -- A broadcast has at most one pending or live dynamic track producer per track - name. -- Concurrent JavaScript publishing-side subscriptions for the same name emit - one request and share its accepted producer. -- Subscription options from all subscribers remain aggregated on that request. - `track::Request` already does this in Rust: it carries `prev_subscription` - and re-combines the aggregate whenever a subscriber changes. -- After that producer closes, a later request creates a new producer but - continues the group and datagram sequence namespace for that broadcast and - name. -- Explicit group and datagram writes advance the shared allocator. -- A new broadcast generation starts each track at sequence 0. -- Rust and JavaScript expose the same lifecycle and sequencing behavior. - -The sequence allocator is shared across producer incarnations without sharing -the closed producer's cache or terminal state. - -### Regression coverage - -- JavaScript: two `BroadcastProducer.subscribe()` calls for one name produce - one request and both subscribers receive from the accepted producer. -- JavaScript: remove or replace #2953's concurrent-producer test, since - concurrent same-name producers should not be representable. -- Rust and JavaScript: close a dynamic producer after group and datagram - sequences have advanced, re-request the same name, and verify the - replacement appends at the next sequence. -- Rust relay model: extend the route-change tests above so a replacement - produced with `append_group()` is delivered immediately rather than skipped - until catch-up. -- Both implementations: verify a separate broadcast generation starts at 0. - -## Closes - -- [#2991](https://github.com/moq-dev/moq/issues/2991) - close this issue when the quest finishes diff --git a/quest/m1/README.md b/quest/m1/README.md index 5cd69305a19..73fb3ef021e 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -54,7 +54,6 @@ blocks. The quests that gated m0 lines moved under them. - [DTX timestamps](/quest/m1/opus-dtx-timestamps.md) - Opus DTX keeps the capture timeline, so voice enables it by default again - [Spent capture budget errors](/quest/m1/capture-exhausted-error.md) - a camera or microphone that runs out of retries sets `out.error` instead of failing silently - [SUBSCRIBE_DROP](/quest/m1/subscribe-drop.md) - every stream group in a lite subscription arrives or is dropped by name, and lite-07 drops its stream count for it -- [#2991](/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md) - one dynamic producer per track name in both languages, with the sequence namespace surviving a replacement - [Subscribe ranges](/quest/m1/subscribe-ranges/README.md) - a lite-07 SUBSCRIBE asks for past and live ranges in either order and replaces FETCH; relays fill misses by range, including over moq-transport - [Live media time](/quest/m1/subscribe-live-time.md) - re-scoped against `set_live`: a lite-07 SUBSCRIBE_OK carries the publisher's current media time only if a reader still needs it - [Cross-relay bursts re-run](/quest/m1/cross-relay-bursts.md) - condition: the #4349 reporter re-runs their A/B/C comparison against current cdn.moq.pro diff --git a/quest/m1/subscribe-ranges/js.md b/quest/m1/subscribe-ranges/js.md index e404e0a471c..20df7d429f8 100644 --- a/quest/m1/subscribe-ranges/js.md +++ b/quest/m1/subscribe-ranges/js.md @@ -32,4 +32,3 @@ IETF FETCH dispatch. Resized from [M] to [L] for it. ## Required - [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the wire this speaks -- [Dynamic track identity](/quest/m1/2991-net-coalesce-dynamic-tracks-and-preserve-sequences-across.md) - settles shared producer identity before adding on-demand requests diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index e2cd88c3d2f..297559e3586 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -8,6 +8,7 @@ //! [Info] is the broadcast's static metadata, fixed for its lifetime. use crate::{cache, stats, track}; use std::{ + collections::HashMap, sync::Arc, task::{Poll, ready}, }; @@ -89,6 +90,12 @@ struct BroadcastState { // coalescing onto it there). requests: Requests, track::Request>, + // Each name's sequence namespace, outliving the producers that serve it: a name means + // the same content for the broadcast's whole life, so a replacement producer continues + // where the last one stopped instead of reusing its sequences. Entries nothing holds or + // wrote are swept as the map grows, so requests for names never served stay bounded. + sequences: HashMap, track::Sequence>, + // Set once the broadcast ends: `Producer::close()` or the last producer-side // handle dropping. Every lookup after it answers `Unroutable`. closing: bool, @@ -104,6 +111,14 @@ impl BroadcastState { } } + /// The sequence namespace for `name`, shared with every earlier track of the name. + fn sequence(&mut self, name: &Arc) -> track::Sequence { + if self.sequences.len() == self.sequences.capacity() { + self.sequences.retain(|_, sequence| !sequence.is_unused()); + } + self.sequences.entry(name.clone()).or_default().clone() + } + /// Resolve every name the broadcast never filled, so subscribers waiting on a /// [`track::Info`] that can no longer arrive fail with `err` instead of parking. /// @@ -265,7 +280,10 @@ impl Producer { return Ok(track); } - let track = track::Producer::new(self.info.clone(), name, info).with_stats(self.stats.clone()); + let sequence = state.sequence(&name); + let track = track::Producer::new(self.info.clone(), name, info) + .with_stats(self.stats.clone()) + .with_sequence(sequence); state.insert_track(track.weak())?; Ok(track) } @@ -282,8 +300,12 @@ impl Producer { /// ends up never filling has to be dropped or rejected. Ending the broadcast /// resolves whatever is left. pub fn reserve_track(&self, name: impl Into>) -> Result { - let request = track::Request::new(self.info.clone(), name).with_stats(self.stats.clone()); - self.state.lock().insert_track(request.weak())?; + let name = name.into(); + let mut state = self.state.lock(); + let request = track::Request::new(self.info.clone(), name.clone()) + .with_stats(self.stats.clone()) + .with_sequence(state.sequence(&name)); + state.insert_track(request.weak())?; Ok(request) } @@ -704,7 +726,7 @@ impl Consumer { // requests map, and the FIFO order. The request inherits the broadcast's // cache pool through its `Arc`, same as a producer-created track. let name: Arc = name.into(); - let request = track::Request::new(self.info.clone(), name.clone()); + let request = track::Request::new(self.info.clone(), name.clone()).with_sequence(state.sequence(&name)); let consumer = request.consume(); // With no handler alive to serve it, the request is dropped: `NotFound` beats @@ -1171,6 +1193,61 @@ mod test { track2.assert_group(); } + /// A name keeps one sequence namespace for the broadcast's life: a replacement producer + /// appends past everything an earlier one wrote, explicit writes included, while a new + /// broadcast starts over. + #[test] + fn replacement_continues_sequences() { + let producer = Info::new().produce(); + let mut dynamic = producer.dynamic(); + let consumer = producer.consume(); + + let track = consumer.track("media").unwrap(); + let mut first = dynamic.assert_request().accept(None); + assert_eq!(first.append_group().unwrap().sequence, 0); + assert_eq!(first.append_datagram(crate::Timestamp::ZERO, b"x").unwrap(), 1); + first.create_group(crate::group::Info { sequence: 8 }).unwrap(); + first.insert_datagram(12, crate::Timestamp::ZERO, b"x").unwrap(); + first.finish().unwrap(); + drop((first, track)); + + let track = consumer.track("media").unwrap(); + let mut second = dynamic.assert_request().accept(None); + assert_eq!(second.append_group().unwrap().sequence, 13); + assert_eq!(second.append_datagram(crate::Timestamp::ZERO, b"x").unwrap(), 14); + second.finish().unwrap(); + drop((second, track)); + + // A track the producer creates itself continues the same namespace. + let third = producer.create_track("media", None).unwrap(); + assert_eq!(third.append_group().unwrap().sequence, 15); + + let next = Info::new().produce(); + assert_eq!( + next.create_track("media", None) + .unwrap() + .append_group() + .unwrap() + .sequence, + 0 + ); + } + + /// Names requested but never served leave nothing behind, so a peer asking for + /// arbitrary names cannot grow the broadcast. + #[test] + fn unserved_names_are_forgotten() { + let producer = Info::new().produce(); + let mut dynamic = producer.dynamic(); + let consumer = producer.consume(); + for i in 0..1000 { + let track = consumer.track(&i.to_string()).unwrap(); + drop(dynamic.assert_request()); + drop(track); + } + assert!(producer.state.lock().sequences.len() < 16); + } + #[tokio::test(start_paused = true)] async fn requested_unused() { let mut broadcast = Info::new().produce().dynamic(); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 86dcb76c376..c20e2333997 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -26,7 +26,7 @@ use std::{ ops::{Bound, RangeBounds}, sync::Arc, sync::OnceLock, - sync::atomic::{AtomicBool, Ordering}, + sync::atomic::{AtomicBool, AtomicU64, Ordering}, task::{Poll, ready}, time::Duration, }; @@ -155,6 +155,28 @@ impl Info { } } +/// The sequence namespace a track name keeps within its broadcast, shared by every +/// producer that serves the name, so a replacement appends after whatever an earlier one +/// wrote. Holds one past the highest sequence written. +#[derive(Clone, Default)] +pub(crate) struct Sequence(Arc); + +impl Sequence { + /// The next sequence an append takes. + fn next(&self) -> u64 { + self.0.load(Ordering::Relaxed) + } + + fn advance(&self, sequence: u64) { + self.0.fetch_max(sequence.saturating_add(1), Ordering::Relaxed); + } + + /// Whether nothing holds it and nothing was written, so forgetting it changes nothing. + pub(crate) fn is_unused(&self) -> bool { + Arc::strong_count(&self.0) == 1 && self.next() == 0 + } +} + #[derive(Default)] pub(crate) struct TrackState { // The publisher's properties, once known; always Some for Subscriber/Producer. @@ -221,6 +243,10 @@ pub(crate) struct TrackState { // datagrams, so it can run ahead of any cached group. max_sequence: Option, + // The name's sequence namespace within the broadcast, which an append continues + // even when an earlier producer of the name wrote past this one's `max_sequence`. + sequence: Sequence, + // The sequence of the newest cached group: the live edge, protected from // eviction by never entering the eviction order until the track is `closed`. // Tracked separately from `max_sequence` because datagrams advance that shared @@ -976,6 +1002,22 @@ impl TrackState { state } + /// Record a written sequence, here and in the name's shared namespace. + fn advance(&mut self, sequence: u64) { + self.max_sequence = Some(self.max_sequence.map_or(sequence, |max| max.max(sequence))); + self.sequence.advance(sequence); + } + + /// The sequence the next append takes: past this track's own writes and past every + /// earlier producer's of the same name. + fn next_sequence(&self) -> Result { + let own = match self.max_sequence { + Some(max) => max.checked_add(1).ok_or(coding::BoundsExceeded)?, + None => 0, + }; + Ok(own.max(self.sequence.next())) + } + /// Reject a sequence that is still cached; a dead (aborted or evicted) /// incarnation is removed so a fresh group can serve the sequence again. /// @@ -1024,7 +1066,7 @@ impl TrackState { self.evict.push_back((sequence, stamp)); } - self.max_sequence = Some(self.max_sequence.map_or(sequence, |max| max.max(sequence))); + self.advance(sequence); self.lookup.insert( sequence, Slot { @@ -1453,6 +1495,13 @@ impl Producer { self } + /// Continue the name's sequence namespace within its broadcast. Set by the broadcast + /// before the track is handed out. + pub(crate) fn with_sequence(self, sequence: Sequence) -> Self { + set_sequence(&self.state, sequence); + self + } + /// The track's name, unique within its broadcast. pub fn name(&self) -> &str { &self.name @@ -1485,10 +1534,7 @@ impl Producer { /// Create a new group with the next sequence number. pub fn append_group(&self) -> Result { let mut state = self.modify()?; - let sequence = match state.max_sequence { - Some(s) => s.checked_add(1).ok_or(coding::BoundsExceeded)?, - None => 0, - }; + let sequence = state.next_sequence()?; if let Some(fin) = state.final_sequence && sequence >= fin { @@ -1525,16 +1571,13 @@ impl Producer { // Normalize into the track's timescale, like frames (see `group::Producer::create_frame`). let timescale = state.info.as_ref().unwrap().timescale; let timestamp = timestamp.convert(timescale).map_err(|_| Error::TimestampMismatch)?; - let sequence = match state.max_sequence { - Some(s) => s.checked_add(1).ok_or(coding::BoundsExceeded)?, - None => 0, - }; + let sequence = state.next_sequence()?; if let Some(fin) = state.final_sequence && sequence >= fin { return Err(Error::Closed); } - state.max_sequence = Some(sequence); + state.advance(sequence); meter.datagram(payload.len() as u64); state.push_datagram(Datagram { sequence, @@ -1573,7 +1616,7 @@ impl Producer { { return Err(Error::Closed); } - state.max_sequence = Some(state.max_sequence.unwrap_or(0).max(sequence)); + state.advance(sequence); meter.datagram(payload.len() as u64); state.push_datagram(Datagram { sequence, @@ -2061,6 +2104,16 @@ impl Drop for Dynamic { } } +fn set_sequence(state: &kio::Producer, sequence: Sequence) { + if let Ok(mut state) = state.write() { + debug_assert!( + state.max_sequence.is_none(), + "a track joins its namespace before writing" + ); + state.sequence = sequence; + } +} + /// Ends the track when the last [`Producer`] or [`Dynamic`] drops. /// /// A refcount rather than a "am I the last one?" check inside `Drop`: that answer is a @@ -4262,6 +4315,13 @@ impl Request { self } + /// Continue the name's sequence namespace within its broadcast. Set by the broadcast + /// before the request is visible to anyone. + pub(crate) fn with_sequence(self, sequence: Sequence) -> Self { + set_sequence(&self.state, sequence); + self + } + /// The requested track name. pub fn name(&self) -> &str { &self.name diff --git a/rs/moq-net/tests/route_change.rs b/rs/moq-net/tests/route_change.rs index 417d1fc249b..baf634858d3 100644 --- a/rs/moq-net/tests/route_change.rs +++ b/rs/moq-net/tests/route_change.rs @@ -461,3 +461,90 @@ route_flap_tests! { flaps_ietf_19: "moq-transport-19", flaps_ietf_22: "moq-transport-22", } + +/// `P` serves `video` on demand and replaces its producer partway through the broadcast, +/// the way a publisher restarts an encoder. `R` reads through `A`, whose route stays up, +/// so the logical track resumes onto the replacement. The replacement continues the +/// track's sequence namespace, so its first group reaches `R` at once instead of being +/// skipped until a fresh counter caught up with what `R` already read. +async fn producer_replaced(version: &str) { + let version: Version = version.parse().unwrap(); + let publisher = produce_origin(1); + let relay = produce_origin(2); + let subscriber = produce_origin(3); + + let broadcast = publisher.create_broadcast("live").unwrap(); + let mut dynamic = broadcast.dynamic(); + broadcast.announce(Default::default()).unwrap(); + let (tx, mut producers) = mpsc::unbounded_channel(); + tokio::spawn(async move { + while let Ok(request) = dynamic.requested_track().await { + if tx.send(request.accept(None)).is_err() { + return; + } + } + }); + + let _p_to_a = link(version, &publisher, &relay).await; + let _a_to_r = link(version, &relay, &subscriber).await; + + let consumer = subscriber.consume(); + consumer.routed("live").await.unwrap(); + let remote = consumer.request_broadcast("live").await.unwrap(); + let prefs = track::Subscription::default().with_max_age(Duration::from_secs(60)); + let sub = remote.track("video").unwrap().subscribe(prefs).await.unwrap(); + let mut rx = read(sub); + + let first = producers.recv().await.unwrap(); + for sequence in 0..2 { + let mut group = first.append_group().unwrap(); + assert_eq!(group.sequence, sequence, "{version}"); + for frame in 0..FRAMES { + group.write_frame(Timestamp::ZERO, payload(sequence, frame)).unwrap(); + assert_eq!(next(&mut rx).await, (sequence, payload(sequence, frame)), "{version}"); + } + group.finish().unwrap(); + } + + // The encoder restarts: the old producer goes away mid-broadcast, and the resumed + // subscription asks for the track again. + first.abort(Error::Cancel).unwrap(); + let second = tokio::time::timeout(Duration::from_secs(10), producers.recv()) + .await + .expect("the track was not requested again") + .unwrap(); + + let mut group = second.append_group().unwrap(); + assert_eq!(group.sequence, 2, "{version}: the replacement restarted its sequences"); + for frame in 0..FRAMES { + group.write_frame(Timestamp::ZERO, payload(2, frame)).unwrap(); + assert_eq!(next(&mut rx).await, (2, payload(2, frame)), "{version}"); + } + group.finish().unwrap(); + + settle().await; + assert!(rx.try_recv().is_err(), "{version}: trailing delivery"); + drop((second, broadcast)); +} + +macro_rules! producer_replaced_tests { + ($($name:ident: $version:literal,)*) => { + $( + #[tokio::test(start_paused = true)] + async fn $name() { + tokio::time::timeout(TEST_TIMEOUT, producer_replaced($version)) + .await + .expect("timed out"); + } + )* + }; +} + +producer_replaced_tests! { + replaced_lite_07: "moq-lite-07-wip", + replaced_lite_06: "moq-lite-06", + replaced_lite_05: "moq-lite-05", + replaced_lite_04: "moq-lite-04", + replaced_ietf_19: "moq-transport-19", + replaced_ietf_22: "moq-transport-22", +} From cf3e4661792567f99d5a4db0561998855378f495 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 22:04:25 -0700 Subject: [PATCH 4/4] fix(js/net): count every pending info lookup as demand An info lookup that joined a request a subscription opened never pinned demand, so the broadcast looked unused once the subscriber left while the lookup still waited. Each requested producer now counts its pending lookups. Also scope the JS doc's sequence continuity to on-demand producers, and require the replaced-producer test's subscription to stay open. Co-Authored-By: Claude Opus 5.5 --- doc/lib/js/net.md | 2 +- js/net/src/broadcast.test.ts | 21 +++++++++++++++ js/net/src/broadcast.ts | 44 ++++++++++++++++++++------------ rs/moq-net/tests/route_change.rs | 5 +++- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/doc/lib/js/net.md b/doc/lib/js/net.md index d615fd73b1d..82a888cc915 100644 --- a/doc/lib/js/net.md +++ b/doc/lib/js/net.md @@ -53,7 +53,7 @@ for (;;) { - **Connections** race WebTransport against WebSocket. `new Connection({ url })` pools one connection per relay URL and reconnects with backoff, which the elements use. Supplying WebTransport/WebSocket options, discovery, delay, `goaway`, or a caller-owned origin selects a private loop; explicit `share: true` refuses those options. On a relay's GOAWAY the connection dials the replacement at once while the old session keeps serving its groups until it closes or the `goaway.handover` cap (default 10s, lowered to the relay's deadline) passes; the handle and its origin stay the same. An empty GOAWAY redials the same URL through a fresh DNS resolve. A redirect is followed onto the same host by default (`goaway.redirect: "follow"` lets the relay pick the host, `"ignore"` never moves), and moves the pooled entry to the new URL; a malformed or refused one, including a host change under a pinned certificate, ends the connection with `Error.RefusedRedirect` instead of reconnecting. `closed` settles when the handle is released (`null` on a clean close); the failure that stopped retrying the current URL is `error`, and a new URL recovers the same handle. A connection owns one send-rate sampler and one `Bandwidth.Allocator`; publishers reserve against it so their encoder targets sum to the estimate instead of each matching it. - **Bandwidth** (`Bandwidth.Allocator`) divides the connection's send-rate estimate by track priority, max-min fair within a tier. An idle track claims nothing. The receive side is untouched. - **Discovery** by any pattern scope (`origin.announced(scope)`, such as `room/*/chat`; default everything). A route event's `kind` says whether the route started, updated, or ended; its `prefix` is the covered prefix relative to the origin, and `captures` reports what the scope's wildcards matched when the prefix pins them. The consumer is an async iterable. `origin.broadcasts(scope)` is a live `Getter>` of the same covered prefixes for UIs that need the current set. A borrowed `Connection.origin` also exposes `dynamic(prefix, route)` for serving paths on demand. -- **Tracks**: a broadcast serves one track per name. Concurrent subscriptions and info lookups share one request and producer, and a producer that replaces an ended one continues the name's group and datagram sequences, which only a new broadcast restarts. +- **Tracks**: a broadcast serves one track per name. Concurrent subscriptions and info lookups share one request and producer, and an on-demand producer that replaces an ended one continues the name's group and datagram sequences, which only a new broadcast restarts. A track from `createTrack` or `insertTrack` starts its own sequences at 0. - **Publisher retention** is optional `Track.Info.maxAge`, measured against media timestamps: omission sets no limit, while zero serves only the live edge. It clamps every subscriber's max delay, and stalled media does not age out merely because wall time passes. Separately, cached groups nobody writes for 30 seconds are reclaimed, except a live track's newest group, which stays replayable until the track closes. - **Timing** is per track. A timed track declares `Track.Info.timescale` (such as `Time.Timescale.MILLI`); omitting it (or passing `null`, which means the same) declares an untimed track. There is no default, unlike Rust's millisecond default. A frame or datagram carries a `timestamp` exactly when its track has a timescale, and a write that disagrees throws `Error.TimestampMismatch`. Nothing fills in a time on receive: moq-lite before 05, and moq-transport without TIMESCALE (always so on drafts 14-16), deliver untimed tracks. moq-lite 05 and 06 can't declare an untimed track, so it goes out at milliseconds with each frame's send time. An untimed group is never stale, so a subscription without a `groups.start` begins at the latest group. - **Subscriptions** carry a priority, a `Time.Milli` max delay measured against media timestamps, and optional `groups` bounds. Subscription options use `maxDelay`; supplying the obsolete subscriber `maxAge` key throws a `TypeError` naming its replacement, including on `update()`. Groups arrive out of order and are read frame by frame, with `Error.TooFarBehind` when a reader asks for a frame the group never held and `Error.GroupTooLarge` when a write exceeds the cache budget and aborts the group. diff --git a/js/net/src/broadcast.test.ts b/js/net/src/broadcast.test.ts index d54ce64fe89..a50fdf4cfd0 100644 --- a/js/net/src/broadcast.test.ts +++ b/js/net/src/broadcast.test.ts @@ -121,6 +121,27 @@ test("publishing-side subscriptions coalesce onto one request", async () => { broadcast.close(); }); +test("an info lookup joining a subscription's request stays demand after the subscriber leaves", async () => { + const broadcast = new BroadcastProducer(); + const demand = broadcast.demand(); + const pulled = wireOf(broadcast).requested(); + const subscriber = broadcast.track("media").subscribe(); + const info = broadcast.track("media").info(); + const request = await pulled; + if (!request) throw new Error("expected request"); + + // Flush every pending notification, so only the lookup is left holding demand. + subscriber.close(); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(request.demand().used.peek()).toBe(false); + expect(demand.used.peek()).toBe(true); + + request.accept({ timescale: Timescale.MILLI }); + await info; + await demand.unused(); + broadcast.close(); +}); + test("an info lookup releases a request nobody subscribed to", async () => { const broadcast = new BroadcastProducer(); const pulled = wireOf(broadcast).requested(); diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index d90758d66db..e07380e0313 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -27,6 +27,19 @@ export interface Announcer { let attachAnnouncer: (producer: Producer, announcer: Announcer) => void; let stampProducer: (producer: Producer, path: Path.Valid) => void; +// Info lookups pending on a requested track. Each is demand, as a pending track request is in Rust, +// though nobody subscribes. +class Lookups { + pending = 0; + // Re-evaluates the track's demand at once; set by watchDemand. + changed = () => {}; + + add(delta: number): void { + this.pending += delta; + this.changed(); + } +} + /** Reactive backing state shared by broadcast producers and consumers. */ class BroadcastState { requested = new Signal([]); @@ -37,6 +50,7 @@ class BroadcastState { tracks = new Map(); // The on-demand producer for each name, shared by every subscription and info lookup until it closes. requests = new Map(); + lookups = new WeakMap(); // Whether something answers on-demand track requests: a wire layer's consumed broadcast, or a // publisher that has started pulling them. The counterpart of a live Rust `broadcast::Dynamic`; // without one, a track nobody publishes is `NotFound` rather than a request nobody will answer. @@ -52,8 +66,8 @@ class BroadcastState { } // Each track updates the aggregate on an edge, so a demand change touches only its track. -// A track counts as demand while `pinned` is set, subscribed or not. -function watchDemand(state: BroadcastState, producer: track.Producer, pinned?: Getter): void { +// A track counts as demand while any `lookups` are pending, subscribed or not. +function watchDemand(state: BroadcastState, producer: track.Producer, lookups?: Lookups): void { if (state.demands.has(producer)) return; const demand = producer.demand(); // A closed track is never demand, and its close already fired, so nothing would dispose a watcher. @@ -63,21 +77,20 @@ function watchDemand(state: BroadcastState, producer: track.Producer, pinned?: G const used = state.closed.peek() === undefined && demand.closed.peek() === undefined && - (pinned?.peek() || demand.used.peek()); + ((lookups?.pending ?? 0) > 0 || demand.used.peek()); if (active === used) return; state.active += used ? 1 : -1; active = used; state.used.set(state.active > 0); }; const disposeUsed = demand.used.subscribe(update); - const disposePinned = pinned?.subscribe(update); + if (lookups) lookups.changed = update; const disposeClosed = demand.closed.subscribe(() => { if (demand.closed.peek() === undefined) return; cleanup(); }); const cleanup = () => { disposeUsed(); - disposePinned?.(); disposeClosed(); if (active) { state.active--; @@ -152,13 +165,8 @@ function lookup(state: BroadcastState, name: string): track.Producer | undefined // weak-dedup: a requested producer is cached in `state.requests` so every subscription and info // lookup fans out from one request instead of opening another. Whoever serves it closes it once // unused (the consuming wire tears the upstream down when its last subscriber leaves), which evicts -// the entry so a later subscribe requests the track again, continuing its sequences. A new request -// counts as demand while `pinned` is set. -function logical( - state: BroadcastState, - name: string, - pinned?: Getter, -): { producer: track.Producer; requested: boolean } { +// the entry so a later subscribe requests the track again, continuing its sequences. +function logical(state: BroadcastState, name: string): { producer: track.Producer; requested: boolean } { if (state.closed.peek() !== undefined) { throw new Error("broadcast is closed"); } @@ -174,7 +182,9 @@ function logical( return { producer, requested: false }; } - watchDemand(state, producer, pinned); + const lookups = new Lookups(); + state.lookups.set(producer, lookups); + watchDemand(state, producer, lookups); state.requests.set(name, producer); void producer.closed.then(() => { if (state.requests.get(name) === producer) state.requests.delete(name); @@ -192,13 +202,13 @@ function subscribe(state: BroadcastState, name: string, options: track.Subscript } async function resolveTrackInfo(state: BroadcastState, name: string): Promise { - // A pending query is demand, as a pending track request is in Rust, though nobody subscribes. - const pending = new Signal(true); - const { producer, requested } = logical(state, name, pending); + const { producer, requested } = logical(state, name); + const lookups = state.lookups.get(producer); + lookups?.add(1); try { return await producer.info(); } finally { - pending.set(false); + lookups?.add(-1); // A finished lookup is no demand: let go of a request it opened that nobody subscribed to // meanwhile. if (requested && !producer.demand().used.peek()) producer.close(); diff --git a/rs/moq-net/tests/route_change.rs b/rs/moq-net/tests/route_change.rs index dec641ada02..d70a6c93ef1 100644 --- a/rs/moq-net/tests/route_change.rs +++ b/rs/moq-net/tests/route_change.rs @@ -723,7 +723,10 @@ async fn producer_replaced(version: &str) { group.finish().unwrap(); settle().await; - assert!(rx.try_recv().is_err(), "{version}: trailing delivery"); + assert!( + matches!(rx.try_recv(), Err(mpsc::TryRecvError::Empty)), + "{version}: trailing delivery, or the subscription ended" + ); drop((second, broadcast)); }