Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
c542d17
fix(net): hold demand across a subscription's TRACK and SUBSCRIBE
Dryvnt Oct 8, 2026
f5dd6f9
fix(net): hold demand across TRACK and SUBSCRIBE in js, and say so in…
Dryvnt Oct 8, 2026
2fbbfd1
fix(net): end a TRACK hold on the requester's reset without an error
Dryvnt Oct 8, 2026
4be67dc
quest: finish TRACK stream demand
Dryvnt Oct 8, 2026
3f91376
fix(transcode): hold the source track only while a rung reads it
Dryvnt Oct 8, 2026
66735d1
docs(net): note the bare-handle demand gap after a subscription ends
Dryvnt Oct 8, 2026
e65605f
fix(net): let a TRACK hold go whenever its requester leaves
Dryvnt Oct 8, 2026
5ef1974
Merge remote-tracking branch 'origin/main' into quest/m0/track-stream…
Dryvnt Oct 8, 2026
6417967
fix(net): keep a pending TRACK_INFO lookup joinable after its holders…
Dryvnt Oct 8, 2026
6e0a99c
fix(net): let a TRACK stream after a released lookup ask again
Dryvnt Oct 8, 2026
5ece935
Merge remote-tracking branch 'origin/main' into quest/m0/track-stream…
Dryvnt Oct 8, 2026
36ee676
test(net): expect one JS request per viewer now that TRACK holds it
Dryvnt Oct 8, 2026
629d92e
Merge remote-tracking branch 'origin/main' into quest/m0/track-stream…
Dryvnt Oct 8, 2026
9c538e8
fix(net): let a JS requester that leaves before the answer release it…
Dryvnt Oct 8, 2026
62246ea
docs(net): a held track consumer starts no live delivery, but can fetch
Dryvnt Oct 8, 2026
b3168d7
Merge remote-tracking branch 'origin/main' into pr-5053
kixelated Oct 8, 2026
627ab66
fix(net): detach a removed track's lookups from its demand watcher
kixelated Oct 8, 2026
363e623
Merge remote-tracking branch 'origin/main' into pr-5053
kixelated Oct 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/lib/rs/moq-net.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ What you use it for, beyond what the concept page already describes:
- **An origin outlives the session.** Broadcasts are created on the origin, and a reconnect announces them again. Closing the session does not delete them.
- **Requests can pin an epoch.** `consumer.request_broadcast(path, Some(epoch))` resolves only through a route announcing that [publisher epoch](/concept/moq-lite#publisher-epochs); `None` takes whichever route wins. The resolved consumer names its epoch in `info().epoch`, pinned or not.
- **One track per name.** Concurrent subscriptions share one request and producer. A producer that replaces an ended one continues the name's group and datagram sequences; only a new broadcast restarts them.
- **Publish only while someone is watching.** `demand()` on a track, group, or broadcast says whether a subscriber is attached, which is how capture and transcode skip work nobody asked for. Holding a broadcast consumer is not demand. A shared fetch stays up until its last reader leaves.
- **Publish only while someone is watching.** `demand()` on a track, group, or broadcast says whether a subscriber is attached, which is how capture and transcode skip work nobody asked for. Holding a broadcast consumer is not demand. Holding a track consumer is, so drop one you are not reading. A shared fetch stays up until its last reader leaves.
- **You drive the session, or `moq-tokio` does.** `connect` and `accept` return a session plus a driver that never reads the clock itself. `moq_net::time::run` polls it on tokio or in the browser. `moq-tokio` and `moq-wasm` hide that. A custom transport implements `moq_net::transport::poll`.
- **A session caps what its peer can hold.** `session::Limits` (via `Client::with_limits` and `Server::with_limits`) bounds announces and subscriptions per session; the defaults suit a relay mesh, so lower them for untrusted peers. Past either, the session closes with `TOO_MANY_REQUESTS`. On moq-transport drafts 14 to 16 they also size the request-ID window, which every request counts against, so very low limits can starve it. Peer-declared lengths are capped before they are buffered.
- **A graceful close waits.** `session.close().await` withdraws announcements and gives finished tracks up to one second to deliver. `abort` ends immediately. IETF drafts 14 through 16 send withdrawals without waiting.
Expand Down
2 changes: 2 additions & 0 deletions drafts/draft-lcurley-moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ A subscriber opens a Track Stream (0x6) to learn a Track's immutable publisher p

The subscriber sends a TRACK message containing the broadcast path and track name.
The publisher replies with a single TRACK_INFO message and then FINs the stream, or resets the stream on error (e.g. the track does not exist).
The open stream is interest in the Track, which the publisher holds until the subscriber closes its side, and a subscriber that goes on to subscribe keeps it open until its Subscribe Stream's first response or reset, so demand never lapses between the two streams at any hop.
The returned properties are fixed for the lifetime of the track, so the subscriber SHOULD cache TRACK_INFO keyed by broadcast path and track name, and reuse it across every SUBSCRIBE and FETCH of the same track over the session that served it.
The properties are fixed for one track, not for the path it arrived on: a path outlives the broadcast on it, and a different broadcast reaching the same path brings its own.
Anything cached against a path, on either side, is therefore scoped to the announcement that carried it and is discarded when that announcement is retracted.
Expand Down Expand Up @@ -1397,6 +1398,7 @@ The `Message Length` describes the payload size on the wire.
- Added `Largest Group` and `Largest Frame` to SUBSCRIBE_OK: the publisher's largest position when it answers, which a subscriber takes as where the live feed is. A publisher MUST answer at once when the requested start is past it. Earlier versions carry no such position, so a subscriber takes the first frame instead.
- Added `Epoch` to ANNOUNCE_START, SUBSCRIBE, TRACK, and FETCH: a UUIDv7 naming the publisher instance, or empty. A path and an Epoch name one Broadcast. A relay MAY move a subscription between routes with the same Epoch, continuing from the first frame the subscriber lacks instead of at a group boundary, and never between routes with different Epochs or none. The newest Epoch wins a path and ends subscriptions to the older one. Replaces the first-hop identity.
- An untimed Track's frames and datagrams carry their send time. lite-05 and lite-06 publishers do the same.
- An open Track Stream is interest in the Track until the subscriber closes it, which a subscriber does once its Subscribe Stream has a response. lite-05 and lite-06 implementations do the same.

- Assigned `moq-lite-07-wip` as this draft's protocol identifier until it is finalized as `moq-lite-07`.
- Switched every variable-length integer, including SETUP parameter values, from QUIC's two-bit length prefix to moq-transport's leading-ones encoding, widening the range to 64 bits.
Expand Down
106 changes: 106 additions & 0 deletions js/net/src/broadcast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,112 @@ test("an info lookup releases a request nobody subscribed to", async () => {
broadcast.close();
});

test("held info lookups keep a request until the last one lets go", async () => {
const broadcast = new BroadcastProducer();
const demand = broadcast.demand();
const pulled = wireOf(broadcast).requested();
const first = new AbortController();
const opened = wireOf(broadcast).resolveTrackInfo("media", first.signal);
const request = await pulled;
if (!request) throw new Error("expected request");
const producer = request.accept();
const second = new AbortController();
await wireOf(broadcast).resolveTrackInfo("media", second.signal);
await opened;

// The lookup that opened the request lets go first, under the one still holding it.
first.abort();
expect(producer.closed.peek()).toBeUndefined();
expect(demand.used.peek()).toBe(true);

second.abort();
expect(producer.closed.peek()).toBeDefined();
expect(demand.used.peek()).toBe(false);
broadcast.close();
});

test("an abandoned info lookup stops counting as demand, and its request goes once answered", async () => {
const broadcast = new BroadcastProducer();
const demand = broadcast.demand();
const pulled = wireOf(broadcast).requested();
const hold = new AbortController();
const info = wireOf(broadcast).resolveTrackInfo("media", hold.signal);
const request = await pulled;
if (!request) throw new Error("expected request");
expect(demand.used.peek()).toBe(true);

hold.abort();
await expect(info).rejects.toThrow();
expect(demand.used.peek()).toBe(false);

// The handler still gets an open track, and the answer lets it go.
const producer = request.accept({ timescale: Timescale.MILLI });
producer.writeString("late");
await producer.closed;
broadcast.close();
});

test("a held info lookup on an inserted track counts as demand until it lets go", async () => {
const broadcast = new BroadcastProducer();
const demand = broadcast.demand();
const track = broadcast.createTrack("media", { timescale: Timescale.MILLI });
const hold = new AbortController();
await wireOf(broadcast).resolveTrackInfo("media", hold.signal);
expect(demand.used.peek()).toBe(true);

hold.abort();
expect(demand.used.peek()).toBe(false);
expect(track.closed.peek()).toBeUndefined();
broadcast.close();
});

test("a held info lookup ending after removeTrack leaves demand alone", async () => {
const broadcast = new BroadcastProducer();
const demand = broadcast.demand();
const track = broadcast.createTrack("media", { timescale: Timescale.MILLI });
const subscriber = track.subscribe();
const hold = new AbortController();
await wireOf(broadcast).resolveTrackInfo("media", hold.signal);
expect(demand.used.peek()).toBe(true);

broadcast.removeTrack("media");
expect(demand.used.peek()).toBe(false);
hold.abort();
expect(demand.used.peek()).toBe(false);
subscriber.close();
await Promise.resolve();
expect(demand.used.peek()).toBe(false);

// Re-inserting the track counts it again.
broadcast.insertTrack(track);
expect(demand.used.peek()).toBe(false);
const resubscriber = track.subscribe();
await Promise.resolve();
expect(demand.used.peek()).toBe(true);

resubscriber.close();
track.close();
broadcast.close();
});

test("removeTrack leaves a track that only a request serves", async () => {
const broadcast = new BroadcastProducer();
const demand = broadcast.demand();
const pulled = wireOf(broadcast).requested();
const subscriber = broadcast.track("media").subscribe();
const request = await pulled;
if (!request) throw new Error("expected request");
const producer = request.accept();

broadcast.removeTrack("media");
expect(demand.used.peek()).toBe(true);
expect(producer.closed.peek()).toBeUndefined();

subscriber.close();
producer.close();
broadcast.close();
});

test("closing a broadcast rejects a dequeued request", async () => {
const broadcast = new BroadcastProducer();
const pulled = wireOf(broadcast).requested();
Expand Down
59 changes: 46 additions & 13 deletions js/net/src/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ 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,
// Info lookups pending or held on a track. Each is demand, as a pending track request is in Rust,
// though nobody subscribes.
class Lookups {
pending = 0;
// Whether a lookup opened the request, so the last lookup to end closes it unless subscribed.
opened = false;
// Re-evaluates the track's demand at once; set by watchDemand.
changed = () => {};

Expand Down Expand Up @@ -92,6 +94,8 @@ function watchDemand(state: BroadcastState, producer: track.Producer, lookups?:
const cleanup = () => {
disposeUsed();
disposeClosed();
// A removed track keeps its lookups, so a hold ending later must not count it again.
if (lookups?.changed === update) lookups.changed = () => {};
if (active) {
state.active--;
active = false;
Expand Down Expand Up @@ -201,18 +205,45 @@ function subscribe(state: BroadcastState, name: string, options: track.Subscript
return logical(state, name).producer.subscribe(options);
}

async function resolveTrackInfo(state: BroadcastState, name: string): Promise<track.Info> {
// A pending lookup is demand, and so is a held one until `hold` aborts, which also abandons it before
// the answer. A request a lookup opened is let go once it is answered and nobody holds or subscribes
// to it; closing it sooner would hand its handler a closed producer.
async function resolveTrackInfo(state: BroadcastState, name: string, hold?: AbortSignal): Promise<track.Info> {
hold?.throwIfAborted();
const { producer, requested } = logical(state, name);
const lookups = state.lookups.get(producer);
lookups?.add(1);
try {
return await producer.info();
} finally {
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();
if (!lookups) return producer.info();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if (requested) lookups.opened = true;
lookups.add(1);

const info = producer.info();
let answered = false;
let ended = false;
const release = () => {
if (lookups.opened && lookups.pending === 0 && !producer.demand().used.peek()) producer.close();
};
info.then(
() => {
answered = true;
if (ended) release();
},
() => {},
);
const end = () => {
ended = true;
lookups.add(-1);
if (answered) release();
};

if (!hold) {
try {
return await info;
} finally {
end();
}
}
hold.addEventListener("abort", end, { once: true });
return untilAborted(info, hold);
}

// Serve a group from the local retained window by subscribing and scanning to the
Expand Down Expand Up @@ -292,7 +323,7 @@ export class Producer {
constructor() {
registerWire(this, {
subscribe: (name, options) => subscribe(this.#state, name, options),
resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),
resolveTrackInfo: (name, hold) => resolveTrackInfo(this.#state, name, hold),
fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),
requested: () => requested(this.#state),
});
Expand Down Expand Up @@ -338,7 +369,9 @@ export class Producer {
throw new Error(`duplicate track: ${track.name}`);
}

watchDemand(this.#state, track);
const lookups = this.#state.lookups.get(track) ?? new Lookups();
this.#state.lookups.set(track, lookups);
watchDemand(this.#state, track, lookups);
Comment on lines +372 to +374

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Detach lookup notifications when removing an inserted track

These inserted tracks now receive a Lookups whose changed callback is watchDemand's update closure, but removeTrack() invokes cleanup at lines 94–103 without clearing that callback. With an active subscriber and a held TRACK lookup, removeTrack() first makes broadcast demand false; aborting the hold then calls lookups.add(-1), and the disposed watcher sees the subscriber still active and increments state.active again. When the subscriber later closes, its demand listener has already been disposed, so broadcast demand stays true and unused() never resolves. Clear or invalidate the lookup callback when disposing the watcher, retaining correct behavior on reinsertion. Extend the removal regression with: subscribe → hold lookup → removeTrack → abort hold → close subscriber, asserting demand remains false after removal.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, fixed in 627ab66: the demand watcher's cleanup now detaches its changed callback from the lookups, so a hold ending after removeTrack no longer counts the track again, and reinsertion attaches a fresh one. Regression: a held info lookup ending after removeTrack leaves demand alone in broadcast.test.ts (subscribe, hold, removeTrack, abort hold, close subscriber, reinsert), which fails without the fix.

(Written by Claude Opus 5.5)

this.#state.tracks.set(track.name, track);

// A finished track keeps serving its cache, so only an abort evicts it.
Expand Down Expand Up @@ -453,7 +486,7 @@ export class Consumer {
this.#state.consumers++;
registerWire(this, {
subscribe: (name, options) => subscribe(this.#state, name, options),
resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name),
resolveTrackInfo: (name, hold) => resolveTrackInfo(this.#state, name, hold),
fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options),
requested: () => requested(this.#state),
});
Expand Down
5 changes: 2 additions & 3 deletions js/net/src/goaway-requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Once } from "@moq/signals";
import { accept } from "./connection/accept.ts";
import { connect } from "./connection/connect.ts";
import type { Drain } from "./connection/goaway.ts";
import { StreamCode } from "./error.ts";
import type { Session } from "./ietf/adapter.ts";
import { GoAway } from "./ietf/goaway.ts";
import { Subscriber } from "./ietf/subscriber.ts";
Expand Down Expand Up @@ -172,9 +171,9 @@ async function handover(kind: "lite" | "ietf"): Promise<void> {
const later = front.track("later").subscribe().ordered();
expect(await later.readString()).toBe("from-draining");

// The publisher no longer holds group 0, so its answer proves the FETCH reached the wire.
// A fresh consumer caches nothing, so the publisher's answer proves the FETCH reached the wire.
const fetched = wireOf(client).consume(Path.from("room")).track("video").fetchGroup(0);
if (kind === "lite") await expect(fetched).rejects.toMatchObject({ code: StreamCode.NotFound });
if (kind === "lite") expect(await (await fetched).readString()).toBe("one");
else await expect(fetched).rejects.toThrow(/not supported/);

// A new announce-interest opens too, and sees the route at the drain cost.
Expand Down
62 changes: 60 additions & 2 deletions js/net/src/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,12 @@ test("integration: lite subscription options and updates reach the publisher", a
const request = await wireOf(broadcast).requested();
if (!request) return;
const producer = request.accept({ timescale: Timescale.MILLI });
if (request.subscription.groups?.start?.included === 1) resolveProducer?.(producer);
// The TRACK lookup opens the request and the SUBSCRIBE joins it, so its options land after.
const check = (subscription = producer.subscription.peek()) => {
if (subscription?.groups?.start?.included === 1) resolveProducer?.(producer);
};
check();
producer.subscription.subscribe(check);
}
})();

Expand Down Expand Up @@ -219,7 +224,12 @@ test("integration: lite carries a fractional maxDelay as a whole millisecond", a
const request = await wireOf(broadcast).requested();
if (!request) return;
const producer = request.accept({ timescale: Timescale.MILLI });
if (request.subscription.groups?.start?.included === 1) resolveProducer?.(producer);
// The TRACK lookup opens the request and the SUBSCRIBE joins it, so its options land after.
const check = (subscription = producer.subscription.peek()) => {
if (subscription?.groups?.start?.included === 1) resolveProducer?.(producer);
};
check();
producer.subscription.subscribe(check);
}
})();

Expand Down Expand Up @@ -364,6 +374,54 @@ test("integration: lite draft-06", async () => {
await runPublishSubscribeFlow(Lite.ALPN_06);
});

// A subscriber learns a track's properties over a TRACK stream before it subscribes. The
// publisher holds the track for that stream until the subscriber closes it, which it does once
// its SUBSCRIBE is answered, so a publisher starting the track on demand sees one request and one
// demand edge per viewer rather than one for each stream with a gap in between.
test.each([Lite.ALPN_05, Lite.ALPN_06, Lite.ALPN_07_WIP])(
"integration: %s demand holds across a subscription's TRACK and SUBSCRIBE",
async (protocol) => {
const pair = createMockTransportPair(protocol);
const origin = new OriginProducer();
const [client, server] = await Promise.all([
connect(url, { transport: pair.client }),
accept(pair.server, url, { publish: origin.consume() }),
]);

const broadcast = publish(origin, Path.from("test"));
const demand = broadcast.demand();
const edges: boolean[] = [];
const dispose = demand.used.subscribe((used) => edges.push(used));
let requests = 0;
const serving = (async () => {
for (;;) {
const request = await wireOf(broadcast).requested();
if (!request) return;
requests++;
request.accept({ timescale: Timescale.MILLI }).writeString("hello");
}
})();

const remote = wireOf(client).consume(Path.from("test"));
const track = remote.track("video").subscribe().ordered();
expect(await track.readString()).toBe("hello");
await sleep(50);
expect(edges).toEqual([true]);
expect(requests).toBe(1);

track.close();
await withTimeout(demand.unused(), 1000, "demand outlived the reader");
expect(edges).toEqual([true, false]);

dispose();
remote.close();
broadcast.close();
await serving;
client.abort();
server.abort();
},
);

test("integration: lite draft-06 announce lifecycle", async () => {
const pair = createMockTransportPair(Lite.ALPN_06);
const origin = new OriginProducer();
Expand Down
Loading
Loading