From 02192cac9dc5f9df1e0792f8032bbe4cb686d7bd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 19 Oct 2026 21:44:34 -0700 Subject: [PATCH 1/4] quest: claim m1/auth/request-update Co-Authored-By: Claude Opus 5.5 From 6437b70ea62584d9f8b2440955768208b60eb2ed Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 19 Oct 2026 22:13:33 -0700 Subject: [PATCH 2/4] fix(net): renew draft-16 announcements in place Draft 16 answers a REQUEST_UPDATE by the update's own Request ID, which the control-stream adapter dropped. Route that answer to the request it updated, in Rust and JS, so a swapped route auth renews a draft-16 PUBLISH_NAMESPACE with REQUEST_UPDATE instead of withdrawing and re-announcing it, and draft-16 subscription updates keep one in flight. An unanswered draft-16 update now withdraws with PUBLISH_NAMESPACE_DONE, and a cancel that beats the answer ends only its request. Completes quest/m1/auth/request-update.md. Co-Authored-By: Claude Opus 5.5 --- doc/concept/moq-lite.md | 4 +- doc/concept/standard.md | 2 +- js/net/src/hop.ts | 2 +- js/net/src/ietf/adapter.test.ts | 74 ++++++++++- js/net/src/ietf/adapter.ts | 24 +++- js/net/src/ietf/publisher.test.ts | 95 ++++++++++++- js/net/src/ietf/publisher.ts | 15 ++- js/net/src/ietf/subscriber.test.ts | 45 +++++++ js/net/src/ietf/subscriber.ts | 11 +- quest/m1/auth/README.md | 3 +- quest/m1/auth/request-race.md | 4 +- quest/m1/auth/request-update.md | 87 ------------ rs/moq-net/src/ietf/adapter.rs | 206 ++++++++++++++++++++++++++--- rs/moq-net/src/ietf/publisher.rs | 150 +++++++++++++++++---- rs/moq-net/src/ietf/subscriber.rs | 8 +- rs/moq-net/src/model/origin.rs | 2 +- 16 files changed, 576 insertions(+), 156 deletions(-) delete mode 100644 quest/m1/auth/request-update.md diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index e8667e90292..eeff301f043 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -206,7 +206,9 @@ exchange on draft-17+ through the [MoQ Auth extension](/draft/moq-auth), limited to namespace prefixes. Older versions have no grant; the URL token keeps working everywhere. On moq-transport an added token is kept even without the extension, so an announcement can name it: it rides that `PUBLISH_NAMESPACE` as its -authorization token. The token belongs to the session that added it, so after a +authorization token. Naming a fresh token renews the announcement in place with +`REQUEST_UPDATE` from draft 16, and re-announces it on drafts 14 and 15, so it +can outlive the token it started with. The token belongs to the session that added it, so after a reconnect the application adds it again and re-announces. moq-lite has no per-request token, so there an announcement rides the session's scope. diff --git a/doc/concept/standard.md b/doc/concept/standard.md index 46a1f5d1cec..f92bc1782fb 100644 --- a/doc/concept/standard.md +++ b/doc/concept/standard.md @@ -29,7 +29,7 @@ subset drops. What a peer actually observes against this implementation: - **History is one group.** In Rust, Object ID gaps must be marked by Prior Object ID Gap properties for a group to be fetchable. A `FETCH` returns one group from the cache, or on drafts 14 through 19 the saved prefix of the group a new subscription just joined. A range of groups is refused, as is a draft 20+ `LOCATION_FILTER` that ends at Largest Object rather than an absolute End Location. On draft 20 and later, `FETCH_OK`'s End Location is the requested end, capped at Largest Object: a range holding no objects gets an empty fetch stream, and only a start past Largest Object is refused, with `INVALID_RANGE`. A relay's copy with no upstream subscription can't know Largest Object short of the track's end, so it echoes the requested end instead. JavaScript publishing refuses every `FETCH`. Datagrams are never fetchable. A Rust reader that only fetches learns the track from `TRACK_STATUS` instead of subscribing, so a finished track stays fetchable; draft 17 still subscribes, since its `TRACK_STATUS` answer cannot say whether the track is timed, and so does a reader whose publisher refuses `TRACK_STATUS` as `NOT_SUPPORTED`. - **Timing.** A track whose `SUBSCRIBE_OK` declares no `TIMESCALE` is untimed, as is every track on drafts 14 through 16. A Rust reader that never subscribed takes the units from `TRACK_STATUS` on draft 18 and later. - **Strict SETUP and parameters.** Repeated unknown `SETUP` options, GREASE included, are accepted if well-formed; a repeated known option is rejected. On draft 17 and later, a `GROUP_ORDER` other than Ascending (1) or Descending (2) closes the session. Message parameters follow the negotiated draft's lists: drafts 14 and 15 ignore an unknown or misplaced parameter, draft 16 ignores one defined only for another message but closes the session on an unknown one, and draft 17 and later close it on both. Other malformed control input closes the session with the draft's code too, such as a server `SETUP` carrying `PATH` or `AUTHORITY`, a request cut short by FIN, or a second `GOAWAY` on one stream; a first `GOAWAY` on a request stream is legal. -- **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, a token on a `SUBSCRIBE` or `PUBLISH_NAMESPACE` is verified like an in-band session token and authorizes that one request alone, 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 renews the token, except on a drafts 14 and 15 `PUBLISH_NAMESPACE`, which has no update, and a refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, including the one drafts 14 and 15 answer a solicited `SUBSCRIBE_NAMESPACE` with, and swapping it renews the request with `REQUEST_UPDATE` from draft 17, or ends and re-sends it before; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` from draft 16 carries no token. The token belongs to the session that added it, so a reconnect means adding it again and re-announcing. A token on any other request is decoded and grants nothing, and a request repeating one is refused with `MALFORMED_AUTH_TOKEN`. `SUBSCRIBE_TRACKS` reads its namespace before refusal; its token is not decoded. On draft 17 and later, the negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. +- **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, a token on a `SUBSCRIBE` or `PUBLISH_NAMESPACE` is verified like an in-band session token and authorizes that one request alone, 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 renews the token, except on a drafts 14 and 15 `PUBLISH_NAMESPACE`, which has no update, and a refused renewal ends only that request. Rust and JavaScript send the token a route names (`Route::auth`) on its `PUBLISH_NAMESPACE`, including the one drafts 14 and 15 answer a solicited `SUBSCRIBE_NAMESPACE` with, and swapping it renews the request with `REQUEST_UPDATE` from draft 16, or ends and re-sends it before; the inline `NAMESPACE` that answers a solicited `SUBSCRIBE_NAMESPACE` from draft 16 carries no token. The token belongs to the session that added it, so a reconnect means adding it again and re-announcing. A token on any other request is decoded and grants nothing, and a request repeating one is refused with `MALFORMED_AUTH_TOKEN`. `SUBSCRIBE_TRACKS` reads its namespace before refusal; its token is not decoded. On draft 17 and later, the negotiated [MoQ Auth extension](/concept/moq-lite#authorization) accepts additional session tokens in band. - **Refused, not fatal.** A legal request this stack does not serve is rejected on its own and the session stays up: `FORWARD=0`, `SUBSCRIBE_TRACKS`, and the fetch forms above get `NOT_SUPPORTED`. Range Filters get `INVALID_FILTER` on draft 19 and later because the advertised filter limit is zero. On draft 21 and later, 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 renews request tokens and judges `NEW_GROUP_REQUEST` as a join (see dynamic groups below). 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`. On draft 18 and later 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. Datagrams retain their IDs, properties, priority, and status. Plain Rust readers and moq-lite peers see consecutive frames and normal datagram payloads; JavaScript supports only normal datagrams at Object ID 0. A malformed datagram closes the session; a relayed one the downstream draft cannot carry is dropped. A relayed Timestamp leaves in the units the downstream response declares, and is dropped where it declares none (drafts 14 through 16), as for a plain frame; an immutable one that would change or vanish is refused. Only property 0x10 is read as a Timestamp. Property 0x06 stays opaque and keeps its value regardless of timestamp units; it is dropped across the draft-19 boundary, or refused when immutable, because draft 19 assigns it to SUBGROUP\_DELIVERY\_TIMEOUT. From draft 15, a status object carrying properties is refused, and one relayed from draft 14 leaves without them. Draft 14 allows them, but an END\_OF\_TRACK carrying properties ends the subscription as unsupported, since a track end has nowhere to keep them. A `FETCH` carries the END\_OF\_GROUP object on drafts 14 and 15; later drafts have no FETCH status, so their 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` (draft 15 and later) 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`. On drafts 15 and 16, whose `TRACK_STATUS` answer has no room for it, a relay copy first learned from `TRACK_STATUS` (a fetch-only reader) does not advertise it to later subscribers. moq-lite hops do not carry requests yet. diff --git a/js/net/src/hop.ts b/js/net/src/hop.ts index 9017b38e936..1dd16b75281 100644 --- a/js/net/src/hop.ts +++ b/js/net/src/hop.ts @@ -107,7 +107,7 @@ export interface Route { * token on its moq-transport PUBLISH_NAMESPACE: any other session, the inline NAMESPACE * answering a solicited SUBSCRIBE_NAMESPACE from draft 16 on (drafts 14 and 15 answer * with a PUBLISH_NAMESPACE, which carries it), and moq-lite advertise the route unnamed. - * Swapping it renews the request with REQUEST_UPDATE from draft 17, or ends and makes it + * Swapping it renews the request with REQUEST_UPDATE from draft 16, or ends and makes it * again before. Local: a relay never forwards it. Mirrors `Route::auth` in rs/moq-net. */ auth?: Auth; diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index 3e8e075ac2c..b4fd440a47e 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -6,8 +6,13 @@ import { Stream } from "../stream.ts"; import { ControlStreamAdapter } from "./adapter.ts"; import { toRequestCode } from "./error.ts"; import { GoAway } from "./goaway.ts"; -import { PublishNamespace, PublishNamespaceCancel, PublishNamespaceDone } from "./publish_namespace.ts"; -import { initialMaxRequestId, MaxRequestId, REQUEST_WINDOW, RequestError } from "./request.ts"; +import { + PublishNamespace, + PublishNamespaceCancel, + PublishNamespaceDone, + PublishNamespaceUpdate, +} from "./publish_namespace.ts"; +import { initialMaxRequestId, MaxRequestId, REQUEST_WINDOW, RequestError, RequestOk } from "./request.ts"; import { Subscribe, SubscribeUpdate } from "./subscribe.ts"; import { SubscribeNamespaceLegacy } from "./subscribe_namespace.ts"; import { TrackStatusRequest } from "./track.ts"; @@ -713,3 +718,68 @@ test("a canceled request ID wait never spends a later grant", async () => { } } }); + +// Draft 16 answers our REQUEST_UPDATE by the update's own request ID. The answer reaches the +// request it updated, and a refusal leaves that request open. Draft 15's reaches nothing. +test("an answer to our update reaches the request it updated on draft 16 only", async () => { + for (const [version, alpn] of [ + [Version.DRAFT_15, ALPN.DRAFT_15], + [Version.DRAFT_16, ALPN.DRAFT_16], + ] as const) { + const { adapter, peer, running } = await windowed(version, alpn, 8n, true); + try { + const stream = adapter.openBi(); + await stream.writer.u53(PublishNamespace.id); + await new PublishNamespace({ requestId: 0n, trackNamespace: Path.from("ns") }).encode( + stream.writer, + version, + ); + const next = () => + Promise.race([ + stream.reader.u53(), + new Promise((resolve) => setTimeout(() => resolve(undefined), WAIT)), + ]); + const renew = async (own: bigint) => { + await stream.writer.u53(SubscribeUpdate.id); + const update = + version === Version.DRAFT_16 + ? new PublishNamespaceUpdate({ requestId: own, namespaceRequestId: 0n, update: {} }) + : new SubscribeUpdate({ requestId: 0n, ownRequestId: own }); + await update.encode(stream.writer, version); + }; + const ok = async (requestId: bigint) => { + await peer.writer.u53(RequestOk.id); + await new RequestOk({ requestId }).encode(peer.writer, version); + }; + + await ok(0n); + expect(await next()).toBe(RequestOk.id); + await RequestOk.decode(stream.reader, version); + + await renew(2n); + await ok(2n); + await renew(4n); + await peer.writer.u53(RequestError.id); + await new RequestError({ requestId: 4n, errorCode: 0, reasonPhrase: "refused" }).encode( + peer.writer, + version, + ); + if (version === Version.DRAFT_15) { + expect(await next()).toBeUndefined(); + continue; + } + expect(await next()).toBe(RequestOk.id); + await RequestOk.decode(stream.reader, version); + expect(await next()).toBe(RequestError.id); + await RequestError.decode(stream.reader, version); + + // Still open: a later message for the request reaches it after the refusal. + await ok(0n); + expect(await next()).toBe(RequestOk.id); + } finally { + peer.close(); + adapter.close(); + void running; + } + } +}); diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index 1d4bb8b3af2..2fdf10ed6c3 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -258,9 +258,10 @@ export class ControlStreamAdapter implements Session { // SubscribeNamespace requestIds — for routing 0x08/0x0E entries that lack requestId (v14/v15) #subscribeNamespaces = new Set(); - // Our updates' own request IDs on drafts 15 and 16, whose answer names that ID rather than - // the request it updated. Nothing waits on it, so it is consumed here. - #updates = new Set(); + // Our updates' own request IDs on drafts 15 and 16 → the request each updated, since the + // answer names only the update. Draft 16's reaches that request's stream; draft 15's is + // consumed here, since nothing there reads one. An entry goes with its answer or its request. + #updates = new Map(); // Incoming stream queue (for acceptBi) #incomingQueue: Stream[] = []; @@ -397,7 +398,12 @@ export class ControlStreamAdapter implements Session { } } else { spent = this.#updateId(toFlush); - if (spent !== undefined && this.version !== Version.DRAFT_14) this.#updates.add(spent); + if ( + spent !== undefined && + registeredRequestId !== undefined && + this.version !== Version.DRAFT_14 + ) + this.#updates.set(spent, registeredRequestId); } await this.#send(toFlush, spent); @@ -504,8 +510,13 @@ export class ControlStreamAdapter implements Session { } const { route, requestId } = classified; - if ((route === Route.Response || route === Route.ErrorResponse) && this.#updates.delete(requestId)) + const updated = this.#updates.get(requestId); + if ((route === Route.Response || route === Route.ErrorResponse) && updated !== undefined) { + // A refused update leaves its request open: the answer is all it ends. + this.#updates.delete(requestId); + if (this.version === Version.DRAFT_16) this.#pushMessage(updated, typeId, size, body); continue; + } switch (route) { case Route.NewRequest: @@ -643,6 +654,9 @@ export class ControlStreamAdapter implements Session { const held = this.#open.delete(requestId); this.#streams.delete(requestId); this.#subscribeNamespaces.delete(requestId); + for (const [update, updated] of this.#updates) { + if (updated === requestId) this.#updates.delete(update); + } const registered = this.#namespacesByRequestId.get(requestId); if (registered !== undefined) { diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index f300507adef..9a36b94eb4f 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -22,7 +22,7 @@ import { Frame, Group as GroupMessage } from "./object.ts"; import { Parameters } from "./parameters.ts"; import { toWire } from "./priority.ts"; import { PublishDone } from "./publish.ts"; -import { PublishNamespace, PublishNamespaceUpdate } from "./publish_namespace.ts"; +import { PublishNamespace, PublishNamespaceDone, PublishNamespaceUpdate } from "./publish_namespace.ts"; import { Publisher } from "./publisher.ts"; import { RequestError, RequestOk } from "./request.ts"; import { GroupOrder, Subscribe, SubscribeError, SubscribeOk, SubscribeUpdate, Unsubscribe } from "./subscribe.ts"; @@ -1699,6 +1699,99 @@ test("swapping a route's token is one REQUEST_UPDATE carrying it", async () => { } }); +/** + * Draft 16 renews in place too: the REQUEST_UPDATE names the PUBLISH_NAMESPACE it renews, + * and the adapter routes its answer back, so the request is neither withdrawn nor made again. + */ +test("draft 16 swaps a route's token with one REQUEST_UPDATE", async () => { + const version = Version.DRAFT_16; + const pair = createMockTransportPair(ALPNS[version]); + const control = await Stream.open(pair.server, { version }); + const adapter = new ControlStreamAdapter(pair.server, control, version, 100n, false); + void adapter.run().catch(() => undefined); + const peer = await Stream.accept(pair.client, version); + if (!peer) throw new Error("no control stream"); + const tokens = new Tokens(); + const { pub, origin } = publisher(pair.server, { session: adapter, auth: tokens }); + const { sent, restore } = carriedTokens(); + try { + const broadcast = origin.createBroadcast(Path.from("mine")); + broadcast.announce({ auth: tokens.issue("first") }); + void pub.runPublishNamespaces(); + + expect(await peer.reader.u53()).toBe(PublishNamespace.id); + const advertised = await PublishNamespace.decode(peer.reader, version); + await peer.writer.u53(RequestOk.id); + await new RequestOk({ requestId: advertised.requestId }).encode(peer.writer, version); + + // The next renewal waits on the answer to this one, which only routing delivers. + for (const token of ["second", "third"]) { + broadcast.announce({ auth: tokens.issue(token) }); + expect(await peer.reader.u53()).toBe(PublishNamespaceUpdate.id); + const update = await PublishNamespaceUpdate.decode(peer.reader, version); + expect(update.namespaceRequestId).toBe(advertised.requestId); + await peer.writer.u53(RequestOk.id); + await new RequestOk({ requestId: update.requestId }).encode(peer.writer, version); + } + + // Answered and still held: nothing withdrawn or advertised again. + const more = watch(peer.reader.u53()); + await flush(); + expect(more.state).toBe("pending"); + expect(sent).toEqual(["first", "second", "third"]); + } finally { + restore(); + origin.close(); + adapter.close(); + } +}); + +/** + * A draft-16 request rides the control stream, where dropping it tells the peer nothing, so + * an unanswered update withdraws it with PUBLISH_NAMESPACE_DONE before offering it again. + */ +test("an unanswered draft 16 update withdraws its request before offering it again", async () => { + jest.useFakeTimers(); + const version = Version.DRAFT_16; + const pair = createMockTransportPair(ALPNS[version]); + const control = await Stream.open(pair.server, { version }); + const adapter = new ControlStreamAdapter(pair.server, control, version, 100n, false); + void adapter.run().catch(() => undefined); + const peer = await Stream.accept(pair.client, version); + if (!peer) throw new Error("no control stream"); + const tokens = new Tokens(); + const { pub, origin } = publisher(pair.server, { session: adapter, auth: tokens }); + const warn = spyOn(console, "warn").mockImplementation(() => void 0); + try { + const broadcast = origin.createBroadcast(Path.from("mine")); + broadcast.announce({ auth: tokens.issue("first") }); + void pub.runPublishNamespaces(); + + expect(await peer.reader.u53()).toBe(PublishNamespace.id); + const advertised = await PublishNamespace.decode(peer.reader, version); + await peer.writer.u53(RequestOk.id); + await new RequestOk({ requestId: advertised.requestId }).encode(peer.writer, version); + + broadcast.announce({ auth: tokens.issue("second") }); + expect(await peer.reader.u53()).toBe(PublishNamespaceUpdate.id); + await PublishNamespaceUpdate.decode(peer.reader, version); + + const next = watch(peer.reader.u53()); + await advanceUntil(() => next.state !== "pending", 10_100); + expect(next.value).toBe(PublishNamespaceDone.id); + expect((await PublishNamespaceDone.decode(peer.reader, version)).requestId).toBe(advertised.requestId); + + const fresh = watch(peer.reader.u53()); + await advanceUntil(() => fresh.state !== "pending", 30_000); + expect(fresh.value).toBe(PublishNamespace.id); + } finally { + warn.mockRestore(); + origin.close(); + adapter.close(); + jest.useRealTimers(); + } +}); + /** A token another session issued is never carried, so swapping between two sends nothing. */ test("a foreign route token is not carried", async () => { const foreign = new Tokens(); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 3766b492bb3..3bea827e1c4 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -1629,7 +1629,7 @@ export class Publisher { * the token the request carries. * * moq-transport renames a request's token with REQUEST_UPDATE, but cannot name one on a - * request that had none, nor drop one, and before draft-17 cannot update a namespace at + * request that had none, nor drop one, and before draft-16 cannot update a namespace at * all: those withdraw the request and make it again. */ #change( @@ -1644,7 +1644,7 @@ export class Publisher { const to = target.carry(next.route.auth); if (from === to) return diff; const version = this.#session.version; - const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16; + const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15; if (from === undefined || to === undefined || legacy) return "restart"; return { ...(diff === "same" ? {} : diff), token: to }; } @@ -1830,7 +1830,11 @@ export class Publisher { (async (): Promise => { const { writer, reader } = request.stream; await writer.u53(PublishNamespaceUpdate.id); - await new PublishNamespaceUpdate({ requestId, update, token }).encode(writer, version); + const namespaceRequestId = version === Version.DRAFT_16 ? request.requestId : undefined; + await new PublishNamespaceUpdate({ requestId, namespaceRequestId, update, token }).encode( + writer, + version, + ); const typeId = await reader.u53(); if (typeId === RequestError.id) { @@ -1852,7 +1856,10 @@ export class Publisher { const e = err instanceof TimeoutError ? controlTimeout(err) : error(err); console.warn(`publish_namespace update failed: broadcast=${path} error=${reason(e)}`); requests.delete(path); - request.stream.abort(e); + // Aborting a request riding the control stream tells the peer nothing, so it would + // hold the namespace beside the one advertised next. + if (version === Version.DRAFT_16) await this.#end(request).catch(() => undefined); + else request.stream.abort(e); return "dropped"; } diff --git a/js/net/src/ietf/subscriber.test.ts b/js/net/src/ietf/subscriber.test.ts index b42b699eb5c..c3e772eb2cd 100644 --- a/js/net/src/ietf/subscriber.test.ts +++ b/js/net/src/ietf/subscriber.test.ts @@ -2303,6 +2303,51 @@ for (const negotiated of [false, true]) { peer.close(); }); } +/** + * Draft 16 answers every update, and the adapter routes the answer back by the update's own + * request ID, so one update is in flight at a time and the next waits for it. + */ +test("a draft 16 subscription update waits for the last one's answer", async () => { + const version = Version.DRAFT_16; + const pair = createMockTransportPair(ALPN.DRAFT_16); + const control = await Stream.open(pair.server, { version }); + const session = new ControlStreamAdapter(pair.server, control, version, 100n, true); + void session.run().catch(() => void 0); + const subscriber = new Subscriber({ session }); + const peer = await nextStream(pair.client); + if (!peer) throw new Error("missing control stream"); + peer.reader.version = version; + peer.writer.version = version; + + const track = subscriber.consume(Path.from("room")).track("video").subscribe({ priority: 1 }); + try { + expect(await peer.reader.u53()).toBe(Subscribe.id); + const request = await Subscribe.decode(peer.reader, version); + await peer.writer.u53(SubscribeOk.id); + await new SubscribeOk({ requestId: request.requestId, trackAlias: ALIAS }).encode(peer.writer, version); + await track.info(); + + const higher = track.fork({ priority: 9 }); + expect(await peer.reader.u53()).toBe(SubscribeUpdate.id); + const first = await SubscribeUpdate.decode(peer.reader, version); + const highest = track.fork({ priority: 20 }); + const next = peer.reader.u53(); + const waiting = await Promise.race([next, new Promise((resolve) => setTimeout(() => resolve("waiting"), 50))]); + expect(waiting).toBe("waiting"); + + await peer.writer.u53(RequestOk.id); + await new RequestOk({ requestId: first.ownRequestId }).encode(peer.writer, version); + expect(await next).toBe(SubscribeUpdate.id); + const second = await SubscribeUpdate.decode(peer.reader, version); + expect(second.parameters.subscriberPriority).toBe(235); + higher.close(); + highest.close(); + } finally { + track.close(); + session.close(); + } +}); + /** Advertise `path` inline under `epoch`, on a session that negotiated MoQ Epoch alone. */ async function inlineEpoch(stream: Stream, path: Path.Valid, epoch: Epoch.Valid): Promise { await stream.writer.u53(SubscribeNamespaceEntry.id); diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 5be09e2db9a..20ddc5aac0e 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -1021,9 +1021,10 @@ export class Subscriber { /** * Follow the aggregate subscriber priority upstream until `done` settles: SUBSCRIBE_UPDATE - * through draft 16, REQUEST_UPDATE after, each under a Request ID of its own. From draft 17 - * the answer comes back on this stream, so one update is in flight at a time and changes - * meanwhile coalesce. No draft lets an update change the group order, so it stays as asked. + * through draft 16, REQUEST_UPDATE after, each under a Request ID of its own. From draft 16 + * the answer comes back on this stream (routed by the adapter on draft 16), so one update is + * in flight at a time and changes meanwhile coalesce. No draft lets an update change the + * group order, so it stays as asked. */ async #runUpdates( stream: Stream, @@ -1036,6 +1037,8 @@ export class Subscriber { ): Promise { const version = this.#session.version; const legacy = version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16; + // Draft 14 answers no update, and draft 15's answer reaches no subscription. + const answered = version !== Version.DRAFT_14 && version !== Version.DRAFT_15; const stopped = done.then( () => null, () => null, @@ -1076,7 +1079,7 @@ export class Subscriber { ? new SubscribeUpdate({ requestId, ownRequestId: own, parameters }) : new SubscribeUpdate({ requestId: own, parameters }); // Counted before writing: the answer can be read before the write's promise resolves. - if (!legacy) unanswered.update((count) => count + 1); + if (answered) unanswered.update((count) => count + 1); try { await stream.writer.u53(SubscribeUpdate.id); await update.encode(stream.writer, version); diff --git a/quest/m1/auth/README.md b/quest/m1/auth/README.md index e16a8a819c4..6161f9c240b 100644 --- a/quest/m1/auth/README.md +++ b/quest/m1/auth/README.md @@ -51,7 +51,7 @@ Decisions settled while planning, recorded so review does not relitigate them: a request racing the token it relies on, is [A request right behind its token](/quest/m1/auth/request-race.md). Step 3 sends outgoing request tokens on moq-transport through `Route::auth` as - `USE_VALUE` ([Live requests follow their auth](/quest/m1/auth/request-update.md)); + `USE_VALUE` ([#5695](https://github.com/moq-dev/moq/pull/5695)); lite has nothing to send, since the token already sits in the union. - ✅ Decided 2026-10-15 (maintainer, #5148): `auth::Handle::authorize` keeps its name and now means the connection credential's grant; the @@ -144,7 +144,6 @@ because `main` assigned 0x3A to NOT_FETCHABLE first. ## Required -- [Live requests follow their auth](/quest/m1/auth/request-update.md) - a route swaps its auth and the session sends the update, keeping the request up - [Auth outlives its session](/quest/m1/auth/issuer.md) - one `auth::Handle` spans a connection's sessions, so a route's auth survives a reconnect - [A request right behind its token](/quest/m1/auth/request-race.md) - a lite request relying on a just-presented token is admitted, with no auth field on any request - [Bindings](/quest/m1/auth/bindings.md) - grants and tokens reach every diff --git a/quest/m1/auth/request-race.md b/quest/m1/auth/request-race.md index b1a7fc308e2..69c5d1c6f11 100644 --- a/quest/m1/auth/request-race.md +++ b/quest/m1/auth/request-race.md @@ -110,5 +110,5 @@ Public API: none. Wire: lite-07-wip draft text only, no new bytes. - [#5927](https://github.com/moq-dev/moq/pull/5927) - the closed named-auth version this replaces -- [Live requests follow their auth](/quest/m1/auth/request-update.md) - - moq-transport request renewal; lite renews through the union +- [#5695](https://github.com/moq-dev/moq/pull/5695) - moq-transport request + renewal; lite renews through the union diff --git a/quest/m1/auth/request-update.md b/quest/m1/auth/request-update.md deleted file mode 100644 index 47249551aec..00000000000 --- a/quest/m1/auth/request-update.md +++ /dev/null @@ -1,87 +0,0 @@ -# [L] Live requests follow their auth - -## Goal - -A publisher renews the credential behind a live announcement without -reopening it. A `Route` holds an optional `moq_net::auth::Auth`; when the -application swaps it, the session sends REQUEST_UPDATE (moq-transport) -carrying the new auth, and the request stays up. Lite sends nothing: a -route's auth already sits in the session union through its AUTH stream, so -lite renews through the union as in -[A request right behind its token](/quest/m1/auth/request-race.md). The first consumer is a client that keeps a -PUBLISH_NAMESPACE alive past its credential's expiry by sending -REQUEST_UPDATE with a fresh token before it expires. - -## Plan - -Step 3 of the unified auth model settled on -[#5148](https://github.com/moq-dev/moq/pull/5148) on 2026-10-15, ranked right -after step 1 (#5148, incoming request tokens as auths on `requests()`) -because it has a consumer now. The decisions are recorded in the -[auth README](/quest/m1/auth/README.md). - -- ✅ Decided 2026-10-15 (maintainer): general request updates, not a - per-request token setter. Each `Route`/`Subscription` carries an - `Option`, and changing it triggers the update automatically. - Unset means the session's scope. -- ✅ Decided 2026-10-15 (maintainer): outgoing tokens go through `add()`; - #5148's `set_request_token` and its renewal sender are not carried forward. -- ✅ Decided 2026-10-16 (maintainer, #5695): `auth::Handle::add()` returns - `auth::Auth`, a cloneable reference to one presented credential that stays - presented until its last reference drops; `Route::auth` and - `Subscription::auth` hold `Option`. Not a cloneable `auth::Token`, - which conflates the credential bytes with the auth a request names, and - not credential bytes on the request, which bypass `add()`. -- ✅ Decided 2026-10-16 (maintainer, #5695): an `Auth` is bound to the auth - handle that issued it. A `Route` lives on an origin that outlives any one - session, so a connection-level handle spans that connection's sessions: - `moq_tokio::Connection::auth().add()` returns a `moq_net::auth::Auth` every - later session of that connection presents. Any other session sends the - request with no auth, under its scope. Not presented by any session, which - would send the credential to every peer the origin feeds, and not bound to - one session, which would make the app re-add and re-announce after every - reconnect. moq-tokio's `connection::Auth` and `connection::Token` are - renamed so the names match across layers. -- ✅ Revisited 2026-10-16 (maintainer, #5695): the connection-level handle is - a session-independent issuer in moq-net, which needs its own refactor, so - it moves to [Auth outlives its session](/quest/m1/auth/issuer.md). Until - then an `Auth` is bound to one session's handle, and a route on a - reconnecting connection is re-added and re-announced after a reconnect. -- ✅ Decided 2026-10-16 (maintainer, #5695): the JS names mirror Rust's in - this PR: JS `Auth` becomes `Handle` and `Token` becomes `Auth`. -- ✅ Decided 2026-10-16 (maintainer, #5695): an acceptor that accepts a - renewal and drops the grant it replaces in one step keeps the request. The - receiver hands over to the accepted renewal rather than ending between the - two. -- ✅ Decided 2026-10-16 (maintainer, #5695): `Route::auth` only. A - `Subscription` is aggregated across subscribers before it reaches the - wire, so `Subscription::auth` and its aggregation rule are a follow-up. -- Renewing many live requests at once must not take one round trip per - request: each request's update is its own in-flight step, polled beside the - session's other announce and subscribe work, so new announcements and - withdrawals never wait behind a renewal. At most one update is in flight - per request, superseded by a newer auth while it waits for credit. Request - IDs reach the wire in allocation order (drafts 14-16 require the next - sequential ID), and request-ID credit bounds how many start at once, with - no new cap. Benchmark in `rs/moq-net/benches` swept over live requests and - sessions sharing an origin, so the cost grows only with the requests - renewed. (From the deleted concurrent-renewal quest, which measured this - against #5148's earlier serial refresh.) -- On moq-transport the update carries the token as USE_VALUE and needs only - step 1. ✅ Decided 2026-10-19 (maintainer, closing - [#5927](https://github.com/moq-dev/moq/pull/5927)): lite requests never - name an auth, so on lite `Route::auth` puts nothing on the wire. A refused - update ends only that request. -- Mirror `Auth` and `Route::auth` in `js/net`; the bindings expose no auth - yet, so [Bindings](/quest/m1/auth/bindings.md) carries them. -- Describe moq-transport request renewal in `doc/concept/moq-lite.md`'s - Authorization section, and in `doc/concept/standard.md`, which describes - only incoming request tokens. - -Public API: a new optional auth on routes in Rust and JS. -Wire: none beyond the existing AUTHORIZATION TOKEN on PUBLISH_NAMESPACE and its -REQUEST_UPDATE. - -## Related - -- [A request right behind its token](/quest/m1/auth/request-race.md) - lite renewal through the union diff --git a/rs/moq-net/src/ietf/adapter.rs b/rs/moq-net/src/ietf/adapter.rs index 5e1ad51183e..c9ccf2a4f7f 100644 --- a/rs/moq-net/src/ietf/adapter.rs +++ b/rs/moq-net/src/ietf/adapter.rs @@ -262,6 +262,9 @@ pub struct VirtualSendStream { /// Accumulates bytes until the request_id can be parsed, /// then registers the stream and flushes. pending: Option, + /// Draft-16 only, once registered: what follows the request, held until whole so + /// each REQUEST_UPDATE is routed back to its request before it leaves. + updates: Option, } struct OutgoingRegistration { @@ -275,19 +278,11 @@ struct OutgoingRegistration { } impl OutgoingRegistration { - /// Try to parse the request_id (and optionally namespace) from the accumulated bytes. + /// Try to parse the request_id (and optionally namespace) from the accumulated bytes, + /// with the length of the message that carried it. /// Returns Ok(None) if not enough data yet, Err if the message is malformed. - fn try_parse(&self) -> Result, crate::Error> { - let mut r = Decoder::new(&self.buf, self.version.into()); - let Ok(type_id) = r.varint() else { - return Ok(None); - }; - let Ok(size) = r.u16() else { - return Ok(None); - }; - - // We know the full message size now: header bytes + body. - let Ok(mut body) = r.sub(size as usize) else { + fn try_parse(&self) -> Result, crate::Error> { + let Some((type_id, len, mut body)) = whole_message(&self.buf, self.version) else { return Ok(None); }; @@ -305,7 +300,7 @@ impl OutgoingRegistration { } } - Ok(Some(request_id)) + Ok(Some((request_id, len))) } fn register(self, request_id: RequestId) { @@ -327,6 +322,7 @@ impl VirtualSendStream { Self { control_tx, pending: None, + updates: None, } } @@ -334,6 +330,7 @@ impl VirtualSendStream { Self { control_tx, pending: Some(pending), + updates: None, } } } @@ -346,11 +343,30 @@ impl VirtualSendStream { if let Some(pending) = &mut self.pending { pending.buf.extend_from_slice(&chunk); - if let Some(request_id) = pending.try_parse()? { + if let Some((request_id, len)) = pending.try_parse()? { let mut pending = self.pending.take().unwrap(); - let buf = std::mem::take(&mut pending.buf).freeze(); + let mut buf = std::mem::take(&mut pending.buf); + let rest = buf.split_off(len); + if pending.version == Version::Draft16 { + self.updates = Some(UpdateRouting { + shared: Arc::clone(&pending.shared), + version: pending.version, + request_id, + buf: BytesMut::new(), + }); + } pending.register(request_id); - if !self.control_tx.push(buf) { + if !self.control_tx.push(buf.freeze()) { + return Err(crate::Error::Closed); + } + if !rest.is_empty() { + return self.push(rest.freeze()); + } + } + } else if let Some(updates) = &mut self.updates { + updates.buf.extend_from_slice(&chunk); + while let Some(message) = updates.next()? { + if !self.control_tx.push(message) { return Err(crate::Error::Closed); } } @@ -362,6 +378,42 @@ impl VirtualSendStream { } } +/// Draft-16 answers a REQUEST_UPDATE on the control stream by the update's own Request +/// ID, which names no stream, so each update we send is recorded against the request it +/// rides, in [`Shared::updates`], before it leaves. Drafts 14 and 15 answer no update of +/// ours that anything reads. +struct UpdateRouting { + shared: Arc, + version: Version, + /// The request whose stream carries the updates. + request_id: RequestId, + buf: BytesMut, +} + +impl UpdateRouting { + /// The next whole message, recorded first when it is an update. + fn next(&mut self) -> Result, crate::Error> { + let Some((type_id, len, mut body)) = whole_message(&self.buf, self.version) else { + return Ok(None); + }; + if type_id == ietf::SubscribeUpdate::ID { + let own = RequestId::decode(&mut body, self.version)?; + self.shared.route_update(own, self.request_id); + } + Ok(Some(self.buf.split_to(len).freeze())) + } +} + +/// The type, whole length, and body of the first message in `buf`, once all of it is there. +fn whole_message(buf: &[u8], version: Version) -> Option<(u64, usize, Decoder<'_>)> { + let mut r = Decoder::new(buf, version.into()); + let type_id = r.varint().ok()?; + let size = r.u16().ok()? as usize; + let len = buf.len() - r.remaining() + size; + let body = r.sub(size).ok()?; + Some((type_id, len, body)) +} + impl crate::transport::poll::SendStream for VirtualSendStream { type Error = crate::Error; type Group = crate::transport::poll::Ungrouped; @@ -392,6 +444,11 @@ impl crate::transport::poll::SendStream for VirtualSendStream { { let _ = self.control_tx.push(pending.buf.freeze()); } + if let Some(updates) = self.updates.take() + && !updates.buf.is_empty() + { + let _ = self.control_tx.push(updates.buf.freeze()); + } Ok(()) } @@ -657,6 +714,11 @@ struct Shared { /// writer, which FINs the matching VirtualRecvStream. streams: Mutex>>, + /// Draft-16: our REQUEST_UPDATEs' own Request IDs → the open request each updates, so + /// the one answer each is owed reaches that request's stream. Bounded by what we sent, + /// and an entry goes with its answer or its request. Locked after `streams`. + updates: Mutex>, + /// Namespace → request_id reverse lookup (for v14/v15 namespace-keyed messages). namespaces: Namespaces, } @@ -705,16 +767,24 @@ impl Shared { } /// Deliver a message to an open virtual stream, dropping it if the stream is gone. + /// An answer to one of our updates goes to the request it updated. /// Returns whether a stream took it. fn push(&self, request_id: RequestId, raw: Bytes) -> bool { - match self.streams.lock().unwrap().get(&request_id) { + let streams = self.streams.lock().unwrap(); + let request_id = self.updates.lock().unwrap().remove(&request_id).unwrap_or(request_id); + match streams.get(&request_id) { Some(tx) => tx.push(raw), None => false, } } /// Deliver a final message and FIN the stream: the removed writer drops after it. + /// A refusal of one of our updates is only its answer, which leaves the request open. fn close(&self, request_id: RequestId, raw: Bytes) { + if self.updates.lock().unwrap().contains_key(&request_id) { + self.push(request_id, raw); + return; + } let tx = self.streams.lock().unwrap().remove(&request_id); self.namespaces.forget(request_id); if let Some(tx) = tx { @@ -722,9 +792,23 @@ impl Shared { } } + /// Expect the answer to our update `own` on `request_id`'s stream, while it is open. + fn route_update(&self, own: RequestId, request_id: RequestId) { + let streams = self.streams.lock().unwrap(); + if streams.contains_key(&request_id) { + self.updates.lock().unwrap().insert(own, request_id); + } + } + /// Drop every routing entry a request owns, whether or not its stream is still around. fn forget(&self, request_id: RequestId) { - self.streams.lock().unwrap().remove(&request_id); + let mut streams = self.streams.lock().unwrap(); + streams.remove(&request_id); + let mut updates = self.updates.lock().unwrap(); + if !updates.is_empty() { + updates.retain(|_, updated| *updated != request_id); + } + drop((updates, streams)); self.namespaces.forget(request_id); } } @@ -1459,6 +1543,92 @@ mod tests { } } + /// Draft-16 answers our REQUEST_UPDATE by the update's own Request ID. The answer + /// reaches the request it updated, a refusal leaves that request open, and an update + /// written in pieces is recorded all the same. + #[moq_clock::test] + async fn our_update_answers_reach_their_request() { + const VERSION: Version = Version::Draft16; + let shared = Arc::new(Shared::default()); + let (mut send, mut recv) = shared.open_outgoing(VERSION); + let update = |request_id| { + encode_msg( + &ietf::PublishNamespaceUpdate { + request_id: RequestId(request_id), + namespace_request_id: Some(RequestId(4)), + hops: None, + cost: None, + authorization_token: None, + }, + VERSION, + ) + }; + + // The request and the first byte of its update in one write, the rest in another. + let first = update(6); + let request = encode_msg(&publish_namespace(RequestId(4), "ns"), VERSION); + send.write_chunk([request.clone(), first.slice(..1)].concat().into()) + .await + .unwrap(); + send.write_chunk(first.slice(1..)).await.unwrap(); + assert_eq!(shared.control.pop().await.unwrap(), request); + assert_eq!(shared.control.pop().await.unwrap(), first); + + let ok = ietf::RequestOk { + request_id: Some(RequestId(6)), + active: None, + }; + assert!(matches!( + classify_msg(VERSION, ietf::RequestOk::ID, &encode_body(&ok, VERSION)).unwrap(), + Route::Response(RequestId(6)) + )); + let ok = encode_msg(&ok, VERSION); + assert!(shared.push(RequestId(6), ok.clone()), "the answer found no stream"); + assert_eq!(recv.read_chunk(usize::MAX).await.unwrap(), Some(ok.clone())); + assert!(!shared.push(RequestId(6), ok), "a second answer was routed"); + + send.write_chunk(update(8)).await.unwrap(); + assert_eq!(shared.control.pop().await.unwrap(), update(8)); + let error = encode_msg( + &ietf::RequestError { + request_id: Some(RequestId(8)), + error_code: 0, + reason_phrase: "refused".into(), + retry_interval: 0, + }, + VERSION, + ); + shared.close(RequestId(8), error.clone()); + assert_eq!(recv.read_chunk(usize::MAX).await.unwrap(), Some(error)); + assert!( + shared.push(RequestId(4), Bytes::from_static(b"later")), + "the refusal closed the request" + ); + + // An update outstanding when its request goes leaves nothing behind. + send.write_chunk(update(10)).await.unwrap(); + assert_eq!(shared.updates.lock().unwrap().len(), 1); + drop(recv); + assert!(shared.updates.lock().unwrap().is_empty()); + } + + /// Before draft-16 an answer to our update names nothing we route. + #[moq_clock::test] + async fn draft_15_update_answers_are_not_routed() { + const VERSION: Version = Version::Draft15; + let shared = Arc::new(Shared::default()); + let (mut send, _recv) = shared.open_outgoing(VERSION); + send.write_chunk(encode_msg(&publish_namespace(RequestId(4), "ns"), VERSION)) + .await + .unwrap(); + // A SUBSCRIBE_UPDATE's header and its own Request ID 6, all that routing reads. + send.write_chunk(Bytes::from_static(&[0x02, 0x00, 0x01, 0x06])) + .await + .unwrap(); + assert!(!shared.push(RequestId(6), Bytes::from_static(b"ok"))); + assert!(shared.updates.lock().unwrap().is_empty()); + } + #[test] fn test_classify_goaway() { let body = Bytes::new(); diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index c61417cff5e..daee23dcb47 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -569,7 +569,7 @@ enum NamespaceEvent { /// What came back for an update in flight. enum Answer { - /// The peer's REQUEST_OK or REQUEST_ERROR. + /// The peer's REQUEST_OK or REQUEST_ERROR, or a draft-16 PUBLISH_NAMESPACE_CANCEL. Message(super::request_stream::Framed), /// The peer ended the request instead, a cancel from draft 17. Ended, @@ -3010,13 +3010,13 @@ where let renamed = wanted && held && held_epoch != epoch; let replaced = renamed && held_epoch.is_some() && epoch.is_some(); // moq-transport renames a request's auth with REQUEST_UPDATE, but cannot name one on - // a request that had none, nor drop one, and before draft-17 cannot update a + // a request that had none, nor drop one, and before draft-16 cannot update a // namespace at all: those withdraw the request and make it again. let restart = renamed || held_auth != auth && (held_auth.is_none() || auth.is_none() - || matches!(self.version, Version::Draft14 | Version::Draft15 | Version::Draft16)); + || matches!(self.version, Version::Draft14 | Version::Draft15)); // An update already waiting or in flight carries whatever is current once it goes // out, and its answer brings the loop back here for anything after that. @@ -3325,7 +3325,7 @@ where (Some(next), Some(held)) => ietf::PublishNamespaceUpdate::between(request_id, &held, &next), _ => ietf::PublishNamespaceUpdate { request_id, - namespace_request_id: None, + namespace_request_id: (self.version == Version::Draft16).then_some(request.request_id), hops: None, cost: None, authorization_token: None, @@ -3439,8 +3439,18 @@ where let refused = match answer { Answer::Late => { tracing::debug!(broadcast = %absolute, "no answer to the update"); - // Abrupt: a peer that never answers is not owed the FIN handshake. - ns.requests.remove(&suffix); + match self.version { + // Dropping a request riding the control stream tells the peer nothing, so + // it would hold the namespace beside the one advertised next. + Version::Draft16 => { + self.withdraw_namespace(&mut ns.target, &mut ns.requests, suffix.clone()) + .await? + } + // Abrupt: a peer that never answers is not owed the FIN handshake. + _ => { + ns.requests.remove(&suffix); + } + } Refused::No } Answer::Ended => { @@ -3475,6 +3485,14 @@ where .await?; self.refusal(msg.retry_interval) } + // Draft-16 carries its cancel as a message, which can beat the answer. + ietf::PublishNamespaceCancel::ID if self.version == Version::Draft16 => { + let msg: ietf::PublishNamespaceCancel = body.message(self.version)?; + tracing::debug!(broadcast = %absolute, message = ?msg, "update cancelled"); + self.withdraw_namespace(&mut ns.target, &mut ns.requests, suffix.clone()) + .await?; + Refused::No + } _ => return Err(Error::UnexpectedMessage), } } @@ -14201,11 +14219,14 @@ mod tests { /// A route's auth rides its PUBLISH_NAMESPACE as the request's token, and swapping it /// renews the request in place: REQUEST_UPDATE on the same stream, carrying only the - /// new token. + /// new token. Draft-16 names the request in the update. #[moq_clock::test] async fn swapping_a_route_auth_is_a_request_update() { - const VERSION: Version = Version::Draft19; + swaps_a_route_auth(Version::Draft16).await; + swaps_a_route_auth(Version::Draft19).await; + } + async fn swaps_a_route_auth(version: Version) { let origin = unheld_origin(); let auth = crate::auth::Handle::ietf(false); let first = auth.present(bytes::Bytes::from_static(b"first-secret"), false).unwrap(); @@ -14217,23 +14238,27 @@ mod tests { .unwrap(); settle().await; - let ok = publish_namespace_ok(VERSION).await; - let (publisher, log) = auth_publisher(&origin, &auth, vec![[ok.clone(), ok].concat()], VERSION); + let ok = publish_namespace_ok(version).await; + let (publisher, log) = auth_publisher(&origin, &auth, vec![[ok.clone(), ok].concat()], version); let mut run = std::pin::pin!(publisher.run_publish_namespaces()); poll_until(&mut run, &log, b"cam", 1).await; - assert_eq!(occurrences(&log, b"cam"), 1, "the advertisement never went out"); + assert_eq!( + occurrences(&log, b"cam"), + 1, + "{version}: the advertisement never went out" + ); assert_eq!( occurrences(&log, b"first-secret"), 1, - "PUBLISH_NAMESPACE carries the auth" + "{version}: PUBLISH_NAMESPACE carries the auth" ); cam.update(crate::origin::Route::default().with_auth(second)).unwrap(); let expected = request_update( - VERSION, + version, &ietf::PublishNamespaceUpdate { request_id: RequestId(3), - namespace_request_id: None, + namespace_request_id: (version == Version::Draft16).then_some(RequestId(1)), hops: None, cost: None, authorization_token: Some(auth_token(b"second-secret")), @@ -14242,9 +14267,67 @@ mod tests { .await; poll_until(&mut run, &log, &expected, 1).await; - assert_eq!(occurrences(&log, &expected), 1, "REQUEST_UPDATE with the new token"); - assert_eq!(occurrences(&log, b"cam"), 1, "PUBLISH_NAMESPACE was not repeated"); - assert_eq!(log.bi_opens(), 1, "the update rode the request's own stream"); + assert_eq!( + occurrences(&log, &expected), + 1, + "{version}: REQUEST_UPDATE with the new token" + ); + // Let its answer land, which leaves the request standing. + for _ in 0..10 { + assert!(futures::poll!(run.as_mut()).is_pending()); + settle().await; + } + assert_eq!( + occurrences(&log, b"cam"), + 1, + "{version}: PUBLISH_NAMESPACE was not repeated" + ); + assert_eq!(log.bi_opens(), 1, "{version}: the update rode the request's own stream"); + } + + /// A draft-16 peer's PUBLISH_NAMESPACE_CANCEL can beat its answer to our update. It + /// withdraws only that namespace, which is offered again, and the loop carries on. + #[moq_clock::test] + async fn a_draft_16_cancel_ahead_of_the_answer_ends_only_its_request() { + const VERSION: Version = Version::Draft16; + + let origin = unheld_origin(); + let auth = crate::auth::Handle::ietf(false); + let first = auth.present(bytes::Bytes::from_static(b"first-secret"), false).unwrap(); + let second = auth + .present(bytes::Bytes::from_static(b"second-secret"), false) + .unwrap(); + let cam = origin + .announce("cam", crate::origin::Route::default().with_auth(first)) + .unwrap(); + settle().await; + + let ok = publish_namespace_ok(VERSION).await; + let cancel = { + let log = crate::lite::test_transport::Log::default(); + let mut writer = + crate::coding::Writer::new(crate::lite::test_transport::SinkSend::new(log.clone()), VERSION); + writer.varint(ietf::PublishNamespaceCancel::ID).await.unwrap(); + writer + .encode(&ietf::PublishNamespaceCancel { + track_namespace: crate::Path::new("cam"), + request_id: RequestId(1), + error_code: 0, + reason_phrase: "gone".into(), + }) + .await + .unwrap(); + log.writes.lock().unwrap().clone() + }; + let (publisher, log) = auth_publisher(&origin, &auth, vec![[ok.clone(), cancel].concat(), ok], VERSION); + let mut run = std::pin::pin!(publisher.run_publish_namespaces()); + poll_until(&mut run, &log, b"first-secret", 1).await; + + cam.update(crate::origin::Route::default().with_auth(second)).unwrap(); + poll_until(&mut run, &log, b"cam", 2).await; + + assert_eq!(occurrences(&log, b"cam"), 2, "the namespace was not offered again"); + assert_eq!(log.bi_opens(), 2, "a fresh request after the cancel"); } /// An auth another handle issued is not this session's to present: the route is @@ -14302,7 +14385,7 @@ mod tests { assert_eq!(log.bi_opens(), 2, "a fresh stream for the fresh request"); } - /// Before draft-17 a namespace cannot be updated, so a renewal withdraws the request + /// Before draft-16 a namespace cannot be updated, so a renewal withdraws the request /// and makes it again with the new token. #[moq_clock::test] async fn a_draft_14_renewal_restarts_the_request() { @@ -14430,11 +14513,15 @@ mod tests { } /// An update the peer never answers drops its request once the timeout passes, and - /// the retry advertises the namespace afresh under the current auth. + /// the retry advertises the namespace afresh under the current auth. Draft-16's request + /// rides the control stream, where only PUBLISH_NAMESPACE_DONE withdraws it. #[moq_clock::test] async fn an_unanswered_renewal_advertises_again() { - const VERSION: Version = Version::Draft19; + advertises_again_unanswered(Version::Draft16).await; + advertises_again_unanswered(Version::Draft19).await; + } + async fn advertises_again_unanswered(version: Version) { let origin = unheld_origin(); let auth = crate::auth::Handle::ietf(false); let present = |token: &'static [u8]| auth.present(bytes::Bytes::from_static(token), false).unwrap(); @@ -14443,8 +14530,8 @@ mod tests { .unwrap(); settle().await; - let ok = publish_namespace_ok(VERSION).await; - let (publisher, log) = auth_publisher(&origin, &auth, vec![ok.clone(), ok], VERSION); + let ok = publish_namespace_ok(version).await; + let (publisher, log) = auth_publisher(&origin, &auth, vec![ok.clone(), ok], version); let mut run = std::pin::pin!(publisher.run_publish_namespaces()); poll_until(&mut run, &log, b"first", 1).await; cam.update(crate::origin::Route::default().with_auth(present(b"second"))) @@ -14462,8 +14549,25 @@ mod tests { assert_eq!( occurrences(&log, b"second"), 2, - "the unanswered renewal was never retried" + "{version}: the unanswered renewal was never retried" ); + if version == Version::Draft16 { + let done = { + let log = crate::lite::test_transport::Log::default(); + let mut writer = + crate::coding::Writer::new(crate::lite::test_transport::SinkSend::new(log.clone()), version); + writer.varint(ietf::PublishNamespaceDone::ID).await.unwrap(); + writer + .encode(&ietf::PublishNamespaceDone { + track_namespace: crate::Path::new("cam"), + request_id: RequestId(1), + }) + .await + .unwrap(); + log.writes.lock().unwrap().clone() + }; + assert_eq!(occurrences(&log, &done), 1, "the peer was never told to let it go"); + } } /// moq-transport has no restart: another source winning a namespace without an epoch diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index c9c69322147..7bed5115a96 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -3117,11 +3117,11 @@ where group: largest.group, object: largest.object.saturating_add(1), }); - // REQUEST_UPDATEs sent and not yet answered. Drafts 14-16 answer on the control - // stream by the update's own Request ID, which reaches no subscription, so only - // later drafts count them. + // REQUEST_UPDATEs sent and not yet answered. Draft-14 answers none, and draft-15's + // answer names only the update's own Request ID, which reaches no subscription, so + // only draft-16 on count them; the adapter routes draft-16's back by that ID. let unanswered = AtomicU64::new(0); - let answered_here = !matches!(self.version, Version::Draft14 | Version::Draft15 | Version::Draft16); + let answered_here = !matches!(self.version, Version::Draft14 | Version::Draft15); let mut fetch_done = fetching.is_none(); // Our grant stopped covering the track, which is aborted rather than kept lingering. diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index a115d7eefff..421c7760efe 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -471,7 +471,7 @@ pub struct Route { /// solicited SUBSCRIBE_NAMESPACE from draft 16 on (drafts 14 and 15 answer with a /// PUBLISH_NAMESPACE, which carries it), and moq-lite (which rides the session's scope) /// advertise the route unnamed. Swapping it is an update that renews the request with - /// REQUEST_UPDATE from draft 17, or ends and makes it again before. After a + /// REQUEST_UPDATE from draft 16, or ends and makes it again before. After a /// reconnect, add the token on the new session and name that. Local, like /// [`Self::source`]: a relay never forwards it. pub auth: Option, From 1c2409cd8092c6633f3b1f74490abac6c7db450f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 19 Oct 2026 23:27:53 -0700 Subject: [PATCH 3/4] fix(net): send no PUBLISH_NAMESPACE_DONE after a draft-16 cancel A draft-16 PUBLISH_NAMESPACE_CANCEL that beats the answer to our update already ended the request, and draft 16 sends no DONE after a cancel. Drop the request locally and offer it again, in Rust and JS. The new draft-16 JS tests settle the mock transport instead of waiting on wall-clock time. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/adapter.test.ts | 15 ++++++--- js/net/src/ietf/publisher.test.ts | 54 +++++++++++++++++++++++++++++- js/net/src/ietf/publisher.ts | 14 ++++++-- js/net/src/ietf/subscriber.test.ts | 11 ++++-- rs/moq-net/src/ietf/publisher.rs | 40 +++++++++++++--------- 5 files changed, 107 insertions(+), 27 deletions(-) diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index b4fd440a47e..b633331b12d 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -734,11 +734,16 @@ test("an answer to our update reaches the request it updated on draft 16 only", stream.writer, version, ); - const next = () => - Promise.race([ - stream.reader.u53(), - new Promise((resolve) => setTimeout(() => resolve(undefined), WAIT)), - ]); + // The mock transport schedules nothing on timers, so settled promise chains are final. + const next = async () => { + let id: number | undefined; + const read = stream.reader.u53().then((value) => { + id = value; + }); + for (let i = 0; i < 20 && id === undefined; i++) await new Promise((resolve) => setImmediate(resolve)); + void read.catch(() => undefined); + return id; + }; const renew = async (own: bigint) => { await stream.writer.u53(SubscribeUpdate.id); const update = diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 9a36b94eb4f..f68947350e4 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -22,7 +22,12 @@ import { Frame, Group as GroupMessage } from "./object.ts"; import { Parameters } from "./parameters.ts"; import { toWire } from "./priority.ts"; import { PublishDone } from "./publish.ts"; -import { PublishNamespace, PublishNamespaceDone, PublishNamespaceUpdate } from "./publish_namespace.ts"; +import { + PublishNamespace, + PublishNamespaceCancel, + PublishNamespaceDone, + PublishNamespaceUpdate, +} from "./publish_namespace.ts"; import { Publisher } from "./publisher.ts"; import { RequestError, RequestOk } from "./request.ts"; import { GroupOrder, Subscribe, SubscribeError, SubscribeOk, SubscribeUpdate, Unsubscribe } from "./subscribe.ts"; @@ -1792,6 +1797,53 @@ test("an unanswered draft 16 update withdraws its request before offering it aga } }); +/** + * A draft-16 PUBLISH_NAMESPACE_CANCEL can beat the answer to our update. The peer already let + * the namespace go, and draft 16 sends no PUBLISH_NAMESPACE_DONE after a cancel, so the next + * message is the namespace offered again. + */ +test("a draft 16 cancel ahead of the answer offers the namespace again without DONE", async () => { + jest.useFakeTimers(); + const version = Version.DRAFT_16; + const pair = createMockTransportPair(ALPNS[version]); + const control = await Stream.open(pair.server, { version }); + const adapter = new ControlStreamAdapter(pair.server, control, version, 100n, false); + void adapter.run().catch(() => undefined); + const peer = await Stream.accept(pair.client, version); + if (!peer) throw new Error("no control stream"); + const tokens = new Tokens(); + const { pub, origin } = publisher(pair.server, { session: adapter, auth: tokens }); + const warn = spyOn(console, "warn").mockImplementation(() => void 0); + try { + const broadcast = origin.createBroadcast(Path.from("mine")); + broadcast.announce({ auth: tokens.issue("first") }); + void pub.runPublishNamespaces(); + + expect(await peer.reader.u53()).toBe(PublishNamespace.id); + const advertised = await PublishNamespace.decode(peer.reader, version); + await peer.writer.u53(RequestOk.id); + await new RequestOk({ requestId: advertised.requestId }).encode(peer.writer, version); + + broadcast.announce({ auth: tokens.issue("second") }); + expect(await peer.reader.u53()).toBe(PublishNamespaceUpdate.id); + await PublishNamespaceUpdate.decode(peer.reader, version); + await peer.writer.u53(PublishNamespaceCancel.id); + await new PublishNamespaceCancel({ requestId: advertised.requestId, reasonPhrase: "gone" }).encode( + peer.writer, + version, + ); + + const next = watch(peer.reader.u53()); + await advanceUntil(() => next.state !== "pending", 30_000); + expect(next.value).toBe(PublishNamespace.id); + } finally { + warn.mockRestore(); + origin.close(); + adapter.close(); + jest.useRealTimers(); + } +}); + /** A token another session issued is never carried, so swapping between two sends nothing. */ test("a foreign route token is not carried", async () => { const foreign = new Tokens(); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 3bea827e1c4..24f4343fcfa 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -1805,8 +1805,9 @@ export class Publisher { * MAX_REQUEST_UPDATES the peer set without reading it; a change landing meanwhile waits * for the next turn. The answer decides whether the peer still holds the namespace: a * REQUEST_ERROR closes the peer's side and withdraws the advertisement, so ours is - * finished too, and an unanswered update is dropped abruptly, since a peer that ignored - * it cannot be assumed to hold either price. Either way the retry re-offers it fresh. + * finished too, a draft-16 cancel ends it with nothing more sent, and an unanswered update + * is dropped, since a peer that ignored it cannot be assumed to hold either price. Either + * way the retry re-offers it fresh. * * A different original publisher is an update like any other: the receiver drains what * it already serves from the old one and never splices the two. @@ -1836,6 +1837,15 @@ export class Publisher { version, ); + // Draft 16's adapter ends the stream on the peer's PUBLISH_NAMESPACE_CANCEL, which + // can beat the answer. The peer already let the namespace go, and draft 16 sends + // no PUBLISH_NAMESPACE_DONE after a cancel. + if (version === Version.DRAFT_16 && (await reader.done())) { + requests.delete(path); + request.stream.abort(new Error("cancelled by the peer")); + return "dropped"; + } + const typeId = await reader.u53(); if (typeId === RequestError.id) { const err = await RequestError.decode(reader, version); diff --git a/js/net/src/ietf/subscriber.test.ts b/js/net/src/ietf/subscriber.test.ts index c3e772eb2cd..d18e12dc1cb 100644 --- a/js/net/src/ietf/subscriber.test.ts +++ b/js/net/src/ietf/subscriber.test.ts @@ -2331,9 +2331,14 @@ test("a draft 16 subscription update waits for the last one's answer", async () expect(await peer.reader.u53()).toBe(SubscribeUpdate.id); const first = await SubscribeUpdate.decode(peer.reader, version); const highest = track.fork({ priority: 20 }); - const next = peer.reader.u53(); - const waiting = await Promise.race([next, new Promise((resolve) => setTimeout(() => resolve("waiting"), 50))]); - expect(waiting).toBe("waiting"); + let sent = false; + const next = peer.reader.u53().then((id) => { + sent = true; + return id; + }); + // The mock transport schedules nothing on timers, so settled promise chains are final. + for (let i = 0; i < 20; i++) await new Promise((resolve) => setImmediate(resolve)); + expect(sent).toBe(false); await peer.writer.u53(RequestOk.id); await new RequestOk({ requestId: first.ownRequestId }).encode(peer.writer, version); diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index daee23dcb47..074c74c7e69 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -3489,8 +3489,9 @@ where ietf::PublishNamespaceCancel::ID if self.version == Version::Draft16 => { let msg: ietf::PublishNamespaceCancel = body.message(self.version)?; tracing::debug!(broadcast = %absolute, message = ?msg, "update cancelled"); - self.withdraw_namespace(&mut ns.target, &mut ns.requests, suffix.clone()) - .await?; + // The peer already let it go, and draft 16 sends no + // PUBLISH_NAMESPACE_DONE after a cancel. + ns.requests.remove(&suffix); Refused::No } _ => return Err(Error::UnexpectedMessage), @@ -13914,6 +13915,21 @@ mod tests { } /// The bytes of one REQUEST_UPDATE, framed as the publisher writes it. + /// PUBLISH_NAMESPACE_DONE withdrawing the first request, "cam". + async fn publish_namespace_done(version: Version) -> Vec { + let log = crate::lite::test_transport::Log::default(); + let mut writer = crate::coding::Writer::new(crate::lite::test_transport::SinkSend::new(log.clone()), version); + writer.varint(ietf::PublishNamespaceDone::ID).await.unwrap(); + writer + .encode(&ietf::PublishNamespaceDone { + track_namespace: crate::Path::new("cam"), + request_id: RequestId(1), + }) + .await + .unwrap(); + log.writes.lock().unwrap().clone() + } + async fn request_update(version: Version, msg: &ietf::PublishNamespaceUpdate) -> Vec { let log = crate::lite::test_transport::Log::default(); let mut writer = crate::coding::Writer::new(crate::lite::test_transport::SinkSend::new(log.clone()), version); @@ -14328,6 +14344,11 @@ mod tests { assert_eq!(occurrences(&log, b"cam"), 2, "the namespace was not offered again"); assert_eq!(log.bi_opens(), 2, "a fresh request after the cancel"); + assert_eq!( + occurrences(&log, &publish_namespace_done(VERSION).await), + 0, + "PUBLISH_NAMESPACE_DONE after the peer's cancel" + ); } /// An auth another handle issued is not this session's to present: the route is @@ -14552,20 +14573,7 @@ mod tests { "{version}: the unanswered renewal was never retried" ); if version == Version::Draft16 { - let done = { - let log = crate::lite::test_transport::Log::default(); - let mut writer = - crate::coding::Writer::new(crate::lite::test_transport::SinkSend::new(log.clone()), version); - writer.varint(ietf::PublishNamespaceDone::ID).await.unwrap(); - writer - .encode(&ietf::PublishNamespaceDone { - track_namespace: crate::Path::new("cam"), - request_id: RequestId(1), - }) - .await - .unwrap(); - log.writes.lock().unwrap().clone() - }; + let done = publish_namespace_done(version).await; assert_eq!(occurrences(&log, &done), 1, "the peer was never told to let it go"); } } From 853f71424a03795e35d4b241474acebe729d6458 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 20 Oct 2026 00:36:00 -0700 Subject: [PATCH 4/4] fix(net): send no PUBLISH_NAMESPACE_DONE after a refused draft-16 update A draft-16 receiver refuses an update with REQUEST_ERROR and then PUBLISH_NAMESPACE_CANCEL, and draft 16 sends no DONE after a cancel. Drop the request locally on a refusal, the same as on a cancel that beat the answer, and re-offer it once the refusal's wait is out, in Rust and JS. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/publisher.test.ts | 54 +++++++++++++++++ js/net/src/ietf/publisher.ts | 35 ++++++----- rs/moq-net/src/ietf/publisher.rs | 99 ++++++++++++++++++++++++------- 3 files changed, 153 insertions(+), 35 deletions(-) diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index f68947350e4..797e3c5d259 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -1844,6 +1844,60 @@ test("a draft 16 cancel ahead of the answer offers the namespace again without D } }); +/** + * A draft-16 receiver refuses an update with REQUEST_ERROR and then PUBLISH_NAMESPACE_CANCEL. + * Draft 16 sends no PUBLISH_NAMESPACE_DONE after a cancel, so the next message is the + * namespace offered again once the refusal's wait is out. + */ +test("a refused draft 16 update offers the namespace again without DONE", async () => { + jest.useFakeTimers(); + const version = Version.DRAFT_16; + const pair = createMockTransportPair(ALPNS[version]); + const control = await Stream.open(pair.server, { version }); + const adapter = new ControlStreamAdapter(pair.server, control, version, 100n, false); + void adapter.run().catch(() => undefined); + const peer = await Stream.accept(pair.client, version); + if (!peer) throw new Error("no control stream"); + const tokens = new Tokens(); + const { pub, origin } = publisher(pair.server, { session: adapter, auth: tokens }); + const warn = spyOn(console, "warn").mockImplementation(() => void 0); + try { + const broadcast = origin.createBroadcast(Path.from("mine")); + broadcast.announce({ auth: tokens.issue("first") }); + void pub.runPublishNamespaces(); + + expect(await peer.reader.u53()).toBe(PublishNamespace.id); + const advertised = await PublishNamespace.decode(peer.reader, version); + await peer.writer.u53(RequestOk.id); + await new RequestOk({ requestId: advertised.requestId }).encode(peer.writer, version); + + broadcast.announce({ auth: tokens.issue("second") }); + expect(await peer.reader.u53()).toBe(PublishNamespaceUpdate.id); + const update = await PublishNamespaceUpdate.decode(peer.reader, version); + await peer.writer.u53(RequestError.id); + await new RequestError({ + requestId: update.requestId, + errorCode: 0x1, + reasonPhrase: "no", + retryInterval: 1n, + }).encode(peer.writer, version); + await peer.writer.u53(PublishNamespaceCancel.id); + await new PublishNamespaceCancel({ requestId: advertised.requestId, reasonPhrase: "no" }).encode( + peer.writer, + version, + ); + + const next = watch(peer.reader.u53()); + await advanceUntil(() => next.state !== "pending", 30_000); + expect(next.value).toBe(PublishNamespace.id); + } finally { + warn.mockRestore(); + origin.close(); + adapter.close(); + jest.useRealTimers(); + } +}); + /** A token another session issued is never carried, so swapping between two sends nothing. */ test("a foreign route token is not carried", async () => { const foreign = new Tokens(); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 24f4343fcfa..7820ef228d2 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -1805,9 +1805,9 @@ export class Publisher { * MAX_REQUEST_UPDATES the peer set without reading it; a change landing meanwhile waits * for the next turn. The answer decides whether the peer still holds the namespace: a * REQUEST_ERROR closes the peer's side and withdraws the advertisement, so ours is - * finished too, a draft-16 cancel ends it with nothing more sent, and an unanswered update - * is dropped, since a peer that ignored it cannot be assumed to hold either price. Either - * way the retry re-offers it fresh. + * finished too (with nothing more sent on draft 16, where the peer cancels it), a draft-16 + * cancel ends it the same way, and an unanswered update is dropped, since a peer that + * ignored it cannot be assumed to hold either price. Either way the retry re-offers it fresh. * * A different original publisher is an update like any other: the receiver drains what * it already serves from the old one and never splices the two. @@ -1838,13 +1838,8 @@ export class Publisher { ); // Draft 16's adapter ends the stream on the peer's PUBLISH_NAMESPACE_CANCEL, which - // can beat the answer. The peer already let the namespace go, and draft 16 sends - // no PUBLISH_NAMESPACE_DONE after a cancel. - if (version === Version.DRAFT_16 && (await reader.done())) { - requests.delete(path); - request.stream.abort(new Error("cancelled by the peer")); - return "dropped"; - } + // can beat the answer. + if (version === Version.DRAFT_16 && (await reader.done())) return "dropped"; const typeId = await reader.u53(); if (typeId === RequestError.id) { @@ -1873,11 +1868,21 @@ export class Publisher { return "dropped"; } - if (answer === "held" && auth) request.auth = auth; - // Before draft-17 the peer already closed out a refused request, and its STOP_SENDING - // can reject the wait on our FIN. That is the withdrawal finishing, not a reason to end - // the loop. - if (answer !== "held") await this.#withdraw(path, requests).catch(() => undefined); + if (answer === "held") { + if (auth) request.auth = auth; + return answer; + } + if (version === Version.DRAFT_16) { + // A draft-16 peer lets the namespace go with PUBLISH_NAMESPACE_CANCEL, ahead of its + // answer or right after a refusal, and no PUBLISH_NAMESPACE_DONE may follow a cancel. + requests.delete(path); + request.stream.abort(new Error("released by the peer")); + } else { + // The peer already closed out a refused request, and its STOP_SENDING can + // reject the wait on our FIN. That is the withdrawal finishing, not a reason to end + // the loop. + await this.#withdraw(path, requests).catch(() => undefined); + } return answer; } diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 074c74c7e69..f09450f11b4 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -3480,9 +3480,18 @@ where ietf::RequestError::ID => { let msg: ietf::RequestError = body.message(self.version)?; tracing::warn!(message = ?msg, "publish_namespace update error"); - // The peer closed its side; closing ours completes the withdrawal. - self.withdraw_namespace(&mut ns.target, &mut ns.requests, suffix.clone()) - .await?; + match self.version { + // The peer follows its refusal with PUBLISH_NAMESPACE_CANCEL, and draft + // 16 sends no PUBLISH_NAMESPACE_DONE after a cancel. + Version::Draft16 => { + ns.requests.remove(&suffix); + } + // The peer closed its side; closing ours completes the withdrawal. + _ => { + self.withdraw_namespace(&mut ns.target, &mut ns.requests, suffix.clone()) + .await? + } + } self.refusal(msg.retry_interval) } // Draft-16 carries its cancel as a message, which can beat the answer. @@ -13914,7 +13923,6 @@ mod tests { slot } - /// The bytes of one REQUEST_UPDATE, framed as the publisher writes it. /// PUBLISH_NAMESPACE_DONE withdrawing the first request, "cam". async fn publish_namespace_done(version: Version) -> Vec { let log = crate::lite::test_transport::Log::default(); @@ -13930,6 +13938,24 @@ mod tests { log.writes.lock().unwrap().clone() } + /// PUBLISH_NAMESPACE_CANCEL letting go of the first request, "cam". + async fn publish_namespace_cancel(version: Version) -> Vec { + let log = crate::lite::test_transport::Log::default(); + let mut writer = crate::coding::Writer::new(crate::lite::test_transport::SinkSend::new(log.clone()), version); + writer.varint(ietf::PublishNamespaceCancel::ID).await.unwrap(); + writer + .encode(&ietf::PublishNamespaceCancel { + track_namespace: crate::Path::new("cam"), + request_id: RequestId(1), + error_code: 0, + reason_phrase: "gone".into(), + }) + .await + .unwrap(); + log.writes.lock().unwrap().clone() + } + + /// The bytes of one REQUEST_UPDATE, framed as the publisher writes it. async fn request_update(version: Version, msg: &ietf::PublishNamespaceUpdate) -> Vec { let log = crate::lite::test_transport::Log::default(); let mut writer = crate::coding::Writer::new(crate::lite::test_transport::SinkSend::new(log.clone()), version); @@ -14319,22 +14345,7 @@ mod tests { settle().await; let ok = publish_namespace_ok(VERSION).await; - let cancel = { - let log = crate::lite::test_transport::Log::default(); - let mut writer = - crate::coding::Writer::new(crate::lite::test_transport::SinkSend::new(log.clone()), VERSION); - writer.varint(ietf::PublishNamespaceCancel::ID).await.unwrap(); - writer - .encode(&ietf::PublishNamespaceCancel { - track_namespace: crate::Path::new("cam"), - request_id: RequestId(1), - error_code: 0, - reason_phrase: "gone".into(), - }) - .await - .unwrap(); - log.writes.lock().unwrap().clone() - }; + let cancel = publish_namespace_cancel(VERSION).await; let (publisher, log) = auth_publisher(&origin, &auth, vec![[ok.clone(), cancel].concat(), ok], VERSION); let mut run = std::pin::pin!(publisher.run_publish_namespaces()); poll_until(&mut run, &log, b"first-secret", 1).await; @@ -14351,6 +14362,54 @@ mod tests { ); } + /// A draft-16 peer refuses an update with REQUEST_ERROR and then PUBLISH_NAMESPACE_CANCEL. + /// Draft 16 sends no PUBLISH_NAMESPACE_DONE after a cancel, so the namespace is only + /// offered again once the refusal's wait is out. + #[moq_clock::test] + async fn a_refused_draft_16_update_sends_no_done() { + const VERSION: Version = Version::Draft16; + + let origin = unheld_origin(); + let auth = crate::auth::Handle::ietf(false); + let first = auth.present(bytes::Bytes::from_static(b"first-secret"), false).unwrap(); + let second = auth + .present(bytes::Bytes::from_static(b"second-secret"), false) + .unwrap(); + let cam = origin + .announce("cam", crate::origin::Route::default().with_auth(first)) + .unwrap(); + settle().await; + + let ok = publish_namespace_ok(VERSION).await; + let refusal = publish_namespace_error(VERSION, 50).await; + let cancel = publish_namespace_cancel(VERSION).await; + let (publisher, log) = auth_publisher( + &origin, + &auth, + vec![[ok.clone(), refusal, cancel].concat(), ok], + VERSION, + ); + let mut run = std::pin::pin!(publisher.run_publish_namespaces()); + poll_until(&mut run, &log, b"first-secret", 1).await; + + cam.update(crate::origin::Route::default().with_auth(second)).unwrap(); + for _ in 0..100 { + assert!(futures::poll!(run.as_mut()).is_pending()); + if occurrences(&log, b"cam") >= 2 { + break; + } + tick().await; + } + + assert_eq!(occurrences(&log, b"cam"), 2, "the namespace was not offered again"); + assert_eq!(log.bi_opens(), 2, "a fresh request after the refusal"); + assert_eq!( + occurrences(&log, &publish_namespace_done(VERSION).await), + 0, + "PUBLISH_NAMESPACE_DONE after the peer's refusal" + ); + } + /// An auth another handle issued is not this session's to present: the route is /// advertised under the session's scope, and the credential never reaches this peer. #[moq_clock::test]