diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index a2276a34b68..af956ab6851 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -101,6 +101,10 @@ jobs: if: ${{ !cancelled() }} run: nix develop --command just js bench broadcasts + - name: JS request ID admission benchmark + if: ${{ !cancelled() }} + run: nix develop --command just js bench request-ids + # A route above several interest heads lands once per head, so its re-price # should expose any slope over heads times received routes. - name: JS origin announce forwarding benchmark diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 709650de844..e8a73f33247 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -284,6 +284,8 @@ Sent when terminating the session, via the transport's session close. | ------- | ------------- | ----------- | | 0x3 | PROTOCOL_VIOLATION | The peer violated this specification. | | ------- | ------------- | ----------- | +| 0x4 | INVALID_REQUEST_ID | A moq-transport request ID was reused or violated that draft’s order or parity rules; moq-lite has no request IDs. | +| ------- | ------------- | ----------- | | 0x6 | KEY_VALUE_FORMATTING_ERROR | A key-value pair was malformed, or repeated more than allowed. | | ------- | ------------- | ----------- | | 0x7 | TOO_MANY_REQUESTS | The peer held more announcements or subscriptions than the endpoint allows. | @@ -1526,6 +1528,8 @@ The `Message Length` describes the payload size on the wire. ## moq-lite-07 +- Listed the shared INVALID_REQUEST_ID (0x4) session code used by moq-transport drafts 14 through 16. + - SUBSCRIBE_OK `Group` names where delivery starts when the subscription resolves, which need not be the first group sent. It is not a new floor. A subscriber does not wait for a group between the requested floor and it, but a publisher still delivers one that arrives later within Subscriber Max Age. - Corrected the SUBSCRIBE note that offset `Group Start` by 1. Only `Group End` and `Frame End` are offset so 0 can mean absent. `Group Start` is an absolute sequence, and 0 is group 0. - A subscription's range bounds datagrams like groups, and FETCH never returns a datagram. diff --git a/js/justfile b/js/justfile index f17ee5aea7f..0aabf84bdc1 100644 --- a/js/justfile +++ b/js/justfile @@ -48,7 +48,7 @@ test: bun test common/declarations.test.ts common/deps.test.ts common/worklet.test.ts bun run --filter='*' --elide-lines=0 test -# Run one net benchmark: broadcasts, forward, reader, frames, track, varint, or lite-varint. +# Run one net benchmark: broadcasts, forward, reader, frames, track, varint, lite-varint, or request-ids. bench name: bun install --frozen-lockfile bun net/bench/{{ name }}.ts diff --git a/js/net/bench/request-ids.ts b/js/net/bench/request-ids.ts new file mode 100644 index 00000000000..27291fe4220 --- /dev/null +++ b/js/net/bench/request-ids.ts @@ -0,0 +1,80 @@ +// Request-ID admission cost across independent sessions and reordered windows. +import { ControlStreamAdapter } from "../src/ietf/adapter.ts"; +import { Version } from "../src/ietf/version.ts"; +import { Stream } from "../src/stream.ts"; + +/** + * Every ID in the window: in order, reversed (one skip to the top, then each fill + * shortens it), or scattered (the top first, then every other ID descending, so each + * splits the lowest skipped run and the remembered runs grow to half the window). + */ +function order(mode: string, window: number): bigint[] { + const all = Array.from({ length: window }, (_, id) => BigInt(id * 2)); + if (mode === "ordered") return all; + if (mode === "reordered") return all.reverse(); + const top = all[window - 1]; + const odd = all.filter((id, index) => index % 2 === 1 && id !== top).reverse(); + const even = all.filter((id, index) => index % 2 === 0 && id !== top).reverse(); + return [top, ...odd, ...even]; +} + +const debug = console.debug; +console.debug = () => undefined; +try { + console.log("mode,sessions,window,admit_us,accepted"); + // Up to the production window, REQUEST_WINDOW. + for (const mode of ["ordered", "reordered", "scattered"]) { + for (const sessions of [1, 8, 32]) { + for (const window of [8, 128, 220_000]) { + const ids = order(mode, window); + // Fewer rounds for the large window, keeping each sweep to a similar budget. + const rounds = Math.max(1, Math.floor(8192 / window)); + let elapsed = 0; + let accepted = 0; + for (let round = 0; round < rounds; round++) { + const fixtures = Array.from({ length: sessions }, () => { + const stream = new Stream({ + readable: new ReadableStream(), + writable: new WritableStream(), + version: Version.DRAFT_16, + }); + const quic = { close() {} } as unknown as WebTransport; + return { + stream, + adapter: new ControlStreamAdapter( + quic, + stream, + Version.DRAFT_16, + 0n, + false, + BigInt(window), + ), + }; + }); + const start = performance.now(); + for (const { adapter } of fixtures) { + for (const id of ids) { + try { + adapter.admitRequest(id); + accepted++; + } catch { + /* Count refusals in baseline comparisons. */ + } + } + } + elapsed += performance.now() - start; + for (const { stream, adapter } of fixtures) { + adapter.close(); + stream.close(); + } + await Promise.resolve(); + } + console.log( + `${mode},${sessions},${window},${((elapsed * 1000) / rounds).toFixed(2)},${accepted / rounds}`, + ); + } + } + } +} finally { + console.debug = debug; +} diff --git a/js/net/src/error.test.ts b/js/net/src/error.test.ts index c6d5756c2af..79f38d012d1 100644 --- a/js/net/src/error.test.ts +++ b/js/net/src/error.test.ts @@ -93,6 +93,11 @@ test("the error type names the registry the code came from", () => { expect(Number(stream.code)).toBe(Number(session?.code)); }); +test("invalid request IDs keep their session registry code", () => { + expect(Number(SessionCode.InvalidRequestId)).toBe(0x4); + expect(fromClose({ closeCode: 0x4 })?.code).toBe(SessionCode.InvalidRequestId); +}); + test("the code types reject the other registry", () => { new StreamError(StreamCode.DeliveryTimeout); new SessionError(SessionCode.Unauthorized); diff --git a/js/net/src/error.ts b/js/net/src/error.ts index c72b5236dfb..55bdde66258 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -36,6 +36,8 @@ export const SessionCode = Object.freeze( Unauthorized: 0x2 as SessionCode, /** A protocol rule was broken; the session is unusable. */ ProtocolViolation: 0x3 as SessionCode, + /** A request ID was reused or violated the negotiated draft’s order or parity rules. */ + InvalidRequestId: 0x4 as SessionCode, /** A key-value pair was malformed or repeated more than allowed. */ KeyValueFormatting: 0x6 as SessionCode, /** The peer went past what the session allows: a request ID past MAX_REQUEST_ID, or too many announcements or subscriptions. */ diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index 6d55936b4d6..1421167722d 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -9,6 +9,7 @@ import { GoAway } from "./goaway.ts"; import { PublishNamespace, PublishNamespaceCancel, PublishNamespaceDone } from "./publish_namespace.ts"; import { initialMaxRequestId, MaxRequestId, RequestError } from "./request.ts"; import { Subscribe, SubscribeUpdate } from "./subscribe.ts"; +import { SubscribeNamespaceLegacy } from "./subscribe_namespace.ts"; import { TrackStatusRequest } from "./track.ts"; import { ALPN, type IetfVersion, Version } from "./version.ts"; @@ -391,10 +392,10 @@ test("a request id with the wrong parity closes the session", async () => { test("a second use of an open request id closes the session", async () => { for (const [version, alpn] of WINDOW_DRAFTS) { - const { pair, peer, running } = await windowed(version, alpn, 2n, false); + const { pair, peer, running } = await windowed(version, alpn, 1n, false); await trackStatus(peer, version, 0n); await trackStatus(peer, version, 0n); - await expect(running).rejects.toThrow("duplicate request id"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -434,9 +435,9 @@ test("drafts 14 to 16 route an update to its second request id", async () => { for (const [version, alpn] of WINDOW_DRAFTS) { const { adapter, peer, running } = await windowed(version, alpn, 4n, false); const state = settled(running); - const target = await subscribe(adapter, peer, version, 2n); - await update(peer, version, 6n, 2n); - expect(await updated(target, version)).toBe(2n); + const target = await subscribe(adapter, peer, version, 0n); + await update(peer, version, 2n, 0n); + expect(await updated(target, version)).toBe(0n); expect(await state()).toBeUndefined(); } }); @@ -488,24 +489,45 @@ test("an update spending an open request id closes the session", async () => { await trackStatus(peer, version, heldId); if (!(await adapter.acceptBi())) throw new Error("no stream"); await update(peer, version, heldId, heldId); - await expect(running).rejects.toThrow("duplicate request id"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } }); -/** Repeated updates raise the maximum, but the peer still cannot hold more than the window open. */ -test("reused update ids cannot hold more than the window", async () => { +/** Request IDs stay spent after their stream or update retires. */ +test("retired and lower request ids close the session", async () => { + for (const [version, alpn] of WINDOW_DRAFTS) { + for (const client of [false, true]) { + const parity = client ? 1n : 0n; + for (const [first, later] of [ + [parity, parity], + [parity + 4n, parity + 2n], + ] as const) { + const { pair, adapter, peer, running } = await windowed(version, alpn, 8n, client); + for (let id = parity; id <= first; id += 2n) { + await trackStatus(peer, version, id); + const stream = await adapter.acceptBi(); + if (!stream) throw new Error("no stream"); + stream.close(); + } + await trackStatus(peer, version, later); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); + expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); + } + } + } +}); + +test("an update id stays spent after the update retires", async () => { for (const [version, alpn] of WINDOW_DRAFTS) { - const { pair, adapter, peer, running } = await windowed(version, alpn, 2n, false); + const { pair, adapter, peer, running } = await windowed(version, alpn, 4n, false); await trackStatus(peer, version, 0n); if (!(await adapter.acceptBi())) throw new Error("no stream"); - for (let i = 0; i < 8; i++) await update(peer, version, 2n, 0n); - await trackStatus(peer, version, 4n); - if (!(await adapter.acceptBi())) throw new Error("no stream"); - await trackStatus(peer, version, 6n); - await expect(running).rejects.toThrow("too many open requests"); - expect((await pair.client.closed).closeCode).toBe(SessionCode.TooManyRequests); + await update(peer, version, 2n, 0n); + await update(peer, version, 2n, 0n); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); + expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -543,6 +565,128 @@ test("more requests than the setup window complete on one session", async () => } }); +// Both requests and updates allocate IDs, even when an update's target is gone. +test("skipped request and update ids close the session", async () => { + for (const [version, alpn] of WINDOW_DRAFTS.filter(([version]) => version !== Version.DRAFT_16)) { + for (const client of [false, true]) { + for (const prefix of [0n, 1n]) { + for (const isUpdate of [false, true]) { + const { pair, peer, running } = await windowed(version, alpn, 8n, client); + const first = client ? 1n : 0n; + if (prefix) await trackStatus(peer, version, first); + const skipped = first + 2n * prefix + 2n; + if (isUpdate) await update(peer, version, skipped, 100n); + else await trackStatus(peer, version, skipped); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); + expect((await pair.client.closed).closeCode).toBe(SessionCode.InvalidRequestId); + } + } + } + } +}); + +// SUBSCRIBE_NAMESPACE uses an independent bidi stream in draft 16, sharing the ID space. +test("draft 16 accepts reordered native and control IDs but never reuses them", async () => { + for (const client of [false, true]) { + for (const nativeFirst of [false, true]) { + const version = Version.DRAFT_16; + const { pair, adapter, peer, running } = await windowed(version, ALPN.DRAFT_16, 8n, client); + void running.catch(() => undefined); + const first = client ? 1n : 0n; + const native = nativeFirst ? first + 2n : first; + const control = nativeFirst ? first : first + 2n; + try { + if (nativeFirst) adapter.admitRequest(native); + await trackStatus(peer, version, control); + const stream = await adapter.acceptBi(); + if (!stream) throw new Error("no reordered control request"); + if (!nativeFirst) adapter.admitRequest(native); + stream.close(); + adapter.releaseRequest(native); + await trackStatus(peer, version, first); + await expect(running).rejects.toThrow("request id was already spent"); + expect((await pair.client.closed).closeCode).toBe(SessionCode.InvalidRequestId); + } finally { + peer.close(); + adapter.close(); + } + } + } +}); + +// Draft 16 remembers a skipped stretch as one run, so a jump costs nothing per skipped ID. +test("draft 16 tracks a jump across a production window as one run", async () => { + const window = 220_000n; + const { adapter, peer } = await windowed(Version.DRAFT_16, ALPN.DRAFT_16, window, false); + const top = 2n * (window - 1n); + try { + for (const id of [top, 100n, 0n, top - 2n, 2n, 98n]) adapter.admitRequest(id); + for (const id of [0n, 2n, 98n, 100n, top - 2n, top]) { + expect(() => adapter.admitRequest(id)).toThrow("request id was already spent"); + } + } finally { + peer.close(); + adapter.close(); + } +}); + +// Splitting runs in descending order spans many chunks; every ID is admitted exactly once. +test("draft 16 admits a scattered window once across run chunks", async () => { + const window = 4096n; + const { adapter, peer } = await windowed(Version.DRAFT_16, ALPN.DRAFT_16, window, false); + const top = 2n * (window - 1n); + const below = Array.from({ length: Number(window - 1n) }, (_, i) => 2n * (window - 2n - BigInt(i))); + const ids = [top, ...below.filter((id) => (id / 2n) % 2n === 1n), ...below.filter((id) => (id / 2n) % 2n === 0n)]; + try { + for (const id of ids) adapter.admitRequest(id); + for (const id of [0n, 2n, 1024n, top - 2n, top]) { + expect(() => adapter.admitRequest(id)).toThrow("request id was already spent"); + } + } finally { + peer.close(); + adapter.close(); + } +}); + +// Allocation precedes an asynchronous encode, so a shorter later request can finish first. +test("drafts 14 and 15 send concurrent requests in request id order", async () => { + for (const [version, alpn] of [ + [Version.DRAFT_14, ALPN.DRAFT_14], + [Version.DRAFT_15, ALPN.DRAFT_15], + ] as const) { + const pair = createMockTransportPair(alpn); + const control = await Stream.open(pair.server, { version }); + const sender = new ControlStreamAdapter(pair.server, control, version, 100n, true); + void sender.run().catch(() => undefined); + const peer = await Stream.accept(pair.client, version); + if (!peer) throw new Error("no control stream"); + const receiver = new ControlStreamAdapter(pair.client, peer, version, 100n, false, 8n); + void receiver.run().catch(() => undefined); + try { + const request = async (namespace: Path.Valid) => { + const requestId = await sender.nextRequestId(); + if (requestId === undefined) throw new Error("no request id"); + const stream = sender.openBi(); + await stream.writer.u53(SubscribeNamespaceLegacy.id); + await new SubscribeNamespaceLegacy({ namespace, requestId }).encode(stream.writer, version); + }; + await Promise.all([request(Path.from("a/b/c/d/e/f/g/h")), request(Path.from("a"))]); + + const received: bigint[] = []; + for (let i = 0; i < 2; i++) { + const stream = await receiver.acceptBi(); + if (!stream) throw new Error("the receiver refused a request"); + expect(await stream.reader.u53()).toBe(SubscribeNamespaceLegacy.id); + received.push((await SubscribeNamespaceLegacy.decode(stream.reader, version)).requestId); + } + expect(received).toEqual([0n, 2n]); + } finally { + sender.close(); + receiver.close(); + } + } +}); + // Canceling the wait must remove the allocation itself, not just stop awaiting it. test("a canceled request ID wait never spends a later grant", async () => { for (const [version, alpn] of WINDOW_DRAFTS) { diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index ba0b429b45b..5259dd4113a 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -10,9 +10,6 @@ import * as Namespace from "./namespace.ts"; import { initialMaxRequestId, MaxRequestId, REQUEST_WINDOW } from "./request.ts"; import { type IetfVersion, Version } from "./version.ts"; -/** Draft-14 to -16: the request ID is not valid for this peer (wrong parity or a duplicate). */ -const INVALID_REQUEST_ID = 0x4; - /** The peer used a request ID the draft closes the session for. */ export class RequestWindowError extends Error { readonly code: number; @@ -95,6 +92,68 @@ interface StreamEntry { type NamespaceDirection = "incoming" | "outgoing"; +/** Runs per chunk before it splits, so a split or removal shifts at most one chunk. */ +const RUN_CHUNK = 256; + +/** + * Skipped request IDs as sorted, disjoint `[start, end)` runs of one parity. + * + * A jump costs one run however far it skips, and only an ID landing inside a run splits it, + * so the runs never outnumber the requests received. They are kept in chunks so a split + * costs the chunk it lands in rather than shifting the whole table. + */ +class MissingRuns { + #chunks: [bigint, bigint][][] = []; + + /** Whether `id` was skipped and not yet received. */ + has(id: bigint): boolean { + return this.#find(id) !== undefined; + } + + /** Mark a skipped `id` received, splitting its run. */ + take(id: bigint): void { + const found = this.#find(id); + if (!found) return; + const [index, run] = found; + const chunk = this.#chunks[index]; + const [start, end] = chunk[run]; + const rest: [bigint, bigint][] = []; + if (start < id) rest.push([start, id]); + if (id + 2n < end) rest.push([id + 2n, end]); + chunk.splice(run, 1, ...rest); + if (chunk.length === 0) this.#chunks.splice(index, 1); + else if (chunk.length > 2 * RUN_CHUNK) this.#chunks.splice(index + 1, 0, chunk.splice(RUN_CHUNK)); + } + + /** Record `[start, end)` as skipped, above every existing run. */ + push(start: bigint, end: bigint): void { + const last = this.#chunks.at(-1); + if (last && last.length < 2 * RUN_CHUNK) last.push([start, end]); + else this.#chunks.push([[start, end]]); + } + + /** The chunk and run holding `id`. */ + #find(id: bigint): [number, number] | undefined { + const index = lastAtOrBelow(this.#chunks, id, (chunk) => chunk[0][0]); + if (index < 0) return undefined; + const chunk = this.#chunks[index]; + const run = lastAtOrBelow(chunk, id, (run) => run[0]); + return run >= 0 && id < chunk[run][1] ? [index, run] : undefined; + } +} + +/** The last index whose key is at or below `id`, or -1, in an array sorted by key. */ +function lastAtOrBelow(items: T[], id: bigint, key: (item: T) => bigint): number { + let lo = 0; + let hi = items.length; + while (lo < hi) { + const mid = (lo + hi) >>> 1; + if (key(items[mid]) <= id) lo = mid + 1; + else hi = mid; + } + return lo - 1; +} + /** * Converts v14-v16 control stream multiplexing into virtual bidi streams. * @@ -146,11 +205,21 @@ export class ControlStreamAdapter implements Session { #maxRequestId: bigint; #maxRequestIdResolves = new Set<() => void>(); + // Drafts 14/15 require our new request IDs in order on the control stream. Callers + // allocate an ID and then encode asynchronously, so a shorter later request can finish + // first: a message spending a new ID waits here until every lower one has gone out. + // Draft 16 sends SUBSCRIBE_NAMESPACE on its own stream and the peer accepts any order. + #sendNext: bigint | undefined; + #sendTurns = new Map void>(); + // The peer's requests, flow controlled as moq-net does: at most `#peerWindow` open at // once, every ID below `#peerMax`, and once half the window has closed the bound rises // by the IDs they spent and goes out as one MAX_REQUEST_ID. #peerWindow: bigint; #peerMax: bigint; + #peerNext: bigint; + // Draft 16 reorders across streams, so it remembers the IDs skipped below #peerNext. + #peerMissing: MissingRuns | undefined; #open = new Set(); #retired = 0n; #granting = false; @@ -181,7 +250,10 @@ export class ControlStreamAdapter implements Session { this.#maxRequestId = maxRequestId; this.#peerWindow = window; this.#peerMax = initialMaxRequestId(!client, window); + this.#peerNext = client ? 1n : 0n; + this.#peerMissing = version === Version.DRAFT_16 ? new MissingRuns() : undefined; this.#requestId = client ? 0n : 1n; + this.#sendNext = version === Version.DRAFT_16 ? undefined : this.#requestId; this.#client = client; } @@ -241,6 +313,7 @@ export class ControlStreamAdapter implements Session { const toFlush = buffer.subarray(0, boundary); buffer = buffer.subarray(boundary); + let spent: bigint | undefined; if (!registered) { // First message: extract requestId and register before flushing const parsed = await this.#tryParseOutgoing(toFlush); @@ -248,10 +321,13 @@ export class ControlStreamAdapter implements Session { registeredRequestId = parsed.requestId; this.#streams.set(parsed.requestId, { controller }); registered = true; + spent = parsed.requestId; } + } else { + spent = this.#updateId(toFlush); } - await this.#writeMutex.runExclusive(() => this.#writer.write(toFlush)); + await this.#send(toFlush, spent); } }, }); @@ -490,16 +566,23 @@ export class ControlStreamAdapter implements Session { // Even IDs are the client's. `#client` is this side, so the peer's IDs are the other parity. const even = (requestId & 1n) === 0n; if (even === this.#client) { - this.#refuse(INVALID_REQUEST_ID, "request id has the wrong parity"); + this.#refuse(SessionCode.InvalidRequestId, "request id has the wrong parity"); } } #admitHeld(requestId: bigint): void { this.#checkPeer(requestId); - if (this.#open.has(requestId)) { - this.#refuse(INVALID_REQUEST_ID, "duplicate request id"); + const unused = + requestId < this.#peerNext + ? this.#peerMissing?.has(requestId) === true + : requestId === this.#peerNext || this.#peerMissing !== undefined; + if (!unused) { + this.#refuse(SessionCode.InvalidRequestId, "request id was already spent or out of sequence"); } this.#checkLive(); + if (requestId < this.#peerNext) this.#peerMissing?.take(requestId); + else if (requestId > this.#peerNext) this.#peerMissing?.push(this.#peerNext, requestId); + if (requestId >= this.#peerNext) this.#peerNext = requestId + 2n; this.#open.add(requestId); } @@ -513,7 +596,7 @@ export class ControlStreamAdapter implements Session { this.#retire(); } - /** A peer reusing IDs below the bound still cannot hold more than the window open. */ + /** Enforce the live-request cap independently of the ID bound. */ #checkLive(): void { if (BigInt(this.#open.size) >= this.#peerWindow) { this.#refuse(SessionCode.TooManyRequests, "too many open requests"); @@ -676,12 +759,44 @@ export class ControlStreamAdapter implements Session { const toFlush = buffer.subarray(0, boundary); buffer = buffer.subarray(boundary); - await this.#writeMutex.runExclusive(() => this.#writer.write(toFlush)); + await this.#send(toFlush, this.#updateId(toFlush)); } }, }); } + /** The update's own request ID if this complete message is SUBSCRIBE_UPDATE or REQUEST_UPDATE. */ + #updateId(message: Uint8Array): bigint | undefined { + const [typeId, afterType] = Varint.decode(message); + if (typeId !== 0x02) return undefined; + return Varint.decodeBigInt(afterType.subarray(2))[0]; + } + + /** Write one complete message, after every lower request ID when it spends a new one. */ + async #send(message: Uint8Array, requestId: bigint | undefined): Promise { + if (requestId === undefined || this.#sendNext === undefined) { + await this.#writeMutex.runExclusive(() => this.#writer.write(message)); + return; + } + if (requestId < this.#sendNext || this.#sendTurns.has(requestId)) { + throw new Error(`request id ${requestId} was already sent`); + } + if (requestId !== this.#sendNext) { + const { promise, resolve } = Promise.withResolvers(); + this.#sendTurns.set(requestId, resolve); + await promise; + if (this.#closed) throw new Error("control stream closed"); + } + try { + await this.#writeMutex.runExclusive(() => this.#writer.write(message)); + } finally { + this.#sendNext = requestId + 2n; + const next = this.#sendTurns.get(this.#sendNext); + this.#sendTurns.delete(this.#sendNext); + next?.(); + } + } + /** Encode raw message bytes: [typeId varint][size u16 BE][body] */ #encodeRaw(typeId: number, size: number, body: Uint8Array): Uint8Array { // v14-v16 always use QUIC varint @@ -932,5 +1047,8 @@ export class ControlStreamAdapter implements Session { // Unblock maxRequestId waiters for (const resolve of this.#maxRequestIdResolves) resolve(); this.#maxRequestIdResolves.clear(); + + for (const resolve of this.#sendTurns.values()) resolve(); + this.#sendTurns.clear(); } } diff --git a/quest/m1/README.md b/quest/m1/README.md index de4f880cf97..dec317962a7 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -192,7 +192,6 @@ session outcomes, and embedder refusals inside session outcomes. - [Datagram replay bound](/quest/m1/datagram-replay-bound.md) - a new Rust datagram subscriber starts within its max delay of the newest datagram, not at a minutes-old buffer - [Catalog colour](/quest/m1/color-catalog.md) - the catalog describes a rendition's colour and HDR properties, which the WebGPU HDR renderer reads - [WebGPU HDR](/quest/m1/webgpu-hdr.md) - HDR renditions play as HDR where the browser and display can show it, and tone-map to SDR elsewhere -- [Request ID order](/quest/m1/request-id-order.md) - drafts 14 to 16 refuse a reused or lower Request ID in both languages - [WebGPU on Safari](/quest/m1/webgpu-safari.md) - the WebGPU renderer is verified on Safari 26 for macOS and iOS - [A/V sync across a break](/quest/m1/watch-break-av-sync.md) - `@moq/watch` never plays pre-break audio out of sync with pre-break video - [moqsrc reconnect](/quest/m1/moqsrc-reconnect.md) - `moqsrc` redials after losing its relay and resumes on the same pads diff --git a/quest/m1/request-id-order.md b/quest/m1/request-id-order.md deleted file mode 100644 index 086701934dd..00000000000 --- a/quest/m1/request-id-order.md +++ /dev/null @@ -1,25 +0,0 @@ -# [S] A reused moq-transport Request ID is refused - -## Goal - -On drafts 14 to 16, `@moq/net` and `moq-net` refuse an incoming request whose -Request ID is not above every ID the peer already used, instead of accepting -any ID below the window's bound. A peer can no longer reuse an ID that named -an earlier request. - -## Plan - -Found while landing [#5011](https://github.com/moq-dev/moq/pull/5011); it -predates it and covers every request type. `js/net/src/ietf/adapter.ts` -deliberately tolerates reuse today ("A peer reusing IDs below the bound still -cannot hold more than the window open"), bounding only how many are open. - -- First confirm what drafts 14 to 16 require of an out-of-order or repeated - Request ID and which session error they name; follow that. -- Keep a high-water mark of the IDs the peer allocates and refuse anything at - or below it, in both languages, beside the existing window checks. -- Tests: a repeated ID and a lower ID are each refused with the drafts' error; - in-order IDs and the MAX_REQUEST_ID grant are unchanged. - -Public API: none. Wire: no format change, but a stricter receiver closes -sessions with peers that reuse IDs today; report it as an interop change. diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index 8d629c23a44..ab49c10f426 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -122,6 +122,11 @@ harness = false name = "session" harness = false +[[bench]] +name = "request-ids" +harness = false +required-features = ["fuzz"] + # Uses the hidden fuzz module to observe source selection without public API. [[bench]] name = "retirement" diff --git a/rs/moq-net/benches/request-ids.rs b/rs/moq-net/benches/request-ids.rs new file mode 100644 index 00000000000..aee593308c5 --- /dev/null +++ b/rs/moq-net/benches/request-ids.rs @@ -0,0 +1,52 @@ +//! Request-ID admission cost across independent sessions and reordered windows. +use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; +use std::{hint::black_box, time::Duration}; + +fn request_ids(c: &mut Criterion) { + let mut group = c.benchmark_group("request-ids"); + group.sample_size(20); + group.warm_up_time(Duration::from_millis(200)); + group.measurement_time(Duration::from_secs(1)); + // Up to the production window, which the session sizes to twice its 110,000 caps. + for mode in ["ordered", "reordered", "scattered"] { + for sessions in [1, 8, 32] { + for window in [8, 128, 220_000] { + let ids = ids(mode, window); + println!( + "admitted,{mode},{sessions},{window},{}", + moq_net::fuzz::request_ids(window, &ids) * sessions + ); + group.bench_function(BenchmarkId::new(mode, format!("{sessions}x{window}")), |b| { + b.iter(|| { + let accepted: usize = (0..sessions) + .map(|_| moq_net::fuzz::request_ids(window, black_box(&ids))) + .sum(); + black_box(accepted) + }); + }); + } + } + } + group.finish(); +} +/// Every ID in the window: in order, reversed (one skip to the top, then each fill +/// shortens it), or scattered (the top first, then every other ID descending, so each +/// splits the lowest skipped run and the remembered runs grow to half the window). +fn ids(mode: &str, window: u64) -> Vec { + let all = (0..window).map(|id| id * 2); + match mode { + "ordered" => all.collect(), + "reordered" => all.rev().collect(), + "scattered" => { + let below = (0..window - 1).rev().map(|id| id * 2); + let mut ids = vec![2 * (window - 1)]; + ids.extend(below.clone().filter(|id| id / 2 % 2 == 1)); + ids.extend(below.filter(|id| id / 2 % 2 == 0)); + ids + } + _ => unreachable!(), + } +} + +criterion_group!(benches, request_ids); +criterion_main!(benches); diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index edc758b9efe..5293cc39949 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -57,6 +57,10 @@ pub enum SessionError { /// A code this version does not recognize, kept verbatim. #[error("unknown code={0}")] Unknown(u32), + + /// A request ID was reused or violated the negotiated draft’s order or parity rules. + #[error("invalid request id")] + InvalidRequestId, } impl SessionError { @@ -67,6 +71,7 @@ impl SessionError { Self::Internal => 0x1, Self::Unauthorized => 0x2, Self::ProtocolViolation => 0x3, + Self::InvalidRequestId => 0x4, Self::KeyValueFormatting => 0x6, Self::TooManyRequests => 0x7, Self::GoawayTimeout => 0x10, @@ -88,6 +93,7 @@ impl SessionError { 0x1 => Self::Internal, 0x2 => Self::Unauthorized, 0x3 => Self::ProtocolViolation, + 0x4 => Self::InvalidRequestId, 0x6 => Self::KeyValueFormatting, 0x7 => Self::TooManyRequests, 0x10 => Self::GoawayTimeout, @@ -655,6 +661,7 @@ mod tests { SessionError::Internal, SessionError::Unauthorized, SessionError::ProtocolViolation, + SessionError::InvalidRequestId, SessionError::KeyValueFormatting, SessionError::TooManyRequests, SessionError::GoawayTimeout, @@ -673,6 +680,7 @@ mod tests { // The moq-transport codes we reuse must keep moq-transport's values. assert_eq!(SessionError::Unauthorized.to_code(), 0x2); + assert_eq!(SessionError::InvalidRequestId.to_code(), 0x4); assert_eq!(SessionError::TooManyRequests.to_code(), 0x7); assert_eq!(SessionError::GoawayTimeout.to_code(), 0x10); assert_eq!(SessionError::Version.to_code(), 0x15); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index ada1fc11215..b76cde858bd 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -1171,6 +1171,11 @@ impl crate::transport::Error for NoError { } } +/// Admit a draft-16 window for the request-ID benchmark, returning the accepted count. +pub fn request_ids(window: u64, ids: &[u64]) -> usize { + ietf::request_ids(window, ids) +} + /// A source-selection watch for the private request-lifetime benchmark. pub struct RequestValidity(crate::model::broadcast::RequestValidity); diff --git a/rs/moq-net/src/ietf/adapter.rs b/rs/moq-net/src/ietf/adapter.rs index 7734c6b83d0..db1eb75de2e 100644 --- a/rs/moq-net/src/ietf/adapter.rs +++ b/rs/moq-net/src/ietf/adapter.rs @@ -1848,7 +1848,7 @@ mod tests { let mut session = ScriptedSession::new(script); let (_, recv) = session.open_bi().await.unwrap(); // We are the server, so the client's request IDs are even. - let control = Control::new(None, false).with_window(window, false); + let control = Control::new(None, false).with_window(window, false, VERSION); let adapter = ControlStreamAdapter::new(session, control, VERSION); let log = Log::default(); let writer = Writer::new(SinkSend::new(log.clone()), VERSION); diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 3ba504d7e2b..b5a85859d91 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -1,6 +1,15 @@ -use std::task::Poll; +use std::{collections::BTreeMap, task::Poll}; -use crate::{Error, SessionError, ietf::RequestId}; +use crate::{ + Error, SessionError, + ietf::{RequestId, Version}, +}; + +/// The skipped `start..end` run holding `id`, if any. +fn missing_run(missing: &BTreeMap, id: u64) -> Option<(u64, u64)> { + let (&start, &end) = missing.range(..=id).next_back()?; + (id < end).then_some((start, end)) +} /// The MAX_REQUEST_ID that first admits `window` requests from a peer. /// @@ -30,6 +39,12 @@ struct Incoming { live: u64, /// Requests retired since the last grant. retired: u64, + /// One past the highest accepted ID, preserving IDs whose requests retired. + next: RequestId, + /// Draft 16 can reorder across streams, so it remembers the IDs skipped below `next` + /// as `start..end` runs. A jump costs one run however far it skips, and only a request + /// landing inside a run splits it, so the runs never outnumber the requests received. + missing: Option>, } #[derive(Clone)] @@ -52,7 +67,7 @@ impl Control { /// /// `client` is our own role, as passed to [`Self::new`]; the window starts at what /// [`initial_max_request_id`] advertised in our SETUP. - pub fn with_window(self, window: u64, client: bool) -> Self { + pub fn with_window(self, window: u64, client: bool, version: Version) -> Self { let max = initial_max_request_id(window, !client); self.state.lock().incoming = Some(Incoming { window, @@ -60,6 +75,8 @@ impl Control { sent: max, live: 0, retired: 0, + next: RequestId(u64::from(client)), + missing: (version == Version::Draft16).then(BTreeMap::new), }); self } @@ -72,17 +89,48 @@ impl Control { /// Admit a request the peer opened, held open until the returned permit drops. /// /// Closes the session with TOO_MANY_REQUESTS when the ID is at or past the - /// MAX_REQUEST_ID we advertised, or the peer reused IDs to hold more than the window. + /// MAX_REQUEST_ID we advertised, or its live requests exceed the window. + /// Drafts 14/15 require the next ID; draft 16 permits cross-stream reordering. + /// Used IDs close with INVALID_REQUEST_ID even after retirement. /// `None` when the peer's requests are not flow controlled. pub fn accept(&self, id: RequestId) -> Result, Error> { let mut state = self.state.lock(); let Some(incoming) = &mut state.incoming else { return Ok(None); }; - if id.0 >= incoming.max || incoming.live >= incoming.window { + if id.0 >= incoming.max { tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); return Err(SessionError::TooManyRequests.into()); } + let run = match &incoming.missing { + Some(missing) if id < incoming.next => missing_run(missing, id.0), + _ => None, + }; + let unused = if id < incoming.next { + run.is_some() + } else { + id == incoming.next || incoming.missing.is_some() + }; + if id.0 % 2 != incoming.next.0 % 2 || !unused { + return Err(SessionError::InvalidRequestId.into()); + } + if incoming.live >= incoming.window { + return Err(SessionError::TooManyRequests.into()); + } + if let Some(missing) = &mut incoming.missing { + if let Some((start, end)) = run { + missing.remove(&start); + if start < id.0 { + missing.insert(start, id.0); + } + if id.0 + 2 < end { + missing.insert(id.0 + 2, end); + } + } else if id > incoming.next { + missing.insert(incoming.next.0, id.0); + } + } + incoming.next.0 = incoming.next.0.max(id.0 + 2); incoming.live += 1; Ok(Some(RequestPermit(self.clone()))) } @@ -174,7 +222,7 @@ mod tests { #[test] fn window_refills_as_requests_retire() { // We are the server, so the client's IDs are even: 0, 2, 4, 6. - let control = Control::new(None, false).with_window(4, false); + let control = Control::new(None, false).with_window(4, false, Version::Draft15); assert_eq!(initial_max_request_id(4, true), 8); let mut open: Vec<_> = (0..4) @@ -200,13 +248,126 @@ mod tests { assert!(control.accept(RequestId(12)).is_err()); } - /// A peer reusing request IDs below the window still cannot hold more than it. + /// An ID remains spent after its request ends, across every request type. + #[test] + fn retired_or_lower_request_ids_are_refused() { + for later in [0, 2] { + let control = Control::new(None, false).with_window(8, false, Version::Draft15); + for id in [0, 2, 4] { + drop(control.accept(RequestId(id)).unwrap()); + } + let err = control.accept(RequestId(later)).err().expect("spent ID refused"); + assert_eq!(SessionError::from(&err).to_code(), 0x4); + } + } + + #[test] + fn a_duplicate_is_invalid_even_when_the_window_is_full() { + for client in [false, true] { + let control = Control::new(None, client).with_window(1, client, Version::Draft15); + let id = RequestId(u64::from(client)); + let _held = control.accept(id).unwrap(); + assert!(matches!( + control.accept(id), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + + /// The first ID and every subsequent request must be exactly the next in sequence. + #[test] + fn skipped_and_wrong_parity_request_ids_are_refused() { + for client in [false, true] { + let first = u64::from(client); + for prefix in [0, 1, 2] { + for gap in [2, 4, 1] { + let control = Control::new(None, client).with_window(8, client, Version::Draft15); + for index in 0..prefix { + drop(control.accept(RequestId(first + 2 * index)).unwrap()); + } + let err = control + .accept(RequestId(first + 2 * prefix + gap)) + .err() + .expect("non-sequential ID refused"); + assert_eq!(SessionError::from(&err), SessionError::InvalidRequestId); + } + } + } + } + + #[test] + fn draft16_independent_streams_can_arrive_out_of_order() { + for client in [false, true] { + let control = Control::new(None, client).with_window(8, client, Version::Draft16); + let first = u64::from(client); + for id in [first + 2, first, first + 6, first + 4] { + drop(control.accept(RequestId(id)).expect("independent stream arrival order")); + } + for id in [first, first + 2, first + 4, first + 6] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + } + + #[test] + fn draft16_remembers_only_missing_ids_while_other_requests_retire() { + let control = Control::new(None, false).with_window(4, false, Version::Draft16); + for id in 1..1024 { + drop(control.accept(RequestId(id * 2)).unwrap()); + let state = control.state.lock(); + let missing = state.incoming.as_ref().unwrap().missing.as_ref().unwrap(); + assert_eq!(missing.iter().collect::>(), [(&0, &2)]); + } + drop(control.accept(RequestId(0)).unwrap()); + assert!( + control + .state + .lock() + .incoming + .as_ref() + .unwrap() + .missing + .as_ref() + .unwrap() + .is_empty() + ); + for id in [0, 2, 2046] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } + } + + /// Skipping to the top of a production-sized window records one run, not every ID. #[test] - fn reused_ids_cannot_exceed_the_window() { - let control = Control::new(None, false).with_window(2, false); - let _a = control.accept(RequestId(0)).unwrap(); - let _b = control.accept(RequestId(0)).unwrap(); - assert!(control.accept(RequestId(2)).is_err()); + fn draft16_a_jump_is_one_run_split_only_by_requests_inside_it() { + let window = 220_000; + let control = Control::new(None, false).with_window(window, false, Version::Draft16); + let top = 2 * (window - 1); + let runs = |control: &Control| { + let state = control.state.lock(); + let missing = state.incoming.as_ref().unwrap().missing.as_ref().unwrap(); + missing.iter().map(|(&start, &end)| (start, end)).collect::>() + }; + + drop(control.accept(RequestId(top)).unwrap()); + assert_eq!(runs(&control), [(0, top)]); + + for id in [100, 0, top - 2] { + drop(control.accept(RequestId(id)).unwrap()); + } + assert_eq!(runs(&control), [(2, 100), (102, top - 2)]); + + for id in [0, 100, top - 2, top] { + assert!(matches!( + control.accept(RequestId(id)), + Err(Error::Session(SessionError::InvalidRequestId)) + )); + } } /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. diff --git a/rs/moq-net/src/ietf/mod.rs b/rs/moq-net/src/ietf/mod.rs index 9ed57e1918b..9446fd6ad38 100644 --- a/rs/moq-net/src/ietf/mod.rs +++ b/rs/moq-net/src/ietf/mod.rs @@ -59,3 +59,13 @@ pub use subscribe_namespace::*; // includes PublishBlocked use subscriber::*; pub use track::*; pub use version::Version; + +#[cfg(any(test, feature = "fuzz"))] +pub(crate) fn request_ids(window: u64, ids: &[u64]) -> usize { + let control = Control::new(None, false).with_window(window, false, Version::Draft16); + let held: Vec<_> = ids + .iter() + .filter_map(|id| control.accept(RequestId(*id)).ok()) + .collect(); + held.len() +} diff --git a/rs/moq-net/src/ietf/session.rs b/rs/moq-net/src/ietf/session.rs index 94c57e46add..ef70b379d93 100644 --- a/rs/moq-net/src/ietf/session.rs +++ b/rs/moq-net/src/ietf/session.rs @@ -207,7 +207,7 @@ where session.close(SessionError::from(&err).to_code(), "setup stream required"); return Err(err); }; - let control = Control::new(request_id_max, client).with_window(limits.requests(), client); + let control = Control::new(request_id_max, client).with_window(limits.requests(), client, version); let adapter = ControlStreamAdapter::new(session.clone(), control.clone(), version); // No AUTH on these drafts, but a limit still reaches both halves.