From 5b01cc4001ee800e395415eb081ab4326dfd6a94 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 06:46:44 -0800 Subject: [PATCH 01/23] chore(quest): claim quest/m1/datagram-tracks/ietf.md Quest-Claim: 72249e0c9b1fec56 From 720bbd9a8b3e5db2bd14ef728856790af68c85d5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:02:49 -0800 Subject: [PATCH 02/23] feat(net): restore IETF datagram tracks --- doc/concept/standard.md | 2 +- js/net/MIGRATE.md | 11 +- js/net/src/ietf/datagram.test.ts | 105 ++- js/net/src/ietf/object.ts | 5 + js/net/src/ietf/publisher.ts | 304 ++++++++- js/net/src/ietf/subscriber.ts | 328 +++++++++- quest/m1/datagram-tracks/README.md | 1 - quest/m1/datagram-tracks/ietf.md | 53 -- quest/m2/net-wrap/e2ee.md | 6 +- rs/moq-crypto/tests/transport.rs | 7 +- rs/moq-net/MIGRATE.md | 8 + rs/moq-net/benches/session/mod.rs | 16 +- rs/moq-net/src/ietf/publisher.rs | 414 +++++++++++- rs/moq-net/src/ietf/subscriber.rs | 615 +++++++++++++++++- rs/moq-net/src/model/datagram.rs | 5 +- .../tests/integration/datagram_delivery.rs | 3 +- test/interop/README.md | 7 +- test/interop/clients/js-native/tail.ts | 66 +- test/interop/clients/rust/tail.rs | 60 +- test/interop/interop.sh | 31 +- 20 files changed, 1900 insertions(+), 147 deletions(-) delete mode 100644 quest/m1/datagram-tracks/ietf.md diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 472e84e7dcb..12ddb300577 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -36,7 +36,7 @@ subset drops. What a peer actually observes against this implementation: - **One `SETUP` credential**, forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A second `SETUP` token closes the session. We advertise no token cache, so an alias reference in `SETUP` closes it with `PROTOCOL_VIOLATION`, one on a request with `UNKNOWN_AUTH_TOKEN_ALIAS`, and a registration on a request with `AUTH_TOKEN_CACHE_OVERFLOW`. Where the Rust side authorizes the peer or verifies tokens, each distinct request token is verified once while any open request on that session names it, and its grant authorizes only requests naming it, even where that authorization already covers it; it never widens the session. It is refused with `NOT_SUPPORTED` when nothing verifies it, when it is empty, or when its Token Type is anything but 0 (out of band). A side that does neither, JavaScript included, ignores the token. Rust refuses a request carrying more than one distinct token with `NOT_SUPPORTED`, since it verifies one; JavaScript, which verifies none, ignores them all. A token-authorized `SUBSCRIBE` still stays within the grant the peer gave us. A token-authorized `PUBLISH_NAMESPACE` needs a grant covering everything beneath the namespace, and the tracks we subscribe under it rely on that announce instead of our limit, ending with it. An update on a token-authorized request moves it to the token it carries; the old token ends with its last naming request. A request is admitted by the session or by a token for good, so an update naming a token on one the session admitted is refused with `NOT_SUPPORTED`. A token-less `PUBLISH_NAMESPACE` our limit does not cover is refused `UNAUTHORIZED`, and one the limit stops covering ends; the publisher re-announces once it widens. Expiry or revocation ends every request naming that token. The relay reports the lease end to its auth server, and a later request naming the token verifies it again. A refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, and swapping it renews the request with `REQUEST_UPDATE`; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` carries no token. A token added to a reconnecting connection's auth handle rides every session it makes, so the announcement keeps it across a reconnect; one added to a single session's handle ends with that session. Rust honors a token on any supported request; discovery reveals only paths it grants, and a joining `FETCH` keeps following its subscription. A request repeating a token is refused with `MALFORMED_AUTH_TOKEN`. The negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. - **Pause delivery.** `FORWARD=0` on a `SUBSCRIBE` or its update drops the subscription, closing its data streams and holding nothing upstream, while the request and its slot stay. `FORWARD=1` subscribes again as a new `SUBSCRIBE` with the same filter would, so nothing carries across the pause: its `REQUEST_OK` names the live edge it joined at. A paused update may change only priority and max age. - **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: `SUBSCRIBE_TRACKS`, multi-group `FETCH`, and joining requests reaching before the joined group get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` from draft 20 because the advertised filter limit is zero. From draft 20, namespaces whose first tuple field is exactly `.` or `.session` get `DOES_NOT_EXIST` before application lookup; other dot prefixes still reach the application. Subscription updates accept priority, negotiated subscriber max age, and delivery timeouts. Rust also carries range changes, keeps bounded subscriptions updatable, renews request tokens, and judges `NEW_GROUP_REQUEST` as a join (see dynamic groups below). From draft 20, a wider or historical fill fails on its separate fetch stream while its subscription stays open; a largest-group fill hands its head to the live subgroup tail. Other supported-but-unimplemented preferences end that subscription; a `TRACK_PROPERTY_FILTER` there closes the session with `PROTOCOL_VIOLATION`, since it belongs only to `SUBSCRIBE_TRACKS`. A namespace update may change its prefix; suffixes after the acknowledgement are relative to that new prefix. -- **Objects and datagrams.** Only subgroup 0 is accepted: a stream for any other subgroup, whether its header gives the ID or takes it from the first Object ID, is stopped and the session stays up. Rust relays preserve Object IDs, gaps, object properties, FIRST\_OBJECT, and END\_OF\_GROUP status objects between moq-transport peers for subgroup 0, including across a moq-lite-07 hop between two relays that negotiate the [ietf](/draft/moq-ietf) extension. Plain Rust readers and moq-lite peers without the extension see consecutive frames. Rust and JavaScript drop every OBJECT\_DATAGRAM until they map a [datagram track](/concept/moq-lite#datagrams) onto moq-transport. A malformed datagram closes the session. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none, as for a plain frame; an immutable one that would change or vanish is refused. On a timed track the frame holds each object's time, so a mutable Timestamp that only repeats the immutable one is not restated; a status object has no frame, so it keeps its Timestamp among its properties. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped between draft 18 and later drafts, or refused when immutable, because draft 20 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. A status object carrying properties is refused. A `FETCH` has no status objects, so its response ends at the last frame. A group cannot resume across route changes by frame ordinal once Object IDs skip ahead of the ordinals on either route; recovery refuses it instead of splicing different objects. +- **Objects and datagrams.** Only subgroup 0 is accepted: a stream for any other subgroup, whether its header gives the ID or takes it from the first Object ID, is stopped and the session stays up. Rust relays preserve Object IDs, gaps, object properties, FIRST\_OBJECT, and END\_OF\_GROUP status objects between moq-transport peers for subgroup 0, including across a moq-lite-07 hop between two relays that negotiate the [ietf](/draft/moq-ietf) extension. Plain Rust readers and moq-lite peers without the extension see consecutive frames. moq-transport maps a [datagram track](/concept/moq-lite#datagrams) to `SUBSCRIBE` and `OBJECT_DATAGRAM`, with its sequence as the group ID and object ID 0. A datagram subscription keeps only those datagrams; a stream subscription drops every datagram. An undefined Object Status closes the session, and so does a malformed datagram. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none, as for a plain frame; an immutable one that would change or vanish is refused. On a timed track the frame holds each object's time, so a mutable Timestamp that only repeats the immutable one is not restated; a status object has no frame, so it keeps its Timestamp among its properties. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped between draft 18 and later drafts, or refused when immutable, because draft 20 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. A status object carrying properties is refused. A `FETCH` has no status objects, so its response ends at the last frame. A group cannot resume across route changes by frame ordinal once Object IDs skip ahead of the ordinals on either route; recovery refuses it instead of splicing different objects. - **Dynamic groups.** A track advertises `DYNAMIC_GROUPS` when its publisher sets a refresh ratio, and a track received with `DYNAMIC_GROUPS` gets a ratio of 0. A `NEW_GROUP_REQUEST` counts as one more subscriber joining, which a `SUBSCRIBE` already is: it asks for a new group only when the newest group's bytes after its first frame exceed the ratio times the first frame. Joins coalesce until the asked-for group arrives. A Rust relay sends `NEW_GROUP_REQUEST` upstream, on the `SUBSCRIBE` or in a `REQUEST_UPDATE`, once a join wants a group past the newest it holds; a relay holding nothing yet asks for none. On a track without a ratio, an update carrying one ends the subscription with `UPDATE_FAILED`. A moq-lite-07 hop (`moq-lite-07-wip`, opt-in) carries the same demand natively: the ratio in `TRACK_INFO` and the asked-for group in `SUBSCRIBE_UPDATE`. Earlier moq-lite versions carry neither, so a track received over one never refreshes. - **Priority.** Higher is served first. This stack's priorities run from -128 to 127 with 0 unset, and go out on the IETF wire, which ranks lower first, as `127 - priority`: an unset priority is IETF 127. An omitted subscriber or publisher priority reads as the drafts' default of IETF 128, one step below unset, so a relay forwards it unchanged. A relay's upstream subscription carries the highest priority its subscribers ask for and updates it as they come and go; its group order stays as the `SUBSCRIBE` asked, since no draft lets an update change it. - **Size.** An object extension block larger than 64 KiB ends that subgroup stream. The session stays up. This cap is ours, not the draft's. diff --git a/js/net/MIGRATE.md b/js/net/MIGRATE.md index e88a594fd64..702433ddd69 100644 --- a/js/net/MIGRATE.md +++ b/js/net/MIGRATE.md @@ -4,6 +4,14 @@ How to update your code for each breaking change, newest first. ## Unreleased +### moq-transport datagram tracks + +Datagram tracks now work over moq-transport using SUBSCRIBE and OBJECT_DATAGRAM. +The sequence is the group ID and the object ID is zero. Stream and datagram +consumers keep only their requested kind; nonzero object IDs are dropped by +datagram consumers. No application API or wire encoding changes are required. + + ### Datagrams use separate track types ([#7846](https://github.com/moq-dev/moq/pull/7846)) Create a datagram track with `broadcast.createDatagrams(name, info)` and read it with @@ -24,8 +32,7 @@ stream resume state and does not move to a replacement route. Lite-07-wip uses DATAGRAMS on stream type 0x9 and carries no Inflated Size. Lite-05/06 keep their wire format and carry both kinds of a name on one SUBSCRIBE, without deduplication. Create a datagram track before or with its same-name stream track on those versions. -Lite-01..04 and transports without datagrams refuse datagram requests. moq-transport -currently refuses them while its separate datagram-track adapter is implemented. +Lite-01..04 and transports without datagrams refuse datagram requests. moq-transport carries them as OBJECT_DATAGRAM with object ID zero. ### Announcements are a listing ([#7678](https://github.com/moq-dev/moq/pull/7678)) diff --git a/js/net/src/ietf/datagram.test.ts b/js/net/src/ietf/datagram.test.ts index 3def32fb43d..6111aa87f48 100644 --- a/js/net/src/ietf/datagram.test.ts +++ b/js/net/src/ietf/datagram.test.ts @@ -1,8 +1,15 @@ import { expect, test } from "bun:test"; -import { ProtocolViolation } from "../error.ts"; -import { encodeVarint } from "../stream.ts"; +import { accept, connect } from "../connection/index.ts"; +import { ProtocolViolation, SessionCode } from "../error.ts"; +import { createMockTransportPair } from "../mock.ts"; +import { Producer as Origin } from "../origin.ts"; +import * as Path from "../path.ts"; +import { Stream, encodeVarint } from "../stream.ts"; +import { Timescale, Timestamp } from "../time.ts"; +import { wireOf } from "../wire.ts"; +import { Connection } from "./connection.ts"; import { ObjectDatagram } from "./datagram.ts"; -import { type IetfVersion, Version } from "./version.ts"; +import { ALPN, type IetfVersion, Version } from "./version.ts"; const ALL: IetfVersion[] = [Version.DRAFT_18, Version.DRAFT_20, Version.DRAFT_22]; @@ -59,3 +66,95 @@ test("rejects invalid types", async () => { } } }); + +/** A server session, so a datagram written by the client is received. */ +async function session() { + const version = Version.DRAFT_22; + const pair = createMockTransportPair(ALPN.DRAFT_22); + const control = await Stream.open(pair.server, { version }); + const connection = new Connection({ + url: new URL("https://example.com"), + quic: pair.server, + control, + version, + client: false, + }); + const writable = pair.client.datagrams.writable; + if (!writable) throw new Error("mock datagrams have no writable"); + return { pair, connection, version, writable }; +} + +test("an undefined object status closes the session", async () => { + const { pair, connection, version, writable } = await session(); + try { + const bytes = new ObjectDatagram({ + trackAlias: 1n, + groupId: 4n, + endOfGroup: false, + body: { status: 1 }, + }).encode(version); + const writer = writable.getWriter(); + await writer.write(bytes); + const closed = await pair.server.closed; + expect(closed.closeCode).toBe(SessionCode.ProtocolViolation); + } finally { + connection.abort(); + } +}); + +test("a defined object status does not close the session", async () => { + const { pair, connection, version, writable } = await session(); + try { + const bytes = new ObjectDatagram({ + trackAlias: 1n, + groupId: 4n, + endOfGroup: false, + body: { status: 0 }, + }).encode(version); + const writer = writable.getWriter(); + await writer.write(bytes); + const open = Symbol("open"); + const result = await Promise.race([pair.server.closed, Bun.sleep(50).then(() => open)]); + expect(result).toBe(open); + } finally { + connection.abort(); + } +}); + +test("a datagram track round-trips over moq-transport", async () => { + const source = new Origin(); + const path = Path.from("live"); + const publication = source.createBroadcast(path); + const datagrams = publication.broadcast.createDatagrams("audio", { + timescale: Timescale.MILLI, + priority: 3, + }); + publication.announce(); + const pair = createMockTransportPair(ALPN.DRAFT_22); + const url = new URL("https://localhost/datagrams"); + const peers = await Promise.all([ + connect({ url, transport: pair.client }), + accept({ url, transport: pair.server, publish: source.consume() }), + ]); + const remote = wireOf(peers[0]).consume(path); + const reader = remote.datagrams("audio"); + reader.setPriority(1); + try { + const info = await reader.info(); + while (datagrams.priority.peek() !== 1) await datagrams.priority.changed(); + expect(info.priority).toBe(3); + expect(info.timescale).toBe(Timescale.MILLI); + datagrams.insert({ sequence: 4n, timestamp: Timestamp.fromMillis(9), payload: new Uint8Array([7]) }); + const received = await reader.recv(); + expect(received?.sequence).toBe(4n); + expect(received?.timestamp?.asMillis()).toBe(9); + expect(received?.payload).toEqual(new Uint8Array([7])); + datagrams.finish(); + expect(await reader.recv()).toBeUndefined(); + } finally { + reader.close(); + await peers[0].close(); + await peers[1].close(); + source.close(); + } +}); diff --git a/js/net/src/ietf/object.ts b/js/net/src/ietf/object.ts index 1abeca2840e..0ccfcee6544 100644 --- a/js/net/src/ietf/object.ts +++ b/js/net/src/ietf/object.ts @@ -14,6 +14,11 @@ const MAX_OBJECT_EXTENSIONS = 64 * 1024; const GROUP_END = 0x03; const END_OF_TRACK = 0x04; +/** Whether `status` is an Object Status the drafts define: Normal, End of Group, or End of Track. */ +export function statusDefined(status: number): boolean { + return status === 0 || status === GROUP_END || status === END_OF_TRACK; +} + // MOQ Object Property ids, shared with draft-ietf-moq-loc-04. An object-scope Timescale // (0x08) is never applied: a track's timeline is its TIMESCALE Track Property alone. const PROP_TIMESTAMP = 0x10n; diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 55034c0c5c3..ef07235e5d8 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -5,6 +5,8 @@ import { Gate } from "../auth_gate.ts"; import type { AuthSession } from "../auth_session.ts"; import { enforceGrant } from "../auth_session.ts"; import type * as broadcast from "../broadcast.ts"; +import * as datagram from "../datagram.ts"; +import * as DatagramStream from "../datagram_stream.ts"; import { Withdrawal } from "../connection/withdrawal.ts"; import { controlTimeout, @@ -38,6 +40,7 @@ import { TimeoutError, withTimeout } from "../util/timeout.ts"; import * as Varint from "../varint.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; import * as Cluster from "./cluster.ts"; +import { encodeDatagram } from "./datagram.ts"; import { requestReason } from "./error.ts"; import { type Fetch, FetchHeader, FetchOk, type FetchType } from "./fetch.ts"; import * as Filter from "./filter.ts"; @@ -498,42 +501,51 @@ export class Publisher { }), ); }; + // Before any await, so a subscriber waiting on this priority is not stuck behind setup. + // A transport without datagrams never asks: the lookup would open an upstream + // subscription nothing here can carry. + const datagramOf = () => { + if (DatagramStream.maxDatagramSize(this.#quic) === 0) return; + const consumer = broadcast.datagrams(msg.trackName); + consumer.setPriority(fromWire(msg.subscriberPriority)); + return consumer; + }; + let datagrams = datagramOf(); let track = subscribe(broadcast); try { - let info: Awaited>; - try { - info = await untilAborted(track.info(), left.signal); - } catch (err) { - if (left.signal.aborted) { - request.abort(cancel); - return; - } - const failed = error(err); - if ( - !(failed instanceof StreamError) || - (failed.code !== StreamCode.NotFound && failed.code !== StreamCode.Unroutable) - ) - throw err; - // The broadcast resolved, so only a vanished route still means a missing publisher; - // a missing track is the publisher's answer. - await request.refuse( - "subscribe", - this.#failed(err, rendezvous && failed.code === StreamCode.Unroutable), - ); + // SUBSCRIBE has no kind bit. Resolve both kinds; an absent or refused kind + // does not prevent the other from answering. + const streams = track.info().then((info) => ({ kind: "stream" as const, info })); + const candidates = [streams]; + const selected = await untilAborted( + datagrams + ? Promise.any([ + ...candidates, + datagrams.info().then((info) => ({ kind: "datagram" as const, info })), + ]) + : streams, + left.signal, + ); + if (selected.kind === "datagram") { + await this.#serveDatagrams(msg, request, datagrams!, selected.info); return; } + const info = selected.info; // Every subscription a resume makes counts toward the same PUBLISH_DONE. const streams: StreamCount = { opened: 0 }; - let paused = await this.#forward({ msg, request, track, info, streams }); + let paused = await this.#forward({ msg, request, track, info, streams, datagrams }); // A pause drops the subscription, and its demand with it; a resume subscribes again as // a new SUBSCRIBE would, so nothing carries across but the request. while (paused) { track.close(); + datagrams?.close(); + datagrams = undefined; if (paused.answer) await Message.write(request.writer, RequestOk.id, new RequestOk(), version); const answer = await this.#pausedSubscribe(msg, request, streams); if (!answer) return; track = subscribe(broadcast); + datagrams = datagramOf(); let resumed: Info; try { resumed = await untilAborted(track.info(), left.signal); @@ -546,11 +558,171 @@ export class Publisher { request.close(); return; } - paused = await this.#forward({ msg, request, track, info: resumed, streams, answer }); + paused = await this.#forward({ msg, request, track, info: resumed, streams, answer, datagrams }); } } finally { track.close(); + datagrams?.close(); + } + } + + /** + * Serve a datagram track on its own SUBSCRIBE: OBJECT_DATAGRAM at object ID 0, then + * PUBLISH_DONE. No END_OF_TRACK object, and no group streams. + */ + async #serveDatagrams( + msg: Subscribe, + request: RequestStream, + datagrams: datagram.Consumer, + info: datagram.Info, + ): Promise { + const version = this.#session.version; + const name = msg.trackNamespace; + const timescale = msg.propertiesWanted ? (info.timescale ?? undefined) : undefined; + const publisher = info.priority; + await Message.write( + request.writer, + SubscribeOk.id, + new SubscribeOk({ + trackAlias: msg.requestId, + properties: msg.propertiesWanted + ? { timescale, priority: toWire(publisher), groupOrder: Properties.DESCENDING } + : {}, + }), + version, + ); + console.debug(`publish ok: broadcast=${name} track=${msg.trackName} datagram`); + + let forward = msg.forward; + let stopped = false; + let subscriberPriority = fromWire(msg.subscriberPriority); + let waiting: Promise | undefined; + let wake = () => {}; + const parked = () => { + waiting ??= new Promise((resolve) => { + wake = () => { + waiting = undefined; + resolve(); + }; + }); + return waiting; + }; + const opened = DatagramStream.datagramWriter(this.#quic); + const sender = opened?.open( + sendOrder({ priority: subscriberPriority, publisher, position: 2 ** 36 - 1 }), + ); + const max = DatagramStream.maxDatagramSize(this.#quic); + const sending = (async () => { + if (!sender) return; + try { + for (;;) { + const datagram = await datagrams.recv(); + if (!datagram || stopped) return; + // A pause holds the datagram already read. Closing the consumer or an update + // that resumes both wake this wait, so the subscription can still end. + while (!forward && !stopped) await parked(); + if (stopped || !forward) return; + if (timescale !== undefined && datagram.timestamp === undefined) { + console.debug(`dropping a datagram without a timestamp: sequence=${datagram.sequence}`); + continue; + } + let body: Uint8Array; + try { + body = await encodeDatagram( + datagram, + { trackAlias: msg.requestId, publisherPriority: toWire(publisher), timescale }, + version, + ); + } catch (err) { + console.debug(`dropping a datagram this draft cannot encode: ${reason(err)}`); + continue; + } + if (body.byteLength > max) { + console.debug( + `dropping datagram larger than the transport limit: size=${body.byteLength} max=${max}`, + ); + continue; + } + await sender.writer.write(body); + } + } finally { + await sender.close(); + opened?.close(); + } + })(); + + let revoke!: () => void; + const revoked = new Promise<"revoked">((resolve) => { + revoke = () => resolve("revoked"); + }); + const watch = this.#gate.watch(name, revoke); + if (watch.revoked()) revoke(); + const updates = (async () => { + for (;;) { + const framed = await request.next(); + if (framed === undefined) { + await request.ended; + return; + } + const { params } = await this.#decodeUpdate(request, framed); + if (params.subscriberPriority !== undefined) { + subscriberPriority = fromWire(params.subscriberPriority); + datagrams.setPriority(subscriberPriority); + sender?.setPriority(sendOrder({ priority: subscriberPriority, publisher, position: 2 ** 36 - 1 })); + } + if (params.forward !== undefined) { + forward = params.forward; + if (forward) wake(); + } + await Message.write(request.writer, RequestOk.id, new RequestOk(), version); + } + })(); + + let unauthorizedEnd = false; + let failed: Error | undefined; + const served = Symbol("served"); + try { + const end = await race([sending.then(() => served), updates, revoked]); + unauthorizedEnd = end === "revoked"; + } catch (err) { + failed = error(err); + if (failed instanceof ProtocolViolation) throw failed; + } finally { + watch.dispose(); + stopped = true; + wake(); + datagrams.close(); + await sending.catch(() => {}); + } + + const updateFailed = failed instanceof UpdateFailed; + try { + await Message.write( + request.writer, + PublishDone.id, + new PublishDone({ + statusCode: unauthorizedEnd + ? PublishDoneStatus.UNAUTHORIZED + : updateFailed + ? PublishDoneStatus.UPDATE_FAILED + : failed + ? PublishDoneStatus.INTERNAL_ERROR + : PublishDoneStatus.TRACK_ENDED, + streamCount: 0n, + reasonPhrase: unauthorizedEnd + ? "not granted" + : updateFailed + ? "update failed" + : failed + ? "internal error" + : "track ended", + }), + version, + ); + } catch { + // The peer may already have closed its half. } + request.close(); } /** @@ -651,6 +823,7 @@ export class Publisher { info, streams = { opened: 0 }, answer, + datagrams, }: { msg: Subscribe; request: RequestStream; @@ -660,6 +833,8 @@ export class Publisher { streams?: StreamCount; /** The update a resumed SUBSCRIBE answers with REQUEST_OK instead of SUBSCRIBE_OK. */ answer?: { requestId: bigint }; + /** A datagram track of the same name, forwarded as OBJECT_DATAGRAM beside the groups. */ + datagrams?: datagram.Consumer; }): Promise { const version = this.#session.version; const name = msg.trackNamespace; @@ -667,6 +842,9 @@ export class Publisher { const order = msg.groupOrder; let cache: TrackSubscriber | undefined; let ranking: Priority | undefined; + // Assigned once forwarding starts. An early pause returns before that, and the finally + // still waits so a task begun on another path cannot outlive the subscription. + let datagramTask: Promise = Promise.resolve(); try { // A Timestamp goes out only when this SUBSCRIBE_OK actually carries TIMESCALE. const timescale = msg.propertiesWanted ? (info.timescale ?? undefined) : undefined; @@ -766,6 +944,17 @@ export class Publisher { } console.debug(`publish ok: broadcast=${name} track=${track.name}`); + // Groups are served below; datagrams ride beside them and must not fail this subscription. + // A burst stays behind the group's send order, so it cannot starve the groups. + datagramTask = datagrams + ? this.#sendDatagrams(datagrams, { + alias: msg.requestId, + propertiesWanted: msg.propertiesWanted, + }).catch((err: unknown) => { + console.debug(`stopping datagrams on this subscription: ${reason(err)}`); + }) + : Promise.resolve(); + // Cancels groups still queued for a stream slot. Only the subscriber leaving counts: // a track that ran out of groups still has to flush the ones already queued, and we // close the stream ourselves below to say so. @@ -828,8 +1017,11 @@ export class Publisher { if (framed === undefined) return request.ended; const { params, requestId: ownRequestId } = await this.#decodeUpdate(request, framed); // update() replaces every option, so carry the rest over as Rust does. - if (params.subscriberPriority !== undefined) - track.update({ ...track.subscription.peek(), priority: fromWire(params.subscriberPriority) }); + if (params.subscriberPriority !== undefined) { + const priority = fromWire(params.subscriberPriority); + track.update({ ...track.subscription.peek(), priority }); + datagrams?.setPriority(priority); + } const maxAge = params.maxAge; if (maxAge !== undefined) { const current = track.subscription.peek(); @@ -1059,6 +1251,8 @@ export class Publisher { } finally { // Groups still draining keep being ranked until they finish. ranking?.close(); + datagrams?.close(); + await datagramTask; track.close(); // Idempotent, and the backstop for a throw between the fork and the fill starting: // #runFill closes it itself on every path it reaches. @@ -1067,6 +1261,68 @@ export class Publisher { return undefined; } + /** + * Forward one datagram track as OBJECT_DATAGRAM beside a stream subscription. + * + * Best-effort: a missing track, an oversized payload, or a datagram the draft cannot + * encode is dropped. The datagram track ending does not end the stream subscription. + */ + async #sendDatagrams( + datagrams: datagram.Consumer, + options: { alias: bigint; propertiesWanted: boolean }, + ): Promise { + let info: datagram.Info; + try { + info = await datagrams.info(); + } catch (err) { + console.debug(`serving the stream track without datagrams: ${reason(err)}`); + return; + } + const opened = DatagramStream.datagramWriter(this.#quic); + if (!opened) return; + const publisher = info.priority; + const timescale = options.propertiesWanted ? (info.timescale ?? undefined) : undefined; + const rank = () => sendOrder({ priority: datagrams.priority, publisher, position: 2 ** 36 - 1 }); + const sender = opened.open(rank()); + const dispose = hooks.datagramPriority(datagrams).subscribe(() => sender.setPriority(rank())); + const version = this.#session.version; + const max = DatagramStream.maxDatagramSize(this.#quic); + try { + for (;;) { + const datagram = await datagrams.recv(); + if (!datagram) return; + if (timescale !== undefined && datagram.timestamp === undefined) { + console.debug(`dropping a datagram without a timestamp: sequence=${datagram.sequence}`); + continue; + } + let body: Uint8Array; + try { + body = await encodeDatagram( + datagram, + { + trackAlias: options.alias, + publisherPriority: toWire(publisher), + timescale, + }, + version, + ); + } catch (err) { + console.debug(`dropping a datagram this draft cannot encode: ${reason(err)}`); + continue; + } + if (body.byteLength > max) { + console.debug(`dropping datagram larger than the transport limit: size=${body.byteLength} max=${max}`); + continue; + } + await sender.writer.write(body); + } + } finally { + dispose(); + await sender.close(); + opened.close(); + } + } + /** * Runs a group and sends its frames using ObjectStream (Subgroup delivery mode). */ diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index a97fbc8d720..79f50589af1 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -4,6 +4,7 @@ import * as announce from "../announced.ts"; import type { Grant } from "../auth.ts"; import type { AuthSession } from "../auth_session.ts"; import * as broadcast from "../broadcast.ts"; +import * as datagram from "../datagram.ts"; import type { Drain } from "../connection/goaway.ts"; import { BroadcastCache } from "../consume.ts"; import * as DatagramStream from "../datagram_stream.ts"; @@ -27,9 +28,9 @@ import { finalSequence, hiddenBelow, hooks, maxDelayOf, scopeCaptures, scopeHead import type { Lister } from "../listing.ts"; import * as Path from "../path.ts"; import { receiveFrame, skipPayload } from "../payload.ts"; -import type { Cursor, Reader, Stream } from "../stream.ts"; +import { Reader, type Cursor, type Stream } from "../stream.ts"; import { Tail } from "../tail.ts"; -import type { Timescale } from "../time.ts"; +import { Timescale, type Timestamp } from "../time.ts"; import type * as track from "../track.ts"; import { untilAborted } from "../util/abort.ts"; import { closeUnwanted } from "../util/announce.ts"; @@ -48,7 +49,15 @@ import { isDraft20 } from "./filter.ts"; import { Join } from "./join.ts"; import * as Message from "./message.ts"; import * as Namespace from "./namespace.ts"; -import { decodeFetchObject, EndOfTrackProperties, Frame, type Group as GroupMessage, ObjectIdGap } from "./object.ts"; +import { + decodeFetchObject, + decodeObjectTime, + EndOfTrackProperties, + Frame, + type Group as GroupMessage, + ObjectIdGap, + statusDefined, +} from "./object.ts"; import { Parameters } from "./parameters.ts"; import { DEFAULT_WIRE, fromWire, toWire } from "./priority.ts"; import { type Publish, PublishDone, PublishDoneStatus, publishDoneClean } from "./publish.ts"; @@ -93,6 +102,19 @@ type Subscription = { fetchIds: bigint[]; }; +/** A stream subscription's alias binding. The object identity is what retirement matches. */ +type TrackBinding = { kind: "track"; subscription: Subscription }; + +/** A datagram subscription's alias binding. Group streams on it are dropped, not written. */ +type DatagramBinding = { + kind: "datagram"; + producer: datagram.Producer; + timescale?: Timescale; + logged: boolean; +}; + +type Binding = TrackBinding | DatagramBinding; + // Out-parameter for #openSubscribe: lets the caller observe partial progress // (stream opened, trackAlias registered) so it can clean up on timeout even // before the setup promise settles. @@ -101,6 +123,8 @@ type SubscribeSetupState = { // The accepted subscription, which a registered alias names. subscription?: Subscription; registeredAlias?: bigint; + /** The alias binding {@link registeredAlias} names, so cleanup retires that same object. */ + binding?: Binding; requests?: Requests; /** * Aborts once the caller has given up. An open still waiting leaves the line, and setup @@ -154,7 +178,7 @@ export class Subscriber { #cluster?: Cluster.Hops; // Publisher-chosen aliases used by incoming group streams. - #aliases = new TrackAliases(); + #aliases = new TrackAliases(); // Units for each track's object Timestamps, from the TIMESCALE Track Property in // SUBSCRIBE_OK. A track missing from this map declared no timeline, so its frames @@ -700,7 +724,7 @@ export class Subscriber { for (;;) { const request = await wireOf(consumer).requestedDatagrams(); if (!request) return; - request.reject(new NotFound("datagram tracks over moq-transport")); + void this.#runDatagram(path, request, instance.validity?.signal); } })(); return this.#consumes.insert(path, instance, consumer); @@ -804,6 +828,7 @@ export class Subscriber { let stream: RequestStream; let subscription: Subscription; + let binding: Binding; let trackAlias: bigint; // The SUBSCRIBER_PRIORITY the request went out with. let priority: number; @@ -817,6 +842,7 @@ export class Subscriber { if (result !== null) { stream = result.stream; subscription = result.subscription; + binding = result.binding; trackAlias = result.alias; priority = result.priority; maxAge = result.maxAge; @@ -851,8 +877,8 @@ export class Subscriber { if ( state.registeredAlias !== undefined && - state.subscription && - this.#aliases.retire(state.registeredAlias, state.subscription) + state.binding && + this.#aliases.retire(state.registeredAlias, state.binding) ) { this.#timescales.delete(state.registeredAlias); } @@ -983,7 +1009,7 @@ export class Subscriber { disposeGrant?.(); // Only the owner tears down the alias metadata: a later subscription may have // reclaimed the alias and installed its own timescale. - if (this.#aliases.retire(trackAlias, subscription)) this.#timescales.delete(trackAlias); + if (this.#aliases.retire(trackAlias, binding)) this.#timescales.delete(trackAlias); } } @@ -1523,6 +1549,7 @@ export class Subscriber { ): Promise<{ stream: RequestStream; subscription: Subscription; + binding: Binding; alias: bigint; priority: number; maxAge?: number; @@ -1605,6 +1632,8 @@ export class Subscriber { if (!known) state.requests = producer.requests(); const tail = new Tail(); const subscription: Subscription = { track: producer, tail, timescale: ok.properties.timescale, fetchIds: [] }; + const binding: Binding = { kind: "track", subscription }; + state.binding = binding; // The subscription starts after Largest, so the joining FETCH or the fill brings its // group's head. With no Largest the track is empty and every group whole. A draft-22 fill // is one of the subscription's own streams, so the Stream Count already waits for it. @@ -1635,7 +1664,7 @@ export class Subscriber { state.fill?.(subscription); try { - this.#aliases.set(ok.trackAlias, subscription, { broadcast, name: request.name }); + this.#aliases.set(ok.trackAlias, binding, { broadcast, name: request.name }); const timescale = ok.properties.timescale; if (timescale !== undefined) { this.#timescales.set(ok.trackAlias, timescale); @@ -1651,6 +1680,7 @@ export class Subscriber { return { stream: state.stream, subscription, + binding, alias: ok.trackAlias, priority: msg.subscriberPriority, maxAge: msg.maxAge, @@ -1820,10 +1850,10 @@ export class Subscriber { throw new Error("subgroups are not supported"); } - let subscription: Subscription; + let binding: Binding; try { // The control message establishing this alias can arrive after the data stream. - subscription = await this.#aliases.get(group.trackAlias); + binding = await this.#aliases.get(group.trackAlias); } catch (err: unknown) { if (err instanceof ProtocolViolation) this.#fail(err.code, err.message); const e = await sessionCause(this.#quic, err); @@ -1837,6 +1867,20 @@ export class Subscriber { return; } + // A datagram subscription keeps only OBJECT_DATAGRAM. A group on it is logged once + // and stopped; the session stays up. + if (binding.kind === "datagram") { + if (!binding.logged) { + binding.logged = true; + console.debug( + `dropping a group stream on a datagram track's subscription: alias=${group.trackAlias} group=${group.groupId}`, + ); + } + stream.stop(new Error("dropping a group stream on a datagram track's subscription")); + return; + } + const subscription = binding.subscription; + const { track, tail } = subscription; // Every data stream counts toward PUBLISH_DONE's Stream Count, even one dropped below. const read = tail.open(group.groupId); @@ -2154,10 +2198,266 @@ export class Subscriber { } } - // Stream subscriptions never admit datagrams; the separate IETF datagram-track adapter - // owns mapping DATAGRAMS demand onto SUBSCRIBE/OBJECT_DATAGRAM. + /** + * Route one OBJECT_DATAGRAM. + * + * An undefined Object Status is a protocol violation on every subscription kind. + * A defined status is not a payload and does not end the track. A datagram subscription + * keeps object ID 0. Anything else is dropped, including every datagram on a stream + * subscription. + */ async #recvDatagram(data: Uint8Array): Promise { - await ObjectDatagram.decode(data, this.#session.version); + const datagram = await ObjectDatagram.decode(data, this.#session.version); + if ("status" in datagram.body && !statusDefined(datagram.body.status)) { + throw new ProtocolViolation(`undefined object status ${datagram.body.status}`); + } + + const binding = this.#aliases.peek(datagram.trackAlias); + if (!binding) { + console.debug(`dropping a datagram for an unknown alias: alias=${datagram.trackAlias}`); + return; + } + if (binding.kind !== "datagram") { + console.debug( + `dropping a datagram on a stream track's subscription: alias=${datagram.trackAlias} group=${datagram.groupId}`, + ); + return; + } + + if ("status" in datagram.body) { + console.debug(`dropping a datagram status: alias=${datagram.trackAlias} group=${datagram.groupId}`); + return; + } + const objectId = datagram.objectId ?? 0n; + if (objectId !== 0n) { + console.debug( + `dropping a datagram with a nonzero object id: alias=${datagram.trackAlias} group=${datagram.groupId} object=${objectId}`, + ); + return; + } + + let timestamp: Timestamp | undefined; + if (datagram.properties) { + try { + // Walk the block either way: a malformed property is a protocol violation on an + // untimed track too. Only a track that declared a timescale keeps the timestamp. + const decoded = await new Reader(undefined, datagram.properties, this.#session.version).decode( + (cursor) => decodeObjectTime(cursor, binding.timescale ?? Timescale.SECOND), + ); + if (binding.timescale !== undefined) timestamp = decoded; + } catch (err) { + const cause = error(err); + if (cause instanceof ProtocolViolation) throw cause; + console.debug(`dropping a datagram whose properties do not decode: ${reason(cause)}`); + return; + } + } + if (binding.timescale !== undefined && timestamp === undefined) { + console.debug( + `dropping a datagram without a timestamp on a timed track: alias=${datagram.trackAlias} group=${datagram.groupId}`, + ); + return; + } + try { + binding.producer.insert({ + sequence: datagram.groupId, + timestamp, + payload: datagram.body.payload, + }); + } catch (err) { + console.debug(`dropping a datagram the track refused: ${reason(err)}`); + } + } + + /** + * Subscribe to one datagram track: its own SUBSCRIBE, live from now, keeping only + * OBJECT_DATAGRAM with object ID 0. + */ + async #runDatagram(broadcast: Path.Valid, request: datagram.Request, valid?: AbortSignal): Promise { + const refused = unauthorized(broadcast); + try { + for (;;) { + const ready = await this.#auth?.ready(broadcast, "subscribe", { + signal: valid, + until: () => Promise.resolve(request.closed), + }); + if (ready !== false) break; + if (request.priority.peek() !== undefined && request.closed.peek() === undefined) continue; + return; + } + } catch (err) { + request.reject(error(err)); + return; + } + if (this.#denied(broadcast)) { + request.reject(refused); + return; + } + if (request.closed.peek() !== undefined) return; + + let requestId: bigint | undefined; + try { + valid?.throwIfAborted(); + requestId = await this.#session.nextRequestId(valid); + } catch (err) { + request.reject(error(err)); + return; + } + if (requestId === undefined) { + request.reject(await this.#closedSession()); + return; + } + + const version = this.#session.version; + let stream: RequestStream | undefined; + let binding: DatagramBinding | undefined; + let alias: bigint | undefined; + try { + stream = await RequestStream.open(this.#session, new Deadline(), { quic: this.#quic, signal: valid }); + const subscriberPriority = toWire(request.priority.peek() ?? 0); + await Message.write( + stream.writer, + Subscribe.id, + new Subscribe({ + requestId, + trackNamespace: broadcast, + trackName: request.name, + subscriberPriority, + groupOrder: "descending", + filter: { kind: "nextObject" }, + propertiesWanted: true, + forward: true, + }), + version, + ); + console.debug(`datagram subscribe written: id=${requestId} broadcast=${broadcast} track=${request.name}`); + + const response = await stream.next(); + if (response === undefined) throw new Error("datagram subscription ended before its response"); + if (response.type === RequestError.id) { + const err = await RequestError.decode(response.reader, version); + request.reject(new Error(requestReason(err.errorCode, err.reasonPhrase, "subscribe", version))); + stream.close(); + return; + } + if (response.type !== SubscribeOk.id) { + throw new ProtocolViolation( + `unexpected message on a datagram subscription: 0x${response.type.toString(16)}`, + ); + } + const ok = await SubscribeOk.decode(response.reader, version); + const producer = request.accept({ + timescale: ok.properties.timescale, + priority: fromWire(ok.properties.priority ?? DEFAULT_WIRE), + }); + binding = { + kind: "datagram", + producer, + timescale: ok.properties.timescale ?? undefined, + logged: false, + }; + alias = ok.trackAlias; + try { + this.#aliases.set(alias, binding, { broadcast, name: request.name }); + } catch (err) { + if (err instanceof DuplicateTrackAlias) this.#fail(SessionCode.ProtocolViolation, err.message); + throw err; + } + if (binding.timescale !== undefined) this.#timescales.set(alias, binding.timescale); + + const unanswered = new Signal(0); + let sent = fromWire(subscriberPriority); + let disposeGrant: Dispose | undefined; + const stop = new AbortController(); + let revoke!: () => void; + const revoked = new Promise<"revoked">((resolve) => { + revoke = () => resolve("revoked"); + }); + disposeGrant = this.#grant?.subscribe(() => { + if (this.#denied(broadcast)) revoke(); + }); + if (this.#denied(broadcast)) revoke(); + + const idle = (async (): Promise<"idle" | "local"> => { + for (;;) { + if (stop.signal.aborted) return "idle"; + const closed = producer.closed.peek(); + if (closed !== undefined) return "local"; + if (producer.priority.peek() === undefined && hooks.closeUnusedDatagrams(producer)) return "idle"; + const next = producer.priority.peek(); + if (next !== undefined && next !== sent && unanswered.peek() === 0) { + const updateId = await this.#session.nextRequestId(valid); + if (updateId === undefined) throw await this.#closedSession(); + const parameters = new Parameters(); + parameters.subscriberPriority = toWire(next); + unanswered.update((count) => count + 1); + try { + await Message.write( + stream.writer, + SubscribeUpdate.id, + new SubscribeUpdate({ requestId: updateId, parameters }), + version, + ); + } catch (err) { + unanswered.update((count) => count - 1); + throw err; + } + sent = next; + continue; + } + await Signal.race(producer.priority, producer.closed); + } + })(); + + const ended = await race([ + this.#readDatagramDone(stream, unanswered, broadcast).then(() => "done" as const), + revoked, + idle, + ]); + stop.abort(); + if (ended === "done") producer.finish(); + else if (ended === "revoked") producer.abort(refused); + else if (!hooks.closeUnusedDatagrams(producer)) producer.abort(new StreamError(StreamCode.Cancel)); + disposeGrant?.(); + if (ended === "done") stream.close(); + else stream.abort(new StreamError(StreamCode.Cancel)); + } catch (err) { + const cause = await sessionCause(this.#quic, err); + if (cause instanceof ProtocolViolation) this.#fail(cause.code, cause.message); + if (binding) binding.producer.abort(cause); + else request.reject(cause); + stream?.abort(cause); + } finally { + if (alias !== undefined && binding && this.#aliases.retire(alias, binding)) this.#timescales.delete(alias); + } + } + + /** Read a datagram subscription's request stream until PUBLISH_DONE. Answers clear `unanswered`. */ + async #readDatagramDone(stream: RequestStream, unanswered: Signal, broadcast: Path.Valid): Promise { + const version = this.#session.version; + let framed: Framed | undefined; + for (;;) { + framed = await stream.next(); + if (framed === undefined) throw new ProtocolViolation("datagram subscription ended without PUBLISH_DONE"); + if (framed.type === PublishDone.id) break; + if ((framed.type === RequestOk.id || framed.type === RequestError.id) && unanswered.peek() > 0) { + if (framed.type === RequestOk.id) await RequestOk.decode(framed.reader, version); + else await RequestError.decode(framed.reader, version); + unanswered.update((count) => count - 1); + continue; + } + throw new ProtocolViolation(`unexpected message on a datagram subscription: 0x${framed.type.toString(16)}`); + } + const done = await PublishDone.decode(framed.reader, version); + if (done.statusCode === PublishDoneStatus.UNAUTHORIZED) throw unauthorized(broadcast); + if (done.statusCode === PublishDoneStatus.MALFORMED_TRACK) { + throw new StreamError(StreamCode.MalformedTrack, { + message: `publish done: malformed track reason=${done.reasonPhrase}`, + }); + } + if (!publishDoneClean(done.statusCode, version)) { + throw new Error(`publish done: status=0x${done.statusCode.toString(16)} reason=${done.reasonPhrase}`); + } } } diff --git a/quest/m1/datagram-tracks/README.md b/quest/m1/datagram-tracks/README.md index e6da124a255..7e9f0206dda 100644 --- a/quest/m1/datagram-tracks/README.md +++ b/quest/m1/datagram-tracks/README.md @@ -120,7 +120,6 @@ datagram track on lite-07, direct and through a relay. ## Required -- [moq-transport datagram tracks](/quest/m1/datagram-tracks/ietf.md) - Rust and JS map a datagram track to SUBSCRIBE and OBJECT_DATAGRAM with object ID 0, keeping the requested kind - [A datagram sent right behind its answer reaches the subscriber](/quest/m1/datagram-tracks/answer-race.md) - receivers decode answers before routing datagrams, and count the ones they still can't route ## Related diff --git a/quest/m1/datagram-tracks/ietf.md b/quest/m1/datagram-tracks/ietf.md deleted file mode 100644 index 1b797ce6182..00000000000 --- a/quest/m1/datagram-tracks/ietf.md +++ /dev/null @@ -1,53 +0,0 @@ -# [S] moq-transport datagram tracks - -## Goal - -Over moq-transport, Rust and JS map a datagram track to IETF SUBSCRIBE and -OBJECT_DATAGRAM, with its sequence as the group ID and object ID 0, and -keep the requested kind when a peer mixes datagrams and streams on one track. - -## Plan - -Decided 2026-11-01 (maintainer), in the -[epic](/quest/m1/datagram-tracks/README.md): - -- A datagram-track subscription keeps only datagrams with object ID 0. - Stream objects arriving on it are dropped and logged once per - subscription. -- A stream-track subscription drops datagrams. -- Outbound, a datagram track sends only OBJECT_DATAGRAM with object ID 0. - A stream track never sends OBJECT_DATAGRAM. -- A datagram with a nonzero object ID on a datagram-track subscription is - dropped and logged. - -Touches `rs/moq-net/src/ietf/datagram.rs` and the JS IETF adapter. Test -against moxygen in the interop lanes where it sends datagrams. - -Rust datagram tracks ([moq-dev/moq#7752](https://github.com/moq-dev/moq/pull/7752)) -left moq-transport without datagrams in Rust until this lands: the IETF -subscriber drops every OBJECT_DATAGRAM, `datagrams()` on an IETF source is -NOT_FOUND, and a stream track never sends one. Restore what it removed: - -- the Rust-JS `datagram` tail lanes in `test/interop/interop.sh`, the - `datagram` mode of `test/interop/clients/js-native/tail.ts`, and their - README bullet; -- moq-e2ee's `datagrams_over_ietf` transport test; -- the moq-transport rows of moq-tokio's `datagram_delivery` bench and - moq-net's `session_delivery_datagram`; -- the OBJECT_DATAGRAM sentence in `doc/concept/standard.md`. - -An OBJECT_DATAGRAM whose Object Status is undefined is a protocol violation -in JS too, as it is in Rust, on every subscription kind. Planned 2026-11-04 -with the maintainer, from -[moq-dev/moq#7846](https://github.com/moq-dev/moq/pull/7846). Rust's -`recv_datagram` (`rs/moq-net/src/ietf/subscriber.rs`, about line 4839) -closes the session with `ProtocolViolation` when `status_defined` -(`rs/moq-net/src/ietf/object.rs`, about line 21: Normal, END_OF_GROUP and -END_OF_TRACK) rejects the status, even while it drops the datagram. JS's -`#recvDatagram` (`js/net/src/ietf/subscriber.ts`) decodes and drops it -silently, on main and after #7846. Check the shared status set once for -both datagram paths, so a datagram-track subscription keeps the check -when it starts accepting datagrams, and add a JS test that an undefined -status closes the session. - -Public API: none beyond the model. Wire: no format change. diff --git a/quest/m2/net-wrap/e2ee.md b/quest/m2/net-wrap/e2ee.md index 35046297d52..e7d6a6171ce 100644 --- a/quest/m2/net-wrap/e2ee.md +++ b/quest/m2/net-wrap/e2ee.md @@ -63,10 +63,8 @@ the reverse (decided 2026-10-20, maintainer): Subscribe ID, Sequence, and Timestamp, since datagram tracks dropped the relayed `ietf` fields and the Inflated Size from it, so `MAX_DATAGRAM_HEADER` (28, `rs/moq-crypto/src/e2ee/limits.rs` and - `js/crypto/src/e2ee/limits.ts`) covers every version. Re-check it if - moq-transport datagram tracks - ([moq-transport datagram tracks](/quest/m1/datagram-tracks/ietf.md)) add - header fields. + `js/crypto/src/e2ee/limits.ts`) covers every version. moq-transport maps a + datagram track onto the existing OBJECT_DATAGRAM and adds no header fields. - Ranges: every moq-net range starts at a group's first frame ([moq-dev/moq#6465](https://github.com/moq-dev/moq/pull/6465)), so the wrapper reads each physical group whole and inflates from frame 0. diff --git a/rs/moq-crypto/tests/transport.rs b/rs/moq-crypto/tests/transport.rs index 1ac5db1ccc6..41bc74fbd21 100644 --- a/rs/moq-crypto/tests/transport.rs +++ b/rs/moq-crypto/tests/transport.rs @@ -1,4 +1,4 @@ -//! Grouped tracks on moq-lite and MoQ Transport, and datagram tracks on moq-lite. +//! Grouped tracks and datagrams on moq-lite and MoQ Transport. mod support; @@ -157,3 +157,8 @@ async fn datagrams_over_lite_05() { async fn max_datagram_over_lite_07() { datagram_roundtrip("moq-lite-07-wip", &[7; moq_crypto::e2ee::MAX_DATAGRAM_PLAINTEXT]).await; } + +#[tokio::test] +async fn datagrams_over_ietf() { + datagram_roundtrip("moq-transport-22", b"opus").await; +} diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 9ee0dae2aa3..4e13897e432 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -4,6 +4,14 @@ How to update your code for each breaking change, newest first. ## Unreleased +### moq-transport datagram tracks + +Datagram tracks now work over moq-transport using SUBSCRIBE and OBJECT_DATAGRAM. +The sequence is the group ID and the object ID is zero. Stream and datagram +consumers keep only their requested kind; nonzero object IDs are dropped by +datagram consumers. No application API or wire encoding changes are required. + + ### Admission completion records its own time `server::Activity::drain()` begins closing admission once its active set empties. diff --git a/rs/moq-net/benches/session/mod.rs b/rs/moq-net/benches/session/mod.rs index 761f544dc61..93a7d41b613 100644 --- a/rs/moq-net/benches/session/mod.rs +++ b/rs/moq-net/benches/session/mod.rs @@ -32,8 +32,8 @@ //! keeps, so a per-group cost that grows with them shows as a slope over `follow`. //! `session_delivery_datagram` writes to a datagram track per broadcast instead of groups: //! one datagram per track per round across publisher and viewer axes, then larger payloads -//! and cadences, on the lite versions (moq-transport carries no datagram tracks). Throughput -//! counts delivered datagrams, each checked for its sequence and payload, so a lost one fails. +//! and cadences, on moq-lite and moq-transport-22. Throughput counts delivered datagrams, +//! each checked for its sequence and payload, so a lost one fails. //! `SESSION_EXPIRY_ONLY=1` uses the standalone 30s-expiry pool and reports groups //! written plus charged cache bytes every 10s during long profiling runs. Each round //! advances the cache clock by a second, so the expiry window spans 30 rounds. @@ -209,9 +209,10 @@ impl Shape { self.viewers * self.watch * self.burst } - /// Whether `version` carries this shape: moq-transport has no datagram tracks. + /// Whether `version` carries this shape. Datagram tracks run on moq-lite and on + /// moq-transport-22, which maps one to OBJECT_DATAGRAM at object ID 0. fn runs_on(&self, version: &str) -> bool { - !self.datagrams || version.parse::().unwrap().is_lite() + !self.datagrams || version.parse::().unwrap().is_lite() || version == "moq-transport-22" } /// The broadcasts viewer `viewer` watches: a contiguous window starting at @@ -1039,9 +1040,10 @@ impl Room { /// Line every viewer up on the same next sequence before the strict rounds. /// /// A consumer is live once its track's info arrives, but on lite-05/06 the SUBSCRIBE - /// that carries the datagrams follows that answer, so a datagram may still miss it. Write - /// until every viewer has read one, then a last one each viewer reads through: the memory - /// transport keeps a subscription's datagrams in order, so nothing earlier arrives after. + /// that carries the datagrams follows that answer, so a datagram may still miss it. + /// moq-transport's SUBSCRIBE is that answer. Write until every viewer has read one, then + /// a last one each viewer reads through: the memory transport keeps a subscription's + /// datagrams in order, so nothing earlier arrives after. async fn sync_datagrams(&mut self) { let waiter = kio::Waiter::noop(); let mut reached = vec![false; self.shape.viewers * self.shape.watch]; diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 10d1d790f23..d63d16c4d55 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -18,7 +18,7 @@ use crate::transport::poll::{GroupOf, SendOptions}; use crate::{ DecodeError, Error, Timescale, Timestamp, - coding::{Encoder, Stream, Writer}, + coding::{Encode as _, Encoder, Stream, Writer}, ietf::{self, Control, EndLocation, FetchHeader, FetchType, Filter, Location, RequestId}, priority::{Priority, PriorityHandle, PriorityQueue}, track::{Order, Subscription}, @@ -1464,15 +1464,58 @@ impl Publisher { Err(err) => return refused(err, false), Ok(None) => return Ok(Subscribed::Withdrawn), }; - let track = match broadcast.track(&msg.track_name) { - Ok(track) => track, - Err(err) => return refused(err, true), + // SUBSCRIBE does not identify the kind. Resolve both kinds and serve the + // first successful answer, preferring streams when both are ready. + let mut along = (self.session.max_datagram_size() > 0) + .then(|| broadcast.datagrams(msg.track_name.as_ref()).ok()).flatten(); + if let Some(datagrams) = &mut along { + datagrams.set_priority(super::priority::from_wire(msg.subscriber_priority)); + } + let track = broadcast.track(&msg.track_name); + let selected = { + let streams = async { + match &track { + Ok(track) => self.subscribe_track(&mut stream, track, &msg, &lapse).await, + Err(err) => Some(Err(err.clone())), + } + }; + let datagrams = async { + match &along { + Some(datagrams) => kio::wait(|waiter| datagrams.poll_info(waiter)).await.map(|_| ()), + None => Err(Error::NotFound), + } + }; + match futures::future::select(Box::pin(streams), Box::pin(datagrams)).await { + futures::future::Either::Left((Some(Err(err)), datagrams)) => { + // Either kind may be absent or refused. Only fail when neither answers. + drop(datagrams); + self.until_withdrawn(&mut stream, super::request_stream::fin_cancels(self.version), |waiter| { + match &along { + Some(datagrams) => datagrams.poll_info(waiter).map(|r| r.map(|_| None).map_err(|_| err.clone())), + None => Poll::Ready(Err(err.clone())), + } + }).await + } + futures::future::Either::Left((result, _)) => result.map(|r| r.map(Some)), + futures::future::Either::Right((Ok(()), _)) => Some(Ok(None)), + futures::future::Either::Right((Err(_), streams)) => streams.await.map(|r| r.map(Some)), + } }; - let (mut track_serve, edge) = match self.subscribe_track(&mut stream, &track, &msg, &lapse).await { - Some(Ok(subscribed)) => subscribed, + let (mut track_serve, edge) = match selected { + Some(Ok(Some(streams))) => streams, + Some(Ok(None)) => { + return self.serve_datagrams( + &mut stream, &msg, along.take().expect("datagram kind resolved"), + &mut request_finished, &mut goaways, &mut updates, &mut acked, + ).await; + } Some(Err(err)) => return refused(err, true), None => return Ok(Subscribed::Withdrawn), }; + let track = track.expect("stream kind resolved"); + if let Some(datagrams) = along { + track_serve = track_serve.with_datagrams(datagrams, msg.properties_wanted); + } let cache = track; // The fill stamps its objects as the subscription does. let timescale = track_serve.timescale; @@ -1615,6 +1658,11 @@ impl Publisher { } }; track_serve.opened = opened.clone(); + if self.session.max_datagram_size() > 0 + && let Ok(datagrams) = broadcast.datagrams(msg.track_name.as_ref()) + { + track_serve = track_serve.with_datagrams(datagrams, msg.properties_wanted); + } save_join(edge.largest, &track, &track_serve.ranking); self.write_request_ok(&mut stream.writer, edge.largest).await?; served = self @@ -1905,6 +1953,188 @@ impl Publisher { .await; } + /// Serve a name that has a datagram track and no stream track. + /// + /// SUBSCRIBE_OK carries the datagram track's properties and no largest location: + /// datagrams are not cached. Each payload is an OBJECT_DATAGRAM with object ID 0. + /// The track's end is PUBLISH_DONE, not an object status. + async fn serve_datagrams( + &self, + stream: &mut Stream, + msg: &ietf::Subscribe<'_>, + mut datagrams: crate::datagram::Consumer, + finished: &mut bool, + goaways: &mut super::request_stream::Messages, + updates: &mut SubscriptionUpdates, + acked: &mut Option>, + ) -> Result { + // Before any await, so a subscriber waiting on this priority is not stuck behind setup. + datagrams.set_priority(super::priority::from_wire(msg.subscriber_priority)); + let info = match self + .until_withdrawn(stream, super::request_stream::fin_cancels(self.version), |waiter| { + datagrams.poll_info(waiter) + }) + .await + { + None => return Ok(Subscribed::Withdrawn), + Some(Ok(info)) => info, + Some(Err(err)) => return Ok(Subscribed::Refused(err.to_string(), err)), + }; + let timescale = info.timescale.filter(|_| msg.properties_wanted); + let publisher = info.priority; + let properties = datagram_properties(&info, msg.properties_wanted); + *acked = Some(Arc::new(AtomicU64::new(0))); + stream + .writer + .encode_message(&ietf::SubscribeOk { + track_alias: msg.request_id.0, + largest: None, + properties, + }) + .await?; + + let order = ietf::GroupOrder::Descending.to_order(); + let mut send_group = self.session.send_group(crate::priority::group( + super::priority::from_wire(msg.subscriber_priority), + publisher, + )); + let mut forward = msg.forward; + let session = self.session.clone(); + let SubscriptionUpdates { grant, renewal } = updates; + + enum Turn { + Datagram(crate::datagram::Datagram), + Ended, + Failed(Error), + Update(super::request_stream::Update), + Cancelled, + Malformed(Error), + Renewal(request_update::Verdict), + } + + loop { + let turn = kio::wait(|waiter| { + if session.poll_closed(waiter).is_ready() || stream.writer.poll_closed(waiter).is_ready() { + return Poll::Ready(Turn::Cancelled); + } + if let Poll::Ready(renewed) = renewal.poll(waiter) { + return Poll::Ready(Turn::Renewal(renewed)); + } + match self.poll_update(stream, finished, goaways, waiter) { + Poll::Ready(UpdateRead::Update(update)) => return Poll::Ready(Turn::Update(update)), + Poll::Ready(UpdateRead::Cancelled) => return Poll::Ready(Turn::Cancelled), + Poll::Ready(UpdateRead::Malformed(err)) => return Poll::Ready(Turn::Malformed(err)), + Poll::Ready(UpdateRead::Failed(err)) => return Poll::Ready(Turn::Failed(err)), + Poll::Pending => {} + } + if forward { + match datagrams.poll_recv(waiter) { + Poll::Ready(Ok(Some(datagram))) => return Poll::Ready(Turn::Datagram(datagram)), + Poll::Ready(Ok(None)) => return Poll::Ready(Turn::Ended), + Poll::Ready(Err(err)) => return Poll::Ready(Turn::Failed(err)), + Poll::Pending => {} + } + } + Poll::Pending + }) + .await; + + match turn { + Turn::Datagram(datagram) => emit_datagram( + &self.session, + self.version, + msg.request_id.0, + order, + send_group.as_ref(), + timescale, + publisher, + &datagram, + ), + Turn::Ended => return Ok(Subscribed::Served(Served::Track(Ok(())), 0)), + Turn::Failed(err) => return Ok(Subscribed::Served(Served::Track(Err(err)), 0)), + // The subscriber left after SUBSCRIBE_OK. The caller still writes PUBLISH_DONE, + // matching a cancelled stream subscription. + Turn::Cancelled => return Ok(Subscribed::Served(Served::Track(Ok(())), 0)), + Turn::Malformed(err) => { + if super::subscriber::is_protocol_violation(&err) { + self.session.clone().close( + super::error::to_session_code(&crate::SessionError::from(&err)), + &err.to_string(), + ); + } + return Ok(Subscribed::Served(Served::Track(Err(err)), 0)); + } + Turn::Renewal(request_update::Verdict { verdict, grant: res }) => { + let rg = grant.as_ref().expect("a renewal without a request grant"); + let Err(refusal) = rg.renew(verdict, res) else { + if let Err(err) = self.write_request_ok(&mut stream.writer, None).await { + return Ok(Subscribed::Served(Served::Track(Err(err)), 0)); + } + continue; + }; + rg.end(refusal.clone()); + return Ok(Subscribed::Served(Served::RenewalRefused(refusal), 0)); + } + Turn::Update(update) if renewal.pending() => match renewal.queue(update, self.version) { + Ok(()) => {} + Err(request_update::Overflow::Session) => { + return Ok(Subscribed::Served( + Served::Track(Err(request_update::too_many(&mut self.session.clone()))), + 0, + )); + } + Err(request_update::Overflow::Request) => { + return Ok(Subscribed::Served(Served::Track(Err(Error::ProtocolViolation)), 0)); + } + }, + Turn::Update(update) => { + // A datagram track has no groups and no stored history. The filter is + // answered; objects it would have excluded were never kept. + let refused = match &update.refused { + Some(err) => Some((err.clone(), "REQUEST_UPDATE parameters not supported")), + None if update.new_group.is_some() => { + Some((Error::Unsupported, "NEW_GROUP_REQUEST on a datagram track")) + } + None if update.fill.is_some() && !Filter::is_draft20(self.version) => { + Some((Error::Unsupported, "REQUEST_UPDATE parameters not supported")) + } + None if update.authorization_token.is_some() + && grant.is_none() + && self.auth.token_decides(crate::auth::Direction::Publish) => + { + Some((Error::Unsupported, TOKEN_ON_SESSION_REQUEST)) + } + None => None, + }; + if let Some((err, reason)) = refused { + self.write_subscribe_error(&mut stream.writer, &err, reason).await?; + return Ok(Subscribed::Served(Served::UpdateFailed(err), 0)); + } + if let Some(priority) = update.priority { + let priority = super::priority::from_wire(priority); + datagrams.set_priority(priority); + if let Some(group) = &mut send_group { + group.set_priority(crate::priority::group(priority, publisher)); + } + } + if let Some(enabled) = update.forward { + forward = enabled; + } + if let (Some(token), Some(rg)) = ( + update + .authorization_token + .filter(|_| self.auth.token_decides(crate::auth::Direction::Publish)), + grant.as_ref(), + ) { + renewal.verify(rg.verify_renewal(&self.auth, token.0)); + continue; + } + self.write_request_ok(&mut stream.writer, None).await?; + } + } + } + } + async fn run_subscription( &self, stream: &mut Stream, @@ -4874,6 +5104,19 @@ struct TrackServe { /// Whether the track advertised DYNAMIC_GROUPS, which an update's NEW_GROUP_REQUEST /// needs. dynamic: bool, + /// The name's datagram track, sent as OBJECT_DATAGRAM beside this subscription's groups. + datagrams: Option, +} + +/// A datagram track forwarded on a subscription that also serves groups. +struct DatagramForward { + consumer: crate::datagram::Consumer, + ready: bool, + ended: bool, + timescale: Option, + publisher: i8, + /// Stamp timestamps only when SUBSCRIBE_OK declared a TIMESCALE. + stamp: bool, } impl TrackServe { @@ -4929,9 +5172,24 @@ impl TrackServe { terminal: Default::default(), budget: kio::coop::Budget::new(32), dynamic, + datagrams: None, } } + /// Also forward `datagrams` as OBJECT_DATAGRAM with object ID 0. + fn with_datagrams(mut self, mut datagrams: crate::datagram::Consumer, stamp: bool) -> Self { + datagrams.set_priority(self.track.subscription().priority); + self.datagrams = Some(DatagramForward { + consumer: datagrams, + ready: false, + ended: false, + timescale: None, + publisher: 0, + stamp, + }); + self + } + fn compression(mut self, negotiated: bool) -> Self { self.compression = negotiated && self.track.info().compress; self @@ -5039,6 +5297,9 @@ impl TrackServe { if let Ok(mut current) = self.priority.write() { *current = priority; } + if let Some(datagrams) = &mut self.datagrams { + datagrams.consumer.set_priority(priority); + } // One move for the whole subscription; its streams keep their place within it. if let Some(group) = &mut self.ranking.group { group.set_priority(crate::priority::group(priority, self.ranking.publisher)); @@ -5067,6 +5328,8 @@ impl TrackServe { self.tail = None; self.opening = None; self.children = kio::Tasks::new(); + // A pause drops the datagram reader with the groups. Resume asks again. + self.datagrams = None; } /// Group machines held: the one waiting for stream credit, and those with a stream. @@ -5159,6 +5422,7 @@ impl TrackServe { let _ = self.terminal.end.set(end); } if self.draining { + self.poll_datagrams(waiter); return self.children.poll(waiter).map(Ok); } @@ -5237,8 +5501,143 @@ impl TrackServe { } // Newly created group machines start now rather than on the next wake. let _ = self.children.poll(waiter); + // Groups first, so a burst of datagrams cannot starve them. + self.poll_datagrams(waiter); Poll::Pending } + + /// Send every buffered datagram as an OBJECT_DATAGRAM with object ID 0. + /// + /// Best-effort: an oversized or unencodable datagram is dropped. The datagram track + /// ending or failing stops this and leaves the stream track alone. + fn poll_datagrams(&mut self, waiter: &kio::Waiter) { + loop { + let datagram = { + let Some(forward) = self.datagrams.as_mut() else { + return; + }; + if forward.ended { + return; + } + if !forward.ready { + match forward.consumer.poll_info(waiter) { + Poll::Pending => return, + Poll::Ready(Ok(info)) => { + forward.timescale = info.timescale.filter(|_| forward.stamp); + forward.publisher = info.priority; + forward.ready = true; + } + Poll::Ready(Err(err)) => { + tracing::debug!(%err, "datagram track ended before it started"); + forward.ended = true; + return; + } + } + } + match forward.consumer.poll_recv(waiter) { + Poll::Pending => return, + Poll::Ready(Ok(None)) => { + forward.ended = true; + return; + } + Poll::Ready(Err(err)) => { + tracing::debug!(%err, "stopping datagrams on this subscription"); + forward.ended = true; + return; + } + Poll::Ready(Ok(Some(datagram))) => { + let timescale = forward.timescale; + let publisher = forward.publisher; + (datagram, timescale, publisher) + } + } + }; + let (datagram, timescale, publisher) = datagram; + emit_datagram( + &self.session, + self.version, + self.request_id.0, + self.ranking.order, + self.ranking.group.as_ref(), + timescale, + publisher, + &datagram, + ); + } + } +} + +/// Send one datagram track payload as OBJECT_DATAGRAM, object ID 0, best-effort. +fn emit_datagram( + session: &Transport, + version: Version, + alias: u64, + order: Order, + group: Option<&GroupOf>, + timescale: Option, + publisher: i8, + datagram: &crate::datagram::Datagram, +) { + let mut properties = Vec::new(); + if let Some(timescale) = timescale { + let Some(timestamp) = datagram.timestamp else { + tracing::debug!(sequence = datagram.sequence, "dropping a datagram without a timestamp"); + return; + }; + if ietf::encode_object_time( + &mut Encoder::new(&mut properties, version.into()), + timestamp, + timescale, + ) + .is_err() + { + tracing::debug!(sequence = datagram.sequence, "dropping a datagram whose timestamp does not encode"); + return; + } + } + let body = ietf::ObjectDatagram { + track_alias: alias, + group_id: datagram.sequence, + object_id: None, + publisher_priority: Some(super::priority::to_wire(publisher)), + end_of_group: true, + properties: (!properties.is_empty()).then_some(properties), + body: ietf::DatagramBody::Payload(datagram.payload.clone()), + }; + let Ok(body) = body.encode_bytes(version) else { + tracing::debug!(sequence = datagram.sequence, "dropping a datagram this draft cannot encode"); + return; + }; + let max = session.max_datagram_size(); + if body.len() > max { + tracing::debug!( + sequence = datagram.sequence, + size = body.len(), + max, + "dropping datagram larger than the transport limit" + ); + return; + } + // Ranked like a stream carrying the group from its start. + let options = SendOptions { + group, + priority: crate::priority::in_group(datagram.sequence, 0, order), + }; + let _ = session.send_datagram(body, options); +} + +/// Track properties for a datagram-only SUBSCRIBE_OK. Nothing is cached, so there is no +/// largest location. The subscriber opted out of properties gets an empty block. +fn datagram_properties(info: &crate::datagram::Info, wanted: bool) -> ietf::Properties { + if !wanted { + return ietf::Properties::default(); + } + ietf::Properties { + timescale: info.timescale, + priority: Some(super::priority::to_wire(info.priority)), + group_order: Some(ietf::GroupOrder::Descending), + ..Default::default() + } } /// Where one request's streams are ranked: the session's queue, keyed by the request's id @@ -7142,7 +7541,7 @@ mod subscribe_cursor_test { #[cfg(test)] mod serve_tests { use super::*; - use crate::coding::{Decode, Decoder, Encode as _}; + use crate::coding::{Decode, Decoder}; use crate::ietf::GroupOrder; use crate::lite::test_transport::{Close, Log, ScriptedSession, Sent, SinkSession}; use crate::model::ProduceTest; @@ -18995,7 +19394,6 @@ mod fill_range_tests { #[cfg(test)] mod compression_tests { use super::*; - use crate::coding::Encode as _; #[test] fn sealed_compression_refuses_incompatible_egress() { for version in [Version::Draft18, Version::Draft20, Version::Draft22] { diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 12b19e6d989..6bc3d0408ef 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -226,9 +226,15 @@ impl State { if let Some(request) = track.pending.take() { request.reject(err.clone()); } + if let Some(request) = track.pending_datagram.take() { + request.reject(err.clone()); + } if let Some(producer) = track.producer { let _ = producer.abort(err.clone()); } + if let Some(producer) = track.datagram.take() { + producer.abort(err.clone()); + } if let Ok(mut fill) = track.fill.write() { fill.abort(err.clone()); } @@ -817,6 +823,16 @@ struct TrackState { // The data streams read so far, which PUBLISH_DONE's Stream Count is checked against. tail: kio::Producer, + + /// The datagram track this subscription asked for, once SUBSCRIBE_OK accepted it. + /// + /// A stream subscription leaves this empty and drops every OBJECT_DATAGRAM. A datagram + /// subscription leaves [`Self::producer`] empty and keeps only object 0. + datagram: Option, + /// The datagram request until SUBSCRIBE_OK accepts it. Abort rejects this. + pending_datagram: Option, + /// Logged once when a group stream arrives on a datagram subscription. + logged_stream: bool, } impl TrackState { @@ -857,6 +873,9 @@ impl TrackState { fetch_id: None, headless_through: None, tail: Default::default(), + datagram: None, + pending_datagram: None, + logged_stream: false, } } } @@ -1113,6 +1132,12 @@ async fn resolve_track_alias( .await } +/// A track or datagram a local reader asked this source for. +enum Asked { + Track(track::Request), + Datagram(crate::datagram::Request), +} + impl Subscriber { #[expect( clippy::too_many_arguments, @@ -1370,6 +1395,16 @@ impl Subscriber { self.state.lock().subscribes.get_mut(&request_id)?.pending.take() } + /// Take the datagram request back out of a subscription that is still setting up. + fn take_pending_datagram(&self, request_id: RequestId) -> Option { + self.state + .lock() + .subscribes + .get_mut(&request_id)? + .pending_datagram + .take() + } + fn remove_subscribe(&self, request_id: RequestId) -> Option { let mut state = self.state.lock(); let track = state.subscribes.remove(&request_id)?; @@ -2795,6 +2830,7 @@ impl Subscriber { route: Option>, ) -> Result<(), Error> { let mut subscribes = TaskSet::owned(); + let mut datagrams = broadcast.datagrams(); let closed_session = self.session.clone(); // Answers the source's listings with the peer's tracks, while anyone lists it. let lister = std::sync::Arc::new( @@ -2812,7 +2848,10 @@ impl Subscriber { self.poll_listing(&path, &lister, &mut listening, waiter); loop { if let Poll::Ready(next) = broadcast.poll_requested_track(waiter) { - return Poll::Ready(Some(next)); + return Poll::Ready(Some(next.map(Asked::Track))); + } + if let Poll::Ready(next) = datagrams.poll_requested(waiter) { + return Poll::Ready(Some(next.map(Asked::Datagram))); } if route.as_ref().is_some_and(|route| route.poll_closed(waiter).is_ready()) { source.close(); @@ -2848,13 +2887,389 @@ impl Subscriber { let broadcast = broadcast.clone(); let route = route.clone(); subscribes.push(async move { - this.run_subscribe(path, broadcast, route, request).await; + match request { + Asked::Track(request) => this.run_subscribe(path, broadcast, route, request).await, + Asked::Datagram(request) => { + drop(broadcast); + this.run_datagram(path, route, request).await; + } + } }); } Ok(()) } + /// Subscribe to one datagram track: its own SUBSCRIBE, live from now, keeping only + /// OBJECT_DATAGRAM with object ID 0. + /// + /// A group stream on this subscription is dropped once and does not end the session. + /// The stream track of the same name is a different subscription. + async fn run_datagram( + &mut self, + broadcast_path: Path, + route: Option>, + request: crate::datagram::Request, + ) { + let setup = Some(self.session.work().start()); + let (auth, path, direction) = ( + self.auth.clone(), + broadcast_path.clone(), + crate::auth::Direction::Subscribe, + ); + let mut gate = match route { + Some(live) => crate::auth::Gate::announced(auth, path, direction, live), + None => crate::auth::Gate::new(auth, path, direction), + }; + loop { + let admitted = kio::wait(|waiter| { + if gate.poll_ready(waiter).is_ready() { + return Poll::Ready(Some(gate.poll_denied(waiter).is_pending())); + } + request.poll_unused(waiter).map(|_| None) + }) + .await; + match admitted { + Some(true) => break, + Some(false) => { + request.reject(Error::Unauthorized); + return; + } + None if request.close_unused() => return, + None => continue, + } + } + drop(setup); + + let track_name = request.name().to_owned(); + let opening = self.session.clone(); + let mut stream = loop { + let mut open = crate::transport::open::Open::default(); + match kio::wait(|waiter| { + if gate.poll_denied(waiter).is_ready() { + return Poll::Ready(Err(Error::Unauthorized)); + } + self.validity.check(waiter)?; + if request.poll_unused(waiter).is_ready() { + return Poll::Ready(Err(Error::Cancel)); + } + Stream::poll_open(&opening, &mut open, self.version, waiter) + }) + .await + { + Ok(stream) => break stream.with_stats(self.origin.stats()), + Err(Error::Cancel) => { + if request.close_unused() { + return; + } + } + Err(err) => { + request.reject(err); + return; + } + } + }; + + let request_id = self.control.next_request_id(); + let mut messages = super::request_stream::Messages::new(self.session.clone(), self.control.client()); + let priority = request.priority().unwrap_or(0); + { + let mut state = self.state.lock(); + state.subscribes.insert( + request_id, + TrackState { + pending_datagram: Some(request), + ..TrackState::pending( + track_name.clone(), + broadcast_path.clone(), + kio::Producer::new(Fill::Done), + ) + }, + ); + } + + let written = stream.writer.buffer_message( + ietf::Subscribe::ID, + &ietf::Subscribe { + max_age: None, + new_group: None, + request_id, + track_namespace: broadcast_path.clone(), + track_name: track_name.as_str().into(), + subscriber_priority: super::priority::to_wire(priority), + group_order: GroupOrder::Descending, + filter: Filter::NextObject, + fill: None, + properties_wanted: true, + forward: true, + rendezvous: false, + range_filters: false, + authorization_token: None, + }, + ); + if let Err(err) = written { + tracing::debug!(%err, "failed to encode datagram subscribe"); + if let Some(request) = self.take_pending_datagram(request_id) { + request.reject(err); + } + self.remove_subscribe(request_id); + return; + } + + tracing::debug!(broadcast = %crate::path::Rooted(self.origin.root(), &broadcast_path), track = %track_name, "datagram subscribe started"); + + enum Setup { + Response(Result), + Unused, + Gone, + Revoked, + } + + // The answer is owned before this pin drops, so the stream is free to cancel or to + // read PUBLISH_DONE. A pin that lived for the rest of the function would hold both. + enum Answer { + Ok(ietf::SubscribeOk), + /// The peer still holds the SUBSCRIBE. + Cancel(Error), + /// The publisher already refused it, so there is nothing to cancel. + Refused, + /// The session or the local request already ended the subscription. + Gone, + } + + let answer = { + let mut response = std::pin::pin!(async { + kio::wait(|waiter| stream.writer.poll_flush(waiter)).await?; + messages.message(&mut stream.reader).await + }); + let setup = loop { + let setup = kio::wait(|waiter| { + if let Err(err) = self.validity.check(waiter) { + return Poll::Ready(Setup::Response(Err(err))); + } + if gate.poll_denied(waiter).is_ready() { + return Poll::Ready(Setup::Revoked); + } + if let Poll::Ready(res) = waiter.poll_future(response.as_mut()) { + return Poll::Ready(Setup::Response(res)); + } + let mut state = self.state.lock(); + let Some(pending) = state + .subscribes + .get_mut(&request_id) + .and_then(|held| held.pending_datagram.as_mut()) + else { + return Poll::Ready(Setup::Gone); + }; + if pending.poll_unused(waiter).is_ready() { + return Poll::Ready(Setup::Unused); + } + Poll::Pending + }) + .await; + match setup { + Setup::Unused => { + let mut state = self.state.lock(); + let Some(pending) = state + .subscribes + .get_mut(&request_id) + .and_then(|held| held.pending_datagram.as_mut()) + else { + break Setup::Gone; + }; + if pending.close_unused() { + break Setup::Unused; + } + } + other => break other, + } + }; + + match setup { + Setup::Gone => Answer::Gone, + Setup::Unused => Answer::Cancel(Error::Cancel), + Setup::Revoked => Answer::Cancel(Error::Unauthorized), + Setup::Response(Err(err)) => { + tracing::debug!(%err, "datagram subscribe response error"); + Answer::Cancel(err) + } + Setup::Response(Ok(super::request_stream::Framed(type_id, body))) + if type_id == ietf::RequestError::ID => + { + let mut data = body.decoder(self.version); + let err = match ietf::RequestError::decode_msg(&mut data, self.version) { + Ok(msg) => request::from_code(msg.error_code, request::Kind::Subscribe, self.version), + Err(err) => Error::Decode(err), + }; + if let Some(request) = self.take_pending_datagram(request_id) { + request.reject(err); + } + self.remove_subscribe(request_id); + Answer::Refused + } + Setup::Response(Ok(super::request_stream::Framed(type_id, body))) + if type_id == ietf::SubscribeOk::ID => + { + match body.message::(self.version) { + Ok(ok) => Answer::Ok(ok), + Err(err) => Answer::Cancel(Error::Decode(err)), + } + } + Setup::Response(Ok(_)) => Answer::Cancel(Error::UnexpectedMessage), + } + }; + + let ok = match answer { + Answer::Gone | Answer::Refused => return, + Answer::Cancel(err) => { + if let Some(request) = self.take_pending_datagram(request_id) { + request.reject(err); + } + self.remove_subscribe(request_id); + self.cancel_subscribe(stream).await; + return; + } + Answer::Ok(ok) => ok, + }; + + if let Some(err) = refused(&ok.properties, self.version, self.peer_setup.get().await.flate) { + if let Some(request) = self.take_pending_datagram(request_id) { + request.reject(err); + } + self.remove_subscribe(request_id); + self.cancel_subscribe(stream).await; + return; + } + + let info = crate::datagram::Info { + timescale: ok.properties.timescale, + priority: super::priority::from_wire( + ok.properties.priority.unwrap_or(super::priority::DEFAULT_WIRE), + ), + }; + let Some(request) = self.take_pending_datagram(request_id) else { + self.remove_subscribe(request_id); + self.cancel_subscribe(stream).await; + return; + }; + let producer = request.accept(info); + { + let mut state = self.state.lock(); + let Some(held) = state.subscribes.get_mut(&request_id) else { + producer.abort(Error::Cancel); + return; + }; + held.datagram = Some(producer.clone()); + held.timescale = info.timescale; + } + if let Err(err) = self.register_alias(request_id, ok.track_alias) { + if matches!(err, Error::Duplicate) { + tracing::warn!(track_alias = %ok.track_alias, "publisher reused a live track alias for another track"); + self.session.close( + super::error::to_session_code(&SessionError::from(&err)), + err.to_string().as_ref(), + ); + } else { + tracing::warn!(track_alias = %ok.track_alias, %err, "could not bind track alias"); + self.cancel_subscribe(stream).await; + } + self.remove_subscribe(request_id); + producer.abort(err); + return; + } + + enum End { + Idle, + Revoked, + Done(Result<(), Error>), + Update(i8), + Failed(Error), + } + + let mut sent = priority; + let unanswered = AtomicU64::new(0); + // The pin borrows the reader until this block ends. Cancelling afterwards moves the stream. + let end = { + let mut done = std::pin::pin!(Self::read_publish_done( + &mut stream.reader, + self.version, + &unanswered, + &mut messages, + |_| {} + )); + loop { + let end = kio::wait(|waiter| { + if gate.poll_denied(waiter).is_ready() { + return Poll::Ready(End::Revoked); + } + if let Err(err) = self.validity.check(waiter) { + return Poll::Ready(End::Failed(err)); + } + if let Poll::Ready(Err(err)) = stream.writer.poll_flush(waiter) { + return Poll::Ready(End::Failed(err)); + } + if producer.poll_unused(waiter).is_ready() { + return Poll::Ready(End::Idle); + } + if let Poll::Ready(res) = waiter.poll_future(done.as_mut()) { + return Poll::Ready(End::Done(res.map(|_| ()))); + } + // One update in flight: its answer shares the stream with PUBLISH_DONE. + if unanswered.load(Ordering::Relaxed) == 0 + && let Poll::Ready(Some(next)) = producer.poll_priority(waiter, Some(sent)) + && next != sent + { + return Poll::Ready(End::Update(next)); + } + Poll::Pending + }) + .await; + match end { + End::Update(next) => { + let update_id = self.control.next_request_id(); + if let Err(err) = stream.writer.buffer_message( + ietf::SubscribeUpdate::ID, + &ietf::SubscribeUpdate { + max_age: None, + request_id: update_id, + subscriber_priority: Some(super::priority::to_wire(next)), + forward: None, + filter: None, + authorization_token: None, + new_group: None, + fill: None, + }, + ) { + break End::Failed(err); + } + sent = next; + unanswered.fetch_add(1, Ordering::Relaxed); + } + other => break other, + } + } + }; + + let clean = matches!(end, End::Done(Ok(()))); + match end { + End::Done(Ok(())) => producer.finish(), + End::Idle => { + if !producer.close_unused() { + // A consumer arrived as this one left. The track is still wanted, but this + // task already decided to leave; ending it lets the reader ask again. + producer.abort(Error::Cancel); + } + } + End::Revoked => producer.abort(Error::Unauthorized), + End::Done(Err(err)) | End::Failed(err) => producer.abort(err), + End::Update(_) => producer.abort(Error::Cancel), + } + self.remove_subscribe(request_id); + if !clean { + self.cancel_subscribe(stream).await; + } + } + /// Answer the source's listings while anyone lists it, from a SUBSCRIBE_TRACKS covering /// it; a failed listing ends its readers, which ask again. fn poll_listing( @@ -4692,6 +5107,26 @@ impl Subscriber { } }; + { + let mut state = self.state.lock(); + let Some(track) = state.subscribes.get_mut(&request_id) else { + return Err(Error::NotFound); + }; + // A datagram subscription keeps datagrams only. One group stream is logged + // per subscription; the session stays up. + if track.datagram.is_some() && track.producer.is_none() { + if !track.logged_stream { + track.logged_stream = true; + tracing::debug!( + track_alias = %group.track_alias, + group = group.group_id, + "dropping a group stream on a datagram track's subscription" + ); + } + return Err(Error::Unsupported); + } + } + let (mut track, timescale, fill, headless, mut reading) = { let state = self.state.lock(); let track = state.subscribes.get(&request_id).ok_or(Error::NotFound)?; @@ -4844,21 +5279,92 @@ impl Subscriber { Ok(()) } - /// Drop an OBJECT_DATAGRAM: a subscription is a stream track's, which takes no datagrams. + /// Route an OBJECT_DATAGRAM. /// - /// A malformed datagram is still a protocol error. + /// An undefined Object Status is a protocol violation on every subscription kind. + /// A defined status is not a payload. A datagram subscription keeps object ID 0. + /// Anything else is dropped, including every datagram on a stream subscription. pub fn recv_datagram(&self, payload: bytes::Bytes) -> Result<(), Error> { let (datagram, _) = ietf::ObjectDatagram::decode_slice(&payload, self.version)?; - if let ietf::DatagramBody::Status(status) = datagram.body - && !super::object::status_defined(status) + if let ietf::DatagramBody::Status(status) = &datagram.body + && !super::object::status_defined(*status) { return Err(Error::Session(SessionError::ProtocolViolation)); } - tracing::debug!( - alias = datagram.track_alias, - group = datagram.group_id, - "dropping a datagram on a stream track's subscription" - ); + + let (mut producer, timescale) = { + let state = self.state.lock(); + let aliases = state.aliases.read(); + let Some(request_id) = aliases.map.get(&datagram.track_alias).and_then(|alias| alias.holder()) else { + tracing::debug!(alias = datagram.track_alias, "dropping a datagram for an unknown alias"); + return Ok(()); + }; + let Some(track) = state.subscribes.get(&request_id) else { + return Ok(()); + }; + match &track.datagram { + Some(producer) => (producer.clone(), track.timescale), + None => { + tracing::debug!( + alias = datagram.track_alias, + group = datagram.group_id, + "dropping a datagram on a stream track's subscription" + ); + return Ok(()); + } + } + }; + + // A defined status names no payload, and it does not end the datagram track. + // PUBLISH_DONE is that end. + let ietf::DatagramBody::Payload(payload) = datagram.body else { + tracing::debug!( + alias = datagram.track_alias, + group = datagram.group_id, + "dropping a datagram status" + ); + return Ok(()); + }; + let object_id = datagram.object_id.unwrap_or(0); + if object_id != 0 { + tracing::debug!( + alias = datagram.track_alias, + group = datagram.group_id, + object_id, + "dropping a datagram with a nonzero object id" + ); + return Ok(()); + } + + let timestamp = match &datagram.properties { + Some(raw) => { + let bytes = bytes::Bytes::copy_from_slice(raw); + match super::object::read(&bytes, self.version, timescale) { + Ok(decoded) => decoded.timestamp, + Err(err) if is_protocol_violation(&err) => return Err(err), + Err(err) => { + tracing::debug!(%err, "dropping a datagram whose properties do not decode"); + return Ok(()); + } + } + } + None => None, + }; + if timescale.is_some() && timestamp.is_none() { + tracing::debug!( + alias = datagram.track_alias, + group = datagram.group_id, + "dropping a datagram without a timestamp on a timed track" + ); + return Ok(()); + } + if let Err(err) = producer.insert(crate::datagram::Datagram { + sequence: datagram.group_id, + timestamp, + payload, + }) { + tracing::debug!(%err, "dropping a datagram the track refused"); + } Ok(()) } } @@ -7397,6 +7903,93 @@ mod tests { assert!(is_protocol_violation(&subscriber.recv_datagram(malformed).unwrap_err())); } + /// A datagram subscription keeps object 0 and drops everything else. An undefined + /// status still closes the session, including on this subscription. + #[kio::test] + async fn a_datagram_subscription_keeps_object_zero() { + use crate::coding::Encode as _; + + let info = crate::datagram::Info::default().with_timescale(Timescale::MILLI); + let producer = info.produce("audio"); + let mut consumer = producer.consume(); + let subscriber = subscriber_with_tracks(&[(RequestId(11), "cam", "audio")]); + subscriber.register_alias(RequestId(11), 7).unwrap(); + { + let mut state = subscriber.state.lock(); + let track = state.subscribes.get_mut(&RequestId(11)).unwrap(); + // This subscription asked for datagrams, not the stream track. + track.producer = None; + track.datagram = Some(producer); + track.timescale = Some(Timescale::MILLI); + } + + let mut properties = Vec::new(); + ietf::encode_object_time( + &mut crate::coding::Encoder::new(&mut properties, Version::Draft18.into()), + crate::Timestamp::from_millis(9).unwrap(), + Timescale::MILLI, + ) + .unwrap(); + let encode = |object_id, body| { + ietf::ObjectDatagram { + track_alias: 7, + group_id: 4, + object_id, + publisher_priority: None, + end_of_group: !matches!(body, ietf::DatagramBody::Status(_)), + properties: Some(properties.clone()), + body, + } + .encode_bytes(Version::Draft18) + .unwrap() + }; + + subscriber + .recv_datagram(encode(None, ietf::DatagramBody::Payload(bytes::Bytes::from_static(b"d")))) + .unwrap(); + match consumer.poll_recv(&kio::Waiter::noop()) { + Poll::Ready(Ok(Some(datagram))) => { + assert_eq!(datagram.sequence, 4); + assert_eq!(datagram.payload.as_ref(), b"d"); + } + other => panic!("object 0 was not delivered: {other:?}"), + } + + subscriber + .recv_datagram(encode( + Some(3), + ietf::DatagramBody::Payload(bytes::Bytes::from_static(b"x")), + )) + .unwrap(); + assert!( + matches!(consumer.poll_recv(&kio::Waiter::noop()), Poll::Pending), + "a nonzero object id must be dropped" + ); + + // Status 0 is defined, so it is not a payload and not a violation. + subscriber + .recv_datagram(encode(None, ietf::DatagramBody::Status(0))) + .unwrap(); + assert!(matches!(consumer.poll_recv(&kio::Waiter::noop()), Poll::Pending)); + + // Status 1 is undefined on every subscription kind. + let undefined = ietf::ObjectDatagram { + track_alias: 7, + group_id: 4, + object_id: None, + publisher_priority: None, + end_of_group: false, + properties: None, + body: ietf::DatagramBody::Status(1), + } + .encode_bytes(Version::Draft18) + .unwrap(); + assert!(matches!( + subscriber.recv_datagram(undefined), + Err(Error::Session(SessionError::ProtocolViolation)) + )); + } + /// One alias naming two different tracks is the collision section 11.1 makes fatal. #[kio::test] async fn an_alias_reused_for_another_track_is_fatal() { diff --git a/rs/moq-net/src/model/datagram.rs b/rs/moq-net/src/model/datagram.rs index e5f53496471..46add01ef5f 100644 --- a/rs/moq-net/src/model/datagram.rs +++ b/rs/moq-net/src/model/datagram.rs @@ -13,8 +13,9 @@ //! //! Delivery is best-effort per hop: a session drops (with a debug log) any datagram whose //! encoded body exceeds the transport's datagram size, and sessions that can't carry -//! datagrams at all (moq-lite before 05, moq-transport for now, or stream-only transports -//! like WebSocket) never deliver them. +//! datagrams at all (moq-lite before 05, or stream-only transports like WebSocket) never +//! deliver them. Moq-transport maps a datagram track onto SUBSCRIBE and OBJECT_DATAGRAM, +//! with the sequence as the group ID and object ID 0. //! //! Moq-lite 07 asks for a datagram track on a request of its own. Moq-lite 05 and 06 carry a //! name's groups and datagrams on one subscription, as moq-transport does: a session serves diff --git a/rs/moq-tokio/tests/integration/datagram_delivery.rs b/rs/moq-tokio/tests/integration/datagram_delivery.rs index 99e93d2b5f8..7673bb68fa4 100644 --- a/rs/moq-tokio/tests/integration/datagram_delivery.rs +++ b/rs/moq-tokio/tests/integration/datagram_delivery.rs @@ -320,8 +320,7 @@ async fn benchmark_datagram_delivery() { println!( "version,cell,viewer,offered,lost_packets,overflowed_packets,undelivered_frame_packets,reordered_packets,p50_ms,p99_ms,max_ms" ); - // moq-transport has no datagram tracks. - for version in ["moq-lite-06", "moq-lite-07-wip"] { + for version in ["moq-lite-06", "moq-lite-07-wip", "moq-transport-22"] { for cell in cells { measure(version, cell).await; } diff --git a/test/interop/README.md b/test/interop/README.md index ced348ff8e0..d13d808867a 100644 --- a/test/interop/README.md +++ b/test/interop/README.md @@ -225,6 +225,11 @@ one cell. The tails check their own connections, so a fallback fails the lane. The in-tree control lanes also run with `--tail`: +- Four untimed 900-byte datagrams cross the relay in both directions between Rust + and each native JS runtime. The subscriber acknowledges resolved metadata before + sending begins, then acknowledges each packet, so this checks framing and bytes + rather than best-effort burst capacity. The publisher stays open until the read is + acknowledged, keeping a reliable track end from overtaking the datagrams. - A JS wire requester gets the Rust publisher's exact largest location through TRACK\_STATUS on every draft. Rust also queries the JS publisher's non-default priority before subscribing, when the answer can describe a track. @@ -238,7 +243,7 @@ The in-tree control lanes also run with `--tail`: The standalone control endpoints use OS-assigned ports and production QMux, SETUP codecs, and request sessions. They verify their negotiated version directly. -The query lanes through moq-relay use their usual version-log check. +The datagram and query lanes through moq-relay use their usual version-log check. Some cases change shape on moq-transport: diff --git a/test/interop/clients/js-native/tail.ts b/test/interop/clients/js-native/tail.ts index e0ecb8c92b6..ac82019b813 100644 --- a/test/interop/clients/js-native/tail.ts +++ b/test/interop/clients/js-native/tail.ts @@ -6,9 +6,11 @@ * it must read a contiguous run of groups through the last one. `compress` publishes a compressed * track with several frames, one empty, in each group. `cached` publishes every group before * announcing, for a FETCH of a past group. `history` writes only the last group live and serves the - * ones below it, never cached, from an in-memory responder on `track.requests()`. `late` writes the head of group 0 before announcing and - * the rest only once the harness relays that the subscriber read it, so a join that skips the head - * stalls rather than racing the publisher. + * ones below it, never cached, from an in-memory responder on `track.requests()`. `datagram` publishes + * an untimed datagram track and sends each 900-byte datagram only after the harness says the subscriber + * read the last one. `late` writes the head of group 0 before announcing and the rest only once the + * harness relays that the subscriber read it, so a join that skips the head stalls rather than racing + * the publisher. * * @module */ @@ -30,6 +32,7 @@ const compress = mode === "compress"; console.debug = console.error; console.info = console.error; const join = mode === "join" || mode === "status"; +const datagram = mode === "datagram"; const status = mode === "status"; const cached = mode === "cached"; const history = mode === "history"; @@ -39,10 +42,10 @@ if ( !url || !path || !["publish", "subscribe"].includes(role) || - ![undefined, "drop", "join", "compress", "status", "cached", "history", "late", "fetch"].includes(mode) + ![undefined, "drop", "join", "compress", "datagram", "status", "cached", "history", "late", "fetch"].includes(mode) ) { throw new Error( - "usage: tail.ts publish|subscribe URL BROADCAST [VERSION [drop|join|compress|status|cached|history|late|fetch]]", + "usage: tail.ts publish|subscribe URL BROADCAST [VERSION [drop|join|compress|datagram|status|cached|history|late|fetch]]", ); } @@ -119,6 +122,27 @@ try { if (role === "publish") { const broadcastPublication = origin.createBroadcast(Moq.Path.from(path)); const broadcast = broadcastPublication.broadcast; + if (datagram) { + // Untimed: the OBJECT_DATAGRAM carries no timestamp, so the subscriber checks the payload alone. + const track = broadcast.createDatagrams("tail"); + broadcastPublication.announce(); + const lines = createInterface({ input: process.stdin }); + const commands = lines[Symbol.asyncIterator](); + try { + while (track.priority.peek() === undefined) await track.priority.changed(); + for (let sequence = 0; sequence < GROUPS; sequence++) { + assert.equal((await commands.next()).value, `datagram ${sequence}`, "missing datagram command"); + track.append(undefined, new Uint8Array(900).fill(sequence)); + } + const ack = await commands.next(); + assert.equal(ack.value, "clean end", "reader did not acknowledge a clean end"); + } finally { + lines.close(); + } + track.finish(); + broadcast.close(); + console.error("tail acknowledged"); + } else { // `cached` and `late` read groups a newer one supersedes, which the default zero max age // would leave stale. const track = broadcast.createTrack("tail", { compress, maxAge: null, ...(status ? { priority: 2 } : {}) }); @@ -157,6 +181,38 @@ try { } broadcast.close(); console.error("tail acknowledged"); + } + } else if (datagram) { + const request = origin.follow(Moq.Path.from(path)); + try { + let broadcast = request.active.peek(); + while (!broadcast) { + await request.active.changed(); + broadcast = request.active.peek(); + } + const track = broadcast.datagrams("tail"); + try { + await track.info(); + process.stdout.write("datagram ready\n"); + for (let sequence = 0; sequence < GROUPS; sequence++) { + const frame = await track.recv(); + assert(frame, "missing datagram"); + assert.equal(frame.sequence, BigInt(sequence), "wrong datagram sequence"); + assert.equal(frame.payload.byteLength, 900, "truncated datagram"); + assert( + frame.payload.every((byte) => byte === sequence), + "corrupt datagram", + ); + assert.equal(frame.timestamp, undefined, "unexpected IETF timestamp"); + process.stdout.write(`datagram ${sequence}\n`); + } + console.error("datagram clean groups=0,1,2,3 bytes=3600"); + } finally { + track.close(); + } + } finally { + request.close(); + } } else { const request = origin.follow(Moq.Path.from(path)); try { diff --git a/test/interop/clients/rust/tail.rs b/test/interop/clients/rust/tail.rs index b9f6aa2115d..9cd083179a7 100644 --- a/test/interop/clients/rust/tail.rs +++ b/test/interop/clients/rust/tail.rs @@ -5,9 +5,11 @@ //! so it must read a contiguous run of groups through the last one. `cached` publishes every //! group before connecting, so a FETCH of a past group never races its write: moq-transport //! refuses a FETCH that starts past the largest group rather than waiting for it. `compress` -//! publishes a compressed track with several frames, one empty, in each group. `late` writes -//! the head of group 0 before connecting and the rest only once the harness relays that the -//! subscriber read it, so a join that skips the head stalls rather than racing the publisher. +//! publishes a compressed track with several frames, one empty, in each group. `datagram` +//! publishes an untimed datagram track and sends each 900-byte datagram only after the harness +//! says the subscriber read the last one. `late` writes the head of group 0 before connecting +//! and the rest only once the harness relays that the subscriber read it, so a join that skips +//! the head stalls rather than racing the publisher. use std::time::Duration; use anyhow::{Context, ensure}; @@ -76,14 +78,14 @@ async fn main() -> anyhow::Result<()> { let args: Vec<_> = std::env::args().collect(); ensure!( (4..=6).contains(&args.len()), - "usage: interop-tail publish|subscribe URL BROADCAST [VERSION [drop|join|cached|compress|status|late]]" + "usage: interop-tail publish|subscribe URL BROADCAST [VERSION [drop|join|cached|compress|datagram|status|late]]" ); let url: url::Url = args[2].parse()?; let mode = args.get(5).map(String::as_str); ensure!( matches!( mode, - None | Some("drop" | "join" | "cached" | "compress" | "status" | "late") + None | Some("drop" | "join" | "cached" | "compress" | "datagram" | "status" | "late") ), "unknown mode: {mode:?}" ); @@ -91,6 +93,7 @@ async fn main() -> anyhow::Result<()> { let join = matches!(mode, Some("join" | "status")); let cached = mode == Some("cached"); let compress = mode == Some("compress"); + let datagram = mode == Some("datagram"); let status = mode == Some("status"); let late = mode == Some("late"); let mut commands = tokio::io::BufReader::new(tokio::io::stdin()).lines(); @@ -106,6 +109,32 @@ async fn main() -> anyhow::Result<()> { match args[1].as_str() { "publish" => { let broadcast = origin.create_broadcast(&args[3])?; + if datagram { + // Untimed: the OBJECT_DATAGRAM carries no timestamp, so the subscriber can + // check the payload alone. Stay open until each one is acknowledged. + let mut track = broadcast.create_datagrams("tail", moq_net::datagram::Info::default())?; + broadcast.announce(Default::default())?; + let session = client.clone().with_publisher(&origin).connect(url).established().await?; + let mut priority = track.priority(); + while priority.is_none() { + priority = kio::wait(|waiter| track.poll_priority(waiter, priority)).await; + } + for sequence in 0..GROUPS { + ensure!( + commands.next_line().await?.as_deref() == Some(&format!("datagram {sequence}")), + "missing datagram command" + ); + track.append(None, vec![sequence as u8; 900])?; + } + ensure!( + commands.next_line().await?.as_deref() == Some("clean end"), + "reader did not acknowledge a clean end" + ); + track.finish(); + broadcast.close(); + session.close().await?; + eprintln!("tail acknowledged"); + } else { let mut track = broadcast.create_track( "tail", moq_net::track::Info::default() @@ -164,6 +193,7 @@ async fn main() -> anyhow::Result<()> { broadcast.close(); session.close().await?; eprintln!("tail acknowledged"); + } } "subscribe" => { let consumer = origin.consume(); @@ -178,6 +208,26 @@ async fn main() -> anyhow::Result<()> { .available() .await? .context("went offline before serving")?; + if datagram { + let mut track = broadcast.datagrams("tail")?; + track.info().await?; + println!("datagram ready"); + for sequence in 0..GROUPS { + let frame = track.recv().await?.context("missing datagram")?; + ensure!(frame.sequence == sequence, "wrong datagram sequence"); + ensure!(frame.payload.len() == 900, "truncated datagram"); + ensure!( + frame.payload.iter().all(|byte| *byte == sequence as u8), + "corrupt datagram" + ); + ensure!(frame.timestamp.is_none(), "unexpected IETF timestamp"); + println!("datagram {sequence}"); + } + eprintln!("datagram clean groups=0,1,2,3 bytes=3600"); + session.close().await?; + client.close().await; + return Ok(()); + } let remote = broadcast.track("tail")?; if status { // Query before subscribing so draft-18+ must use TRACK_STATUS, diff --git a/test/interop/interop.sh b/test/interop/interop.sh index 6ca445e9897..90b0d447f83 100755 --- a/test/interop/interop.sh +++ b/test/interop/interop.sh @@ -518,6 +518,8 @@ plan_pass() { record "not run" case=tail publisher=rust subscriber="$runtime" record "not run" case=tail publisher="$runtime" subscriber=rust if [[ "$VERSION" == moq-transport-* ]]; then + record "not run" case=datagram publisher=rust subscriber="$runtime" + record "not run" case=datagram publisher="$runtime" subscriber=rust record "not run" case="wire track status" publisher=rust subscriber="$runtime" record "not run" case=goaway publisher=rust subscriber="$runtime" [[ "$runtime" != js-native-bun ]] || record "not run" case=goaway publisher="$runtime" subscriber=rust @@ -1940,7 +1942,10 @@ run_tail_pair() { mode="join" want='tail join clean end=4 groups=[0-3](,[0-3])*' fi - if [[ "$kind" == "track status" ]]; then + if [[ "$kind" == datagram ]]; then + mode=datagram + want='datagram clean groups=0,1,2,3 bytes=3600' + elif [[ "$kind" == "track status" ]]; then mode=status want="track status answered" elif [[ "$kind" == compress ]]; then @@ -1961,8 +1966,26 @@ run_tail_pair() { # Each side's token grants this broadcast alone, like a media round's. harness_spawn "$name-pub" "$LOGS/$name-pub.log" run_tail_publisher "$fifo" "$pub" publish "$broadcast" "$(token_url --publish "$(publish_grant "$broadcast")")" "$version" "$mode" pid="$HARNESS_PID" - harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" "$mode" - subscriber_pid="$HARNESS_PID" + if [[ "$mode" == datagram ]]; then + # Subscription metadata confirms the alias is bound. An acknowledgement per datagram + # avoids testing a best-effort receive buffer's burst capacity. + mkfifo "$LOGS/datagram-ack" + exec 8<>"$LOGS/datagram-ack" + harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_subscriber "$LOGS/datagram-ack" "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" "$mode" + subscriber_pid="$HARNESS_PID" + local ack sequence + if read -r -t "$TIMEOUT" ack <&8 && [[ "$ack" == "datagram ready" ]]; then + for sequence in 0 1 2 3; do + printf 'datagram %s\n' "$sequence" >&9 + read -r -t "$TIMEOUT" ack <&8 && [[ "$ack" == "datagram $sequence" ]] || break + done + fi + exec 8>&- + rm "$LOGS/datagram-ack" + else + harness_spawn "$name-sub" "$LOGS/$name-sub.log" run_tail_client "$sub" subscribe "$broadcast" "$(token_url --subscribe "$(subscribe_grant "$broadcast")")" "$version" "$mode" + subscriber_pid="$HARNESS_PID" + fi harness_wait "$subscriber_pid" || true # Like run_native, a verified data result survives the NAPI addon's exit crash. if read=$(grep -xE "$want" "$LOGS/$name-sub.log") && { [[ "$mode" != status ]] || grep -qx "track status answered" "$LOGS/$name-sub.log"; }; then @@ -2526,6 +2549,8 @@ run_tails() { run_tail_pair rust "$runtime" run_tail_pair "$runtime" rust if [[ "$VERSION" == moq-transport-* ]]; then + run_tail_pair rust "$runtime" datagram + run_tail_pair "$runtime" rust datagram run_control_case "$runtime" status run_control_case "$runtime" goaway [[ "$runtime" != js-native-bun ]] || run_control_case "$runtime" reverse-goaway From f6546ddd7aacd38741860f04799496dd31e54b01 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:06:18 -0800 Subject: [PATCH 03/23] fix(net): resolve IETF subscription kinds without error matching --- js/net/MIGRATE.md | 5 +- js/net/src/ietf/datagram.test.ts | 2 +- js/net/src/ietf/publisher.ts | 30 +++---- js/net/src/ietf/subscriber.ts | 9 +- rs/moq-net/MIGRATE.md | 3 +- rs/moq-net/src/ietf/publisher.rs | 109 ++++++++++++++---------- rs/moq-net/src/ietf/subscriber.rs | 9 +- test/interop/clients/js-native/tail.ts | 74 ++++++++--------- test/interop/clients/rust/tail.rs | 111 +++++++++++++------------ 9 files changed, 191 insertions(+), 161 deletions(-) diff --git a/js/net/MIGRATE.md b/js/net/MIGRATE.md index 702433ddd69..94d985c3c3d 100644 --- a/js/net/MIGRATE.md +++ b/js/net/MIGRATE.md @@ -6,12 +6,11 @@ How to update your code for each breaking change, newest first. ### moq-transport datagram tracks -Datagram tracks now work over moq-transport using SUBSCRIBE and OBJECT_DATAGRAM. +Datagram tracks now work over moq-transport using SUBSCRIBE and OBJECT\_DATAGRAM. The sequence is the group ID and the object ID is zero. Stream and datagram consumers keep only their requested kind; nonzero object IDs are dropped by datagram consumers. No application API or wire encoding changes are required. - ### Datagrams use separate track types ([#7846](https://github.com/moq-dev/moq/pull/7846)) Create a datagram track with `broadcast.createDatagrams(name, info)` and read it with @@ -32,7 +31,7 @@ stream resume state and does not move to a replacement route. Lite-07-wip uses DATAGRAMS on stream type 0x9 and carries no Inflated Size. Lite-05/06 keep their wire format and carry both kinds of a name on one SUBSCRIBE, without deduplication. Create a datagram track before or with its same-name stream track on those versions. -Lite-01..04 and transports without datagrams refuse datagram requests. moq-transport carries them as OBJECT_DATAGRAM with object ID zero. +Lite-01..04 and transports without datagrams refuse datagram requests. moq-transport carries them as OBJECT\_DATAGRAM with object ID zero. ### Announcements are a listing ([#7678](https://github.com/moq-dev/moq/pull/7678)) diff --git a/js/net/src/ietf/datagram.test.ts b/js/net/src/ietf/datagram.test.ts index 6111aa87f48..02ec94231c7 100644 --- a/js/net/src/ietf/datagram.test.ts +++ b/js/net/src/ietf/datagram.test.ts @@ -4,7 +4,7 @@ import { ProtocolViolation, SessionCode } from "../error.ts"; import { createMockTransportPair } from "../mock.ts"; import { Producer as Origin } from "../origin.ts"; import * as Path from "../path.ts"; -import { Stream, encodeVarint } from "../stream.ts"; +import { encodeVarint, Stream } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; import { wireOf } from "../wire.ts"; import { Connection } from "./connection.ts"; diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index ef07235e5d8..96d1dcd7ef0 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -5,9 +5,9 @@ import { Gate } from "../auth_gate.ts"; import type { AuthSession } from "../auth_session.ts"; import { enforceGrant } from "../auth_session.ts"; import type * as broadcast from "../broadcast.ts"; -import * as datagram from "../datagram.ts"; -import * as DatagramStream from "../datagram_stream.ts"; import { Withdrawal } from "../connection/withdrawal.ts"; +import type * as datagram from "../datagram.ts"; +import * as DatagramStream from "../datagram_stream.ts"; import { controlTimeout, error, @@ -516,19 +516,21 @@ export class Publisher { try { // SUBSCRIBE has no kind bit. Resolve both kinds; an absent or refused kind // does not prevent the other from answering. - const streams = track.info().then((info) => ({ kind: "stream" as const, info })); - const candidates = [streams]; + const streamInfo = track.info().then((info) => ({ kind: "stream" as const, info })); + const datagramConsumer = datagrams; const selected = await untilAborted( - datagrams + datagramConsumer ? Promise.any([ - ...candidates, - datagrams.info().then((info) => ({ kind: "datagram" as const, info })), - ]) - : streams, + streamInfo, + datagramConsumer + .info() + .then((info) => ({ kind: "datagram" as const, info, consumer: datagramConsumer })), + ]).catch(() => streamInfo) + : streamInfo, left.signal, ); if (selected.kind === "datagram") { - await this.#serveDatagrams(msg, request, datagrams!, selected.info); + await this.#serveDatagrams(msg, request, selected.consumer, selected.info); return; } const info = selected.info; @@ -608,9 +610,7 @@ export class Publisher { return waiting; }; const opened = DatagramStream.datagramWriter(this.#quic); - const sender = opened?.open( - sendOrder({ priority: subscriberPriority, publisher, position: 2 ** 36 - 1 }), - ); + const sender = opened?.open(sendOrder({ priority: subscriberPriority, publisher, position: 2 ** 36 - 1 })); const max = DatagramStream.maxDatagramSize(this.#quic); const sending = (async () => { if (!sender) return; @@ -1311,7 +1311,9 @@ export class Publisher { continue; } if (body.byteLength > max) { - console.debug(`dropping datagram larger than the transport limit: size=${body.byteLength} max=${max}`); + console.debug( + `dropping datagram larger than the transport limit: size=${body.byteLength} max=${max}`, + ); continue; } await sender.writer.write(body); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 79f50589af1..6525ec492dd 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -4,9 +4,9 @@ import * as announce from "../announced.ts"; import type { Grant } from "../auth.ts"; import type { AuthSession } from "../auth_session.ts"; import * as broadcast from "../broadcast.ts"; -import * as datagram from "../datagram.ts"; import type { Drain } from "../connection/goaway.ts"; import { BroadcastCache } from "../consume.ts"; +import type * as datagram from "../datagram.ts"; import * as DatagramStream from "../datagram_stream.ts"; import { closeError, @@ -28,7 +28,7 @@ import { finalSequence, hiddenBelow, hooks, maxDelayOf, scopeCaptures, scopeHead import type { Lister } from "../listing.ts"; import * as Path from "../path.ts"; import { receiveFrame, skipPayload } from "../payload.ts"; -import { Reader, type Cursor, type Stream } from "../stream.ts"; +import { type Cursor, Reader, type Stream } from "../stream.ts"; import { Tail } from "../tail.ts"; import { Timescale, type Timestamp } from "../time.ts"; import type * as track from "../track.ts"; @@ -926,7 +926,7 @@ export class Subscriber { const revoked = new Promise((resolve) => { revoke = () => resolve(revokedEnded); }); - disposeGrant = this.#grant?.subscribe(() => { + const disposeGrant = this.#grant?.subscribe(() => { if (this.#denied(broadcast)) revoke(); }); // The grant may have shrunk during setup, before this watcher existed. @@ -2367,13 +2367,12 @@ export class Subscriber { const unanswered = new Signal(0); let sent = fromWire(subscriberPriority); - let disposeGrant: Dispose | undefined; const stop = new AbortController(); let revoke!: () => void; const revoked = new Promise<"revoked">((resolve) => { revoke = () => resolve("revoked"); }); - disposeGrant = this.#grant?.subscribe(() => { + const disposeGrant = this.#grant?.subscribe(() => { if (this.#denied(broadcast)) revoke(); }); if (this.#denied(broadcast)) revoke(); diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 4e13897e432..051198ae517 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -6,12 +6,11 @@ How to update your code for each breaking change, newest first. ### moq-transport datagram tracks -Datagram tracks now work over moq-transport using SUBSCRIBE and OBJECT_DATAGRAM. +Datagram tracks now work over moq-transport using SUBSCRIBE and OBJECT\_DATAGRAM. The sequence is the group ID and the object ID is zero. Stream and datagram consumers keep only their requested kind; nonzero object IDs are dropped by datagram consumers. No application API or wire encoding changes are required. - ### Admission completion records its own time `server::Activity::drain()` begins closing admission once its active set empties. diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index d63d16c4d55..5979cb50380 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -1467,7 +1467,8 @@ impl Publisher { // SUBSCRIBE does not identify the kind. Resolve both kinds and serve the // first successful answer, preferring streams when both are ready. let mut along = (self.session.max_datagram_size() > 0) - .then(|| broadcast.datagrams(msg.track_name.as_ref()).ok()).flatten(); + .then(|| broadcast.datagrams(msg.track_name.as_ref()).ok()) + .flatten(); if let Some(datagrams) = &mut along { datagrams.set_priority(super::priority::from_wire(msg.subscriber_priority)); } @@ -1485,29 +1486,46 @@ impl Publisher { None => Err(Error::NotFound), } }; - match futures::future::select(Box::pin(streams), Box::pin(datagrams)).await { - futures::future::Either::Left((Some(Err(err)), datagrams)) => { - // Either kind may be absent or refused. Only fail when neither answers. - drop(datagrams); - self.until_withdrawn(&mut stream, super::request_stream::fin_cancels(self.version), |waiter| { - match &along { - Some(datagrams) => datagrams.poll_info(waiter).map(|r| r.map(|_| None).map_err(|_| err.clone())), - None => Poll::Ready(Err(err.clone())), - } - }).await - } + let choice = futures::future::select(Box::pin(streams), Box::pin(datagrams)).await; + match choice { futures::future::Either::Left((result, _)) => result.map(|r| r.map(Some)), futures::future::Either::Right((Ok(()), _)) => Some(Ok(None)), futures::future::Either::Right((Err(_), streams)) => streams.await.map(|r| r.map(Some)), } }; + let selected = match selected { + Some(Err(err)) if along.is_some() => { + self.until_withdrawn( + &mut stream, + super::request_stream::fin_cancels(self.version), + |waiter| { + along + .as_ref() + .expect("checked above") + .poll_info(waiter) + .map(|r| r.map(|_| None).map_err(|_| err.clone())) + }, + ) + .await + } + other => other, + }; let (mut track_serve, edge) = match selected { Some(Ok(Some(streams))) => streams, Some(Ok(None)) => { - return self.serve_datagrams( - &mut stream, &msg, along.take().expect("datagram kind resolved"), - &mut request_finished, &mut goaways, &mut updates, &mut acked, - ).await; + return self + .serve_datagrams( + &mut stream, + &msg, + along.take().expect("datagram kind resolved"), + DatagramRequest { + finished: &mut request_finished, + goaways: &mut goaways, + updates: &mut updates, + acked: &mut acked, + }, + ) + .await; } Some(Err(err)) => return refused(err, true), None => return Ok(Subscribed::Withdrawn), @@ -1963,11 +1981,14 @@ impl Publisher { stream: &mut Stream, msg: &ietf::Subscribe<'_>, mut datagrams: crate::datagram::Consumer, - finished: &mut bool, - goaways: &mut super::request_stream::Messages, - updates: &mut SubscriptionUpdates, - acked: &mut Option>, + request: DatagramRequest<'_>, ) -> Result { + let DatagramRequest { + finished, + goaways, + updates, + acked, + } = request; // Before any await, so a subscriber waiting on this priority is not stuck behind setup. datagrams.set_priority(super::priority::from_wire(msg.subscriber_priority)); let info = match self @@ -2044,8 +2065,10 @@ impl Publisher { &self.session, self.version, msg.request_id.0, - order, - send_group.as_ref(), + SendOptions { + group: send_group.as_ref(), + priority: crate::priority::in_group(datagram.sequence, 0, order), + }, timescale, publisher, &datagram, @@ -2099,8 +2122,7 @@ impl Publisher { Some((Error::Unsupported, "REQUEST_UPDATE parameters not supported")) } None if update.authorization_token.is_some() - && grant.is_none() - && self.auth.token_decides(crate::auth::Direction::Publish) => + && grant.is_none() && self.auth.token_decides(crate::auth::Direction::Publish) => { Some((Error::Unsupported, TOKEN_ON_SESSION_REQUEST)) } @@ -5108,6 +5130,13 @@ struct TrackServe { datagrams: Option, } +struct DatagramRequest<'a> { + finished: &'a mut bool, + goaways: &'a mut super::request_stream::Messages, + updates: &'a mut SubscriptionUpdates, + acked: &'a mut Option>, +} + /// A datagram track forwarded on a subscription that also serves groups. struct DatagramForward { consumer: crate::datagram::Consumer, @@ -5557,8 +5586,10 @@ impl TrackServe { &self.session, self.version, self.request_id.0, - self.ranking.order, - self.ranking.group.as_ref(), + SendOptions { + group: self.ranking.group.as_ref(), + priority: crate::priority::in_group(datagram.sequence, 0, self.ranking.order), + }, timescale, publisher, &datagram, @@ -5572,8 +5603,7 @@ fn emit_datagram( session: &Transport, version: Version, alias: u64, - order: Order, - group: Option<&GroupOf>, + options: SendOptions<'_, GroupOf>, timescale: Option, publisher: i8, datagram: &crate::datagram::Datagram, @@ -5584,14 +5614,11 @@ fn emit_datagram( tracing::debug!(sequence = datagram.sequence, "dropping a datagram without a timestamp"); return; }; - if ietf::encode_object_time( - &mut Encoder::new(&mut properties, version.into()), - timestamp, - timescale, - ) - .is_err() - { - tracing::debug!(sequence = datagram.sequence, "dropping a datagram whose timestamp does not encode"); + if ietf::encode_object_time(&mut Encoder::new(&mut properties, version.into()), timestamp, timescale).is_err() { + tracing::debug!( + sequence = datagram.sequence, + "dropping a datagram whose timestamp does not encode" + ); return; } } @@ -5605,7 +5632,10 @@ fn emit_datagram( body: ietf::DatagramBody::Payload(datagram.payload.clone()), }; let Ok(body) = body.encode_bytes(version) else { - tracing::debug!(sequence = datagram.sequence, "dropping a datagram this draft cannot encode"); + tracing::debug!( + sequence = datagram.sequence, + "dropping a datagram this draft cannot encode" + ); return; }; let max = session.max_datagram_size(); @@ -5618,11 +5648,6 @@ fn emit_datagram( ); return; } - // Ranked like a stream carrying the group from its start. - let options = SendOptions { - group, - priority: crate::priority::in_group(datagram.sequence, 0, order), - }; let _ = session.send_datagram(body, options); } diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 6bc3d0408ef..66186196d36 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -3143,9 +3143,7 @@ impl Subscriber { let info = crate::datagram::Info { timescale: ok.properties.timescale, - priority: super::priority::from_wire( - ok.properties.priority.unwrap_or(super::priority::DEFAULT_WIRE), - ), + priority: super::priority::from_wire(ok.properties.priority.unwrap_or(super::priority::DEFAULT_WIRE)), }; let Some(request) = self.take_pending_datagram(request_id) else { self.remove_subscribe(request_id); @@ -7945,7 +7943,10 @@ mod tests { }; subscriber - .recv_datagram(encode(None, ietf::DatagramBody::Payload(bytes::Bytes::from_static(b"d")))) + .recv_datagram(encode( + None, + ietf::DatagramBody::Payload(bytes::Bytes::from_static(b"d")), + )) .unwrap(); match consumer.poll_recv(&kio::Waiter::noop()) { Poll::Ready(Ok(Some(datagram))) => { diff --git a/test/interop/clients/js-native/tail.ts b/test/interop/clients/js-native/tail.ts index ac82019b813..05e38d55f8d 100644 --- a/test/interop/clients/js-native/tail.ts +++ b/test/interop/clients/js-native/tail.ts @@ -143,44 +143,44 @@ try { broadcast.close(); console.error("tail acknowledged"); } else { - // `cached` and `late` read groups a newer one supersedes, which the default zero max age - // would leave stale. - const track = broadcast.createTrack("tail", { compress, maxAge: null, ...(status ? { priority: 2 } : {}) }); - if (cached) write(track); - if (history) serveHistory(track); - let head: Moq.Group.Producer | undefined; - if (late) { - head = track.appendGroup(); - for (const payload of frames(0).slice(0, LATE_HEAD)) head.writeFrame({ payload }); - } - broadcastPublication.announce(); - const lines = createInterface({ input: process.stdin }); - const commands = lines[Symbol.asyncIterator](); - if (head) { - assert.equal((await commands.next()).value, "late tail", "subscriber did not read the head"); - for (const payload of frames(0).slice(LATE_HEAD)) head.writeFrame({ payload }); - head.finish(); - for (let sequence = 1; sequence < LATE_GROUPS; sequence++) { - const group = track.appendGroup(); - for (const payload of frames(sequence)) group.writeFrame({ payload }); - group.finish(); + // `cached` and `late` read groups a newer one supersedes, which the default zero max age + // would leave stale. + const track = broadcast.createTrack("tail", { compress, maxAge: null, ...(status ? { priority: 2 } : {}) }); + if (cached) write(track); + if (history) serveHistory(track); + let head: Moq.Group.Producer | undefined; + if (late) { + head = track.appendGroup(); + for (const payload of frames(0).slice(0, LATE_HEAD)) head.writeFrame({ payload }); } - track.finish(); - track.close(); - } else if (!status && !cached && !history) { - const demand = track.demand(); - while (demand.wanted.peek() === undefined) await demand.wanted.changed(); - write(track); - track.close(); - } - try { - const ack = await commands.next(); - assert.equal(ack.value, "clean end", "reader did not acknowledge a clean end"); - } finally { - lines.close(); - } - broadcast.close(); - console.error("tail acknowledged"); + broadcastPublication.announce(); + const lines = createInterface({ input: process.stdin }); + const commands = lines[Symbol.asyncIterator](); + if (head) { + assert.equal((await commands.next()).value, "late tail", "subscriber did not read the head"); + for (const payload of frames(0).slice(LATE_HEAD)) head.writeFrame({ payload }); + head.finish(); + for (let sequence = 1; sequence < LATE_GROUPS; sequence++) { + const group = track.appendGroup(); + for (const payload of frames(sequence)) group.writeFrame({ payload }); + group.finish(); + } + track.finish(); + track.close(); + } else if (!status && !cached && !history) { + const demand = track.demand(); + while (demand.wanted.peek() === undefined) await demand.wanted.changed(); + write(track); + track.close(); + } + try { + const ack = await commands.next(); + assert.equal(ack.value, "clean end", "reader did not acknowledge a clean end"); + } finally { + lines.close(); + } + broadcast.close(); + console.error("tail acknowledged"); } } else if (datagram) { const request = origin.follow(Moq.Path.from(path)); diff --git a/test/interop/clients/rust/tail.rs b/test/interop/clients/rust/tail.rs index 9cd083179a7..2322a74745f 100644 --- a/test/interop/clients/rust/tail.rs +++ b/test/interop/clients/rust/tail.rs @@ -114,7 +114,12 @@ async fn main() -> anyhow::Result<()> { // check the payload alone. Stay open until each one is acknowledged. let mut track = broadcast.create_datagrams("tail", moq_net::datagram::Info::default())?; broadcast.announce(Default::default())?; - let session = client.clone().with_publisher(&origin).connect(url).established().await?; + let session = client + .clone() + .with_publisher(&origin) + .connect(url) + .established() + .await?; let mut priority = track.priority(); while priority.is_none() { priority = kio::wait(|waiter| track.poll_priority(waiter, priority)).await; @@ -135,64 +140,64 @@ async fn main() -> anyhow::Result<()> { session.close().await?; eprintln!("tail acknowledged"); } else { - let mut track = broadcast.create_track( - "tail", - moq_net::track::Info::default() - .with_timescale(moq_net::Timescale::MILLI) - .with_compress(compress) - // `cached` and `late` read groups a newer one supersedes, which the default - // zero max age would leave stale. - .with_max_age(None), - )?; - broadcast.announce(Default::default())?; - if cached { - write(&mut track, mode)?; - } - let mut head = None; - if late { - let mut group = track.append_group()?; - for frame in frames(0, mode).into_iter().take(LATE_HEAD) { - group.write_frame(moq_net::Timestamp::ZERO, frame)?; - } - head = Some(group); - } - let session = client - .clone() - .with_publisher(&origin) - .connect(url) - .established() - .await?; - if let Some(mut group) = head { - ensure!( - commands.next_line().await?.as_deref() == Some("late tail"), - "subscriber did not read the head" - ); - for frame in frames(0, mode).into_iter().skip(LATE_HEAD) { - group.write_frame(moq_net::Timestamp::ZERO, frame)?; + let mut track = broadcast.create_track( + "tail", + moq_net::track::Info::default() + .with_timescale(moq_net::Timescale::MILLI) + .with_compress(compress) + // `cached` and `late` read groups a newer one supersedes, which the default + // zero max age would leave stale. + .with_max_age(None), + )?; + broadcast.announce(Default::default())?; + if cached { + write(&mut track, mode)?; } - group.finish()?; - for sequence in 1..LATE_GROUPS { + let mut head = None; + if late { let mut group = track.append_group()?; - for frame in frames(sequence, mode) { + for frame in frames(0, mode).into_iter().take(LATE_HEAD) { + group.write_frame(moq_net::Timestamp::ZERO, frame)?; + } + head = Some(group); + } + let session = client + .clone() + .with_publisher(&origin) + .connect(url) + .established() + .await?; + if let Some(mut group) = head { + ensure!( + commands.next_line().await?.as_deref() == Some("late tail"), + "subscriber did not read the head" + ); + for frame in frames(0, mode).into_iter().skip(LATE_HEAD) { group.write_frame(moq_net::Timestamp::ZERO, frame)?; } group.finish()?; + for sequence in 1..LATE_GROUPS { + let mut group = track.append_group()?; + for frame in frames(sequence, mode) { + group.write_frame(moq_net::Timestamp::ZERO, frame)?; + } + group.finish()?; + } + track.finish()?; + } else if !cached { + let mut demand = track.demand(); + while demand.wanted_changed().await?.is_none() {} + write(&mut track, mode)?; } - track.finish()?; - } else if !cached { - let mut demand = track.demand(); - while demand.wanted_changed().await?.is_none() {} - write(&mut track, mode)?; - } - // A finished track is not a drained transport. Keep the session alive - // until the harness confirms the subscriber checked every byte and EOF. - ensure!( - commands.next_line().await?.as_deref() == Some("clean end"), - "reader did not acknowledge a clean end" - ); - broadcast.close(); - session.close().await?; - eprintln!("tail acknowledged"); + // A finished track is not a drained transport. Keep the session alive + // until the harness confirms the subscriber checked every byte and EOF. + ensure!( + commands.next_line().await?.as_deref() == Some("clean end"), + "reader did not acknowledge a clean end" + ); + broadcast.close(); + session.close().await?; + eprintln!("tail acknowledged"); } } "subscribe" => { From 4a47d0297c5bb1776b3f4719e59929cdcbf20410 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:07:37 -0800 Subject: [PATCH 04/23] fix(net): retain stream grant cleanup during datagram setup --- js/net/src/ietf/subscriber.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 6525ec492dd..9d348aebb31 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -926,7 +926,7 @@ export class Subscriber { const revoked = new Promise((resolve) => { revoke = () => resolve(revokedEnded); }); - const disposeGrant = this.#grant?.subscribe(() => { + disposeGrant = this.#grant?.subscribe(() => { if (this.#denied(broadcast)) revoke(); }); // The grant may have shrunk during setup, before this watcher existed. From 7c96ccb0370271c29f70e6f2546eaf579d3c9579 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:09:36 -0800 Subject: [PATCH 05/23] fix(net): answer cancelled and refused subscription setup --- js/net/src/ietf/publisher.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 96d1dcd7ef0..fe95373d578 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -528,7 +528,12 @@ export class Publisher { ]).catch(() => streamInfo) : streamInfo, left.signal, - ); + ).catch(async (err) => { + if (left.signal.aborted) request.abort(cancel); + else await request.refuse("subscribe", this.#failed(err, false)); + return undefined; + }); + if (!selected) return; if (selected.kind === "datagram") { await this.#serveDatagrams(msg, request, selected.consumer, selected.info); return; From 28f562e9a6ea975f2db1c1825f3498db5ec1af4c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:11:29 -0800 Subject: [PATCH 06/23] fix(net): represent unused datagram requests as control flow --- rs/moq-net/src/ietf/subscriber.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 66186196d36..62c88a37ba6 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -2951,14 +2951,14 @@ impl Subscriber { } self.validity.check(waiter)?; if request.poll_unused(waiter).is_ready() { - return Poll::Ready(Err(Error::Cancel)); + return Poll::Ready(Ok(None)); } - Stream::poll_open(&opening, &mut open, self.version, waiter) + Stream::poll_open(&opening, &mut open, self.version, waiter).map(|r| r.map(Some)) }) .await { - Ok(stream) => break stream.with_stats(self.origin.stats()), - Err(Error::Cancel) => { + Ok(Some(stream)) => break stream.with_stats(self.origin.stats()), + Ok(None) => { if request.close_unused() { return; } From cb2913156613a1a768831b6c35c23e909bfcc1c5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:16:29 -0800 Subject: [PATCH 07/23] refactor(net): carry alias failure scope explicitly --- rs/moq-net/src/ietf/subscriber.rs | 37 ++++++++++++++++-------- rs/moq-net/src/ietf/subscriber/tracks.rs | 4 +-- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 62c88a37ba6..4e6828f9b5a 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -1138,6 +1138,12 @@ enum Asked { Datagram(crate::datagram::Request), } +#[derive(Debug)] +struct AliasFailure { + error: Error, + session: bool, +} + impl Subscriber { #[expect( clippy::too_many_arguments, @@ -1351,16 +1357,20 @@ impl Subscriber { /// all LargestObject, so they are indistinguishable and we cannot: that costs the one /// subscription ([`Error::Unsupported`]). The same alias naming a *different* track is /// the collision section 11.1 makes fatal ([`Error::Duplicate`]). - fn register_alias(&self, request_id: RequestId, alias: u64) -> Result<(), Error> { + fn register_alias(&self, request_id: RequestId, alias: u64) -> Result<(), AliasFailure> { let mut state = self.state.lock(); if !state.subscribes.contains_key(&request_id) { - return Err(Error::NotFound); + return Err(AliasFailure { + error: Error::NotFound, + session: false, + }); } if let Err(err) = insert_track_alias(&state.aliases, alias, request_id) { - return Err(match self.alias_names_same_track(&state, alias, request_id) { - true => Error::Unsupported, - false => err, + let same_track = self.alias_names_same_track(&state, alias, request_id); + return Err(AliasFailure { + error: if same_track { Error::Unsupported } else { err }, + session: !same_track, }); } @@ -3160,8 +3170,8 @@ impl Subscriber { held.datagram = Some(producer.clone()); held.timescale = info.timescale; } - if let Err(err) = self.register_alias(request_id, ok.track_alias) { - if matches!(err, Error::Duplicate) { + if let Err(AliasFailure { error: err, session }) = self.register_alias(request_id, ok.track_alias) { + if session { tracing::warn!(track_alias = %ok.track_alias, "publisher reused a live track alias for another track"); self.session.close( super::error::to_session_code(&SessionError::from(&err)), @@ -3953,8 +3963,8 @@ impl Subscriber { // A resumed subscription stays bound to the alias it was held with. if held.is_some() { self.rebind_alias(request_id, Alias::Active(request_id)); - } else if let Err(err) = self.register_alias(request_id, alias) { - if matches!(err, Error::Duplicate) { + } else if let Err(AliasFailure { error: err, session }) = self.register_alias(request_id, alias) { + if session { tracing::warn!(track_alias = %alias, "publisher reused a live track alias for another track"); self.session.close( super::error::to_session_code(&SessionError::from(&err)), @@ -7854,7 +7864,10 @@ mod tests { subscriber.register_alias(RequestId(11), 7).unwrap(); assert!( - matches!(subscriber.register_alias(RequestId(13), 7), Err(Error::Unsupported)), + matches!( + subscriber.register_alias(RequestId(13), 7), + Err(AliasFailure { session: false, .. }) + ), "a shared alias must not be reported as the fatal collision", ); } @@ -8000,7 +8013,7 @@ mod tests { assert!(matches!( subscriber.register_alias(RequestId(13), 7), - Err(Error::Duplicate) + Err(AliasFailure { session: true, .. }) )); } @@ -8014,7 +8027,7 @@ mod tests { assert!(matches!( subscriber.register_alias(RequestId(13), 7), - Err(Error::Duplicate) + Err(AliasFailure { session: true, .. }) )); } diff --git a/rs/moq-net/src/ietf/subscriber/tracks.rs b/rs/moq-net/src/ietf/subscriber/tracks.rs index 9690aae226f..71e0a8bc571 100644 --- a/rs/moq-net/src/ietf/subscriber/tracks.rs +++ b/rs/moq-net/src/ietf/subscriber/tracks.rs @@ -733,14 +733,14 @@ impl Subscriber { /// Bind the alias a held subscription's PUBLISH or SUBSCRIBE_OK chose, paused, closing /// the session if it names another track. fn bind_alias(&self, request_id: RequestId, alias: u64) -> Result<(), Error> { - let err = match self.register_alias(request_id, alias) { + let AliasFailure { error: err, session } = match self.register_alias(request_id, alias) { Ok(()) => { self.rebind_alias(request_id, Alias::Paused(request_id)); return Ok(()); } Err(err) => err, }; - if matches!(err, Error::Duplicate) { + if session { tracing::warn!(track_alias = %alias, "publisher reused a live track alias for another track"); self.session.clone().close( super::super::error::to_session_code(&SessionError::from(&err)), From ef43f6de8ce2abcef7e34caf96091935235b647f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:19:28 -0800 Subject: [PATCH 08/23] fix(net): preserve stream properties across relayed subscriptions --- js/net/src/broadcast.ts | 2 ++ js/net/src/ietf/publisher.ts | 8 ++++++-- js/net/src/wire.ts | 2 ++ rs/moq-net/src/ietf/publisher.rs | 3 +++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index e07d002d8ca..dd5e64f8973 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -391,6 +391,7 @@ export class Producer { constructor() { registerWire(this, { + localDatagrams: !this.#state.unheld, subscribe: (name, options) => subscribe(this.#state, name, options), resolveTrackInfo: (name, hold) => resolveTrackInfo(this.#state, name, hold), largest: (name) => largest(this.#state, name), @@ -547,6 +548,7 @@ export class Consumer { } this.#state.consumers++; registerWire(this, { + localDatagrams: !this.#state.unheld, subscribe: (name, options) => subscribe(this.#state, name, options), resolveTrackInfo: (name, hold) => resolveTrackInfo(this.#state, name, hold), largest: (name) => largest(this.#state, name), diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index fe95373d578..9808e55246a 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -519,14 +519,18 @@ export class Publisher { const streamInfo = track.info().then((info) => ({ kind: "stream" as const, info })); const datagramConsumer = datagrams; const selected = await untilAborted( - datagramConsumer + datagramConsumer && wireOf(broadcast).localDatagrams ? Promise.any([ streamInfo, datagramConsumer .info() .then((info) => ({ kind: "datagram" as const, info, consumer: datagramConsumer })), ]).catch(() => streamInfo) - : streamInfo, + : streamInfo.catch(async (err) => { + if (!datagramConsumer) throw err; + const info = await datagramConsumer.info(); + return { kind: "datagram" as const, info, consumer: datagramConsumer }; + }), left.signal, ).catch(async (err) => { if (left.signal.aborted) request.abort(cancel); diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index d8a7e59e99c..ec79ad1be12 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -20,6 +20,8 @@ import type * as track from "./track.ts"; /** The protocol-facing operations behind a broadcast handle. */ export interface Broadcast { + /** Whether datagram metadata comes from an application rather than a peer's kindless SUBSCRIBE. */ + localDatagrams: boolean; datagrams(name: string): datagram.Consumer; liveDatagrams(name: string): datagram.Consumer | undefined; createDatagrams(name: string, info: Partial): datagram.Producer; diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 5979cb50380..b3da524d4c4 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -1481,6 +1481,9 @@ impl Publisher { } }; let datagrams = async { + if !broadcast.has_local_datagrams() || broadcast.publishes_track(&msg.track_name) { + return futures::future::pending().await; + } match &along { Some(datagrams) => kio::wait(|waiter| datagrams.poll_info(waiter)).await.map(|_| ()), None => Err(Error::NotFound), From 053792635b6ed83c5a5598fb844b50bc1c705c64 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:21:16 -0800 Subject: [PATCH 09/23] fix(net): stamp datagrams in the advertised subscription timescale --- js/net/src/ietf/publisher.ts | 6 +++--- rs/moq-net/src/ietf/publisher.rs | 7 ++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 9808e55246a..2ef32e2166a 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -958,7 +958,7 @@ export class Publisher { datagramTask = datagrams ? this.#sendDatagrams(datagrams, { alias: msg.requestId, - propertiesWanted: msg.propertiesWanted, + timescale, }).catch((err: unknown) => { console.debug(`stopping datagrams on this subscription: ${reason(err)}`); }) @@ -1278,7 +1278,7 @@ export class Publisher { */ async #sendDatagrams( datagrams: datagram.Consumer, - options: { alias: bigint; propertiesWanted: boolean }, + options: { alias: bigint; timescale?: Timescale }, ): Promise { let info: datagram.Info; try { @@ -1290,7 +1290,7 @@ export class Publisher { const opened = DatagramStream.datagramWriter(this.#quic); if (!opened) return; const publisher = info.priority; - const timescale = options.propertiesWanted ? (info.timescale ?? undefined) : undefined; + const timescale = options.timescale; const rank = () => sendOrder({ priority: datagrams.priority, publisher, position: 2 ** 36 - 1 }); const sender = opened.open(rank()); const dispose = hooks.datagramPriority(datagrams).subscribe(() => sender.setPriority(rank())); diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index b3da524d4c4..ee9de4a4dc1 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -5147,8 +5147,6 @@ struct DatagramForward { ended: bool, timescale: Option, publisher: i8, - /// Stamp timestamps only when SUBSCRIBE_OK declared a TIMESCALE. - stamp: bool, } impl TrackServe { @@ -5215,9 +5213,8 @@ impl TrackServe { consumer: datagrams, ready: false, ended: false, - timescale: None, + timescale: self.timescale.filter(|_| stamp), publisher: 0, - stamp, }); self } @@ -5555,7 +5552,7 @@ impl TrackServe { match forward.consumer.poll_info(waiter) { Poll::Pending => return, Poll::Ready(Ok(info)) => { - forward.timescale = info.timescale.filter(|_| forward.stamp); + // Object times use the stream subscription's advertised timescale. forward.publisher = info.priority; forward.ready = true; } From 72e401da7fbb25f6bb317e43749acdfb29070011 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:25:53 -0800 Subject: [PATCH 10/23] test(net): verify IETF datagram delivery and mixed track names --- rs/moq-net/tests/integration/datagram.rs | 57 +++++++++--------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/rs/moq-net/tests/integration/datagram.rs b/rs/moq-net/tests/integration/datagram.rs index f9a6fe309c7..1410a1594c7 100644 --- a/rs/moq-net/tests/integration/datagram.rs +++ b/rs/moq-net/tests/integration/datagram.rs @@ -255,12 +255,18 @@ async fn a_lite07_stream_subscription_never_takes_datagrams() { ); } -/// Lite-05/06 carry a datagram track as a SUBSCRIBE of its name, keeping only the group +/// Lite-05/06 and moq-transport carry a datagram track as a SUBSCRIBE of its name, keeping only the group /// datagrams. Both ends run this crate, so this covers asking an old publisher for a /// datagram track and serving an old subscriber's SUBSCRIBE from one. #[kio::test] -async fn lite05_and_lite06_map_a_datagram_track_onto_a_subscription() { - for version in ["moq-lite-05", "moq-lite-06"] { +async fn legacy_versions_map_a_datagram_track_onto_a_subscription() { + for version in [ + "moq-lite-05", + "moq-lite-06", + "moq-transport-18", + "moq-transport-20", + "moq-transport-22", + ] { let chain = Chain::new(&[version]).await; let mut producer = chain.publication.create_datagrams(TRACK, timed()).unwrap(); producer.append(millis(1), &b"early"[..]).unwrap(); @@ -289,13 +295,20 @@ async fn lite05_and_lite06_map_a_datagram_track_onto_a_subscription() { } } -/// Lite-05/06 carry a name's stream track and datagram track on one subscription, as -/// moq-transport does, so a name with both delivers each to its own reader, directly and +/// Lite-05/06 and moq-transport carry a name's stream track and datagram track on one +/// subscription, so a name with both delivers each to its own reader, directly and /// through a relay whose hops are both old. The datagrams ride the subscription, at its /// priority. #[kio::test] -async fn lite05_and_lite06_carry_both_kinds_of_a_name() { - for hops in [&["moq-lite-05"][..], &["moq-lite-06"], &["moq-lite-06", "moq-lite-06"]] { +async fn legacy_versions_carry_both_kinds_of_a_name() { + for hops in [ + &["moq-lite-05"][..], + &["moq-lite-06"], + &["moq-lite-06", "moq-lite-06"], + &["moq-transport-18"], + &["moq-transport-22"], + &["moq-transport-18", "moq-transport-22"], + ] { let chain = Chain::new(hops).await; let mut track = chain .publication @@ -531,33 +544,3 @@ async fn a_datagram_track_needs_datagrams() { assert!(matches!(err, Error::Unsupported), "{version}: {err:?}"); } } - -/// moq-transport carries no datagram tracks: asking for one finds nothing, and the stream -/// track of the same name is served as usual. -#[kio::test] -async fn moq_transport_has_no_datagram_tracks() { - let chain = Chain::new(&["moq-transport-22"]).await; - let mut track = chain - .publication - .create_track(TRACK, moq_net::track::Info::default().with_timescale(Timescale::MILLI)) - .unwrap(); - let _datagrams = chain.publication.create_datagrams(TRACK, timed()).unwrap(); - let remote = chain.remote().await; - assert!(matches!(remote.datagrams(TRACK), Err(Error::NotFound))); - - let mut subscriber = kio::clock::timeout(TIMEOUT, remote.track(TRACK).unwrap().subscribe(None)) - .await - .expect("subscribe timeout") - .expect("subscribe"); - support::harness::subscribed(&track).await; - track.write_frame(millis(1), &b"frame"[..]).unwrap(); - let mut group = kio::clock::timeout(TIMEOUT, subscriber.recv_group()) - .await - .expect("group timeout") - .unwrap() - .expect("a group"); - assert_eq!( - &group.read_frame().await.unwrap().expect("a frame").payload[..], - b"frame" - ); -} From 693b8f6d723c0bf663ab7a3fca0a362dfa986708 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:30:23 -0800 Subject: [PATCH 11/23] test(net): account for both IETF subscription kinds in routing --- rs/moq-net/tests/integration/dial_split_horizon.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rs/moq-net/tests/integration/dial_split_horizon.rs b/rs/moq-net/tests/integration/dial_split_horizon.rs index 6b3ebf74f1d..36190396ca0 100644 --- a/rs/moq-net/tests/integration/dial_split_horizon.rs +++ b/rs/moq-net/tests/integration/dial_split_horizon.rs @@ -212,8 +212,11 @@ async fn split_horizon(version: &str, [publisher_hop, subscriber_hop]: [Hop; 2]) kio::clock::sleep(SETTLE).await; let from_announcer = traffic(&to_publisher.registry, stats::Role::Publisher); + // IETF SUBSCRIBE carries no kind bit, so the relay asks for both the stream + // and datagram tracks. Both requests must reach the announcing edge. + let expected = if version.is_lite() { 1 } else { 2 }; assert_eq!( - from_announcer.subscriptions_started, 1, + from_announcer.subscriptions_started, expected, "the subscribe was not routed to the edge that announced it" ); let back_to_subscriber = traffic(&to_subscriber.registry, stats::Role::Publisher); From 53cc8d136eadebcc1fee3e3c5fd33bf38b252624 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:37:07 -0800 Subject: [PATCH 12/23] docs(quest): record IETF upstream subscription sharing blocker --- quest/m1/datagram-tracks/README.md | 1 + quest/m1/datagram-tracks/ietf.md | 67 ++++++++++++++++++++++++++++++ quest/m2/net-wrap/e2ee.md | 6 ++- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 quest/m1/datagram-tracks/ietf.md diff --git a/quest/m1/datagram-tracks/README.md b/quest/m1/datagram-tracks/README.md index 7e9f0206dda..e6da124a255 100644 --- a/quest/m1/datagram-tracks/README.md +++ b/quest/m1/datagram-tracks/README.md @@ -120,6 +120,7 @@ datagram track on lite-07, direct and through a relay. ## Required +- [moq-transport datagram tracks](/quest/m1/datagram-tracks/ietf.md) - Rust and JS map a datagram track to SUBSCRIBE and OBJECT_DATAGRAM with object ID 0, keeping the requested kind - [A datagram sent right behind its answer reaches the subscriber](/quest/m1/datagram-tracks/answer-race.md) - receivers decode answers before routing datagrams, and count the ones they still can't route ## Related diff --git a/quest/m1/datagram-tracks/ietf.md b/quest/m1/datagram-tracks/ietf.md new file mode 100644 index 00000000000..e2780289107 --- /dev/null +++ b/quest/m1/datagram-tracks/ietf.md @@ -0,0 +1,67 @@ +# [S] moq-transport datagram tracks + +## Goal + +Over moq-transport, Rust and JS map a datagram track to IETF SUBSCRIBE and +OBJECT_DATAGRAM, with its sequence as the group ID and object ID 0, and +keep the requested kind when a peer mixes datagrams and streams on one track. + +## Plan + +Implementation checkpoint: [moq-dev/moq#7959](https://github.com/moq-dev/moq/pull/7959). +Rust and JS adapters, direct delivery, interop lanes, and mixed-kind filtering are +implemented. Full interop passed. The remaining blocker is upstream subscription +sharing: a relay currently opens separate stream and datagram subscriptions for +one IETF name. The extra subscription broadens the stream's demand and breaks +ordering, range updates, and max-age aggregation. Share upstream interest between +the model kinds without matching errors or adding IETF-only public model API, +then rerun the complete net suite, CI, interop, and local review. + +The failing integration tests are `group_order::merged_subscribers_keep_their_own_order`, +`subscribe_ranges::{draft18_updates_narrow_then_widen,draft22_cold_widening_finishes_the_largest_group,draft22_resolves_cached_time_ends,draft22_updates_both_bounds_across_cached_groups,unresolved_time_reader_never_changes_sibling_demand}`, +and `subscriber_max_age::relay_forwards_the_most_tolerant_subscriber_max_age`. + + +Decided 2026-11-01 (maintainer), in the +[epic](/quest/m1/datagram-tracks/README.md): + +- A datagram-track subscription keeps only datagrams with object ID 0. + Stream objects arriving on it are dropped and logged once per + subscription. +- A stream-track subscription drops datagrams. +- Outbound, a datagram track sends only OBJECT_DATAGRAM with object ID 0. + A stream track never sends OBJECT_DATAGRAM. +- A datagram with a nonzero object ID on a datagram-track subscription is + dropped and logged. + +Touches `rs/moq-net/src/ietf/datagram.rs` and the JS IETF adapter. Test +against moxygen in the interop lanes where it sends datagrams. + +Rust datagram tracks ([moq-dev/moq#7752](https://github.com/moq-dev/moq/pull/7752)) +left moq-transport without datagrams in Rust until this lands: the IETF +subscriber drops every OBJECT_DATAGRAM, `datagrams()` on an IETF source is +NOT_FOUND, and a stream track never sends one. Restore what it removed: + +- the Rust-JS `datagram` tail lanes in `test/interop/interop.sh`, the + `datagram` mode of `test/interop/clients/js-native/tail.ts`, and their + README bullet; +- moq-e2ee's `datagrams_over_ietf` transport test; +- the moq-transport rows of moq-tokio's `datagram_delivery` bench and + moq-net's `session_delivery_datagram`; +- the OBJECT_DATAGRAM sentence in `doc/concept/standard.md`. + +An OBJECT_DATAGRAM whose Object Status is undefined is a protocol violation +in JS too, as it is in Rust, on every subscription kind. Planned 2026-11-04 +with the maintainer, from +[moq-dev/moq#7846](https://github.com/moq-dev/moq/pull/7846). Rust's +`recv_datagram` (`rs/moq-net/src/ietf/subscriber.rs`, about line 4839) +closes the session with `ProtocolViolation` when `status_defined` +(`rs/moq-net/src/ietf/object.rs`, about line 21: Normal, END_OF_GROUP and +END_OF_TRACK) rejects the status, even while it drops the datagram. JS's +`#recvDatagram` (`js/net/src/ietf/subscriber.ts`) decodes and drops it +silently, on main and after #7846. Check the shared status set once for +both datagram paths, so a datagram-track subscription keeps the check +when it starts accepting datagrams, and add a JS test that an undefined +status closes the session. + +Public API: none beyond the model. Wire: no format change. diff --git a/quest/m2/net-wrap/e2ee.md b/quest/m2/net-wrap/e2ee.md index e7d6a6171ce..35046297d52 100644 --- a/quest/m2/net-wrap/e2ee.md +++ b/quest/m2/net-wrap/e2ee.md @@ -63,8 +63,10 @@ the reverse (decided 2026-10-20, maintainer): Subscribe ID, Sequence, and Timestamp, since datagram tracks dropped the relayed `ietf` fields and the Inflated Size from it, so `MAX_DATAGRAM_HEADER` (28, `rs/moq-crypto/src/e2ee/limits.rs` and - `js/crypto/src/e2ee/limits.ts`) covers every version. moq-transport maps a - datagram track onto the existing OBJECT_DATAGRAM and adds no header fields. + `js/crypto/src/e2ee/limits.ts`) covers every version. Re-check it if + moq-transport datagram tracks + ([moq-transport datagram tracks](/quest/m1/datagram-tracks/ietf.md)) add + header fields. - Ranges: every moq-net range starts at a group's first frame ([moq-dev/moq#6465](https://github.com/moq-dev/moq/pull/6465)), so the wrapper reads each physical group whole and inflates from frame 0. From 1c5e5f04a2939c969c37fcabb4428e18d57a754e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 08:37:47 -0800 Subject: [PATCH 13/23] test(net): retain the single-upstream-subscription invariant --- rs/moq-net/tests/integration/dial_split_horizon.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/rs/moq-net/tests/integration/dial_split_horizon.rs b/rs/moq-net/tests/integration/dial_split_horizon.rs index 36190396ca0..6b3ebf74f1d 100644 --- a/rs/moq-net/tests/integration/dial_split_horizon.rs +++ b/rs/moq-net/tests/integration/dial_split_horizon.rs @@ -212,11 +212,8 @@ async fn split_horizon(version: &str, [publisher_hop, subscriber_hop]: [Hop; 2]) kio::clock::sleep(SETTLE).await; let from_announcer = traffic(&to_publisher.registry, stats::Role::Publisher); - // IETF SUBSCRIBE carries no kind bit, so the relay asks for both the stream - // and datagram tracks. Both requests must reach the announcing edge. - let expected = if version.is_lite() { 1 } else { 2 }; assert_eq!( - from_announcer.subscriptions_started, expected, + from_announcer.subscriptions_started, 1, "the subscribe was not routed to the edge that announced it" ); let back_to_subscriber = traffic(&to_subscriber.registry, stats::Role::Publisher); From f55236443df7bbf3de972ce58ec3e07e8d409d28 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 11:28:32 -0800 Subject: [PATCH 14/23] fix(net): separate passive forwarding from shared subscription demand --- js/net/src/datagram.test.ts | 20 +++++++ js/net/src/datagram.ts | 24 +++++--- js/net/src/ietf/datagram.test.ts | 65 +++++++++++++++++++++ js/net/src/ietf/publisher.ts | 1 + js/net/src/ietf/subscriber.ts | 65 ++++++--------------- js/net/src/internal.ts | 5 ++ js/net/src/lite/publisher.ts | 19 ++++-- rs/moq-net/src/ietf/subscriber.rs | 29 ++++----- rs/moq-net/src/ietf/subscriber/datagrams.rs | 46 +++++++++++++-- rs/moq-net/src/lite/publisher.rs | 11 +++- rs/moq-net/src/model/broadcast.rs | 2 +- rs/moq-net/src/model/datagram.rs | 52 ++++++++++++++--- rs/moq-net/tests/integration/datagram.rs | 7 ++- 13 files changed, 247 insertions(+), 99 deletions(-) diff --git a/js/net/src/datagram.test.ts b/js/net/src/datagram.test.ts index a2d6ab801cf..023f29fbedd 100644 --- a/js/net/src/datagram.test.ts +++ b/js/net/src/datagram.test.ts @@ -185,3 +185,23 @@ test("closing one waiting datagram consumer releases its info wait while another second.close(); track.finish(); }); + +test("passive forwarding drains a finished track without adding demand", async () => { + const producer = new Datagram.Producer("audio").accept({}); + const observer = producer.consume(); + hooks.passiveDatagrams(observer); + const copy = observer.clone(); + expect(producer.priority.peek()).toBeUndefined(); + const reader = producer.consume(); + reader.setPriority(-7); + expect(producer.priority.peek()).toBe(-7); + reader.close(); + expect(producer.priority.peek()).toBeUndefined(); + producer.append(undefined, Uint8Array.of(1)); + producer.finish(); + for (const consumer of [observer, copy]) { + expect((await consumer.recv())?.payload).toEqual(Uint8Array.of(1)); + expect(await consumer.recv()).toBeUndefined(); + consumer.close(); + } +}); diff --git a/js/net/src/datagram.ts b/js/net/src/datagram.ts index 2e111d3847d..9a4e1aad3fa 100644 --- a/js/net/src/datagram.ts +++ b/js/net/src/datagram.ts @@ -45,7 +45,7 @@ class State { closed = new Once(); changed = new Signal(0); priority = new Signal(undefined); - consumers = new Map(); + consumers = new Map(); buffer: Datagram[] = []; bytes = 0; offset = 0n; @@ -65,9 +65,10 @@ class State { updatePriority(): void { let highest: number | undefined; - for (const value of this.consumers.values()) highest = Math.max(highest ?? -128, value); + for (const value of this.consumers.values()) + if (value !== undefined) highest = Math.max(highest ?? -128, value); this.priority.set(highest); - if (highest === undefined) { + if (this.consumers.size === 0) { if (this.info.peek() === undefined) this.end(new StreamError(StreamCode.Cancel)); if (this.closed.peek() !== undefined) this.release(); } @@ -88,7 +89,7 @@ async function info(state: State, local?: GetPromise): Promise Consumer; +let consume: (name: string, state: State, cursor?: bigint, priority?: number | null) => Consumer; /** Writes a live datagram track, independently of any stream track of the same name. */ export class Producer { @@ -230,13 +231,13 @@ export class Consumer { #closed = new Once(); #priority: Signal; - private constructor(name: string, state: State, cursor?: bigint, value = 0) { + private constructor(name: string, state: State, cursor?: bigint, value: number | null = 0) { this.name = name; this.#state = state; this.#cursor = cursor ?? state.offset + BigInt(state.buffer.length); - this.#priority = new Signal(value); + this.#priority = new Signal(value ?? 0); if (state.closed.peek() === undefined || cursor !== undefined) { - state.consumers.set(this, value); + state.consumers.set(this, value ?? undefined); state.updatePriority(); } } @@ -244,6 +245,12 @@ export class Consumer { consume = (name, state, cursor, priority) => new Consumer(name, state, cursor, priority); hooks.datagramPriority = (consumer) => consumer.#priority; hooks.datagramClosed = (consumer) => consumer.#closed; + hooks.passiveDatagrams = (consumer) => { + if (consumer.#state.consumers.has(consumer)) { + consumer.#state.consumers.set(consumer, undefined); + consumer.#state.updatePriority(); + } + }; } /** The track's publisher properties, once accepted. */ @@ -267,7 +274,8 @@ export class Consumer { /** Another consumer at the same position and priority. */ clone(): Consumer { if (this.#closed.peek() !== undefined) throw new Error("datagram consumer is closed"); - return consume(this.name, this.#state, this.#cursor, this.#priority.peek()); + const passive = this.#state.consumers.has(this) && this.#state.consumers.get(this) === undefined; + return consume(this.name, this.#state, this.#cursor, passive ? null : this.#priority.peek()); } /** Read the next datagram as a copy this consumer owns, skipping any dropped by the bounded shared buffer. */ diff --git a/js/net/src/ietf/datagram.test.ts b/js/net/src/ietf/datagram.test.ts index 02ec94231c7..153b3ed8813 100644 --- a/js/net/src/ietf/datagram.test.ts +++ b/js/net/src/ietf/datagram.test.ts @@ -158,3 +158,68 @@ test("a datagram track round-trips over moq-transport", async () => { source.close(); } }); + +// Either model kind can arrive first; the relay must retain one upstream request and +// route each payload only to readers of its kind, even after one reader leaves. +test("a relay shares one IETF subscription across stream and datagram readers", async () => { + for (const datagramFirst of [false, true]) { + const source = new Origin(); + const relay = new Origin(); + const path = Path.from("shared"); + const publication = source.createBroadcast(path); + const track = publication.broadcast.createTrack("audio", { timescale: Timescale.MILLI }); + const datagrams = publication.broadcast.createDatagrams("audio", { timescale: Timescale.MICRO }); + publication.announce(); + const upstream = createMockTransportPair(ALPN.DRAFT_18); + const url = new URL("https://localhost/shared"); + const peers = await Promise.all([ + connect({ url, transport: upstream.client }), + accept({ url, transport: upstream.server, publish: source.consume() }), + ]); + const original = wireOf(peers[0]).consume(path); + const requests = relay.requestsRoute(path); + const cursor = requests.requested(); + const routing = (async () => { + for await (const request of cursor) request.serve(original); + })(); + const downstream = createMockTransportPair(ALPN.DRAFT_22); + const viewers = await Promise.all([ + connect({ url, transport: downstream.client }), + accept({ url, transport: downstream.server, publish: relay.consume() }), + ]); + const remote = wireOf(viewers[0]).consume(path); + const stream = datagramFirst ? undefined : remote.track("audio").subscribe(); + if (stream) await stream.info(); + const streamCount = stream && upstream.client.sendStreams.bidi.length; + const reader = remote.datagrams("audio"); + await reader.info(); + const before = streamCount ?? upstream.client.sendStreams.bidi.length; + const subscriber = stream ?? remote.track("audio").subscribe(); + await subscriber.info(); + expect(upstream.client.sendStreams.bidi.length).toBe(before); + try { + datagrams.insert({ sequence: 4n, timestamp: Timestamp.fromMillis(9), payload: Uint8Array.of(7) }); + const group = track.appendGroup(); + group.writeFrame({ timestamp: Timestamp.fromMillis(10), payload: Uint8Array.of(8) }); + group.finish(); + const received = await reader.recv(); + expect(received?.payload).toEqual(Uint8Array.of(7)); + expect(received?.timestamp?.asMillis()).toBe(9); + const receivedGroup = await subscriber.recvGroup(); + expect((await receivedGroup?.readFrame())?.payload).toEqual(Uint8Array.of(8)); + receivedGroup?.close(); + subscriber.close(); + datagrams.insert({ sequence: 5n, timestamp: Timestamp.fromMillis(11), payload: Uint8Array.of(9) }); + expect((await reader.recv())?.payload).toEqual(Uint8Array.of(9)); + } finally { + subscriber.close(); + reader.close(); + for (const peer of viewers) await peer.close(); + requests.close(); + await routing; + for (const peer of peers) await peer.close(); + relay.close(); + source.close(); + } + } +}); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 8767af9aa33..0761b2406de 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -511,6 +511,7 @@ export class Publisher { ? broadcast.datagrams(msg.trackName) : wireOf(broadcast).liveDatagrams(msg.trackName); if (local) consumer?.setPriority(fromWire(msg.subscriberPriority)); + else if (consumer) hooks.passiveDatagrams(consumer); return consumer; }; let datagrams = datagramOf(); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index aca371265df..346d4079ca4 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -103,18 +103,8 @@ type Subscription = { fetchIds: bigint[]; }; -/** A stream subscription's alias binding. The object identity is what retirement matches. */ -type TrackBinding = { kind: "track"; subscription: Subscription }; - -/** A datagram subscription's alias binding. Group streams on it are dropped, not written. */ -type DatagramBinding = { - kind: "datagram"; - producer: datagram.Producer; - timescale?: Timescale; - logged: boolean; -}; - -type Binding = TrackBinding | DatagramBinding; +/** Both model kinds share this alias; group and datagram receive paths route to their own producer. */ +type Binding = { kind: "track"; subscription: Subscription }; // Out-parameter for #openSubscribe: lets the caller observe partial progress // (stream opened, trackAlias registered) so it can clean up on timeout even @@ -1886,18 +1876,6 @@ export class Subscriber { return; } - // A datagram subscription keeps only OBJECT_DATAGRAM. A group on it is logged once - // and stopped; the session stays up. - if (binding.kind === "datagram") { - if (!binding.logged) { - binding.logged = true; - console.debug( - `dropping a group stream on a datagram track's subscription: alias=${group.trackAlias} group=${group.groupId}`, - ); - } - stream.stop(new Error("dropping a group stream on a datagram track's subscription")); - return; - } const subscription = binding.subscription; const { track, tail } = subscription; @@ -2221,9 +2199,8 @@ export class Subscriber { * Route one OBJECT_DATAGRAM. * * An undefined Object Status is a protocol violation on every subscription kind. - * A defined status is not a payload and does not end the track. A datagram subscription - * keeps object ID 0. Anything else is dropped, including every datagram on a stream - * subscription. + * A defined status is not a payload and does not end the track. Only object ID 0 + * reaches the datagram model; the stream model never receives datagrams. */ async #recvDatagram(data: Uint8Array): Promise { const datagram = await ObjectDatagram.decode(data, this.#session.version); @@ -2232,13 +2209,10 @@ export class Subscriber { } const alias = this.#aliases.peek(datagram.trackAlias); - const binding = - alias?.kind === "track" - ? alias.subscription.datagrams && { - producer: alias.subscription.datagrams.producer, - timescale: alias.subscription.timescale, - } - : alias; + const binding = alias?.subscription.datagrams && { + producer: alias.subscription.datagrams.producer, + timescale: alias.subscription.timescale, + }; if (!binding) { console.debug(`dropping a datagram for an unknown alias: alias=${datagram.trackAlias}`); return; @@ -2258,19 +2232,10 @@ export class Subscriber { let timestamp: Timestamp | undefined; if (datagram.properties) { - try { - // Walk the block either way: a malformed property is a protocol violation on an - // untimed track too. Only a track that declared a timescale keeps the timestamp. - const decoded = await new Reader(undefined, datagram.properties, this.#session.version).decode( - (cursor) => decodeObjectTime(cursor, binding.timescale ?? Timescale.SECOND), - ); - if (binding.timescale !== undefined) timestamp = decoded; - } catch (err) { - const cause = error(err); - if (cause instanceof ProtocolViolation) throw cause; - console.debug(`dropping a datagram whose properties do not decode: ${reason(cause)}`); - return; - } + const decoded = await new Reader(undefined, datagram.properties, this.#session.version).decode((cursor) => + decodeObjectTime(cursor, binding.timescale ?? Timescale.SECOND), + ); + if (binding.timescale !== undefined) timestamp = decoded; } if (binding.timescale !== undefined && timestamp === undefined) { console.debug( @@ -2317,7 +2282,7 @@ export class Subscriber { /** Ask through the stream model's shared lookup, so either kind opens the same SUBSCRIBE. */ async #runDatagram(source: broadcast.Consumer, request: datagram.Request, valid?: AbortSignal): Promise { - if (DatagramStream.maxDatagramSize(this.#quic) === 0) { + if (!this.#quic || DatagramStream.maxDatagramSize(this.#quic) === 0) { request.reject(new Error("transport does not support datagrams")); return; } @@ -2328,6 +2293,9 @@ export class Subscriber { } pending.set(request.name, request); const subscriber = wireOf(source).subscribe(request.name, { priority: request.priority.peek() ?? 0 }); + const dispose = request.priority.subscribe((priority) => { + if (priority !== undefined) subscriber.update({ priority }); + }); try { await untilAborted( race([ @@ -2341,6 +2309,7 @@ export class Subscriber { } catch (err) { if (pending.get(request.name) === request) request.reject(error(err)); } finally { + dispose(); subscriber.close(); if (pending.get(request.name) === request) pending.delete(request.name); } diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index b038b8445c0..b0296b35b7b 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -447,6 +447,8 @@ export const hooks: { closeUnusedDatagrams: (producer: datagram.Producer) => boolean; /** Follow this consumer's subscriber priority for transport scheduling. */ datagramPriority: (consumer: datagram.Consumer) => Getter; + /** Forward data carried by an existing stream without contributing another subscription's demand. */ + passiveDatagrams: (consumer: datagram.Consumer) => void; /** Settles once this consumer is closed, the end of whatever request it serves. */ datagramClosed: (consumer: datagram.Consumer) => GetPromise; /** @@ -746,6 +748,9 @@ export const hooks: { closeUnusedDatagrams: () => { throw new Error("datagram module not loaded"); }, + passiveDatagrams: () => { + throw new Error("datagram module not loaded"); + }, datagramPriority: () => { throw new Error("datagram module not loaded"); }, diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 8cefca22724..eda9be88fb8 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1010,6 +1010,7 @@ export class Publisher { endFrame: msg.endFrame, }; let legacy: datagram.Consumer | undefined; + const legacyPriority = new Signal(msg.priority); let controls: SubscriptionControls | undefined; // Lite-05 and later name every sequence below the end that this subscription never // got with SUBSCRIBE_DROP, so the subscriber settles without waiting out its grace @@ -1080,8 +1081,9 @@ export class Publisher { if (this.#datagramWriter && !hasDatagramTracks(this.version)) { legacy = wireOf(front).liveDatagrams(msg.track); if (legacy) { - legacy.setPriority(msg.priority); - datagrams = this.#runDatagrams(legacy, { sub: msg.id, stamp }) + if (wireOf(front).localDatagrams) legacy.setPriority(msg.priority); + else hooks.passiveDatagrams(legacy); + datagrams = this.#runDatagrams(legacy, { sub: msg.id, stamp, priority: legacyPriority }) .then(() => {}) .catch((err) => console.debug(`datagram send stopped: ${reason(err)}`)); } @@ -1098,7 +1100,8 @@ export class Publisher { version: this.version, live: new LiveRanges(this.version, asked), apply: (update, live) => { - legacy?.setPriority(update.priority); + legacyPriority.set(update.priority); + if (wireOf(front).localDatagrams) legacy?.setPriority(update.priority); if (hasRanges(this.version)) { const resolved = hooks.servingResolved(track); const entries = live.map(({ id, entry }) => { @@ -2053,12 +2056,16 @@ export class Publisher { } // Shared by dedicated lite-07 requests and the old SUBSCRIBE carry-both mapping. - async #runDatagrams(track: datagram.Consumer, options: { sub: bigint; stamp?: Stamp }): Promise { + async #runDatagrams( + track: datagram.Consumer, + options: { sub: bigint; stamp?: Stamp; priority?: Getter }, + ): Promise { if (!this.#datagramWriter) throw new Error("transport does not support datagrams"); const info = await track.info(); - const rank = () => sendOrder({ priority: track.priority, publisher: info.priority }); + const priority = options.priority ?? hooks.datagramPriority(track); + const rank = () => sendOrder({ priority: priority.peek(), publisher: info.priority }); const sender = this.#datagramWriter.open(rank()); - const dispose = hooks.datagramPriority(track).subscribe(() => sender.setPriority(rank())); + const dispose = priority.subscribe(() => sender.setPriority(rank())); const left = hooks.datagramClosed(track); const { writer } = sender; const maxSize = Math.min(DatagramMessage.MAX_SIZE, DatagramStream.maxDatagramSize(this.#quic)); diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 5fade7886c0..779b6beb44f 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -192,7 +192,7 @@ fn retire_track_alias(aliases: &TrackAliases, alias: u64, request_id: RequestId) #[derive(Default)] struct State { - datagram_requests: HashMap, + datagram_requests: HashMap, crate::datagram::Request)>, // Each active subscription subscribes: HashMap, @@ -224,7 +224,7 @@ impl State { /// Active receive tasks abort their own groups. Abort any head waiting for its /// tail here, along with the track. Ordinary unsubscribe removes its entry. fn abort(&mut self, err: &Error) { - for (_, request) in self.datagram_requests.drain() { + for (_, (_, request)) in self.datagram_requests.drain() { request.reject(err.clone()); } for (_, mut track) in self.subscribes.drain() { @@ -420,9 +420,6 @@ impl Fill { /// [`Self::cancel`], with `err` as the reason the head was cut short. fn abort(&mut self, err: Error) { - for (_, request) in self.datagram_requests.drain() { - request.reject(err.clone()); - } match std::mem::replace(self, Fill::Done) { Fill::Ready { producer, .. } => { let _ = producer.abort(err); @@ -3300,15 +3297,16 @@ impl RunBroadcast { .poll_listing(&self.path, &self.lister, &mut self.listening, waiter); loop { while let Poll::Ready(Ok(request)) = self.datagrams.poll_requested(waiter) { - if self.this.session.max_datagram_size() == 0 { request.reject(Error::Unsupported); continue; } + if self.this.session.max_datagram_size() == 0 { + request.reject(Error::Unsupported); + continue; + } let key = (self.path.clone(), request.name().to_owned()); let consumer = self.source.consume(); match consumer.track(request.name()) { Ok(track) => { - let priority = request.priority().unwrap_or(0); - self.this.state.lock().datagram_requests.insert(key.clone(), request); self.subscribes - .push(Requested::new(self.this.clone(), key, track, priority)); + .push(Requested::new(self.this.clone(), key, track, request)); } Err(err) => request.reject(err), } @@ -4392,7 +4390,9 @@ impl SubscribeOnce { true => track.start_at(largest.map_or(0, |largest| largest.group)), false => track.start_at(self.start), }; - if this.session.max_datagram_size() > 0 && let Some(source) = &self.source { + if this.session.max_datagram_size() > 0 + && let Some(source) = &self.source + { self.shared = Shared::new(this, source, self.key.clone(), &track); } if let Some(staged) = staged { @@ -5769,14 +5769,7 @@ impl Subscriber { let timestamp = match &datagram.properties { Some(raw) => { let bytes = bytes::Bytes::copy_from_slice(raw); - match super::object::read(&bytes, self.version, timescale) { - Ok(decoded) => decoded.timestamp, - Err(err) if is_protocol_violation(&err) => return Err(err), - Err(err) => { - tracing::debug!(%err, "dropping a datagram whose properties do not decode"); - return Ok(()); - } - } + super::object::read(&bytes, self.version, timescale)?.timestamp } None => None, }; diff --git a/rs/moq-net/src/ietf/subscriber/datagrams.rs b/rs/moq-net/src/ietf/subscriber/datagrams.rs index 1a10b92f32b..99d6cef9f69 100644 --- a/rs/moq-net/src/ietf/subscriber/datagrams.rs +++ b/rs/moq-net/src/ietf/subscriber/datagrams.rs @@ -6,12 +6,27 @@ use crate::datagram; pub(super) struct Requested { this: Subscriber, key: tracks::TrackKey, + id: std::sync::Arc<()>, subscribing: track::Subscribing, + priority: i8, } impl Requested { - pub(super) fn new(this: Subscriber, key: tracks::TrackKey, track: track::Consumer, priority: i8) -> Self { + pub(super) fn new( + this: Subscriber, + key: tracks::TrackKey, + track: track::Consumer, + request: datagram::Request, + ) -> Self { + let priority = request.priority().unwrap_or(0); + let id = std::sync::Arc::new(()); + this.state + .lock() + .datagram_requests + .insert(key.clone(), (id.clone(), request)); Self { + id, + priority, this, key, subscribing: track @@ -20,17 +35,20 @@ impl Requested { } } fn reject(&self, err: Error) { - if let Some(request) = self.this.state.lock().datagram_requests.remove(&self.key) { - request.reject(err); + let mut state = self.this.state.lock(); + if let Entry::Occupied(entry) = state.datagram_requests.entry(self.key.clone()) + && std::sync::Arc::ptr_eq(&entry.get().0, &self.id) + { + entry.remove().1.reject(err); } } } impl kio::Task for Requested { type Output = (); fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { - { + let priority = { let state = self.this.state.lock(); - if let Some(request) = state.datagram_requests.get(&self.key) + if let Some((_, request)) = state.datagram_requests.get(&self.key) && request.poll_unused(waiter).is_ready() && request.close_unused() { @@ -38,6 +56,22 @@ impl kio::Task for Requested { self.reject(Error::Cancel); return Poll::Ready(()); } + state + .datagram_requests + .get(&self.key) + .and_then(|(_, request)| request.priority()) + }; + if let Some(priority) = priority + && priority != self.priority + { + self.priority = priority; + if let Err(err) = self + .subscribing + .update(track::Subscription::default().with_priority(priority)) + { + self.reject(err); + return Poll::Ready(()); + } } match ready!(self.subscribing.poll_ok(waiter)) { Ok(_) => {} @@ -71,7 +105,7 @@ impl Shared { .with_priority(track.info().priority); let pending = this.state.lock().datagram_requests.remove(&key); let producer = match pending { - Some(request) => request.accept(info), + Some((_, request)) => request.accept(info), None => source.create_datagrams(key.1, info).ok()?, }; Some(Self { diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 2a4fbdfcb07..2a7af27d40b 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2699,6 +2699,7 @@ impl Legacy { /// Lite-05/06: a name's datagram track carried on the subscription of its stream track, as /// one subscription carries both kinds there. A subscriber keeps the kind it asked for. struct LegacyDatagrams { + demand: bool, consumer: datagram::Consumer, id: u64, priority: i8, @@ -2726,8 +2727,12 @@ impl LegacyDatagrams { .flatten() .filter(|asked| matches!(asked.poll_info(&kio::Waiter::noop()), Poll::Ready(Ok(_)))); let mut consumer = answered.or_else(|| broadcast.datagrams_live(&name))?; - consumer.set_priority(priority); + let demand = broadcast.has_local_datagrams(); + if demand { + consumer.set_priority(priority); + } Some(Self { + demand, consumer, id, priority, @@ -2739,7 +2744,9 @@ impl LegacyDatagrams { /// Move the datagrams to the subscriber's new `priority`. fn steer(&mut self, priority: i8) { self.priority = priority; - self.consumer.set_priority(priority); + if self.demand { + self.consumer.set_priority(priority); + } if let Some(send) = &mut self.send { send.steer(priority); } diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index 7d068b215ea..69b0a8aa3aa 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -1156,7 +1156,7 @@ impl Consumer { drop(state); return Some(source.datagrams_live(name)?.with_stats(self.stats.clone())); } - state.datagrams.get(name)?.try_consume(self.stats.clone()) + state.datagrams.get(name)?.observe(self.stats.clone()) } /// Whether datagram lookups reach a local producer or handler, rather than a session that diff --git a/rs/moq-net/src/model/datagram.rs b/rs/moq-net/src/model/datagram.rs index 46add01ef5f..7806938d293 100644 --- a/rs/moq-net/src/model/datagram.rs +++ b/rs/moq-net/src/model/datagram.rs @@ -141,7 +141,7 @@ struct State { // Live producer-side handles: producers and an unanswered request. producers: usize, // Each live consumer's subscriber priority. - priorities: slab::Slab, + priorities: slab::Slab>, } impl State { @@ -195,7 +195,7 @@ impl State { } fn priority(&self) -> Option { - self.priorities.iter().map(|(_, priority)| *priority).max() + self.priorities.iter().filter_map(|(_, priority)| *priority).max() } } @@ -209,7 +209,11 @@ pub(super) struct Weak { impl Weak { /// Join the track as a new consumer, unless it already ended. pub(super) fn try_consume(&self, stats: stats::Scope) -> Option { - Consumer::join(self.name.clone(), &self.state, stats) + Consumer::join(self.name.clone(), &self.state, stats, Some(DEFAULT_PRIORITY)) + } + /// Observe a live track without adding demand; its stream subscription already holds that. + pub(super) fn observe(&self, stats: stats::Scope) -> Option { + Consumer::join(self.name.clone(), &self.state, stats, None) } } @@ -314,8 +318,13 @@ impl Producer { /// A new consumer of the track, starting with the next datagram written. pub fn consume(&self) -> Consumer { - Consumer::join(self.name.clone(), &self.state, stats::Scope::default()) - .unwrap_or_else(|| Consumer::ended(self.name.clone(), &self.state)) + Consumer::join( + self.name.clone(), + &self.state, + stats::Scope::default(), + Some(DEFAULT_PRIORITY), + ) + .unwrap_or_else(|| Consumer::ended(self.name.clone(), &self.state)) } /// End the track cleanly: consumers read `None` once they have read what is buffered. @@ -477,12 +486,12 @@ pub struct Consumer { impl Consumer { /// Join a live track, or `None` once it ended. - fn join(name: Arc, state: &kio::Shared, stats: stats::Scope) -> Option { + fn join(name: Arc, state: &kio::Shared, stats: stats::Scope, priority: Option) -> Option { let mut guard = state.lock(); if guard.end.is_some() { return None; } - let slot = guard.priorities.insert(DEFAULT_PRIORITY); + let slot = guard.priorities.insert(priority); let cursor = guard.offset + guard.buffer.len() as u64; drop(guard); Some(Self { @@ -565,14 +574,14 @@ impl Consumer { /// This consumer's subscriber priority. pub fn priority(&self) -> i8 { self.slot - .and_then(|slot| self.state.read().priorities.get(slot).copied()) + .and_then(|slot| self.state.read().priorities.get(slot).copied().flatten()) .unwrap_or(DEFAULT_PRIORITY) } /// Set this consumer's subscriber priority, higher first. pub fn set_priority(&mut self, priority: i8) { if let Some(slot) = self.slot { - self.state.lock().priorities[slot] = priority; + self.state.lock().priorities[slot] = Some(priority); } } @@ -632,6 +641,31 @@ mod tests { } } + #[test] + fn passive_forwarding_keeps_the_tail_without_adding_demand() { + let mut producer = Info::default().produce("audio"); + let mut observer = producer.weak().observe(stats::Scope::default()).unwrap(); + let mut copy = observer.clone(); + assert_eq!(producer.priority(), None); + let mut reader = producer.consume(); + reader.set_priority(-7); + assert_eq!(producer.priority(), Some(-7)); + drop(reader); + assert_eq!(producer.priority(), None); + producer.append(None, &b"tail"[..]).unwrap(); + producer.finish(); + for consumer in [&mut observer, &mut copy] { + let Poll::Ready(Ok(Some(datagram))) = consumer.poll_recv(&kio::Waiter::noop()) else { + panic!("tail lost"); + }; + assert_eq!(datagram.payload.as_ref(), b"tail"); + assert!(matches!( + consumer.poll_recv(&kio::Waiter::noop()), + Poll::Ready(Ok(None)) + )); + } + } + #[test] fn append_counts_its_own_sequence_past_inserts() { let mut producer = Info::default().produce("dg"); diff --git a/rs/moq-net/tests/integration/datagram.rs b/rs/moq-net/tests/integration/datagram.rs index ca70e39ab7c..9ea4cfd08f5 100644 --- a/rs/moq-net/tests/integration/datagram.rs +++ b/rs/moq-net/tests/integration/datagram.rs @@ -532,7 +532,12 @@ async fn an_old_subscriber_keeps_a_stream_track_across_mixed_hops() { /// A datagram track can't be asked for over a version or transport without datagrams. #[kio::test] async fn a_datagram_track_needs_datagrams() { - for (version, transport) in [("moq-lite-03", true), ("moq-lite-04", true), ("moq-lite-07-wip", false), ("moq-transport-22", false)] { + for (version, transport) in [ + ("moq-lite-03", true), + ("moq-lite-04", true), + ("moq-lite-07-wip", false), + ("moq-transport-22", false), + ] { let chain = Chain::new(&[version]).await; if !transport { // The second node subscribes through the session it accepted. From 09a2632b8e35fdb18804e384c12d430cd280d449 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 12:00:49 -0800 Subject: [PATCH 15/23] fix(net): rearm shared datagram demand and cover idle rejoin --- js/net/src/broadcast.ts | 8 +++---- js/net/src/datagram.test.ts | 7 ++++-- js/net/src/datagram.ts | 7 +----- js/net/src/ietf/datagram.test.ts | 8 ++++++- js/net/src/ietf/publisher.ts | 1 - js/net/src/ietf/subscriber.ts | 12 +++++----- js/net/src/internal.ts | 4 ++-- js/net/src/lite/publisher.ts | 1 - rs/moq-net/src/ietf/publisher.rs | 3 +-- rs/moq-net/src/ietf/subscriber.rs | 6 ++--- rs/moq-net/src/ietf/subscriber/datagrams.rs | 8 +++---- rs/moq-net/src/model/broadcast.rs | 3 ++- rs/moq-net/src/model/datagram.rs | 3 ++- rs/moq-net/tests/integration/datagram.rs | 26 +++++++++++++++++++++ 14 files changed, 63 insertions(+), 34 deletions(-) diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index dd5e64f8973..31390b7625b 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -69,14 +69,14 @@ class BroadcastState { } // Datagram tracks never enter the stream listing or cache. -function liveDatagrams(state: BroadcastState, name: string): datagram.Consumer | undefined { +function liveDatagrams(state: BroadcastState, name: string, passive = false): datagram.Consumer | undefined { const producer = state.datagrams.get(name); if (!producer) return; if (producer.closed.peek() !== undefined) { state.datagrams.delete(name); return; } - return producer.consume(); + return passive ? hooks.observeDatagrams(producer) : producer.consume(); } function createDatagrams(state: BroadcastState, name: string, info: Partial): datagram.Producer { @@ -398,7 +398,7 @@ export class Producer { fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => requested(this.#state), datagrams: (name) => datagrams(this.#state, name), - liveDatagrams: (name) => liveDatagrams(this.#state, name), + liveDatagrams: (name) => liveDatagrams(this.#state, name, true), createDatagrams: (name, info) => createDatagrams(this.#state, name, info), requestedDatagrams: () => requestedDatagrams(this.#state), tracks: (owner) => listTracks(this.#state.listing, owner), @@ -555,7 +555,7 @@ export class Consumer { fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => requested(this.#state), datagrams: (name) => datagrams(this.#state, name), - liveDatagrams: (name) => liveDatagrams(this.#state, name), + liveDatagrams: (name) => liveDatagrams(this.#state, name, true), createDatagrams: (name, info) => createDatagrams(this.#state, name, info), requestedDatagrams: () => requestedDatagrams(this.#state), tracks: (owner) => this.#tracks(owner), diff --git a/js/net/src/datagram.test.ts b/js/net/src/datagram.test.ts index 023f29fbedd..0a1af9eba18 100644 --- a/js/net/src/datagram.test.ts +++ b/js/net/src/datagram.test.ts @@ -188,9 +188,12 @@ test("closing one waiting datagram consumer releases its info wait while another test("passive forwarding drains a finished track without adding demand", async () => { const producer = new Datagram.Producer("audio").accept({}); - const observer = producer.consume(); - hooks.passiveDatagrams(observer); + const changes: (number | undefined)[] = []; + const dispose = producer.priority.subscribe((value) => changes.push(value)); + const observer = hooks.observeDatagrams(producer); const copy = observer.clone(); + expect(changes).toEqual([]); + dispose(); expect(producer.priority.peek()).toBeUndefined(); const reader = producer.consume(); reader.setPriority(-7); diff --git a/js/net/src/datagram.ts b/js/net/src/datagram.ts index 9a4e1aad3fa..ee570c3cf48 100644 --- a/js/net/src/datagram.ts +++ b/js/net/src/datagram.ts @@ -102,6 +102,7 @@ export class Producer { } static { + hooks.observeDatagrams = (producer) => consume(producer.name, producer.#state, undefined, null); hooks.closeUnusedDatagrams = (producer) => { if (producer.#state.consumers.size > 0) return false; producer.abort(new StreamError(StreamCode.Cancel)); @@ -245,12 +246,6 @@ export class Consumer { consume = (name, state, cursor, priority) => new Consumer(name, state, cursor, priority); hooks.datagramPriority = (consumer) => consumer.#priority; hooks.datagramClosed = (consumer) => consumer.#closed; - hooks.passiveDatagrams = (consumer) => { - if (consumer.#state.consumers.has(consumer)) { - consumer.#state.consumers.set(consumer, undefined); - consumer.#state.updatePriority(); - } - }; } /** The track's publisher properties, once accepted. */ diff --git a/js/net/src/ietf/datagram.test.ts b/js/net/src/ietf/datagram.test.ts index 153b3ed8813..bce718cb184 100644 --- a/js/net/src/ietf/datagram.test.ts +++ b/js/net/src/ietf/datagram.test.ts @@ -191,7 +191,7 @@ test("a relay shares one IETF subscription across stream and datagram readers", const stream = datagramFirst ? undefined : remote.track("audio").subscribe(); if (stream) await stream.info(); const streamCount = stream && upstream.client.sendStreams.bidi.length; - const reader = remote.datagrams("audio"); + let reader = remote.datagrams("audio"); await reader.info(); const before = streamCount ?? upstream.client.sendStreams.bidi.length; const subscriber = stream ?? remote.track("audio").subscribe(); @@ -211,6 +211,12 @@ test("a relay shares one IETF subscription across stream and datagram readers", subscriber.close(); datagrams.insert({ sequence: 5n, timestamp: Timestamp.fromMillis(11), payload: Uint8Array.of(9) }); expect((await reader.recv())?.payload).toEqual(Uint8Array.of(9)); + reader.close(); + while (track.subscription.peek() !== undefined) await track.subscription.changed(); + reader = remote.datagrams("audio"); + await reader.info(); + datagrams.insert({ sequence: 6n, timestamp: Timestamp.fromMillis(12), payload: Uint8Array.of(10) }); + expect((await reader.recv())?.payload).toEqual(Uint8Array.of(10)); } finally { subscriber.close(); reader.close(); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 0761b2406de..8767af9aa33 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -511,7 +511,6 @@ export class Publisher { ? broadcast.datagrams(msg.trackName) : wireOf(broadcast).liveDatagrams(msg.trackName); if (local) consumer?.setPriority(fromWire(msg.subscriberPriority)); - else if (consumer) hooks.passiveDatagrams(consumer); return consumer; }; let datagrams = datagramOf(); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 346d4079ca4..d8f6b503923 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -104,7 +104,7 @@ type Subscription = { }; /** Both model kinds share this alias; group and datagram receive paths route to their own producer. */ -type Binding = { kind: "track"; subscription: Subscription }; +type Binding = Subscription; // Out-parameter for #openSubscribe: lets the caller observe partial progress // (stream opened, trackAlias registered) so it can clean up on timeout even @@ -1635,7 +1635,7 @@ export class Subscriber { if (!known) state.requests = producer.requests(); const tail = new Tail(); const subscription: Subscription = { track: producer, tail, timescale: ok.properties.timescale, fetchIds: [] }; - const binding: Binding = { kind: "track", subscription }; + const binding = subscription; state.binding = binding; // The subscription starts after Largest, so the joining FETCH or the fill brings its // group's head. With no Largest the track is empty and every group whole. A draft-22 fill @@ -1876,7 +1876,7 @@ export class Subscriber { return; } - const subscription = binding.subscription; + const subscription = binding; const { track, tail } = subscription; // Every data stream counts toward PUBLISH_DONE's Stream Count, even one dropped below. @@ -2209,9 +2209,9 @@ export class Subscriber { } const alias = this.#aliases.peek(datagram.trackAlias); - const binding = alias?.subscription.datagrams && { - producer: alias.subscription.datagrams.producer, - timescale: alias.subscription.timescale, + const binding = alias?.datagrams && { + producer: alias.datagrams.producer, + timescale: alias.timescale, }; if (!binding) { console.debug(`dropping a datagram for an unknown alias: alias=${datagram.trackAlias}`); diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index b0296b35b7b..716b9986511 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -448,7 +448,7 @@ export const hooks: { /** Follow this consumer's subscriber priority for transport scheduling. */ datagramPriority: (consumer: datagram.Consumer) => Getter; /** Forward data carried by an existing stream without contributing another subscription's demand. */ - passiveDatagrams: (consumer: datagram.Consumer) => void; + observeDatagrams: (producer: datagram.Producer) => datagram.Consumer; /** Settles once this consumer is closed, the end of whatever request it serves. */ datagramClosed: (consumer: datagram.Consumer) => GetPromise; /** @@ -748,7 +748,7 @@ export const hooks: { closeUnusedDatagrams: () => { throw new Error("datagram module not loaded"); }, - passiveDatagrams: () => { + observeDatagrams: () => { throw new Error("datagram module not loaded"); }, datagramPriority: () => { diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index eda9be88fb8..b80407e7fab 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1082,7 +1082,6 @@ export class Publisher { legacy = wireOf(front).liveDatagrams(msg.track); if (legacy) { if (wireOf(front).localDatagrams) legacy.setPriority(msg.priority); - else hooks.passiveDatagrams(legacy); datagrams = this.#runDatagrams(legacy, { sub: msg.id, stamp, priority: legacyPriority }) .then(() => {}) .catch((err) => console.debug(`datagram send stopped: ${reason(err)}`)); diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 986f0d314da..3a103b36b59 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -9044,7 +9044,7 @@ mod subscribe_cursor_test { #[cfg(test)] mod serve_tests { use super::*; - use crate::coding::{Decode, Decoder, Encode as _}; + use crate::coding::{Decode, Decoder}; use crate::ietf::GroupOrder; use crate::lite::test_transport::{Close, Log, ScriptedSession, Sent, SinkSession}; use crate::model::ProduceTest; @@ -20891,7 +20891,6 @@ mod fill_range_tests { #[cfg(test)] mod compression_tests { use super::*; - use crate::coding::Encode as _; #[test] fn sealed_compression_refuses_incompatible_egress() { for version in [Version::Draft18, Version::Draft20, Version::Draft22] { diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 779b6beb44f..494f4267986 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -3301,7 +3301,7 @@ impl RunBroadcast { request.reject(Error::Unsupported); continue; } - let key = (self.path.clone(), request.name().to_owned()); + let key = (self.path.clone(), request.name().into()); let consumer = self.source.consume(); match consumer.track(request.name()) { Ok(track) => { @@ -5711,7 +5711,7 @@ impl Subscriber { kio::wait(|waiter| recv.poll(waiter, self, stream)).await } - /// Drop an OBJECT_DATAGRAM: a subscription is a stream track's, which takes no datagrams. + /// Route OBJECT_DATAGRAM to the shared subscription's datagram model half. /// /// A malformed datagram is still a protocol error. pub fn recv_datagram(&self, payload: bytes::Bytes) -> Result<(), Error> { @@ -8963,7 +8963,7 @@ mod tests { { let mut state = subscriber.state.lock(); let track = state.subscribes.get_mut(&RequestId(11)).unwrap(); - // This subscription asked for datagrams, not the stream track. + // Only the datagram model half is present in this receive fixture. track.producer = None; track.datagram = Some(producer); track.timescale = Some(Timescale::MILLI); diff --git a/rs/moq-net/src/ietf/subscriber/datagrams.rs b/rs/moq-net/src/ietf/subscriber/datagrams.rs index 99d6cef9f69..62bc9e13486 100644 --- a/rs/moq-net/src/ietf/subscriber/datagrams.rs +++ b/rs/moq-net/src/ietf/subscriber/datagrams.rs @@ -116,14 +116,14 @@ impl Shared { }) } pub(super) fn poll(&mut self, waiter: &kio::Waiter) { - if let Poll::Ready(priority) = self.producer.poll_priority(waiter, self.priority) { + while let Poll::Ready(priority) = self.producer.poll_priority(waiter, self.priority) { self.priority = priority; match priority { None => self.held = None, Some(priority) => match &mut self.held { Some(held) => { if let Err(err) = held.update(held.subscription().with_priority(priority)) { - self.producer.abort(err); + self.producer.clone().abort(err); } } None => { @@ -140,8 +140,8 @@ impl Shared { pub(super) fn end(&mut self, end: Result<(), Error>) { self.held = None; match end { - Ok(()) => self.producer.finish(), - Err(err) => self.producer.abort(err), + Ok(()) => self.producer.clone().finish(), + Err(err) => self.producer.clone().abort(err), } } } diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index 69b0a8aa3aa..e7dccb1f28a 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -1144,7 +1144,8 @@ impl Consumer { } /// Join the datagram track `name` if one is published or already asked for, asking no - /// handler for it otherwise. + /// handler for it otherwise. The reader adds no demand until `set_priority` is called: + /// an existing stream subscription already carries this forwarding work. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn datagrams_live(&self, name: &str) -> Option { let state = self.state.lock(); diff --git a/rs/moq-net/src/model/datagram.rs b/rs/moq-net/src/model/datagram.rs index 7806938d293..8b44002e0f4 100644 --- a/rs/moq-net/src/model/datagram.rs +++ b/rs/moq-net/src/model/datagram.rs @@ -140,7 +140,7 @@ struct State { end: Option>, // Live producer-side handles: producers and an unanswered request. producers: usize, - // Each live consumer's subscriber priority. + // Each live consumer's priority, or None for passive forwarding over an existing stream. priorities: slab::Slab>, } @@ -211,6 +211,7 @@ impl Weak { pub(super) fn try_consume(&self, stats: stats::Scope) -> Option { Consumer::join(self.name.clone(), &self.state, stats, Some(DEFAULT_PRIORITY)) } + /// Observe a live track without adding demand; its stream subscription already holds that. pub(super) fn observe(&self, stats: stats::Scope) -> Option { Consumer::join(self.name.clone(), &self.state, stats, None) diff --git a/rs/moq-net/tests/integration/datagram.rs b/rs/moq-net/tests/integration/datagram.rs index 9ea4cfd08f5..882749edc18 100644 --- a/rs/moq-net/tests/integration/datagram.rs +++ b/rs/moq-net/tests/integration/datagram.rs @@ -549,3 +549,29 @@ async fn a_datagram_track_needs_datagrams() { assert!(matches!(err, Error::Unsupported), "{version}: {err:?}"); } } + +/// Keeping the broadcast handle across an idle subscription must not leave its datagram +/// readers attached to the old shared producer or accept stale stream metadata as an answer. +#[kio::test] +async fn ietf_datagram_readers_rejoin_a_lingering_broadcast() { + let chain = Chain::new(&["moq-transport-18", "moq-transport-22"]).await; + let mut producer = chain.publication.create_datagrams(TRACK, timed()).unwrap(); + let remote = chain.remote().await; + for priority in [2, 3] { + let mut consumer = remote.datagrams(TRACK).unwrap(); + consumer.set_priority(priority); + kio::clock::timeout(TIMEOUT, datagram_priority(&producer, Some(priority))) + .await + .expect("the rejoined request never reached the publisher"); + kio::clock::timeout(TIMEOUT, consumer.info()) + .await + .expect("metadata timeout") + .unwrap(); + producer.append(millis(priority as u64), &b"live"[..]).unwrap(); + assert_eq!(recv(&mut consumer).await.unwrap().unwrap().payload.as_ref(), b"live"); + drop(consumer); + kio::clock::timeout(TIMEOUT, datagram_priority(&producer, None)) + .await + .expect("the shared subscription stayed wanted"); + } +} From fc1da930876536517ab534437a46a76b0dcef032 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 12:04:31 -0800 Subject: [PATCH 16/23] fix(net): await shared datagram acceptance after idle resume --- doc/concept/standard.md | 2 +- js/net/MIGRATE.md | 3 +- quest/m1/datagram-tracks/README.md | 10 ++-- quest/m1/datagram-tracks/ietf.md | 61 --------------------- quest/m2/net-wrap/e2ee.md | 7 +-- rs/moq-net/MIGRATE.md | 3 +- rs/moq-net/src/ietf/subscriber/datagrams.rs | 26 +++++---- 7 files changed, 27 insertions(+), 85 deletions(-) delete mode 100644 quest/m1/datagram-tracks/ietf.md diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 12ddb300577..fb01f9701a2 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -36,7 +36,7 @@ subset drops. What a peer actually observes against this implementation: - **One `SETUP` credential**, forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A second `SETUP` token closes the session. We advertise no token cache, so an alias reference in `SETUP` closes it with `PROTOCOL_VIOLATION`, one on a request with `UNKNOWN_AUTH_TOKEN_ALIAS`, and a registration on a request with `AUTH_TOKEN_CACHE_OVERFLOW`. Where the Rust side authorizes the peer or verifies tokens, each distinct request token is verified once while any open request on that session names it, and its grant authorizes only requests naming it, even where that authorization already covers it; it never widens the session. It is refused with `NOT_SUPPORTED` when nothing verifies it, when it is empty, or when its Token Type is anything but 0 (out of band). A side that does neither, JavaScript included, ignores the token. Rust refuses a request carrying more than one distinct token with `NOT_SUPPORTED`, since it verifies one; JavaScript, which verifies none, ignores them all. A token-authorized `SUBSCRIBE` still stays within the grant the peer gave us. A token-authorized `PUBLISH_NAMESPACE` needs a grant covering everything beneath the namespace, and the tracks we subscribe under it rely on that announce instead of our limit, ending with it. An update on a token-authorized request moves it to the token it carries; the old token ends with its last naming request. A request is admitted by the session or by a token for good, so an update naming a token on one the session admitted is refused with `NOT_SUPPORTED`. A token-less `PUBLISH_NAMESPACE` our limit does not cover is refused `UNAUTHORIZED`, and one the limit stops covering ends; the publisher re-announces once it widens. Expiry or revocation ends every request naming that token. The relay reports the lease end to its auth server, and a later request naming the token verifies it again. A refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, and swapping it renews the request with `REQUEST_UPDATE`; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` carries no token. A token added to a reconnecting connection's auth handle rides every session it makes, so the announcement keeps it across a reconnect; one added to a single session's handle ends with that session. Rust honors a token on any supported request; discovery reveals only paths it grants, and a joining `FETCH` keeps following its subscription. A request repeating a token is refused with `MALFORMED_AUTH_TOKEN`. The negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. - **Pause delivery.** `FORWARD=0` on a `SUBSCRIBE` or its update drops the subscription, closing its data streams and holding nothing upstream, while the request and its slot stay. `FORWARD=1` subscribes again as a new `SUBSCRIBE` with the same filter would, so nothing carries across the pause: its `REQUEST_OK` names the live edge it joined at. A paused update may change only priority and max age. - **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: `SUBSCRIBE_TRACKS`, multi-group `FETCH`, and joining requests reaching before the joined group get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` from draft 20 because the advertised filter limit is zero. From draft 20, namespaces whose first tuple field is exactly `.` or `.session` get `DOES_NOT_EXIST` before application lookup; other dot prefixes still reach the application. Subscription updates accept priority, negotiated subscriber max age, and delivery timeouts. Rust also carries range changes, keeps bounded subscriptions updatable, renews request tokens, and judges `NEW_GROUP_REQUEST` as a join (see dynamic groups below). From draft 20, a wider or historical fill fails on its separate fetch stream while its subscription stays open; a largest-group fill hands its head to the live subgroup tail. Other supported-but-unimplemented preferences end that subscription; a `TRACK_PROPERTY_FILTER` there closes the session with `PROTOCOL_VIOLATION`, since it belongs only to `SUBSCRIBE_TRACKS`. A namespace update may change its prefix; suffixes after the acknowledgement are relative to that new prefix. -- **Objects and datagrams.** Only subgroup 0 is accepted: a stream for any other subgroup, whether its header gives the ID or takes it from the first Object ID, is stopped and the session stays up. Rust relays preserve Object IDs, gaps, object properties, FIRST\_OBJECT, and END\_OF\_GROUP status objects between moq-transport peers for subgroup 0, including across a moq-lite-07 hop between two relays that negotiate the [ietf](/draft/moq-ietf) extension. Plain Rust readers and moq-lite peers without the extension see consecutive frames. moq-transport maps a [datagram track](/concept/moq-lite#datagrams) to `SUBSCRIBE` and `OBJECT_DATAGRAM`, with its sequence as the group ID and object ID 0. A datagram subscription keeps only those datagrams; a stream subscription drops every datagram. An undefined Object Status closes the session, and so does a malformed datagram. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none, as for a plain frame; an immutable one that would change or vanish is refused. On a timed track the frame holds each object's time, so a mutable Timestamp that only repeats the immutable one is not restated; a status object has no frame, so it keeps its Timestamp among its properties. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped between draft 18 and later drafts, or refused when immutable, because draft 20 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. A status object carrying properties is refused. A `FETCH` has no status objects, so its response ends at the last frame. A group cannot resume across route changes by frame ordinal once Object IDs skip ahead of the ordinals on either route; recovery refuses it instead of splicing different objects. +- **Objects and datagrams.** Only subgroup 0 is accepted: a stream for any other subgroup, whether its header gives the ID or takes it from the first Object ID, is stopped and the session stays up. Rust relays preserve Object IDs, gaps, object properties, FIRST\_OBJECT, and END\_OF\_GROUP status objects between moq-transport peers for subgroup 0, including across a moq-lite-07 hop between two relays that negotiate the [ietf](/draft/moq-ietf) extension. Plain Rust readers and moq-lite peers without the extension see consecutive frames. moq-transport maps a [datagram track](/concept/moq-lite#datagrams) to `SUBSCRIBE` and `OBJECT_DATAGRAM`, with its sequence as the group ID and object ID 0. One upstream subscription serves both kinds of a name. Stream consumers receive only stream objects, and datagram consumers receive only object 0 datagrams, including through relays. An undefined Object Status closes the session, and so does a malformed datagram. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none, as for a plain frame; an immutable one that would change or vanish is refused. On a timed track the frame holds each object's time, so a mutable Timestamp that only repeats the immutable one is not restated; a status object has no frame, so it keeps its Timestamp among its properties. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped between draft 18 and later drafts, or refused when immutable, because draft 20 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. A status object carrying properties is refused. A `FETCH` has no status objects, so its response ends at the last frame. A group cannot resume across route changes by frame ordinal once Object IDs skip ahead of the ordinals on either route; recovery refuses it instead of splicing different objects. - **Dynamic groups.** A track advertises `DYNAMIC_GROUPS` when its publisher sets a refresh ratio, and a track received with `DYNAMIC_GROUPS` gets a ratio of 0. A `NEW_GROUP_REQUEST` counts as one more subscriber joining, which a `SUBSCRIBE` already is: it asks for a new group only when the newest group's bytes after its first frame exceed the ratio times the first frame. Joins coalesce until the asked-for group arrives. A Rust relay sends `NEW_GROUP_REQUEST` upstream, on the `SUBSCRIBE` or in a `REQUEST_UPDATE`, once a join wants a group past the newest it holds; a relay holding nothing yet asks for none. On a track without a ratio, an update carrying one ends the subscription with `UPDATE_FAILED`. A moq-lite-07 hop (`moq-lite-07-wip`, opt-in) carries the same demand natively: the ratio in `TRACK_INFO` and the asked-for group in `SUBSCRIBE_UPDATE`. Earlier moq-lite versions carry neither, so a track received over one never refreshes. - **Priority.** Higher is served first. This stack's priorities run from -128 to 127 with 0 unset, and go out on the IETF wire, which ranks lower first, as `127 - priority`: an unset priority is IETF 127. An omitted subscriber or publisher priority reads as the drafts' default of IETF 128, one step below unset, so a relay forwards it unchanged. A relay's upstream subscription carries the highest priority its subscribers ask for and updates it as they come and go; its group order stays as the `SUBSCRIBE` asked, since no draft lets an update change it. - **Size.** An object extension block larger than 64 KiB ends that subgroup stream. The session stays up. This cap is ours, not the draft's. diff --git a/js/net/MIGRATE.md b/js/net/MIGRATE.md index 94d985c3c3d..e613eb1963d 100644 --- a/js/net/MIGRATE.md +++ b/js/net/MIGRATE.md @@ -7,7 +7,8 @@ How to update your code for each breaking change, newest first. ### moq-transport datagram tracks Datagram tracks now work over moq-transport using SUBSCRIBE and OBJECT\_DATAGRAM. -The sequence is the group ID and the object ID is zero. Stream and datagram +The sequence is the group ID and the object ID is zero. One upstream subscription +serves both kinds of a name, including through relays. Stream and datagram consumers keep only their requested kind; nonzero object IDs are dropped by datagram consumers. No application API or wire encoding changes are required. diff --git a/quest/m1/datagram-tracks/README.md b/quest/m1/datagram-tracks/README.md index e6da124a255..6b754d4a0cd 100644 --- a/quest/m1/datagram-tracks/README.md +++ b/quest/m1/datagram-tracks/README.md @@ -54,10 +54,11 @@ Decided 2026-11-01 (maintainer, in `/quest-plan`): counter. The datagram types have no group, frame, range, fetch, or cache API, so the group terminology disappears for datagrams. - **moq-transport mixing.** ✅ Keep the requested kind. - - A datagram-track subscription maps to IETF SUBSCRIBE and keeps only - datagrams (object 0). Stream objects arriving on it are dropped and - logged. - - A stream-track subscription drops datagrams. + - One upstream IETF SUBSCRIBE serves both kinds of a name, as on lite-05/06. + Decided 2026-11-05 (maintainer), on + [moq-dev/moq#7959](https://github.com/moq-dev/moq/pull/7959). + - Stream consumers receive only stream objects; datagram consumers receive + only datagrams with object ID 0. A relay forwards both kinds independently. - Outbound, a datagram track sends only OBJECT_DATAGRAM with object ID 0. - **Archive.** ✅ moq-archive skips datagram tracks. A datagram track is live-only, and an archive's playback doesn't include it. @@ -120,7 +121,6 @@ datagram track on lite-07, direct and through a relay. ## Required -- [moq-transport datagram tracks](/quest/m1/datagram-tracks/ietf.md) - Rust and JS map a datagram track to SUBSCRIBE and OBJECT_DATAGRAM with object ID 0, keeping the requested kind - [A datagram sent right behind its answer reaches the subscriber](/quest/m1/datagram-tracks/answer-race.md) - receivers decode answers before routing datagrams, and count the ones they still can't route ## Related diff --git a/quest/m1/datagram-tracks/ietf.md b/quest/m1/datagram-tracks/ietf.md deleted file mode 100644 index a20c2690d77..00000000000 --- a/quest/m1/datagram-tracks/ietf.md +++ /dev/null @@ -1,61 +0,0 @@ -# [S] moq-transport datagram tracks - -## Goal - -Over moq-transport, Rust and JS map a datagram track to IETF SUBSCRIBE and -OBJECT_DATAGRAM, with its sequence as the group ID and object ID 0, and -keep the requested kind when a peer mixes datagrams and streams on one track. - -## Plan - -Maintainer decision, 2026-11-05: “7959 should make one upstream IETF -subscription like 05/06”. Share the upstream SUBSCRIBE for a name across local -stream and datagram consumers, with independent requested-kind delivery and -correct aggregate stream demand. Preserve full relay support and keep the -sharing machinery private to the IETF adapter. - -Implementation: [moq-dev/moq#7959](https://github.com/moq-dev/moq/pull/7959). - -Decided 2026-11-01 (maintainer), in the -[epic](/quest/m1/datagram-tracks/README.md): - -- A datagram-track subscription keeps only datagrams with object ID 0. - Stream objects arriving on it are dropped and logged once per - subscription. -- A stream-track subscription drops datagrams. -- Outbound, a datagram track sends only OBJECT_DATAGRAM with object ID 0. - A stream track never sends OBJECT_DATAGRAM. -- A datagram with a nonzero object ID on a datagram-track subscription is - dropped and logged. - -Touches `rs/moq-net/src/ietf/datagram.rs` and the JS IETF adapter. Test -against moxygen in the interop lanes where it sends datagrams. - -Rust datagram tracks ([moq-dev/moq#7752](https://github.com/moq-dev/moq/pull/7752)) -left moq-transport without datagrams in Rust until this lands: the IETF -subscriber drops every OBJECT_DATAGRAM, `datagrams()` on an IETF source is -NOT_FOUND, and a stream track never sends one. Restore what it removed: - -- the Rust-JS `datagram` tail lanes in `test/interop/interop.sh`, the - `datagram` mode of `test/interop/clients/js-native/tail.ts`, and their - README bullet; -- moq-e2ee's `datagrams_over_ietf` transport test; -- the moq-transport rows of moq-tokio's `datagram_delivery` bench and - moq-net's `session_delivery_datagram`; -- the OBJECT_DATAGRAM sentence in `doc/concept/standard.md`. - -An OBJECT_DATAGRAM whose Object Status is undefined is a protocol violation -in JS too, as it is in Rust, on every subscription kind. Planned 2026-11-04 -with the maintainer, from -[moq-dev/moq#7846](https://github.com/moq-dev/moq/pull/7846). Rust's -`recv_datagram` (`rs/moq-net/src/ietf/subscriber.rs`, about line 4839) -closes the session with `ProtocolViolation` when `status_defined` -(`rs/moq-net/src/ietf/object.rs`, about line 21: Normal, END_OF_GROUP and -END_OF_TRACK) rejects the status, even while it drops the datagram. JS's -`#recvDatagram` (`js/net/src/ietf/subscriber.ts`) decodes and drops it -silently, on main and after #7846. Check the shared status set once for -both datagram paths, so a datagram-track subscription keeps the check -when it starts accepting datagrams, and add a JS test that an undefined -status closes the session. - -Public API: none beyond the model. Wire: no format change. diff --git a/quest/m2/net-wrap/e2ee.md b/quest/m2/net-wrap/e2ee.md index 35046297d52..2010446d9a4 100644 --- a/quest/m2/net-wrap/e2ee.md +++ b/quest/m2/net-wrap/e2ee.md @@ -63,10 +63,9 @@ the reverse (decided 2026-10-20, maintainer): Subscribe ID, Sequence, and Timestamp, since datagram tracks dropped the relayed `ietf` fields and the Inflated Size from it, so `MAX_DATAGRAM_HEADER` (28, `rs/moq-crypto/src/e2ee/limits.rs` and - `js/crypto/src/e2ee/limits.ts`) covers every version. Re-check it if - moq-transport datagram tracks - ([moq-transport datagram tracks](/quest/m1/datagram-tracks/ietf.md)) add - header fields. + `js/crypto/src/e2ee/limits.ts`) covers moq-lite. [moq-dev/moq#7959](https://github.com/moq-dev/moq/pull/7959) + restores moq-transport datagrams using existing OBJECT_DATAGRAM framing, + without adding header fields. Re-check the allowance if that framing changes. - Ranges: every moq-net range starts at a group's first frame ([moq-dev/moq#6465](https://github.com/moq-dev/moq/pull/6465)), so the wrapper reads each physical group whole and inflates from frame 0. diff --git a/rs/moq-net/MIGRATE.md b/rs/moq-net/MIGRATE.md index 9aa2e5f18c4..0dcf4c743f8 100644 --- a/rs/moq-net/MIGRATE.md +++ b/rs/moq-net/MIGRATE.md @@ -7,7 +7,8 @@ How to update your code for each breaking change, newest first. ### moq-transport datagram tracks Datagram tracks now work over moq-transport using SUBSCRIBE and OBJECT\_DATAGRAM. -The sequence is the group ID and the object ID is zero. Stream and datagram +The sequence is the group ID and the object ID is zero. One upstream subscription +serves both kinds of a name, including through relays. Stream and datagram consumers keep only their requested kind; nonzero object IDs are dropped by datagram consumers. No application API or wire encoding changes are required. diff --git a/rs/moq-net/src/ietf/subscriber/datagrams.rs b/rs/moq-net/src/ietf/subscriber/datagrams.rs index 62bc9e13486..e0c9e41ba66 100644 --- a/rs/moq-net/src/ietf/subscriber/datagrams.rs +++ b/rs/moq-net/src/ietf/subscriber/datagrams.rs @@ -48,18 +48,18 @@ impl kio::Task for Requested { fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { let priority = { let state = self.this.state.lock(); - if let Some((_, request)) = state.datagram_requests.get(&self.key) - && request.poll_unused(waiter).is_ready() - && request.close_unused() - { + let Some((id, request)) = state.datagram_requests.get(&self.key) else { + return Poll::Ready(()); + }; + if !std::sync::Arc::ptr_eq(id, &self.id) { + return Poll::Ready(()); + } + if request.poll_unused(waiter).is_ready() && request.close_unused() { drop(state); self.reject(Error::Cancel); return Poll::Ready(()); } - state - .datagram_requests - .get(&self.key) - .and_then(|(_, request)| request.priority()) + request.priority() }; if let Some(priority) = priority && priority != self.priority @@ -73,11 +73,13 @@ impl kio::Task for Requested { return Poll::Ready(()); } } - match ready!(self.subscribing.poll_ok(waiter)) { - Ok(_) => {} - Err(err) => self.reject(err), + if let Poll::Ready(Err(err)) = self.subscribing.poll_ok(waiter) { + self.reject(err); + return Poll::Ready(()); } - Poll::Ready(()) + // A lingering track already knows its metadata. Only Shared accepting this request + // confirms that its new upstream subscription also owns the datagram half. + Poll::Pending } } impl Drop for Requested { From 525a608063c8a523974efc7bee1b805a45861873 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 12:27:24 -0800 Subject: [PATCH 17/23] fix(net): share datagram subscription lifecycle across pause and resume --- doc/concept/standard.md | 2 +- js/net/src/ietf/datagram.test.ts | 10 +- js/net/src/ietf/publisher.test.ts | 50 ++++++ js/net/src/ietf/publisher.ts | 233 ++++++-------------------- js/net/src/ietf/subscriber.ts | 42 +++-- rs/moq-net/src/ietf/publisher.rs | 139 +++++++++------ rs/moq-net/src/ietf/subscriber.rs | 22 ++- rs/moq-net/src/lite/test_transport.rs | 9 +- 8 files changed, 258 insertions(+), 249 deletions(-) diff --git a/doc/concept/standard.md b/doc/concept/standard.md index fb01f9701a2..45cecd8c889 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -36,7 +36,7 @@ subset drops. What a peer actually observes against this implementation: - **One `SETUP` credential**, forwarded to the [auth server](/bin/relay/auth#the-contract) unverified. A second `SETUP` token closes the session. We advertise no token cache, so an alias reference in `SETUP` closes it with `PROTOCOL_VIOLATION`, one on a request with `UNKNOWN_AUTH_TOKEN_ALIAS`, and a registration on a request with `AUTH_TOKEN_CACHE_OVERFLOW`. Where the Rust side authorizes the peer or verifies tokens, each distinct request token is verified once while any open request on that session names it, and its grant authorizes only requests naming it, even where that authorization already covers it; it never widens the session. It is refused with `NOT_SUPPORTED` when nothing verifies it, when it is empty, or when its Token Type is anything but 0 (out of band). A side that does neither, JavaScript included, ignores the token. Rust refuses a request carrying more than one distinct token with `NOT_SUPPORTED`, since it verifies one; JavaScript, which verifies none, ignores them all. A token-authorized `SUBSCRIBE` still stays within the grant the peer gave us. A token-authorized `PUBLISH_NAMESPACE` needs a grant covering everything beneath the namespace, and the tracks we subscribe under it rely on that announce instead of our limit, ending with it. An update on a token-authorized request moves it to the token it carries; the old token ends with its last naming request. A request is admitted by the session or by a token for good, so an update naming a token on one the session admitted is refused with `NOT_SUPPORTED`. A token-less `PUBLISH_NAMESPACE` our limit does not cover is refused `UNAUTHORIZED`, and one the limit stops covering ends; the publisher re-announces once it widens. Expiry or revocation ends every request naming that token. The relay reports the lease end to its auth server, and a later request naming the token verifies it again. A refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, and swapping it renews the request with `REQUEST_UPDATE`; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` carries no token. A token added to a reconnecting connection's auth handle rides every session it makes, so the announcement keeps it across a reconnect; one added to a single session's handle ends with that session. Rust honors a token on any supported request; discovery reveals only paths it grants, and a joining `FETCH` keeps following its subscription. A request repeating a token is refused with `MALFORMED_AUTH_TOKEN`. The negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. - **Pause delivery.** `FORWARD=0` on a `SUBSCRIBE` or its update drops the subscription, closing its data streams and holding nothing upstream, while the request and its slot stay. `FORWARD=1` subscribes again as a new `SUBSCRIBE` with the same filter would, so nothing carries across the pause: its `REQUEST_OK` names the live edge it joined at. A paused update may change only priority and max age. - **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: `SUBSCRIBE_TRACKS`, multi-group `FETCH`, and joining requests reaching before the joined group get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` from draft 20 because the advertised filter limit is zero. From draft 20, namespaces whose first tuple field is exactly `.` or `.session` get `DOES_NOT_EXIST` before application lookup; other dot prefixes still reach the application. Subscription updates accept priority, negotiated subscriber max age, and delivery timeouts. Rust also carries range changes, keeps bounded subscriptions updatable, renews request tokens, and judges `NEW_GROUP_REQUEST` as a join (see dynamic groups below). From draft 20, a wider or historical fill fails on its separate fetch stream while its subscription stays open; a largest-group fill hands its head to the live subgroup tail. Other supported-but-unimplemented preferences end that subscription; a `TRACK_PROPERTY_FILTER` there closes the session with `PROTOCOL_VIOLATION`, since it belongs only to `SUBSCRIBE_TRACKS`. A namespace update may change its prefix; suffixes after the acknowledgement are relative to that new prefix. -- **Objects and datagrams.** Only subgroup 0 is accepted: a stream for any other subgroup, whether its header gives the ID or takes it from the first Object ID, is stopped and the session stays up. Rust relays preserve Object IDs, gaps, object properties, FIRST\_OBJECT, and END\_OF\_GROUP status objects between moq-transport peers for subgroup 0, including across a moq-lite-07 hop between two relays that negotiate the [ietf](/draft/moq-ietf) extension. Plain Rust readers and moq-lite peers without the extension see consecutive frames. moq-transport maps a [datagram track](/concept/moq-lite#datagrams) to `SUBSCRIBE` and `OBJECT_DATAGRAM`, with its sequence as the group ID and object ID 0. One upstream subscription serves both kinds of a name. Stream consumers receive only stream objects, and datagram consumers receive only object 0 datagrams, including through relays. An undefined Object Status closes the session, and so does a malformed datagram. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none, as for a plain frame; an immutable one that would change or vanish is refused. On a timed track the frame holds each object's time, so a mutable Timestamp that only repeats the immutable one is not restated; a status object has no frame, so it keeps its Timestamp among its properties. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped between draft 18 and later drafts, or refused when immutable, because draft 20 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. A status object carrying properties is refused. A `FETCH` has no status objects, so its response ends at the last frame. A group cannot resume across route changes by frame ordinal once Object IDs skip ahead of the ordinals on either route; recovery refuses it instead of splicing different objects. +- **Objects and datagrams.** Only subgroup 0 is accepted: a stream for any other subgroup, whether its header gives the ID or takes it from the first Object ID, is stopped and the session stays up. Rust relays preserve Object IDs, gaps, object properties, FIRST\_OBJECT, and END\_OF\_GROUP status objects between moq-transport peers for subgroup 0, including across a moq-lite-07 hop between two relays that negotiate the [ietf](/draft/moq-ietf) extension. Plain Rust readers and moq-lite peers without the extension see consecutive frames. moq-transport maps a [datagram track](/concept/moq-lite#datagrams) to `SUBSCRIBE` and `OBJECT_DATAGRAM`, with its sequence as the group ID and object ID 0. One upstream subscription serves both kinds of a name. Stream consumers receive only stream objects, and datagram consumers receive only object 0 datagrams, including through relays. An undefined Object Status or malformed datagram framing closes the session. Unreadable object properties drop only that datagram. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none, as for a plain frame; an immutable one that would change or vanish is refused. On a timed track the frame holds each object's time, so a mutable Timestamp that only repeats the immutable one is not restated; a status object has no frame, so it keeps its Timestamp among its properties. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped between draft 18 and later drafts, or refused when immutable, because draft 20 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. A status object carrying properties is refused. A `FETCH` has no status objects, so its response ends at the last frame. A group cannot resume across route changes by frame ordinal once Object IDs skip ahead of the ordinals on either route; recovery refuses it instead of splicing different objects. - **Dynamic groups.** A track advertises `DYNAMIC_GROUPS` when its publisher sets a refresh ratio, and a track received with `DYNAMIC_GROUPS` gets a ratio of 0. A `NEW_GROUP_REQUEST` counts as one more subscriber joining, which a `SUBSCRIBE` already is: it asks for a new group only when the newest group's bytes after its first frame exceed the ratio times the first frame. Joins coalesce until the asked-for group arrives. A Rust relay sends `NEW_GROUP_REQUEST` upstream, on the `SUBSCRIBE` or in a `REQUEST_UPDATE`, once a join wants a group past the newest it holds; a relay holding nothing yet asks for none. On a track without a ratio, an update carrying one ends the subscription with `UPDATE_FAILED`. A moq-lite-07 hop (`moq-lite-07-wip`, opt-in) carries the same demand natively: the ratio in `TRACK_INFO` and the asked-for group in `SUBSCRIBE_UPDATE`. Earlier moq-lite versions carry neither, so a track received over one never refreshes. - **Priority.** Higher is served first. This stack's priorities run from -128 to 127 with 0 unset, and go out on the IETF wire, which ranks lower first, as `127 - priority`: an unset priority is IETF 127. An omitted subscriber or publisher priority reads as the drafts' default of IETF 128, one step below unset, so a relay forwards it unchanged. A relay's upstream subscription carries the highest priority its subscribers ask for and updates it as they come and go; its group order stays as the `SUBSCRIBE` asked, since no draft lets an update change it. - **Size.** An object extension block larger than 64 KiB ends that subgroup stream. The session stays up. This cap is ours, not the draft's. diff --git a/js/net/src/ietf/datagram.test.ts b/js/net/src/ietf/datagram.test.ts index bce718cb184..37f1651189d 100644 --- a/js/net/src/ietf/datagram.test.ts +++ b/js/net/src/ietf/datagram.test.ts @@ -1,4 +1,4 @@ -import { expect, test } from "bun:test"; +import { expect, spyOn, test } from "bun:test"; import { accept, connect } from "../connection/index.ts"; import { ProtocolViolation, SessionCode } from "../error.ts"; import { createMockTransportPair } from "../mock.ts"; @@ -144,6 +144,14 @@ test("a datagram track round-trips over moq-transport", async () => { while (datagrams.priority.peek() !== 1) await datagrams.priority.changed(); expect(info.priority).toBe(3); expect(info.timescale).toBe(Timescale.MILLI); + // A peer's unreadable properties lose one best-effort payload, not the session. + const encode = ObjectDatagram.prototype.encode; + const corrupt = spyOn(ObjectDatagram.prototype, "encode").mockImplementation(function (this: ObjectDatagram, version) { + corrupt.mockRestore(); + return encode.call(new ObjectDatagram({ ...this, properties: Uint8Array.of(0x10) }), version); + }); + datagrams.insert({ sequence: 3n, timestamp: Timestamp.fromMillis(8), payload: Uint8Array.of(0) }); + datagrams.insert({ sequence: 4n, timestamp: Timestamp.fromMillis(9), payload: new Uint8Array([7]) }); const received = await reader.recv(); expect(received?.sequence).toBe(4n); diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 5acaa91461a..5d01166a968 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -25,6 +25,7 @@ import type { Producer as TrackProducer } from "../track.ts"; import { unwanted } from "../util/demand.ts"; import { wireOf } from "../wire.ts"; import type * as Cluster from "./cluster.ts"; +import { ObjectDatagram } from "./datagram.ts"; import { toRequestCode } from "./error.ts"; import { Fetch, FetchHeader } from "./fetch.ts"; import * as Filter from "./filter.ts"; @@ -3583,6 +3584,55 @@ test("a SUBSCRIBE pauses without demand and resumes as a new subscriber", async } }); +test("a datagram-only SUBSCRIBE releases demand on pause and resumes", async () => { + for (const startPaused of [false, true]) { + const fx = fixture(V22); + const datagrams = fx.broadcast.createDatagrams("audio", {}); + const received = fx.pair.client.datagrams.readable.getReader(); + const { client } = await runSubscribe( + fx, + new Subscribe({ + requestId: 0n, + trackNamespace: Path.from("test"), + trackName: "audio", + subscriberPriority: 128, + forward: !startPaused, + }), + ); + const forward = async (id: bigint, on: boolean) => { + const parameters = new Parameters(); + parameters.forward = on; + await client.writer.u53(SubscribeUpdate.id); + await new SubscribeUpdate({ requestId: id, parameters }).encode(client.writer, V22); + expect(await client.reader.u53()).toBe(RequestOk.id); + await RequestOk.decode(client.reader, V22); + }; + try { + if (!startPaused) { + datagrams.append(undefined, Uint8Array.of(1)); + expect((await received.read()).done).toBe(false); + await forward(2n, false); + } + while (datagrams.priority.peek() !== undefined) await datagrams.priority.changed(); + datagrams.append(undefined, Uint8Array.of(2)); + await forward(4n, true); + datagrams.append(undefined, Uint8Array.of(3)); + const next = await received.read(); + if (!next.value) throw new Error("no resumed datagram"); + const decoded = await ObjectDatagram.decode(next.value, V22); + expect(decoded.body).toEqual({ payload: Uint8Array.of(3) }); + datagrams.finish(); + expect(await client.reader.u53()).toBe(PublishDone.id); + expect((await PublishDone.decode(client.reader, V22)).streamCount).toBe(0n); + } finally { + received.releaseLock(); + client.close(); + datagrams.finish(); + fx.close(); + } + } +}); + test("a paused SUBSCRIBE ends once its grant is revoked", async () => { const grant = new Signal(undefined); const fx = fixture(V22, grant); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 8767af9aa33..e2aff3b9bf0 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -29,7 +29,14 @@ import { sendPayload } from "../payload.ts"; import { Priority, sendOrder } from "../priority.ts"; import { type Stream, Writer } from "../stream.ts"; import { Timescale } from "../time.ts"; -import type { Groups, Info, Range, Position as TrackPosition, Subscriber as TrackSubscriber } from "../track.ts"; +import { + type Groups, + type Info, + type Range, + type Position as TrackPosition, + Producer as TrackProducer, + type Subscriber as TrackSubscriber, +} from "../track.ts"; import { untilAborted } from "../util/abort.ts"; import { toNumber } from "../util/bigint.ts"; import { nextChange } from "../util/change.ts"; @@ -516,36 +523,35 @@ export class Publisher { let datagrams = datagramOf(); let track = subscribe(broadcast); - try { - // SUBSCRIBE has no kind bit. Resolve both kinds; an absent or refused kind - // does not prevent the other from answering. + let datagramTrack: TrackProducer | undefined; + const resolve = async () => { + // A relay learns both halves from the same stream response. Only local + // application handlers can answer either kind independently. const streamInfo = track.info().then((info) => ({ kind: "stream" as const, info })); - const datagramConsumer = datagrams; + const consumer = datagrams; const selected = await untilAborted( - datagramConsumer && wireOf(broadcast).localDatagrams + consumer && wireOf(broadcast).localDatagrams ? Promise.any([ streamInfo, - datagramConsumer - .info() - .then((info) => ({ kind: "datagram" as const, info, consumer: datagramConsumer })), + consumer.info().then((info) => ({ kind: "datagram" as const, info })), ]).catch(() => streamInfo) - : streamInfo.catch(async (err) => { - if (!datagramConsumer) throw err; - const info = await datagramConsumer.info(); - return { kind: "datagram" as const, info, consumer: datagramConsumer }; - }), + : streamInfo, left.signal, - ).catch(async (err) => { + ); + if (selected.kind === "datagram") { + track.close(); + datagramTrack = new TrackProducer(msg.trackName).accept(selected.info); + track = hooks.parked(datagramTrack.subscribe({ priority: fromWire(msg.subscriberPriority) })); + } + return selected.info; + }; + try { + const info = await resolve().catch(async (err) => { if (left.signal.aborted) request.abort(cancel); - else await request.refuse("subscribe", this.#failed(err, false)); + else await request.refuse("subscribe", this.#failed(err, rendezvous, true)); return undefined; }); - if (!selected) return; - if (selected.kind === "datagram") { - await this.#serveDatagrams(msg, request, selected.consumer, selected.info); - return; - } - const info = selected.info; + if (!info) return; datagrams ??= datagramOf(); // Every subscription a resume makes counts toward the same PUBLISH_DONE. const streams: StreamCount = { opened: 0 }; @@ -557,11 +563,14 @@ export class Publisher { streams, datagrams, datagramDemand: wireOf(broadcast).localDatagrams, + datagramTrack, }); // A pause drops the subscription, and its demand with it; a resume subscribes again as // a new SUBSCRIBE would, so nothing carries across but the request. while (paused) { track.close(); + datagramTrack?.abort(cancel); + datagramTrack = undefined; datagrams?.close(); datagrams = undefined; if (paused.answer) await Message.write(request.writer, RequestOk.id, new RequestOk(), version); @@ -571,7 +580,7 @@ export class Publisher { datagrams = datagramOf(); let resumed: Info; try { - resumed = await untilAborted(track.info(), left.signal); + resumed = await resolve(); // A resume answers with the live edge, which only a granted subscriber learns. if (!this.#gate.allows(name)) throw unauthorized(name); } catch (err) { @@ -590,171 +599,16 @@ export class Publisher { answer, datagrams, datagramDemand: wireOf(broadcast).localDatagrams, + datagramTrack, }); } } finally { track.close(); + datagramTrack?.abort(cancel); datagrams?.close(); } } - /** - * Serve a datagram track on its own SUBSCRIBE: OBJECT_DATAGRAM at object ID 0, then - * PUBLISH_DONE. No END_OF_TRACK object, and no group streams. - */ - async #serveDatagrams( - msg: Subscribe, - request: RequestStream, - datagrams: datagram.Consumer, - info: datagram.Info, - ): Promise { - const version = this.#session.version; - const name = msg.trackNamespace; - const timescale = msg.propertiesWanted ? (info.timescale ?? undefined) : undefined; - const publisher = info.priority; - await Message.write( - request.writer, - SubscribeOk.id, - new SubscribeOk({ - trackAlias: msg.requestId, - properties: msg.propertiesWanted - ? { timescale, priority: toWire(publisher), groupOrder: Properties.DESCENDING } - : {}, - }), - version, - ); - console.debug(`publish ok: broadcast=${name} track=${msg.trackName} datagram`); - - let forward = msg.forward; - let stopped = false; - let subscriberPriority = fromWire(msg.subscriberPriority); - let waiting: Promise | undefined; - let wake = () => {}; - const parked = () => { - waiting ??= new Promise((resolve) => { - wake = () => { - waiting = undefined; - resolve(); - }; - }); - return waiting; - }; - const opened = DatagramStream.datagramWriter(this.#quic); - const sender = opened?.open(sendOrder({ priority: subscriberPriority, publisher, position: 2 ** 36 - 1 })); - const max = DatagramStream.maxDatagramSize(this.#quic); - const sending = (async () => { - if (!sender) return; - try { - for (;;) { - const datagram = await datagrams.recv(); - if (!datagram || stopped) return; - // A pause holds the datagram already read. Closing the consumer or an update - // that resumes both wake this wait, so the subscription can still end. - while (!forward && !stopped) await parked(); - if (stopped || !forward) return; - if (timescale !== undefined && datagram.timestamp === undefined) { - console.debug(`dropping a datagram without a timestamp: sequence=${datagram.sequence}`); - continue; - } - let body: Uint8Array; - try { - body = await encodeDatagram( - datagram, - { trackAlias: msg.requestId, publisherPriority: toWire(publisher), timescale }, - version, - ); - } catch (err) { - console.debug(`dropping a datagram this draft cannot encode: ${reason(err)}`); - continue; - } - if (body.byteLength > max) { - console.debug( - `dropping datagram larger than the transport limit: size=${body.byteLength} max=${max}`, - ); - continue; - } - await sender.writer.write(body); - } - } finally { - await sender.close(); - opened?.close(); - } - })(); - - let revoke!: () => void; - const revoked = new Promise<"revoked">((resolve) => { - revoke = () => resolve("revoked"); - }); - const watch = this.#gate.watch(name, revoke); - if (watch.revoked()) revoke(); - const updates = (async () => { - for (;;) { - const framed = await request.next(); - if (framed === undefined) { - await request.ended; - return; - } - const { params } = await this.#decodeUpdate(request, framed); - if (params.subscriberPriority !== undefined) { - subscriberPriority = fromWire(params.subscriberPriority); - datagrams.setPriority(subscriberPriority); - sender?.setPriority(sendOrder({ priority: subscriberPriority, publisher, position: 2 ** 36 - 1 })); - } - if (params.forward !== undefined) { - forward = params.forward; - if (forward) wake(); - } - await Message.write(request.writer, RequestOk.id, new RequestOk(), version); - } - })(); - - let unauthorizedEnd = false; - let failed: Error | undefined; - const served = Symbol("served"); - try { - const end = await race([sending.then(() => served), updates, revoked]); - unauthorizedEnd = end === "revoked"; - } catch (err) { - failed = error(err); - if (failed instanceof ProtocolViolation) throw failed; - } finally { - watch.dispose(); - stopped = true; - wake(); - datagrams.close(); - await sending.catch(() => {}); - } - - const updateFailed = failed instanceof UpdateFailed; - try { - await Message.write( - request.writer, - PublishDone.id, - new PublishDone({ - statusCode: unauthorizedEnd - ? PublishDoneStatus.UNAUTHORIZED - : updateFailed - ? PublishDoneStatus.UPDATE_FAILED - : failed - ? PublishDoneStatus.INTERNAL_ERROR - : PublishDoneStatus.TRACK_ENDED, - streamCount: 0n, - reasonPhrase: unauthorizedEnd - ? "not granted" - : updateFailed - ? "update failed" - : failed - ? "internal error" - : "track ended", - }), - version, - ); - } catch { - // The peer may already have closed its half. - } - request.close(); - } - /** * Hold a paused SUBSCRIBE, answering its updates, until one resumes it: the update to * answer then, or none once the request ended. @@ -855,6 +709,7 @@ export class Publisher { answer, datagrams, datagramDemand, + datagramTrack, }: { msg: Subscribe; request: RequestStream; @@ -867,6 +722,8 @@ export class Publisher { /** A datagram track of the same name, forwarded as OBJECT_DATAGRAM beside the groups. */ datagrams?: datagram.Consumer; datagramDemand: boolean; + /** Metadata-only track whose lifetime follows the datagram sender. */ + datagramTrack?: TrackProducer; }): Promise { const version = this.#session.version; const name = msg.trackNamespace; @@ -984,9 +841,13 @@ export class Publisher { alias: msg.requestId, timescale, priority: datagramPriority, - }).catch((err: unknown) => { - console.debug(`stopping datagrams on this subscription: ${reason(err)}`); - }) + }).then( + () => datagramTrack?.finish(), + (err: unknown) => { + if (datagramTrack) datagramTrack.abort(error(err)); + else console.debug(`stopping datagrams on this subscription: ${reason(err)}`); + }, + ) : Promise.resolve(); // Cancels groups still queued for a stream slot. Only the subscriber leaving counts: @@ -1217,7 +1078,7 @@ export class Publisher { // PUBLISH_DONE has no end location: an END_OF_TRACK object is what tells the // subscriber where the track ended. const final = hooks.subscriberEnd(track); - if (ended && !publishError && final !== undefined && !streams.terminalSent) { + if (!datagramTrack && ended && !publishError && final !== undefined && !streams.terminalSent) { await this.#runEndOfTrack({ requestId: msg.requestId, final, @@ -2838,11 +2699,11 @@ export class Publisher { } /** What a request that failed to resolve is refused with. */ - #failed(err: unknown, wait = false): Refusal { + #failed(err: unknown, wait = false, resolved = false): Refusal { const e = error(err); const condition = e instanceof StreamError && (e.code === StreamCode.NotFound || e.code === StreamCode.Unroutable) - ? wait + ? wait && (!resolved || e.code === StreamCode.Unroutable) ? "timeout" : "does_not_exist" : "internal"; diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index d8f6b503923..1b0b3878134 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -1,5 +1,5 @@ import { Milli } from "@moq/clock"; -import { type Dispose, type GetPromise, type Getter, race, Signal } from "@moq/signals"; +import { type Dispose, type GetPromise, type Getter, Once, race, Signal } from "@moq/signals"; import * as announce from "../announced.ts"; import type { Grant } from "../auth.ts"; import type { AuthSession } from "../auth_session.ts"; @@ -158,7 +158,10 @@ function sees(filter: Filter, path: Path.Valid): boolean { */ export class Subscriber { #streamSources = new WeakMap(); - #datagramRequests = new WeakMap>(); + #datagramRequests = new WeakMap< + broadcast.Consumer, + Map }> + >(); #session: Session; // The transport, so a request cut off by the session's close ends with the session's @@ -2232,10 +2235,15 @@ export class Subscriber { let timestamp: Timestamp | undefined; if (datagram.properties) { - const decoded = await new Reader(undefined, datagram.properties, this.#session.version).decode((cursor) => - decodeObjectTime(cursor, binding.timescale ?? Timescale.SECOND), - ); - if (binding.timescale !== undefined) timestamp = decoded; + try { + const decoded = await new Reader(undefined, datagram.properties, this.#session.version).decode( + (cursor) => decodeObjectTime(cursor, binding.timescale ?? Timescale.SECOND), + ); + if (binding.timescale !== undefined) timestamp = decoded; + } catch (err) { + console.debug(`dropping a datagram with unreadable object properties: ${reason(err)}`); + return; + } } if (binding.timescale !== undefined && timestamp === undefined) { console.debug( @@ -2257,9 +2265,17 @@ export class Subscriber { /** Bind both kinds to the same upstream response, without adding stream demand for passive forwarding. */ #bindDatagrams(source: broadcast.Consumer, track: track.Producer, info: Partial) { const pending = this.#datagramRequests.get(source); - const request = pending?.get(track.name); + const entry = pending?.get(track.name); + let producer: datagram.Producer; + try { + producer = entry ? entry.request.accept(info) : wireOf(source).createDatagrams(track.name, info); + } catch (err) { + entry?.request.reject(error(err)); + console.debug(`serving the stream track without datagrams: ${reason(err)}`); + return; + } pending?.delete(track.name); - const producer = request ? request.accept(info) : wireOf(source).createDatagrams(track.name, info); + entry?.accepted.set(true); let held: track.Subscriber | undefined; const sync = () => { const priority = producer.priority.peek(); @@ -2291,7 +2307,8 @@ export class Subscriber { pending = new Map(); this.#datagramRequests.set(source, pending); } - pending.set(request.name, request); + const entry = { request, accepted: new Once() }; + pending.set(request.name, entry); const subscriber = wireOf(source).subscribe(request.name, { priority: request.priority.peek() ?? 0 }); const dispose = request.priority.subscribe((priority) => { if (priority !== undefined) subscriber.update({ priority }); @@ -2299,7 +2316,8 @@ export class Subscriber { try { await untilAborted( race([ - subscriber.info(), + // Cached stream metadata alone does not accept a returning datagram request. + Promise.all([subscriber.info(), entry.accepted]), Promise.resolve(request.closed).then((): never => { throw new StreamError(StreamCode.Cancel); }), @@ -2307,11 +2325,11 @@ export class Subscriber { valid, ); } catch (err) { - if (pending.get(request.name) === request) request.reject(error(err)); + if (pending.get(request.name) === entry) request.reject(error(err)); } finally { dispose(); subscriber.close(); - if (pending.get(request.name) === request) pending.delete(request.name); + if (pending.get(request.name) === entry) pending.delete(request.name); } } } diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 3a103b36b59..c240e5d3336 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -1369,6 +1369,7 @@ impl SubscribeRequest { /// An admitted SUBSCRIBE's serve, which ends however its authority does: dropping it /// releases everything it holds upstream. struct Serving { + resuming: bool, datagrams: Option, stream_error: Option, /// The filter the SUBSCRIBE asked for, which every saved join boundary follows. @@ -1413,11 +1414,6 @@ enum ServingStep { Pausing, /// Paused, answering updates until one resumes it. Paused(WaitResume), - /// Subscribing again for the update resuming it. - Resume { - track: track::Consumer, - subscribe: SubscribeTrack, - }, /// Flushing the error that fails the resuming update. ResumeFailed(Error), /// Flushing the REQUEST_OK answering the resuming update. @@ -1437,6 +1433,7 @@ impl Serving { fn new(publisher: &Publisher, msg: &ietf::Subscribe<'_>) -> Self { let fin_cancels = super::request_stream::fin_cancels(publisher.version); Self { + resuming: false, datagrams: None, stream_error: None, filter: msg.filter, @@ -1513,10 +1510,13 @@ impl Serving { Poll::Pending => None, }; let broadcast = self.broadcast.as_ref().expect("resolved"); + let datagram_only = + broadcast.has_local_datagrams() && !broadcast.publishes_track(&request.msg.track_name); + if !datagram_only && self.stream_error.is_some() { + self.datagrams = None; + } if selected.is_none() - && broadcast.has_local_datagrams() - && !broadcast.publishes_track(&request.msg.track_name) - && let Some(datagrams) = &self.datagrams + && datagram_only && let Some(datagrams) = &self.datagrams { match datagrams.poll_info(waiter) { Poll::Ready(Ok(info)) => { @@ -1548,6 +1548,10 @@ impl Serving { if self.datagrams.is_none() && let Some(err) = &self.stream_error { + if self.resuming { + self.step = Self::resume_failed(request, err.clone())?; + continue; + } return Poll::Ready(Ok(refused(err.clone(), request.msg.rendezvous, true))); } return Poll::Pending; @@ -1564,8 +1568,16 @@ impl Serving { broadcast.has_local_datagrams(), ); } - self.track = Some(serve); - self.step = ServingStep::Properties { cache, edge }; + if self.resuming { + serve.opened = request.acked.clone().expect("acked before serving"); + Publisher::buffer_request_ok(&mut stream.writer, edge.largest)?; + request.save_join(self.filter, edge.largest, &cache, &serve.ranking); + self.track = Some(serve); + self.step = ServingStep::Resumed(cache); + } else { + self.track = Some(serve); + self.step = ServingStep::Properties { cache, edge }; + } } ServingStep::Properties { edge, .. } => { let flate = ready!(request.publisher.peer_setup.poll_get(waiter)).flate; @@ -1670,44 +1682,24 @@ impl Serving { return Poll::Ready(Ok(Subscribed::Served(served, request.opened()))); } let broadcast = self.broadcast.as_ref().expect("resolved before serving"); - self.step = match broadcast.track(&request.msg.track_name) { - Ok(track) => ServingStep::Resume { - track, - subscribe: SubscribeTrack::new(), - }, - Err(err) => Self::resume_failed(request, err)?, - }; - } - ServingStep::Resume { track, subscribe } => { - let subscribed = - ready!(subscribe.poll(waiter, &request.publisher, stream, track, &request.msg, &request.lapse)); - let ServingStep::Resume { track, .. } = std::mem::replace(&mut self.step, ServingStep::Done) else { - unreachable!("matched above"); - }; - self.step = match subscribed { - // Withdrawn mid-setup, it ends as it would have while paused. - None => { - return Poll::Ready(Ok(Subscribed::Served(Served::Track(Ok(())), request.opened()))); - } - // The resuming update is owed an answer, so it fails. - Some(Err(err)) => Self::resume_failed(request, err)?, - Some(Ok((mut serve, edge))) => { - serve.opened = request.acked.clone().expect("acked before serving"); - if let Some(broadcast) = &self.broadcast - && let Some(datagrams) = broadcast.datagrams_live(&request.msg.track_name) - { - serve = serve.with_datagrams( - datagrams, - request.msg.properties_wanted, - broadcast.has_local_datagrams(), - ); + self.resuming = true; + self.stream_error = None; + let local = broadcast.has_local_datagrams(); + self.datagrams = (request.publisher.session.max_datagram_size() > 0) + .then(|| { + if local { + broadcast.datagrams(&request.msg.track_name).ok() + } else { + broadcast.datagrams_live(&request.msg.track_name) } - request.save_join(self.filter, edge.largest, &track, &serve.ranking); - let stream = request.stream.as_mut().expect("open until PUBLISH_DONE"); - Publisher::buffer_request_ok(&mut stream.writer, edge.largest)?; - self.track = Some(serve); - ServingStep::Resumed(track) - } + }) + .flatten(); + if local && let Some(datagrams) = &mut self.datagrams { + datagrams.set_priority(super::priority::from_wire(request.msg.subscriber_priority)); + } + self.step = ServingStep::Subscribe { + track: broadcast.track(&request.msg.track_name), + subscribe: SubscribeTrack::new(), }; } ServingStep::ResumeFailed(_) => { @@ -9996,6 +9988,59 @@ mod serve_tests { } } + /// A datagram-only name resumes through the same kind selection as its first request. + #[kio::test] + async fn a_datagram_only_subscription_releases_demand_on_pause_and_resumes() { + for version in [Version::Draft18, Version::Draft22] { + for start_paused in [false, true] { + let h = serve_session( + version, + crate::track::Info::timed(), + crate::origin::Route::default(), + ScriptedSession::new(Vec::new()).with_datagrams(1200), + ); + let mut datagrams = h + ._broadcast + .create_datagrams("audio", crate::datagram::Info::default()) + .unwrap(); + let request = h.session.clone(); + let stream = Stream::open(&crate::driver::Transport::new(request.clone()), version) + .await + .unwrap(); + let mut msg = subscribe(Filter::NextObject, None); + msg.track_name = "audio".into(); + msg.forward = !start_paused; + let mut serve = std::pin::pin!(h.publisher.clone().run_subscribe_stream(stream, msg)); + serve_until(serve.as_mut(), "SUBSCRIBE_OK", || !h.log.trail().is_empty()).await; + if !start_paused { + datagrams.append(None, &b"before"[..]).unwrap(); + serve_until(serve.as_mut(), "the datagram", || { + h.log.datagrams.lock().unwrap().len() == 1 + }) + .await; + request.push(&forward_update(version, 2, false)); + } + serve_until(serve.as_mut(), "demand released", || datagrams.priority().is_none()).await; + let sent = h.log.datagrams.lock().unwrap().len(); + datagrams.append(None, &b"paused"[..]).unwrap(); + request.push(&forward_update(version, 4, true)); + serve_until(serve.as_mut(), "resumed demand", || datagrams.priority().is_some()).await; + datagrams.append(None, &b"after"[..]).unwrap(); + serve_until(serve.as_mut(), "the resumed datagram", || { + h.log.datagrams.lock().unwrap().len() == sent + 1 + }) + .await; + datagrams.finish(); + kio::clock::timeout(std::time::Duration::from_secs(10), serve) + .await + .expect("datagram end") + .unwrap(); + let wire = Wire::read(&h.log, version); + assert_eq!(wire.publish_done, Some((ietf::PublishDoneStatus::TrackEnded.code(), 0))); + } + } + } + /// A resume joins as a new SUBSCRIBE would, so a joining FETCH after it is served from where /// the resume joined rather than refused as the pause left it. #[kio::test] diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 494f4267986..49b1effef6d 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -5769,7 +5769,13 @@ impl Subscriber { let timestamp = match &datagram.properties { Some(raw) => { let bytes = bytes::Bytes::copy_from_slice(raw); - super::object::read(&bytes, self.version, timescale)?.timestamp + match super::object::read(&bytes, self.version, timescale) { + Ok(object) => object.timestamp, + Err(err) => { + tracing::debug!(%err, "dropping a datagram with unreadable object properties"); + return Ok(()); + } + } } None => None, }; @@ -8990,6 +8996,20 @@ mod tests { .unwrap() }; + let unreadable = ietf::ObjectDatagram { + track_alias: 7, + group_id: 3, + object_id: None, + publisher_priority: None, + end_of_group: false, + properties: Some(vec![0x10]), + body: ietf::DatagramBody::Payload(bytes::Bytes::from_static(b"bad")), + } + .encode_bytes(Version::Draft18) + .unwrap(); + subscriber.recv_datagram(unreadable).unwrap(); + assert!(consumer.poll_recv(&kio::Waiter::noop()).is_pending()); + subscriber .recv_datagram(encode( None, diff --git a/rs/moq-net/src/lite/test_transport.rs b/rs/moq-net/src/lite/test_transport.rs index 304c7123dd4..f4977e9e85e 100644 --- a/rs/moq-net/src/lite/test_transport.rs +++ b/rs/moq-net/src/lite/test_transport.rs @@ -936,6 +936,7 @@ impl poll::Chunks for Script { #[derive(Clone)] pub struct ScriptedSession { pub log: Log, + max_datagram_size: usize, /// Optional notifications for tests that drive per-stream readiness queues. wake: Option>, stats: SinkStats, @@ -966,6 +967,11 @@ pub struct ScriptedSession { } impl ScriptedSession { + pub fn with_datagrams(mut self, max_size: usize) -> Self { + self.max_datagram_size = max_size; + self + } + pub fn without_credit(self) -> Self { *self.credit.write().ok().expect("credit is live") = false; self @@ -983,6 +989,7 @@ impl ScriptedSession { pub fn new(script: Vec) -> Self { Self { log: Log::default(), + max_datagram_size: 0, wake: None, stats: SinkStats::default(), close: Default::default(), @@ -1243,7 +1250,7 @@ impl poll::Session for ScriptedSession { } fn max_datagram_size(&self) -> usize { - 0 + self.max_datagram_size } fn address(&self) -> crate::transport::Address { From 11bc10a3076d6b602428bbe2bb82d42c1a29fd0f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 12:29:07 -0800 Subject: [PATCH 18/23] style(net): format datagram property coverage --- js/net/src/ietf/datagram.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/js/net/src/ietf/datagram.test.ts b/js/net/src/ietf/datagram.test.ts index 37f1651189d..3de8821b15b 100644 --- a/js/net/src/ietf/datagram.test.ts +++ b/js/net/src/ietf/datagram.test.ts @@ -146,7 +146,10 @@ test("a datagram track round-trips over moq-transport", async () => { expect(info.timescale).toBe(Timescale.MILLI); // A peer's unreadable properties lose one best-effort payload, not the session. const encode = ObjectDatagram.prototype.encode; - const corrupt = spyOn(ObjectDatagram.prototype, "encode").mockImplementation(function (this: ObjectDatagram, version) { + const corrupt = spyOn(ObjectDatagram.prototype, "encode").mockImplementation(function ( + this: ObjectDatagram, + version, + ) { corrupt.mockRestore(); return encode.call(new ObjectDatagram({ ...this, properties: Uint8Array.of(0x10) }), version); }); From 073a0fc3854e1c8244873d159a988ca61f343760 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 22:37:20 -0800 Subject: [PATCH 19/23] fix(net): resume relay datagrams and share the IETF session writer --- js/net/src/ietf/datagram.test.ts | 6 +- js/net/src/ietf/publisher.test.ts | 153 +++++++++++++++++++++++++++++- js/net/src/ietf/publisher.ts | 20 +++- 3 files changed, 170 insertions(+), 9 deletions(-) diff --git a/js/net/src/ietf/datagram.test.ts b/js/net/src/ietf/datagram.test.ts index 3de8821b15b..8d90aeb3b45 100644 --- a/js/net/src/ietf/datagram.test.ts +++ b/js/net/src/ietf/datagram.test.ts @@ -124,7 +124,7 @@ test("a defined object status does not close the session", async () => { test("a datagram track round-trips over moq-transport", async () => { const source = new Origin(); const path = Path.from("live"); - const publication = source.createBroadcast(path); + const publication = source.route(path, { hidden: true }); const datagrams = publication.broadcast.createDatagrams("audio", { timescale: Timescale.MILLI, priority: 3, @@ -177,7 +177,7 @@ test("a relay shares one IETF subscription across stream and datagram readers", const source = new Origin(); const relay = new Origin(); const path = Path.from("shared"); - const publication = source.createBroadcast(path); + const publication = source.route(path, { hidden: true }); const track = publication.broadcast.createTrack("audio", { timescale: Timescale.MILLI }); const datagrams = publication.broadcast.createDatagrams("audio", { timescale: Timescale.MICRO }); publication.announce(); @@ -188,7 +188,7 @@ test("a relay shares one IETF subscription across stream and datagram readers", accept({ url, transport: upstream.server, publish: source.consume() }), ]); const original = wireOf(peers[0]).consume(path); - const requests = relay.requestsRoute(path); + const requests = relay.routePrefix(path); const cursor = requests.requested(); const routing = (async () => { for await (const request of cursor) request.serve(original); diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index c820a8a701e..bce722051da 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -3,6 +3,7 @@ import { Elapsed, Manual, Milli, Nano, sleep, timeout, yieldNow } from "@moq/clo import { Signal } from "@moq/signals"; import type { Credential, Grant } from "../auth.ts"; import { Producer as BroadcastProducer } from "../broadcast.ts"; +import { accept, connect } from "../connection/index.ts"; import { error, fromTransport, @@ -2198,6 +2199,7 @@ const ALPNS: Record = { function fixture( version: IetfVersion = V22, grant?: Signal, + pair = createMockTransportPair(ALPNS[version]), ): { pair: ReturnType; pub: Publisher; @@ -2206,7 +2208,6 @@ function fixture( version: IetfVersion; close: () => void; } { - const pair = createMockTransportPair(ALPNS[version]); const session = new NativeSession(pair.server, version, false); const { pub, origin } = publisher(pair.server, { session, grant }); const broadcast = publish(origin, Path.from("test")); @@ -3626,6 +3627,156 @@ test("a SUBSCRIBE pauses without demand and resumes as a new subscriber", async } }); +test("a paused relay subscription reacquires its upstream datagrams", async () => { + const source = new OriginProducer(); + const route = source.route(Path.from("test")); + const track = route.broadcast.createTrack("audio", {}); + const datagrams = route.broadcast.createDatagrams("audio", {}); + const upstream = createMockTransportPair(ALPN.DRAFT_18); + const url = new URL("https://localhost/resume"); + const peers = await Promise.all([ + connect({ url, transport: upstream.client }), + accept({ url, transport: upstream.server, publish: source.consume() }), + ]); + const original = wireOf(peers[0]).consume(Path.from("test")); + const relay = new OriginProducer(); + const requests = relay.routePrefix(Path.from("test")); + const cursor = requests.requested(); + const routing = (async () => { + for await (const request of cursor) request.serve(original); + })(); + const fx = fixture(V22); + fx.pub.close(); + fx.pub = new Publisher({ + quic: fx.pair.server, + session: new NativeSession(fx.pair.server, V22, false), + publish: relay.consume(), + requiresSolicitation: false, + }); + const received = fx.pair.client.datagrams.readable.getReader(); + const { client } = await runSubscribe( + fx, + new Subscribe({ + requestId: 0n, + trackNamespace: Path.from("test"), + trackName: "audio", + subscriberPriority: 128, + }), + ); + const forward = async (id: bigint, on: boolean) => { + const parameters = new Parameters(); + parameters.forward = on; + await client.writer.u53(SubscribeUpdate.id); + await new SubscribeUpdate({ requestId: id, parameters }).encode(client.writer, V22); + expect(await client.reader.u53()).toBe(RequestOk.id); + await RequestOk.decode(client.reader, V22); + }; + try { + datagrams.append(undefined, Uint8Array.of(1)); + expect((await received.read()).done).toBe(false); + await forward(2n, false); + while (track.subscription.peek() !== undefined) await track.subscription.changed(); + const before = upstream.client.sendStreams.bidi.length; + await forward(4n, true); + expect(upstream.client.sendStreams.bidi.length).toBe(before + 1); + datagrams.append(undefined, Uint8Array.of(2)); + const next = await received.read(); + if (!next.value) throw new Error("no resumed relay datagram"); + expect((await ObjectDatagram.decode(next.value, V22)).body).toEqual({ payload: Uint8Array.of(2) }); + } finally { + client.close(); + fx.pub.close(); + received.releaseLock(); + fx.close(); + requests.close(); + await routing; + for (const peer of peers) peer.abort(); + original.close(); + relay.close(); + source.close(); + } +}); + +test.each(["writable", "createWritable"] as const)( + "concurrent IETF subscriptions share the %s datagram transport", + async (datagramWritable) => { + const fx = fixture(V22, undefined, createMockTransportPair(ALPN.DRAFT_22, { datagramWritable })); + const producers = ["audio", "video"].map((name) => fx.broadcast.createDatagrams(name, {})); + const received = fx.pair.client.datagrams.readable.getReader(); + const clients: Stream[] = []; + try { + for (const [i, name] of ["audio", "video"].entries()) { + const { client } = await runSubscribe( + fx, + new Subscribe({ + requestId: BigInt(i * 2), + trackNamespace: Path.from("test"), + trackName: name, + subscriberPriority: 128, + }), + ); + clients.push(client); + } + for (const [i, producer] of producers.entries()) { + producer.append(undefined, Uint8Array.of(i)); + const next = await received.read(); + if (!next.value) throw new Error("missing concurrent datagram"); + const decoded = await ObjectDatagram.decode(next.value, V22); + expect(decoded.trackAlias).toBe(BigInt(i * 2)); + expect(decoded.body).toEqual({ payload: Uint8Array.of(i) }); + } + } finally { + for (const client of clients) client.close(); + for (const producer of producers) producer.finish(); + received.releaseLock(); + fx.pub.close(); + fx.close(); + } + }, +); + +test("a paused IETF subscription leaves a blocked datagram write", async () => { + const pair = createMockTransportPair(ALPN.DRAFT_22); + const started = Promise.withResolvers(); + const release = Promise.withResolvers(); + Object.defineProperty(pair.server.datagrams, "writable", { + value: new WritableStream({ + write: async () => { + started.resolve(); + await release.promise; + }, + }), + }); + const fx = fixture(V22, undefined, pair); + const datagrams = fx.broadcast.createDatagrams("audio", {}); + const { client } = await runSubscribe( + fx, + new Subscribe({ + requestId: 0n, + trackNamespace: Path.from("test"), + trackName: "audio", + subscriberPriority: 128, + }), + ); + try { + datagrams.append(undefined, Uint8Array.of(1)); + await started.promise; + const parameters = new Parameters(); + parameters.forward = false; + await client.writer.u53(SubscribeUpdate.id); + await new SubscribeUpdate({ requestId: 2n, parameters }).encode(client.writer, V22); + expect(await client.reader.u53()).toBe(RequestOk.id); + await RequestOk.decode(client.reader, V22); + expect(datagrams.priority.peek()).toBeUndefined(); + } finally { + release.resolve(); + client.close(); + datagrams.finish(); + fx.pub.close(); + fx.close(); + } +}); + test("a datagram-only SUBSCRIBE releases demand on pause and resumes", async () => { for (const startPaused of [false, true]) { const fx = fixture(V22); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index a94da9d5f86..96aa319d289 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -324,6 +324,7 @@ export class Publisher { return this.#withdrawal.close(); } #quic: WebTransport; + #datagramWriter?: ReturnType; #session: Session; #requiresSolicitation: boolean; // What a joining FETCH needs from each draft-18 SUBSCRIBE, by its request ID, while it lasts: @@ -406,6 +407,7 @@ export class Publisher { ready?: Promise; }) { this.#quic = quic; + this.#datagramWriter = DatagramStream.datagramWriter(quic); this.#grant = grant; this.#gate = new Gate(grant); this.#auth = auth; @@ -421,7 +423,10 @@ export class Publisher { } /** The origin owns broadcasts; transport teardown ends this publisher's served requests. @internal */ - close(): void {} + close(): void { + this.#datagramWriter?.close(); + this.#datagramWriter = undefined; + } /** * Handles an incoming SUBSCRIBE request on a bidi stream. @@ -581,6 +586,7 @@ export class Publisher { let resumed: Info; try { resumed = await resolve(); + datagrams ??= datagramOf(); // A resume answers with the live edge, which only a granted subscriber learns. if (!this.#gate.allows(name)) throw unauthorized(name); } catch (err) { @@ -1174,7 +1180,7 @@ export class Publisher { console.debug(`serving the stream track without datagrams: ${reason(err)}`); return; } - const opened = DatagramStream.datagramWriter(this.#quic); + const opened = this.#datagramWriter; if (!opened) return; const publisher = info.priority; const timescale = options.timescale; @@ -1183,6 +1189,7 @@ export class Publisher { const dispose = options.priority.subscribe(() => sender.setPriority(rank())); const version = this.#session.version; const max = DatagramStream.maxDatagramSize(this.#quic); + const left = hooks.datagramClosed(datagrams); try { for (;;) { const datagram = await datagrams.recv(); @@ -1212,12 +1219,15 @@ export class Publisher { ); continue; } - await sender.writer.write(body); + // A paused subscription must release its consumer even when transport writes stall. + await race([sender.writer.ready, left]); + if (left.peek() !== undefined) return; + await race([sender.writer.write(body), left]); + if (left.peek() !== undefined) return; } } finally { dispose(); - await sender.close(); - opened.close(); + await race([sender.close(), left]); } } From 4928def55eb18c07f5ccb2a243227b5f8c70e3e5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 22:37:55 -0800 Subject: [PATCH 20/23] chore(quest): remove merged datagram dependencies --- quest/m1/datagram-tracks/README.md | 2 +- quest/m2/recut-held.md | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/quest/m1/datagram-tracks/README.md b/quest/m1/datagram-tracks/README.md index 48c7f6ad9d4..3b4e7c8f90b 100644 --- a/quest/m1/datagram-tracks/README.md +++ b/quest/m1/datagram-tracks/README.md @@ -1,4 +1,4 @@ -# Datagram tracks +# [S] Datagram tracks ## Goal diff --git a/quest/m2/recut-held.md b/quest/m2/recut-held.md index 59b0ee3575d..d2ffa84ac4c 100644 --- a/quest/m2/recut-held.md +++ b/quest/m2/recut-held.md @@ -30,7 +30,6 @@ padding probes in [lite07-finalize](/quest/m2/lite07-finalize.md). ## Required -- [IETF datagram tracks](/quest/m1/datagram-tracks/ietf.md) - claim held datagram-tracks - [rind CPU error](/quest/m1/rind/cpu-error.md) - claim held rind - [Connect policy](/quest/m1/session-policy/connect.md) - claim held session-policy - [Release on leave](/quest/m1/stream-credit/release-on-leave.md) - claim held stream-credit From f340141b3cef4802b365f902ef17dbeb58409812 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 22:43:12 -0800 Subject: [PATCH 21/23] test(net): adapt datagram pause coverage to origin routes --- rs/moq-net/src/ietf/publisher.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 6af87d5050d..a990665223b 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -9981,11 +9981,11 @@ mod serve_tests { let h = serve_session( version, crate::track::Info::timed(), - crate::origin::Route::default(), ScriptedSession::new(Vec::new()).with_datagrams(1200), ); let mut datagrams = h ._broadcast + .broadcast() .create_datagrams("audio", crate::datagram::Info::default()) .unwrap(); let request = h.session.clone(); From c9cbd10b2d79f7aa97bc9ec9f8e7e61d48281bb4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 6 Nov 2026 00:52:01 -0800 Subject: [PATCH 22/23] fix(net): route IETF datagrams received with their answer or end --- js/net/src/ietf/datagram.test.ts | 124 ++++++++++++++++++++++- js/net/src/ietf/subscriber.ts | 36 ++++++- rs/moq-net/tests/integration/datagram.rs | 37 +++++++ 3 files changed, 193 insertions(+), 4 deletions(-) diff --git a/js/net/src/ietf/datagram.test.ts b/js/net/src/ietf/datagram.test.ts index 8d90aeb3b45..978c00e816d 100644 --- a/js/net/src/ietf/datagram.test.ts +++ b/js/net/src/ietf/datagram.test.ts @@ -1,11 +1,13 @@ import { expect, spyOn, test } from "bun:test"; +import { yieldNow } from "@moq/clock"; import { accept, connect } from "../connection/index.ts"; import { ProtocolViolation, SessionCode } from "../error.ts"; -import { createMockTransportPair } from "../mock.ts"; +import { createMockTransportPair, type MockTransport } from "../mock.ts"; import { Producer as Origin } from "../origin.ts"; import * as Path from "../path.ts"; import { encodeVarint, Stream } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; +import { Budget } from "../util/yield.ts"; import { wireOf } from "../wire.ts"; import { Connection } from "./connection.ts"; import { ObjectDatagram } from "./datagram.ts"; @@ -240,3 +242,123 @@ test("a relay shares one IETF subscription across stream and datagram readers", } } }); + +function lagAnswers(transport: MockTransport, lag: number): void { + if (lag === 0) return; + const lagging = (writable: WritableStream) => { + const writer = writable.getWriter(); + let chain = Promise.resolve(); + const later = (op: () => Promise) => { + // A write the session's close fails is lost, as it would be on the wire. + chain = chain + .then(async () => { + for (let i = 0; i < lag; i++) await Promise.resolve(); + await op(); + }) + .catch(() => {}); + return chain; + }; + return new WritableStream({ + // Copied: the write resolves now, after which the stream may reuse its buffer. + write: (chunk) => { + const copy = chunk.slice(); + void later(() => writer.write(copy)); + }, + close: () => later(() => writer.close()), + abort: (reason) => writer.abort(reason), + }); + }; + const incoming = transport.incomingBidirectionalStreams.pipeThrough( + new TransformStream({ + transform: (bidi, controller) => + controller.enqueue({ + readable: bidi.readable, + writable: lagging(bidi.writable), + } as WebTransportBidirectionalStream), + }), + ); + Object.defineProperty(transport, "incomingBidirectionalStreams", { value: incoming }); +} + +for (const alpn of [ALPN.DRAFT_18, ALPN.DRAFT_20, ALPN.DRAFT_22]) { + for (const [lag, finish] of [ + [0, false], + [100, false], + [0, true], + [100, true], + ] as const) { + test(`${alpn}: a datagram sent right behind its answer arrives (answer ${lag} microtasks late${finish ? ", then the end" : ""})`, async () => { + const source = new Origin(); + const publication = source.route(Path.from("live"), { hidden: true }); + const datagrams = publication.broadcast.createDatagrams("audio"); + publication.announce(); + const pair = createMockTransportPair(alpn); + lagAnswers(pair.server, lag); + const [client, server] = await Promise.all([ + connect({ url: new URL("https://localhost/datagrams"), transport: pair.client }), + accept({ + url: new URL("https://localhost/datagrams"), + transport: pair.server, + publish: source.consume(), + }), + ]); + const remote = wireOf(client).consume(Path.from("live")); + const reader = remote.datagrams("audio"); + // Only the subscription carries this priority: a lite-06 TRACK lookup asks for none. + reader.setPriority(5); + try { + while (datagrams.priority.peek() !== 5) await datagrams.priority.changed(); + datagrams.append(undefined, Uint8Array.of(1)); + if (finish) datagrams.finish(); + await reader.info(); + if (!finish) datagrams.append(undefined, Uint8Array.of(2)); + expect((await reader.recv())?.payload).toEqual(Uint8Array.of(1)); + if (finish) expect(await reader.recv()).toBeUndefined(); + else expect((await reader.recv())?.payload).toEqual(Uint8Array.of(2)); + } finally { + reader.close(); + remote.close(); + client.abort(); + server.abort(); + source.close(); + } + }); + } +} + +for (const alpn of [ALPN.DRAFT_18, ALPN.DRAFT_20, ALPN.DRAFT_22]) { + test(`${alpn}: a datagram burst reaches its reader before the end across a receive yield`, async () => { + const source = new Origin(); + const publication = source.route(Path.from("live"), { hidden: true }); + const datagrams = publication.broadcast.createDatagrams("audio"); + publication.announce(); + const pair = createMockTransportPair(alpn); + const url = new URL("https://localhost/datagrams"); + const [client, server] = await Promise.all([ + connect({ url, transport: pair.client }), + accept({ url, transport: pair.server, publish: source.consume() }), + ]); + const remote = wireOf(client).consume(Path.from("live")); + const reader = remote.datagrams("audio"); + reader.setPriority(5); + let polls = 0; + const budget = spyOn(Budget.prototype, "poll").mockImplementation(() => + ++polls % 32 === 0 ? yieldNow() : undefined, + ); + try { + await reader.info(); + while (datagrams.priority.peek() !== 5) await datagrams.priority.changed(); + for (let i = 0; i < 64; i++) datagrams.append(undefined, Uint8Array.of(i)); + datagrams.finish(); + for (let i = 0; i < 64; i++) expect((await reader.recv())?.payload).toEqual(Uint8Array.of(i)); + expect(await reader.recv()).toBeUndefined(); + } finally { + budget.mockRestore(); + reader.close(); + remote.close(); + client.abort(); + server.abort(); + source.close(); + } + }); +} diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 6f3ffb2c7af..9ac6792beaa 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -1,4 +1,4 @@ -import { Milli } from "@moq/clock"; +import { Milli, yieldNow } from "@moq/clock"; import { type Dispose, type GetPromise, type Getter, Once, race, Signal } from "@moq/signals"; import * as announce from "../announced.ts"; import type { Grant } from "../auth.ts"; @@ -38,6 +38,7 @@ import { Deadline } from "../util/deadline.ts"; import { abandoned as demandAbandoned, isAbandoned, unrequested, unwanted } from "../util/demand.ts"; import { LIMITS, type Limits, SlotMap, Slots } from "../util/limits.ts"; import { min } from "../util/sequence.ts"; +import { Budget } from "../util/yield.ts"; import { bounded, wireSpan } from "../window.ts"; import { wireOf } from "../wire.ts"; import { DuplicateTrackAlias, RetiredTrackAlias, TrackAliases } from "./aliases.ts"; @@ -175,6 +176,7 @@ export class Subscriber { // Publisher-chosen aliases used by incoming group streams. #aliases = new TrackAliases(); + #receiveYield?: Promise; // Units for each track's object Timestamps, from the TIMESCALE Track Property in // SUBSCRIBE_OK. A track missing from this map declared no timeline, so its frames @@ -1372,7 +1374,10 @@ export class Subscriber { if (done.statusCode !== PublishDoneStatus.TRACK_ENDED) { throw new Error(`publish done: subscription ended reason=${done.reasonPhrase}`); } - subscription.datagrams?.producer.finish(); + if (subscription.datagrams) { + await this.#datagramsRouted(); + subscription.datagrams.producer.finish(); + } // The track ended and every stream is accounted for, so no tail is coming: a head nothing // claimed is its whole group. A draft-18 joining FETCH is not in the Stream Count; its // head held the settle. @@ -2211,8 +2216,11 @@ export class Subscriber { const reader = DatagramStream.datagramReader(this.#quic); if (!reader) return; + const budget = new Budget(); try { for (;;) { + const yielded = budget.poll(); + if (yielded !== undefined) await this.#yieldDatagrams(yielded); // The stream errors once the session closes, which ends this loop like any other. const next = await reader.read().catch(() => undefined); if (!next || next.done) return; @@ -2223,6 +2231,22 @@ export class Subscriber { } } + async #yieldDatagrams(yielded: Promise): Promise { + this.#receiveYield = yielded; + await yielded; + this.#receiveYield = undefined; + } + + // A clean end waits for datagrams already received with it. A budget yield or an + // unanswered alias can interrupt that drain; each next host task lets it continue. + async #datagramsRouted(): Promise { + await yieldNow(); + while (this.#receiveYield !== undefined) { + await this.#receiveYield; + await yieldNow(); + } + } + /** * Route one OBJECT_DATAGRAM. * @@ -2236,7 +2260,13 @@ export class Subscriber { throw new ProtocolViolation(`undefined object status ${datagram.body.status}`); } - const alias = this.#aliases.peek(datagram.trackAlias); + let alias = this.#aliases.peek(datagram.trackAlias); + if (!alias) { + // The answer can arrive in the same packet, behind its datagram. Keep only + // this receive slot while the control stream decodes, then drop an unknown alias. + await this.#yieldDatagrams(yieldNow()); + alias = this.#aliases.peek(datagram.trackAlias); + } const binding = alias?.datagrams && { producer: alias.datagrams.producer, timescale: alias.timescale, diff --git a/rs/moq-net/tests/integration/datagram.rs b/rs/moq-net/tests/integration/datagram.rs index 366480ec36e..f0eff7eea39 100644 --- a/rs/moq-net/tests/integration/datagram.rs +++ b/rs/moq-net/tests/integration/datagram.rs @@ -727,3 +727,40 @@ async fn a_track_ended_under_a_datagram_flood_keeps_its_stream() { .unwrap_or_else(|_| panic!("{version}: the track never released its stream")); } } + +/// The publisher's answer, first datagram, and end can all reach one receive pass. +#[kio::test] +async fn an_ietf_datagram_sent_with_its_answer_and_end_arrives() { + for version in ["moq-transport-18", "moq-transport-20", "moq-transport-22"] { + let chain = Chain::new(&[version]).await; + let mut producer = chain.publication.broadcast().create_datagrams(TRACK, timed()).unwrap(); + let mut consumer = chain.remote().await.datagrams(TRACK).unwrap(); + consumer.set_priority(5); + datagram_priority(&producer, Some(5)).await; + producer.append(millis(1), &b"first"[..]).unwrap(); + producer.finish(); + consumer.info().await.unwrap(); + let datagram = recv(&mut consumer).await.unwrap().expect("first datagram"); + assert_eq!(&datagram.payload[..], b"first", "{version}"); + assert!(recv(&mut consumer).await.unwrap().is_none(), "{version}"); + } +} + +/// Ending a track must preserve its queued datagrams across the receiver's budget. +#[kio::test] +async fn an_ietf_datagram_burst_sent_with_its_end_arrives() { + for version in ["moq-transport-18", "moq-transport-20", "moq-transport-22"] { + let chain = Chain::new(&[version]).await; + let mut producer = chain.publication.broadcast().create_datagrams(TRACK, timed()).unwrap(); + let mut consumer = chain.consume(&producer, 5).await; + for i in 0..64 { + producer.append(millis(i), bytes::Bytes::from(vec![i as u8])).unwrap(); + } + producer.finish(); + for i in 0..64 { + let datagram = recv(&mut consumer).await.unwrap().expect("queued datagram"); + assert_eq!(datagram.sequence, i, "{version}"); + } + assert!(recv(&mut consumer).await.unwrap().is_none(), "{version}"); + } +} From 2f35155c92fcbdd94aed777e78fd99a93d16a0a6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 6 Nov 2026 01:25:16 -0800 Subject: [PATCH 23/23] fix(net): preserve IETF datagram progress and receive ordering --- rs/moq-net/src/ietf/publisher.rs | 106 ++++++++++++++++------- rs/moq-net/src/ietf/session.rs | 66 +++++++++++--- rs/moq-net/src/ietf/subscriber.rs | 44 +++++++--- rs/moq-net/tests/integration/datagram.rs | 33 +++---- 4 files changed, 175 insertions(+), 74 deletions(-) diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index a990665223b..1a727e60b44 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -39,6 +39,25 @@ fn serving_subscription(subscriber_priority: u8) -> Subscription { } } +/// Resolve the name's datagram half, holding demand only on a local publication. +fn serving_datagrams( + request: &SubscribeRequest, + broadcast: &crate::broadcast::Consumer, +) -> Option { + if request.publisher.session.max_datagram_size() == 0 { + return None; + } + let mut datagrams = if broadcast.has_local_datagrams() { + broadcast.datagrams(&request.msg.track_name).ok() + } else { + broadcast.datagrams_live(&request.msg.track_name) + }?; + if broadcast.has_local_datagrams() { + datagrams.set_priority(super::priority::from_wire(request.msg.subscriber_priority)); + } + Some(datagrams) +} + /// The Track Properties an answer describing the track carries: SUBSCRIBE_OK, /// TRACK_STATUS_OK, and FETCH_OK. Empty when the requester opted out with /// INCLUDE_PROPERTIES=0, which keeps the block present but empty. @@ -1469,19 +1488,7 @@ impl Serving { Err(err) => return Poll::Ready(Ok(refused(err, request.msg.rendezvous, false))), Ok(None) => return Poll::Ready(Ok(Subscribed::Withdrawn)), }; - let local = broadcast.has_local_datagrams(); - self.datagrams = (request.publisher.session.max_datagram_size() > 0) - .then(|| { - if local { - broadcast.datagrams(&request.msg.track_name).ok() - } else { - broadcast.datagrams_live(&request.msg.track_name) - } - }) - .flatten(); - if local && let Some(datagrams) = &mut self.datagrams { - datagrams.set_priority(super::priority::from_wire(request.msg.subscriber_priority)); - } + self.datagrams = serving_datagrams(request, &broadcast); let track = broadcast.track(&request.msg.track_name); self.broadcast = Some(broadcast); self.step = ServingStep::Subscribe { @@ -1506,7 +1513,13 @@ impl Serving { self.stream_error = Some(err); None } - Poll::Ready(None) => return Poll::Ready(Ok(Subscribed::Withdrawn)), + Poll::Ready(None) => { + return Poll::Ready(Ok(if self.resuming { + Subscribed::Served(Served::Track(Ok(())), request.opened()) + } else { + Subscribed::Withdrawn + })); + } Poll::Pending => None, }; let broadcast = self.broadcast.as_ref().expect("resolved"); @@ -1684,19 +1697,7 @@ impl Serving { let broadcast = self.broadcast.as_ref().expect("resolved before serving"); self.resuming = true; self.stream_error = None; - let local = broadcast.has_local_datagrams(); - self.datagrams = (request.publisher.session.max_datagram_size() > 0) - .then(|| { - if local { - broadcast.datagrams(&request.msg.track_name).ok() - } else { - broadcast.datagrams_live(&request.msg.track_name) - } - }) - .flatten(); - if local && let Some(datagrams) = &mut self.datagrams { - datagrams.set_priority(super::priority::from_wire(request.msg.subscriber_priority)); - } + self.datagrams = serving_datagrams(request, broadcast); self.step = ServingStep::Subscribe { track: broadcast.track(&request.msg.track_name), subscribe: SubscribeTrack::new(), @@ -6734,6 +6735,14 @@ impl TrackServe { if self.datagram_track.is_some() { self.poll_datagrams(waiter); } + let result = self.poll_groups(waiter); + if self.datagram_track.is_none() { + self.poll_datagrams(waiter); + } + result + } + + fn poll_groups(&mut self, waiter: &kio::Waiter) -> Poll> { // Before the groups, so one finishing on this poll sees an end declared beside it. if self.terminal.end.get().is_none() && let Poll::Ready(Ok(end)) = self.track.poll_finished(waiter) @@ -6819,9 +6828,6 @@ impl TrackServe { } // Newly created group machines start now rather than on the next wake. let _ = self.children.poll(waiter); - if self.datagram_track.is_none() { - self.poll_datagrams(waiter); - } Poll::Pending } /// Send every buffered datagram as an OBJECT_DATAGRAM with object ID 0. @@ -9973,6 +9979,42 @@ mod serve_tests { } } + #[kio::test] + async fn datagrams_continue_after_a_stream_range_is_exhausted() { + let broadcast = crate::broadcast::Producer::new(Default::default()); + let track = broadcast.create_track("audio", track::Info::default()).unwrap(); + let subscriber = track.subscribe(Subscription::default().with_start(0).with_end(1)); + let mut group = track.create_group(group::Info { sequence: 0 }).unwrap(); + group.write_frame(None, &b"bounded"[..]).unwrap(); + group.finish().unwrap(); + let mut datagrams = broadcast.create_datagrams("audio", Default::default()).unwrap(); + let consumer = broadcast.consume().datagrams("audio").unwrap(); + let session = ScriptedSession::new(Vec::new()).with_datagrams(1200); + let mut serving = TrackServe::new( + crate::driver::Transport::new(session.clone()), + subscriber, + RequestId(0), + Version::Draft22, + ServeRange { + end: Some(EndLocation { group: 0, object: None }), + ..Default::default() + }, + None, + ) + .with_datagrams(consumer, false, true); + let _group = kio::wait(|waiter| serving.track.poll_recv_group(waiter)) + .await + .unwrap() + .expect("bounded group"); + assert!(matches!( + serving.track.poll_recv_group(&kio::Waiter::noop()), + Poll::Ready(Ok(None)) + )); + datagrams.append(None, &b"live"[..]).unwrap(); + assert!(serving.poll(&kio::Waiter::noop()).is_pending()); + assert_eq!(session.log.datagrams.lock().unwrap().len(), 1); + } + /// A datagram-only name resumes through the same kind selection as its first request. #[kio::test] async fn a_datagram_only_subscription_releases_demand_on_pause_and_resumes() { @@ -15774,6 +15816,10 @@ mod serve_tests { .await .unwrap_or_else(|_| panic!("{version}: outlived its withdrawal")) .unwrap(); + assert!( + Wire::read(&h.log, version).publish_done.is_some(), + "{version}: missing PUBLISH_DONE" + ); settle().await; assert!(h.track.subscription().is_none(), "{version}: held its demand"); } diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index 7ab881730c1..f0256e50404 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -462,8 +462,8 @@ impl Launch { let datagrams = (session.max_datagram_size() != 0).then(|| Datagrams { session: session.clone(), subscriber: subscriber.clone(), - budget: kio::coop::Budget::new(32), - charged: false, + held: Vec::new(), + caught_up: true, }); // Keep the session's SUBSCRIBE_NAMESPACE requests open as discovery decides, each on // its own stream; with no subscribe half, ask nothing. @@ -518,6 +518,15 @@ impl Running { } // Whichever side closed it, read what already arrived. ready!(self.race.unis.poll_drain(waiter)); + let _ = self.race.tasks.poll(waiter); + if let Some(datagrams) = &mut self.race.datagrams { + let _ = datagrams.poll(waiter, false); + if !datagrams.caught_up { + return Poll::Pending; + } + } + self.race.subscriber.datagrams_routed(); + let _ = self.race.tasks.poll(waiter); let res = self.ending.take().expect("draining an ended session"); // Closed cleanly by dropping or draining: readers see what the peer's clean // close would have shown them. @@ -581,7 +590,7 @@ impl Race { if let Poll::Ready(err) = poll_failed(&mut self.dispatch, |dispatch| dispatch.poll(waiter).map(Err)) { return Poll::Ready(Err(err)); } - if let Poll::Ready(err) = poll_failed(&mut self.datagrams, |datagrams| datagrams.poll(waiter)) { + if let Poll::Ready(err) = poll_failed(&mut self.datagrams, |datagrams| datagrams.poll(waiter, true)) { return Poll::Ready(Err(err)); } if let Poll::Ready(err) = poll_failed(&mut self.goaway, |goaway| { @@ -608,6 +617,12 @@ impl Race { if let Poll::Ready(err) = poll_failed(&mut self.publish_namespaces, |publish| publish.poll(waiter)) { return Poll::Ready(Err(err)); } + if let Poll::Ready(err) = poll_failed(&mut self.datagrams, |datagrams| datagrams.poll(waiter, false)) { + return Poll::Ready(Err(err)); + } + if self.datagrams.as_ref().is_none_or(|datagrams| datagrams.caught_up) { + self.subscriber.datagrams_routed(); + } Poll::Pending } } @@ -1358,23 +1373,46 @@ fn died_before_header(err: &Error) -> bool { struct Datagrams { session: Transport, subscriber: Subscriber, - /// A peer keeping datagrams ready must not hold the session's other tasks. - budget: kio::coop::Budget, - /// This pass of the budget is spent, waiting for the next datagram. - charged: bool, + /// Unknown aliases held before the source machines run: at most one receive budget. + held: Vec, + /// Whether the receive pass read the queue empty rather than yielding to its budget. + caught_up: bool, } impl Datagrams { - fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { + fn poll(&mut self, waiter: &kio::Waiter, hold: bool) -> Poll> { + if !hold { + for payload in std::mem::take(&mut self.held) { + self.route(payload, false)?; + } + } + let mut budget = kio::coop::Budget::new(32); loop { - if !self.charged { - ready!(self.budget.poll_yield(waiter)); - self.charged = true; + self.caught_up = false; + ready!(budget.poll_yield(waiter)); + self.caught_up = true; + let payload = match ready!(self.session.poll_recv_datagram(waiter)) { + Ok(payload) => payload, + Err(err) => { + for payload in std::mem::take(&mut self.held) { + self.route(payload, false)?; + } + return Poll::Ready(Err(Error::from_transport(err))); + } + }; + self.route(payload, hold)?; + } + } + + fn route(&mut self, payload: bytes::Bytes, hold: bool) -> Result<(), Error> { + if let Some(alias) = self.subscriber.recv_datagram(payload.clone())? { + if hold { + self.held.push(payload); + } else { + tracing::debug!(alias, "dropping a datagram for an unknown alias"); } - let payload = ready!(self.session.poll_recv_datagram(waiter)).map_err(Error::from_transport)?; - self.charged = false; - self.subscriber.recv_datagram(payload)?; } + Ok(()) } } diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index dbb699400c1..09063a00e51 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -192,6 +192,8 @@ fn retire_track_alias(aliases: &TrackAliases, alias: u64, request_id: RequestId) #[derive(Default)] struct State { + /// Clean ends keep their request streams until the driver drains datagrams received with them. + datagram_ends: Option>, datagram_requests: HashMap, crate::datagram::Request)>, // Each active subscription subscribes: HashMap, @@ -224,6 +226,7 @@ impl State { /// Active receive tasks abort their own groups. Abort any head waiting for its /// tail here, along with the track. Ordinary unsubscribe removes its entry. fn abort(&mut self, err: &Error) { + let _ = self.datagram_ends.take(); for (_, (_, request)) in self.datagram_requests.drain() { request.reject(err.clone()); } @@ -3674,6 +3677,8 @@ enum OnceStep { count: u64, end: Result<(), Error>, }, + /// The stream tail settled; keep the alias until datagrams received with the end route. + Datagrams(kio::Consumer<()>), /// Walking away from it: cancelling the request while the fetches it drives go on. Cancel { cancelling: Cancelling, aborted: bool }, } @@ -4066,12 +4071,26 @@ impl SubscribeOnce { Err(err) => fill.abort(err.clone()), } } + let datagrams = end.is_ok() && self.shared.is_some(); live.publisher_ended(end); + if datagrams { + let mut state = this.state.lock(); + self.step = + OnceStep::Datagrams(state.datagram_ends.get_or_insert_with(Default::default).consume()); + continue; + } // The publisher already ended the request, so there is nothing to cancel. if let Some(step) = self.finish(this, false) { return step; } } + OnceStep::Datagrams(routed) => { + let _ = group_fetches.poll(waiter); + ready!(routed.poll_closed(waiter)); + if let Some(step) = self.finish(this, false) { + return step; + } + } OnceStep::Cancel { cancelling, aborted } => { let _ = group_fetches.poll(waiter); ready!(cancelling.poll(waiter)); @@ -5723,10 +5742,16 @@ impl Subscriber { kio::wait(|waiter| recv.poll(waiter, self, stream)).await } + /// Release clean ends after the driver read the transport's datagram queue empty. + pub(super) fn datagrams_routed(&self) { + let _ = self.state.lock().datagram_ends.take(); + } + /// Route OBJECT_DATAGRAM to the shared subscription's datagram model half. /// + /// Return an unknown alias so the driver can try after decoding the answers received with it. /// A malformed datagram is still a protocol error. - pub fn recv_datagram(&self, payload: bytes::Bytes) -> Result<(), Error> { + pub fn recv_datagram(&self, payload: bytes::Bytes) -> Result, Error> { let (datagram, _) = ietf::ObjectDatagram::decode_slice(&payload, self.version)?; if let ietf::DatagramBody::Status(status) = &datagram.body && !super::object::status_defined(*status) @@ -5738,11 +5763,10 @@ impl Subscriber { let state = self.state.lock(); let aliases = state.aliases.read(); let Some(request_id) = aliases.map.get(&datagram.track_alias).and_then(|alias| alias.holder()) else { - tracing::debug!(alias = datagram.track_alias, "dropping a datagram for an unknown alias"); - return Ok(()); + return Ok(Some(datagram.track_alias)); }; let Some(track) = state.subscribes.get(&request_id) else { - return Ok(()); + return Ok(None); }; match &track.datagram { Some(producer) => (producer.clone(), track.timescale), @@ -5752,7 +5776,7 @@ impl Subscriber { group = datagram.group_id, "dropping a datagram on a stream track's subscription" ); - return Ok(()); + return Ok(None); } } }; @@ -5765,7 +5789,7 @@ impl Subscriber { group = datagram.group_id, "dropping a datagram status" ); - return Ok(()); + return Ok(None); }; let object_id = datagram.object_id.unwrap_or(0); if object_id != 0 { @@ -5775,7 +5799,7 @@ impl Subscriber { object_id, "dropping a datagram with a nonzero object id" ); - return Ok(()); + return Ok(None); } let timestamp = match &datagram.properties { @@ -5785,7 +5809,7 @@ impl Subscriber { Ok(object) => object.timestamp, Err(err) => { tracing::debug!(%err, "dropping a datagram with unreadable object properties"); - return Ok(()); + return Ok(None); } } } @@ -5797,7 +5821,7 @@ impl Subscriber { group = datagram.group_id, "dropping a datagram without a timestamp on a timed track" ); - return Ok(()); + return Ok(None); } if let Err(err) = producer.insert(crate::datagram::Datagram { sequence: datagram.group_id, @@ -5806,7 +5830,7 @@ impl Subscriber { }) { tracing::debug!(%err, "dropping a datagram the track refused"); } - Ok(()) + Ok(None) } } diff --git a/rs/moq-net/tests/integration/datagram.rs b/rs/moq-net/tests/integration/datagram.rs index f0eff7eea39..40a6c6b269f 100644 --- a/rs/moq-net/tests/integration/datagram.rs +++ b/rs/moq-net/tests/integration/datagram.rs @@ -573,6 +573,10 @@ async fn ietf_datagram_readers_rejoin_a_lingering_broadcast() { .unwrap(); producer.append(millis(priority as u64), &b"live"[..]).unwrap(); assert_eq!(recv(&mut consumer).await.unwrap().unwrap().payload.as_ref(), b"live"); + consumer.set_priority(-3); + kio::clock::timeout(TIMEOUT, datagram_priority(&producer, Some(-3))) + .await + .expect("the rejoined reader's lowered priority never arrived"); drop(consumer); kio::clock::timeout(TIMEOUT, datagram_priority(&producer, None)) .await @@ -586,7 +590,14 @@ async fn ietf_datagram_readers_rejoin_a_lingering_broadcast() { /// datagram, and routes the datagram before it ends the track. #[kio::test] async fn a_datagram_sent_right_behind_its_answer_arrives() { - for version in ["moq-lite-05", "moq-lite-06", "moq-lite-07-wip"] { + for version in [ + "moq-lite-05", + "moq-lite-06", + "moq-lite-07-wip", + "moq-transport-18", + "moq-transport-20", + "moq-transport-22", + ] { for finish in [false, true] { let chain = Chain::new(&[version]).await; let requests = chain.publication.broadcast().requests(); @@ -613,7 +624,7 @@ async fn a_datagram_sent_right_behind_its_answer_arrives() { // The second node subscribes through the session it accepted. assert_eq!( chain.pairs[0].server.stats().datagrams_unrouted, - Some(0), + version.parse::().unwrap().is_lite().then_some(0), "{version} finish={finish}" ); } @@ -728,24 +739,6 @@ async fn a_track_ended_under_a_datagram_flood_keeps_its_stream() { } } -/// The publisher's answer, first datagram, and end can all reach one receive pass. -#[kio::test] -async fn an_ietf_datagram_sent_with_its_answer_and_end_arrives() { - for version in ["moq-transport-18", "moq-transport-20", "moq-transport-22"] { - let chain = Chain::new(&[version]).await; - let mut producer = chain.publication.broadcast().create_datagrams(TRACK, timed()).unwrap(); - let mut consumer = chain.remote().await.datagrams(TRACK).unwrap(); - consumer.set_priority(5); - datagram_priority(&producer, Some(5)).await; - producer.append(millis(1), &b"first"[..]).unwrap(); - producer.finish(); - consumer.info().await.unwrap(); - let datagram = recv(&mut consumer).await.unwrap().expect("first datagram"); - assert_eq!(&datagram.payload[..], b"first", "{version}"); - assert!(recv(&mut consumer).await.unwrap().is_none(), "{version}"); - } -} - /// Ending a track must preserve its queued datagrams across the receiver's budget. #[kio::test] async fn an_ietf_datagram_burst_sent_with_its_end_arrives() {