From f5a5968a983c9a6ab061949c996b565e19649290 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 09:29:53 -0700 Subject: [PATCH 1/7] chore: claim request ID order quest Co-Authored-By: GPT-6 From 88a1503542cf7ec8bf7221ab7847d4a9350c5066 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 10:17:38 -0700 Subject: [PATCH 2/7] fix(moq-net): permanently spend incoming IETF request IDs Co-Authored-By: GPT-6 --- drafts/draft-lcurley-moq-lite.md | 4 ++++ js/net/src/error.test.ts | 5 ++++ js/net/src/error.ts | 2 ++ js/net/src/ietf/adapter.test.ts | 41 +++++++++++++++++++++++--------- js/net/src/ietf/adapter.ts | 13 +++++----- quest/m1/README.md | 1 - quest/m1/request-id-order.md | 25 ------------------- rs/moq-net/src/error.rs | 8 +++++++ rs/moq-net/src/ietf/control.rs | 24 +++++++++++++------ 9 files changed, 72 insertions(+), 51 deletions(-) delete mode 100644 quest/m1/request-id-order.md diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index dc963c4150b..3239df81dcb 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, decreased, or had the wrong parity; 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. | @@ -1423,6 +1425,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/net/src/error.test.ts b/js/net/src/error.test.ts index 210d39efa71..c4a291f47fc 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 f13d7dc536d..3789f7f77ea 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, decreased, or had the wrong parity. */ + 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 b23db67212d..7196f0a9409 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -376,7 +376,7 @@ test("a second use of an open request id closes the session", async () => { const { pair, peer, running } = await windowed(version, alpn, 2n, 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"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -470,24 +470,43 @@ 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"); 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) { - const { pair, adapter, peer, running } = await windowed(version, alpn, 2n, false); + 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); + await trackStatus(peer, version, first); + 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"); + 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, 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"); + expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index b5811b928a0..7b1a1e2d8c6 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -9,9 +9,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; @@ -149,6 +146,7 @@ export class ControlStreamAdapter implements Session { // by the IDs they spent and goes out as one MAX_REQUEST_ID. #peerWindow: bigint; #peerMax: bigint; + #peerLast: bigint | undefined; #open = new Set(); #retired = 0n; #granting = false; @@ -482,16 +480,17 @@ 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"); + if (this.#peerLast !== undefined && requestId <= this.#peerLast) { + this.#refuse(SessionCode.InvalidRequestId, "request id was already spent"); } this.#checkLive(); + this.#peerLast = requestId; this.#open.add(requestId); } @@ -505,7 +504,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"); diff --git a/quest/m1/README.md b/quest/m1/README.md index 25c59174d55..ba38b5a091b 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -196,7 +196,6 @@ ladders. - [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 - [Import catalog drop](/quest/m1/import-catalog-drop.md) - moq-cli import never drops a catalog producer without finishing it - [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 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/src/error.rs b/rs/moq-net/src/error.rs index d9b38c9eaea..a453718b9c1 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, decreased, or had the wrong parity. + #[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, @@ -647,6 +653,7 @@ mod tests { SessionError::Internal, SessionError::Unauthorized, SessionError::ProtocolViolation, + SessionError::InvalidRequestId, SessionError::KeyValueFormatting, SessionError::TooManyRequests, SessionError::GoawayTimeout, @@ -665,6 +672,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/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 3ba504d7e2b..5996f8eed8f 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -30,6 +30,8 @@ struct Incoming { live: u64, /// Requests retired since the last grant. retired: u64, + /// The highest accepted ID, including requests that already retired. + last: Option, } #[derive(Clone)] @@ -60,6 +62,7 @@ impl Control { sent: max, live: 0, retired: 0, + last: None, }); self } @@ -72,17 +75,22 @@ 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. + /// Reused or decreasing 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 incoming.last.is_some_and(|last| id <= last) { + return Err(SessionError::InvalidRequestId.into()); + } if id.0 >= incoming.max || incoming.live >= incoming.window { tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); return Err(SessionError::TooManyRequests.into()); } + incoming.last = Some(id); incoming.live += 1; Ok(Some(RequestPermit(self.clone()))) } @@ -200,13 +208,15 @@ 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 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 retired_or_lower_request_ids_are_refused() { + for (first, later) in [(0, 0), (4, 2)] { + let control = Control::new(None, false).with_window(8, false); + drop(control.accept(RequestId(first)).unwrap()); + let err = control.accept(RequestId(later)).err().expect("spent ID refused"); + assert_eq!(SessionError::from(&err).to_code(), 0x4); + } } /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. From 97776e4b47a28817e18b7ddb94a96a58a340986d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 17:52:38 -0700 Subject: [PATCH 3/7] fix(net): require the next incoming IETF request ID Co-Authored-By: GPT-6 Astra --- drafts/draft-lcurley-moq-lite.md | 2 +- js/net/src/error.ts | 2 +- js/net/src/ietf/adapter.test.ts | 46 ++++++++++++++++++------- js/net/src/ietf/adapter.ts | 9 ++--- rs/moq-net/src/error.rs | 2 +- rs/moq-net/src/ietf/control.rs | 59 ++++++++++++++++++++++++++------ 6 files changed, 91 insertions(+), 29 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index bc4c208f683..e9cedb117fe 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -284,7 +284,7 @@ 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, decreased, or had the wrong parity; moq-lite has no request IDs. | +| 0x4 | INVALID_REQUEST_ID | A moq-transport request ID was reused, skipped, decreased, or had the wrong parity; moq-lite has no request IDs. | | ------- | ------------- | ----------- | | 0x6 | KEY_VALUE_FORMATTING_ERROR | A key-value pair was malformed, or repeated more than allowed. | | ------- | ------------- | ----------- | diff --git a/js/net/src/error.ts b/js/net/src/error.ts index 3920c056ded..c0e7d6ea8eb 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -36,7 +36,7 @@ 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, decreased, or had the wrong parity. */ + /** A request ID was reused, skipped, decreased, or had the wrong parity. */ InvalidRequestId: 0x4 as SessionCode, /** A key-value pair was malformed or repeated more than allowed. */ KeyValueFormatting: 0x6 as SessionCode, diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index 7196f0a9409..a67717bfd0d 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -373,10 +373,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("request id was already spent"); + await expect(running).rejects.toThrow("request id is not next in sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -416,9 +416,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(); } }); @@ -470,7 +470,7 @@ 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("request id was already spent"); + await expect(running).rejects.toThrow("request id is not next in sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } @@ -486,12 +486,14 @@ test("retired and lower request ids close the session", async () => { [parity + 4n, parity + 2n], ] as const) { const { pair, adapter, peer, running } = await windowed(version, alpn, 8n, client); - await trackStatus(peer, version, first); - const stream = await adapter.acceptBi(); - if (!stream) throw new Error("no stream"); - stream.close(); + 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"); + await expect(running).rejects.toThrow("request id is not next in sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } @@ -505,7 +507,7 @@ test("an update id stays spent after the update retires", async () => { if (!(await adapter.acceptBi())) throw new Error("no stream"); await update(peer, version, 2n, 0n); await update(peer, version, 2n, 0n); - await expect(running).rejects.toThrow("request id was already spent"); + await expect(running).rejects.toThrow("request id is not next in sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -543,3 +545,23 @@ test("more requests than the setup window complete on one session", async () => await reading; } }); + +// 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) { + 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 is not next in sequence"); + expect((await pair.client.closed).closeCode).toBe(SessionCode.InvalidRequestId); + } + } + } + } +}); diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index 7b1a1e2d8c6..0a082903a63 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -146,7 +146,7 @@ export class ControlStreamAdapter implements Session { // by the IDs they spent and goes out as one MAX_REQUEST_ID. #peerWindow: bigint; #peerMax: bigint; - #peerLast: bigint | undefined; + #peerNext: bigint; #open = new Set(); #retired = 0n; #granting = false; @@ -177,6 +177,7 @@ export class ControlStreamAdapter implements Session { this.#maxRequestId = maxRequestId; this.#peerWindow = window; this.#peerMax = initialMaxRequestId(!client, window); + this.#peerNext = client ? 1n : 0n; this.#requestId = client ? 0n : 1n; this.#client = client; } @@ -486,11 +487,11 @@ export class ControlStreamAdapter implements Session { #admitHeld(requestId: bigint): void { this.#checkPeer(requestId); - if (this.#peerLast !== undefined && requestId <= this.#peerLast) { - this.#refuse(SessionCode.InvalidRequestId, "request id was already spent"); + if (requestId !== this.#peerNext) { + this.#refuse(SessionCode.InvalidRequestId, "request id is not next in sequence"); } this.#checkLive(); - this.#peerLast = requestId; + this.#peerNext += 2n; this.#open.add(requestId); } diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index 7e0199cd042..b438349c30f 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -58,7 +58,7 @@ pub enum SessionError { #[error("unknown code={0}")] Unknown(u32), - /// A request ID was reused, decreased, or had the wrong parity. + /// A request ID was reused, skipped, decreased, or had the wrong parity. #[error("invalid request id")] InvalidRequestId, } diff --git a/rs/moq-net/src/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 5996f8eed8f..552d998c066 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -30,8 +30,8 @@ struct Incoming { live: u64, /// Requests retired since the last grant. retired: u64, - /// The highest accepted ID, including requests that already retired. - last: Option, + /// The next ID the peer must use, including requests that already retired. + next: RequestId, } #[derive(Clone)] @@ -62,7 +62,7 @@ impl Control { sent: max, live: 0, retired: 0, - last: None, + next: RequestId(u64::from(client)), }); self } @@ -76,21 +76,24 @@ impl Control { /// /// Closes the session with TOO_MANY_REQUESTS when the ID is at or past the /// MAX_REQUEST_ID we advertised, or its live requests exceed the window. - /// Reused or decreasing IDs close with INVALID_REQUEST_ID even after retirement. + /// Non-sequential 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 incoming.last.is_some_and(|last| id <= last) { + 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()); + } + if id != incoming.next { return Err(SessionError::InvalidRequestId.into()); } - if id.0 >= incoming.max || incoming.live >= incoming.window { - tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); + if incoming.live >= incoming.window { return Err(SessionError::TooManyRequests.into()); } - incoming.last = Some(id); + incoming.next.increment(); incoming.live += 1; Ok(Some(RequestPermit(self.clone()))) } @@ -211,14 +214,50 @@ mod tests { /// An ID remains spent after its request ends, across every request type. #[test] fn retired_or_lower_request_ids_are_refused() { - for (first, later) in [(0, 0), (4, 2)] { + for later in [0, 2] { let control = Control::new(None, false).with_window(8, false); - drop(control.accept(RequestId(first)).unwrap()); + 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); + 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); + 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); + } + } + } + } + /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. #[test] fn unlimited_without_a_window() { From 90b84903e4d5d30db487225073d0aed8290e9990 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 18:08:12 -0700 Subject: [PATCH 4/7] quest: retain draft-16 request ordering decision Co-Authored-By: GPT-6 Astra --- quest/m1/README.md | 1 + quest/m1/request-id-order.md | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 quest/m1/request-id-order.md diff --git a/quest/m1/README.md b/quest/m1/README.md index f20331f5418..2dcf40041ca 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -209,6 +209,7 @@ bench all require its crate. - [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) - refuse spent IDs while preserving valid draft-16 stream reordering - [Import catalog drop](/quest/m1/import-catalog-drop.md) - moq-cli import never drops a catalog producer without finishing it - [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 diff --git a/quest/m1/request-id-order.md b/quest/m1/request-id-order.md new file mode 100644 index 00000000000..af42b590f68 --- /dev/null +++ b/quest/m1/request-id-order.md @@ -0,0 +1,40 @@ +# [S] A reused moq-transport Request ID is refused + +## Goal + +On drafts 14 to 16, `@moq/net` and `moq-net` refuse invalid incoming Request +IDs, including IDs whose requests already retired, without rejecting valid +independent-stream reordering. In-order requests and MAX_REQUEST_ID grants +continue to work. + +## Plan + +[PR #5235](https://github.com/moq-dev/moq/pull/5235) implements exact-next +admission in both languages. Regressions cover repeated, decreasing, +skipped, wrong-parity and retired IDs, including updates and both peer +roles. Request-limit errors remain distinct from invalid-ID errors. + +Section 9.1 of drafts 14 to 16 starts at the peer's parity and increments +by two for each request. Draft 16 section 9.25 also puts SUBSCRIBE_NAMESPACE +on an independent bidi stream with that same Request ID space. Its arrival +can reorder against the control stream; one arrival-order counter does not +establish sender order across those streams. + +Settle the draft-16 compatibility policy before completing this quest: + +- Literal exact-next admission across all streams follows section 9.1 but + can reject transport reordering. +- Exact order for drafts 14/15, with bounded spent-ID tracking for draft 16, + preserves reordering. Recommended direction; define the lifetime/window + bound before implementing it. Retired IDs must never become reusable. +- Buffer draft-16 gaps until the missing requests arrive. This preserves + order but can delay refusing a genuinely skipped ID. + +Test control/native arrival reordering deterministically, as well as +retired-ID reuse and long-lived namespace requests while control requests +retire. Do not add retries or timing allowances. Run `just check` and +`just test interop --all`. + +Public API: matching named InvalidRequestId session errors in Rust and JS. +Wire: unchanged framing, stricter refusal with existing error 0x4; report +any draft-16 acceptance policy explicitly. From 0afe789fa2990f30ccd6b1c321f4fce2751a59f3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 18:41:58 -0700 Subject: [PATCH 5/7] fix(net): allow bounded draft-16 request ID reordering Follow upstream receiver guidance while retaining strict draft-14/15 order. Remember only missing IDs backed by unreturned window credits, and benchmark both arrival orders across sessions and window sizes. Co-Authored-By: GPT-6 Astra --- .github/workflows/nightly.yml | 4 ++ drafts/draft-lcurley-moq-lite.md | 2 +- js/justfile | 2 +- js/net/bench/request-ids.ts | 63 ++++++++++++++++++++++ js/net/src/error.ts | 2 +- js/net/src/ietf/adapter.test.ts | 41 +++++++++++--- js/net/src/ietf/adapter.ts | 18 +++++-- quest/m1/README.md | 1 - quest/m1/request-id-order.md | 40 -------------- rs/moq-net/Cargo.toml | 5 ++ rs/moq-net/benches/request-ids.rs | 36 +++++++++++++ rs/moq-net/src/error.rs | 2 +- rs/moq-net/src/fuzz.rs | 5 ++ rs/moq-net/src/ietf/adapter.rs | 2 +- rs/moq-net/src/ietf/control.rs | 89 +++++++++++++++++++++++++++---- rs/moq-net/src/ietf/mod.rs | 10 ++++ rs/moq-net/src/ietf/session.rs | 2 +- 17 files changed, 257 insertions(+), 67 deletions(-) create mode 100644 js/net/bench/request-ids.ts delete mode 100644 quest/m1/request-id-order.md create mode 100644 rs/moq-net/benches/request-ids.rs diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index df96ff12ef4..65cdd7900b3 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -97,6 +97,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 e9cedb117fe..19962696295 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -284,7 +284,7 @@ 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, skipped, decreased, or had the wrong parity; moq-lite has no request IDs. | +| 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. | | ------- | ------------- | ----------- | diff --git a/js/justfile b/js/justfile index 68f09ae8526..aa59127c628 100644 --- a/js/justfile +++ b/js/justfile @@ -47,7 +47,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..157486d0ae4 --- /dev/null +++ b/js/net/bench/request-ids.ts @@ -0,0 +1,63 @@ +// 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"; + +const debug = console.debug; +console.debug = () => undefined; +try { + console.log("mode,sessions,window,admit_us,accepted"); + for (const reversed of [false, true]) { + for (const sessions of [1, 8, 32]) { + for (const window of [8, 32, 128]) { + const ids = Array.from({ length: window }, (_, id) => BigInt(id * 2)); + if (reversed) ids.reverse(); + let elapsed = 0; + let accepted = 0; + for (let round = 0; round < 64; 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( + `${reversed ? "reordered" : "ordered"},${sessions},${window},${((elapsed * 1000) / 64).toFixed(2)},${accepted}`, + ); + } + } + } +} finally { + console.debug = debug; +} diff --git a/js/net/src/error.ts b/js/net/src/error.ts index c0e7d6ea8eb..55bdde66258 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -36,7 +36,7 @@ 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, skipped, decreased, or had the wrong parity. */ + /** 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, diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index a67717bfd0d..63df9df603a 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -376,7 +376,7 @@ test("a second use of an open request id closes the session", async () => { 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("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -470,7 +470,7 @@ 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("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } @@ -493,7 +493,7 @@ test("retired and lower request ids close the session", async () => { stream.close(); } await trackStatus(peer, version, later); - await expect(running).rejects.toThrow("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } } @@ -507,7 +507,7 @@ test("an update id stays spent after the update retires", async () => { if (!(await adapter.acceptBi())) throw new Error("no stream"); await update(peer, version, 2n, 0n); await update(peer, version, 2n, 0n); - await expect(running).rejects.toThrow("request id is not next in sequence"); + await expect(running).rejects.toThrow("request id was already spent or out of sequence"); expect((await pair.client.closed).closeCode).toBe(INVALID_REQUEST_ID); } }); @@ -548,7 +548,7 @@ 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) { + 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]) { @@ -558,10 +558,39 @@ test("skipped request and update ids close the session", async () => { 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 is not next in sequence"); + 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(); + } + } + } +}); diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index 0a082903a63..aae1f27814f 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -147,6 +147,9 @@ export class ControlStreamAdapter implements Session { #peerWindow: bigint; #peerMax: bigint; #peerNext: bigint; + // Draft 16 reorders across streams. Every missing ID retains an unreturned window + // credit, so this set is bounded by the window rather than the session lifetime. + #peerMissing: Set | undefined; #open = new Set(); #retired = 0n; #granting = false; @@ -178,6 +181,7 @@ export class ControlStreamAdapter implements Session { this.#peerWindow = window; this.#peerMax = initialMaxRequestId(!client, window); this.#peerNext = client ? 1n : 0n; + this.#peerMissing = version === Version.DRAFT_16 ? new Set() : undefined; this.#requestId = client ? 0n : 1n; this.#client = client; } @@ -487,11 +491,19 @@ export class ControlStreamAdapter implements Session { #admitHeld(requestId: bigint): void { this.#checkPeer(requestId); - if (requestId !== this.#peerNext) { - this.#refuse(SessionCode.InvalidRequestId, "request id is not next in sequence"); + const unused = + requestId < this.#peerNext + ? this.#peerMissing?.has(requestId) + : requestId === this.#peerNext || this.#peerMissing !== undefined; + if (!unused) { + this.#refuse(SessionCode.InvalidRequestId, "request id was already spent or out of sequence"); } this.#checkLive(); - this.#peerNext += 2n; + if (this.#peerMissing) { + for (let skipped = this.#peerNext; skipped < requestId; skipped += 2n) this.#peerMissing.add(skipped); + this.#peerMissing.delete(requestId); + } + if (requestId >= this.#peerNext) this.#peerNext = requestId + 2n; this.#open.add(requestId); } diff --git a/quest/m1/README.md b/quest/m1/README.md index 2dcf40041ca..f20331f5418 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -209,7 +209,6 @@ bench all require its crate. - [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) - refuse spent IDs while preserving valid draft-16 stream reordering - [Import catalog drop](/quest/m1/import-catalog-drop.md) - moq-cli import never drops a catalog producer without finishing it - [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 diff --git a/quest/m1/request-id-order.md b/quest/m1/request-id-order.md deleted file mode 100644 index af42b590f68..00000000000 --- a/quest/m1/request-id-order.md +++ /dev/null @@ -1,40 +0,0 @@ -# [S] A reused moq-transport Request ID is refused - -## Goal - -On drafts 14 to 16, `@moq/net` and `moq-net` refuse invalid incoming Request -IDs, including IDs whose requests already retired, without rejecting valid -independent-stream reordering. In-order requests and MAX_REQUEST_ID grants -continue to work. - -## Plan - -[PR #5235](https://github.com/moq-dev/moq/pull/5235) implements exact-next -admission in both languages. Regressions cover repeated, decreasing, -skipped, wrong-parity and retired IDs, including updates and both peer -roles. Request-limit errors remain distinct from invalid-ID errors. - -Section 9.1 of drafts 14 to 16 starts at the peer's parity and increments -by two for each request. Draft 16 section 9.25 also puts SUBSCRIBE_NAMESPACE -on an independent bidi stream with that same Request ID space. Its arrival -can reorder against the control stream; one arrival-order counter does not -establish sender order across those streams. - -Settle the draft-16 compatibility policy before completing this quest: - -- Literal exact-next admission across all streams follows section 9.1 but - can reject transport reordering. -- Exact order for drafts 14/15, with bounded spent-ID tracking for draft 16, - preserves reordering. Recommended direction; define the lifetime/window - bound before implementing it. Retired IDs must never become reusable. -- Buffer draft-16 gaps until the missing requests arrive. This preserves - order but can delay refusing a genuinely skipped ID. - -Test control/native arrival reordering deterministically, as well as -retired-ID reuse and long-lived namespace requests while control requests -retire. Do not add retries or timing allowances. Run `just check` and -`just test interop --all`. - -Public API: matching named InvalidRequestId session errors in Rust and JS. -Wire: unchanged framing, stricter refusal with existing error 0x4; report -any draft-16 acceptance policy explicitly. diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index 0aaecfb0ed5..0d9d0cef71a 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -121,3 +121,8 @@ harness = false [[bench]] name = "session" harness = false + +[[bench]] +name = "request-ids" +harness = false +required-features = ["fuzz"] diff --git a/rs/moq-net/benches/request-ids.rs b/rs/moq-net/benches/request-ids.rs new file mode 100644 index 00000000000..5d1031dbe92 --- /dev/null +++ b/rs/moq-net/benches/request-ids.rs @@ -0,0 +1,36 @@ +//! 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)); + for reversed in [false, true] { + for sessions in [1, 8, 32] { + for window in [8, 32, 128] { + let mut ids: Vec<_> = (0..window).map(|id| id * 2).collect(); + if reversed { + ids.reverse(); + } + let mode = if reversed { "reordered" } else { "ordered" }; + 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(); +} +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 b438349c30f..5293cc39949 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -58,7 +58,7 @@ pub enum SessionError { #[error("unknown code={0}")] Unknown(u32), - /// A request ID was reused, skipped, decreased, or had the wrong parity. + /// A request ID was reused or violated the negotiated draft’s order or parity rules. #[error("invalid request id")] InvalidRequestId, } diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index fbf13059ad0..8f42dde6004 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) +} + #[cfg(test)] mod tests { use super::*; 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 552d998c066..6561bb054b0 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -1,6 +1,9 @@ -use std::task::Poll; +use std::{collections::HashSet, task::Poll}; -use crate::{Error, SessionError, ietf::RequestId}; +use crate::{ + Error, SessionError, + ietf::{RequestId, Version}, +}; /// The MAX_REQUEST_ID that first admits `window` requests from a peer. /// @@ -30,8 +33,11 @@ struct Incoming { live: u64, /// Requests retired since the last grant. retired: u64, - /// The next ID the peer must use, including requests that already retired. + /// One past the highest accepted ID, preserving IDs whose requests retired. next: RequestId, + /// Draft 16 can reorder across streams. Missing IDs retain unreturned window credits, + /// so remembering only these holes is bounded by the request window, not session age. + missing: Option>, } #[derive(Clone)] @@ -54,7 +60,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, @@ -63,6 +69,7 @@ impl Control { live: 0, retired: 0, next: RequestId(u64::from(client)), + missing: (version == Version::Draft16).then(HashSet::new), }); self } @@ -76,7 +83,8 @@ impl Control { /// /// Closes the session with TOO_MANY_REQUESTS when the ID is at or past the /// MAX_REQUEST_ID we advertised, or its live requests exceed the window. - /// Non-sequential IDs close with INVALID_REQUEST_ID even after retirement. + /// 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(); @@ -87,13 +95,24 @@ impl Control { tracing::warn!(id = %id, max = incoming.max, live = incoming.live, "peer exceeded MAX_REQUEST_ID"); return Err(SessionError::TooManyRequests.into()); } - if id != incoming.next { + let unused = if id < incoming.next { + incoming.missing.as_ref().is_some_and(|missing| missing.contains(&id)) + } 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()); } - incoming.next.increment(); + if let Some(missing) = &mut incoming.missing { + for skipped in (incoming.next.0..id.0).step_by(2) { + missing.insert(RequestId(skipped)); + } + missing.remove(&id); + } + incoming.next.0 = incoming.next.0.max(id.0 + 2); incoming.live += 1; Ok(Some(RequestPermit(self.clone()))) } @@ -185,7 +204,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) @@ -215,7 +234,7 @@ mod tests { #[test] fn retired_or_lower_request_ids_are_refused() { for later in [0, 2] { - let control = Control::new(None, false).with_window(8, false); + let control = Control::new(None, false).with_window(8, false, Version::Draft15); for id in [0, 2, 4] { drop(control.accept(RequestId(id)).unwrap()); } @@ -227,7 +246,7 @@ mod tests { #[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); + 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!( @@ -244,7 +263,7 @@ mod tests { 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); + 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()); } @@ -258,6 +277,54 @@ mod tests { } } + #[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.len(), 1); + assert!(missing.contains(&RequestId(0))); + } + 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)) + )); + } + } + /// Draft-17+ has no MAX_REQUEST_ID: nothing is tracked or granted. #[test] fn unlimited_without_a_window() { 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 e5f9dfb6a97..7e6cae38318 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. From 719f32ca2b67d62f6c18548e6e7891af3b813e58 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 16:03:08 -0700 Subject: [PATCH 6/7] fix(moq-net): send IETF request IDs in order and track draft-16 gaps as runs The JS sender allocated a request ID before an asynchronous encode, so a shorter later request could reach the draft-14/15 control stream first and trip the strict receiver. Messages that spend a new ID now wait until every lower ID has been written. Draft-16 missing IDs were materialized one entry per skipped ID, so one request near MAX_REQUEST_ID inserted up to the whole window. They are now start..end runs in Rust (BTreeMap) and JS (sorted array), and the admission benchmarks sweep up to the 220,000 production window, including a worst-case split order. Co-Authored-By: Claude Opus 5.5 --- js/net/bench/request-ids.ts | 29 +++++++--- js/net/src/ietf/adapter.test.ts | 56 +++++++++++++++++++ js/net/src/ietf/adapter.ts | 90 +++++++++++++++++++++++++++---- rs/moq-net/benches/request-ids.rs | 30 ++++++++--- rs/moq-net/src/ietf/control.rs | 67 +++++++++++++++++++---- 5 files changed, 237 insertions(+), 35 deletions(-) diff --git a/js/net/bench/request-ids.ts b/js/net/bench/request-ids.ts index 157486d0ae4..27291fe4220 100644 --- a/js/net/bench/request-ids.ts +++ b/js/net/bench/request-ids.ts @@ -3,18 +3,35 @@ 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"); - for (const reversed of [false, true]) { + // 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, 32, 128]) { - const ids = Array.from({ length: window }, (_, id) => BigInt(id * 2)); - if (reversed) ids.reverse(); + 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 < 64; round++) { + for (let round = 0; round < rounds; round++) { const fixtures = Array.from({ length: sessions }, () => { const stream = new Stream({ readable: new ReadableStream(), @@ -53,7 +70,7 @@ try { await Promise.resolve(); } console.log( - `${reversed ? "reordered" : "ordered"},${sessions},${window},${((elapsed * 1000) / 64).toFixed(2)},${accepted}`, + `${mode},${sessions},${window},${((elapsed * 1000) / rounds).toFixed(2)},${accepted / rounds}`, ); } } diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index 5056e20ff31..d3a262a4566 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"; @@ -613,6 +614,61 @@ test("draft 16 accepts reordered native and control IDs but never reuses them", } }); +// 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(); + } +}); + +// 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 d3915410b79..665552484b5 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -143,15 +143,23 @@ 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. Every missing ID retains an unreturned window - // credit, so this set is bounded by the window rather than the session lifetime. - #peerMissing: Set | undefined; + // Draft 16 reorders across streams, so it remembers the IDs skipped below #peerNext as + // sorted [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. + #peerMissing: [bigint, bigint][] | undefined; #open = new Set(); #retired = 0n; #granting = false; @@ -183,8 +191,9 @@ export class ControlStreamAdapter implements Session { this.#peerWindow = window; this.#peerMax = initialMaxRequestId(!client, window); this.#peerNext = client ? 1n : 0n; - this.#peerMissing = version === Version.DRAFT_16 ? new Set() : undefined; + this.#peerMissing = version === Version.DRAFT_16 ? [] : undefined; this.#requestId = client ? 0n : 1n; + this.#sendNext = version === Version.DRAFT_16 ? undefined : this.#requestId; this.#client = client; } @@ -244,6 +253,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); @@ -251,10 +261,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); } }, }); @@ -499,22 +512,42 @@ export class ControlStreamAdapter implements Session { #admitHeld(requestId: bigint): void { this.#checkPeer(requestId); + const run = requestId < this.#peerNext ? this.#missingRun(requestId) : -1; const unused = - requestId < this.#peerNext - ? this.#peerMissing?.has(requestId) - : requestId === this.#peerNext || this.#peerMissing !== undefined; + requestId < this.#peerNext ? run >= 0 : requestId === this.#peerNext || this.#peerMissing !== undefined; if (!unused) { this.#refuse(SessionCode.InvalidRequestId, "request id was already spent or out of sequence"); } this.#checkLive(); if (this.#peerMissing) { - for (let skipped = this.#peerNext; skipped < requestId; skipped += 2n) this.#peerMissing.add(skipped); - this.#peerMissing.delete(requestId); + if (run >= 0) { + const [start, end] = this.#peerMissing[run]; + const rest: [bigint, bigint][] = []; + if (start < requestId) rest.push([start, requestId]); + if (requestId + 2n < end) rest.push([requestId + 2n, end]); + this.#peerMissing.splice(run, 1, ...rest); + } else if (requestId > this.#peerNext) { + this.#peerMissing.push([this.#peerNext, requestId]); + } } if (requestId >= this.#peerNext) this.#peerNext = requestId + 2n; this.#open.add(requestId); } + /** The index of the skipped draft-16 run holding `requestId`, or -1. */ + #missingRun(requestId: bigint): number { + const runs = this.#peerMissing; + if (!runs) return -1; + let lo = 0; + let hi = runs.length; + while (lo < hi) { + const mid = (lo + hi) >>> 1; + if (runs[mid][0] <= requestId) lo = mid + 1; + else hi = mid; + } + return lo > 0 && requestId < runs[lo - 1][1] ? lo - 1 : -1; + } + /** * An update spends its own request ID like any request but keeps no stream, so the * slot is retired at once. The ID is spent even when the target is already gone. @@ -688,12 +721,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 @@ -944,5 +1009,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/rs/moq-net/benches/request-ids.rs b/rs/moq-net/benches/request-ids.rs index 5d1031dbe92..aee593308c5 100644 --- a/rs/moq-net/benches/request-ids.rs +++ b/rs/moq-net/benches/request-ids.rs @@ -7,14 +7,11 @@ fn request_ids(c: &mut Criterion) { group.sample_size(20); group.warm_up_time(Duration::from_millis(200)); group.measurement_time(Duration::from_secs(1)); - for reversed in [false, true] { + // 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, 32, 128] { - let mut ids: Vec<_> = (0..window).map(|id| id * 2).collect(); - if reversed { - ids.reverse(); - } - let mode = if reversed { "reordered" } else { "ordered" }; + 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 @@ -32,5 +29,24 @@ fn request_ids(c: &mut Criterion) { } 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/ietf/control.rs b/rs/moq-net/src/ietf/control.rs index 6561bb054b0..b5a85859d91 100644 --- a/rs/moq-net/src/ietf/control.rs +++ b/rs/moq-net/src/ietf/control.rs @@ -1,10 +1,16 @@ -use std::{collections::HashSet, task::Poll}; +use std::{collections::BTreeMap, task::Poll}; 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. /// /// A client's request IDs are even from 0 and a server's odd from 1, each stepping by 2. @@ -35,9 +41,10 @@ struct Incoming { retired: u64, /// One past the highest accepted ID, preserving IDs whose requests retired. next: RequestId, - /// Draft 16 can reorder across streams. Missing IDs retain unreturned window credits, - /// so remembering only these holes is bounded by the request window, not session age. - missing: Option>, + /// 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)] @@ -69,7 +76,7 @@ impl Control { live: 0, retired: 0, next: RequestId(u64::from(client)), - missing: (version == Version::Draft16).then(HashSet::new), + missing: (version == Version::Draft16).then(BTreeMap::new), }); self } @@ -95,8 +102,12 @@ impl Control { 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 { - incoming.missing.as_ref().is_some_and(|missing| missing.contains(&id)) + run.is_some() } else { id == incoming.next || incoming.missing.is_some() }; @@ -107,10 +118,17 @@ impl Control { return Err(SessionError::TooManyRequests.into()); } if let Some(missing) = &mut incoming.missing { - for skipped in (incoming.next.0..id.0).step_by(2) { - missing.insert(RequestId(skipped)); + 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); } - missing.remove(&id); } incoming.next.0 = incoming.next.0.max(id.0 + 2); incoming.live += 1; @@ -301,8 +319,7 @@ mod tests { 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.len(), 1); - assert!(missing.contains(&RequestId(0))); + assert_eq!(missing.iter().collect::>(), [(&0, &2)]); } drop(control.accept(RequestId(0)).unwrap()); assert!( @@ -325,6 +342,34 @@ mod tests { } } + /// Skipping to the top of a production-sized window records one run, not every ID. + #[test] + 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. #[test] fn unlimited_without_a_window() { From 094a4076c0bb11650527ce89538cef580d905a77 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 18:38:49 -0700 Subject: [PATCH 7/7] fix(net): chunk JS draft-16 missing-ID runs A single sorted array made each split shift every later run, so an adversarial descending split order cost O(window^2) in total. Runs now live in chunks of at most 512, so a split shifts one chunk. At the 220,000 window the worst-case order drops from 823 ms to 85 ms for one session. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/adapter.test.ts | 18 ++++++ js/net/src/ietf/adapter.ts | 102 ++++++++++++++++++++++---------- 2 files changed, 88 insertions(+), 32 deletions(-) diff --git a/js/net/src/ietf/adapter.test.ts b/js/net/src/ietf/adapter.test.ts index d3a262a4566..1421167722d 100644 --- a/js/net/src/ietf/adapter.test.ts +++ b/js/net/src/ietf/adapter.test.ts @@ -630,6 +630,24 @@ test("draft 16 tracks a jump across a production window as one run", async () => } }); +// 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 [ diff --git a/js/net/src/ietf/adapter.ts b/js/net/src/ietf/adapter.ts index 665552484b5..5259dd4113a 100644 --- a/js/net/src/ietf/adapter.ts +++ b/js/net/src/ietf/adapter.ts @@ -92,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. * @@ -156,10 +218,8 @@ export class ControlStreamAdapter implements Session { #peerWindow: bigint; #peerMax: bigint; #peerNext: bigint; - // Draft 16 reorders across streams, so it remembers the IDs skipped below #peerNext as - // sorted [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. - #peerMissing: [bigint, bigint][] | undefined; + // Draft 16 reorders across streams, so it remembers the IDs skipped below #peerNext. + #peerMissing: MissingRuns | undefined; #open = new Set(); #retired = 0n; #granting = false; @@ -191,7 +251,7 @@ export class ControlStreamAdapter implements Session { this.#peerWindow = window; this.#peerMax = initialMaxRequestId(!client, window); this.#peerNext = client ? 1n : 0n; - this.#peerMissing = version === Version.DRAFT_16 ? [] : undefined; + 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; @@ -512,42 +572,20 @@ export class ControlStreamAdapter implements Session { #admitHeld(requestId: bigint): void { this.#checkPeer(requestId); - const run = requestId < this.#peerNext ? this.#missingRun(requestId) : -1; const unused = - requestId < this.#peerNext ? run >= 0 : requestId === this.#peerNext || this.#peerMissing !== undefined; + 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 (this.#peerMissing) { - if (run >= 0) { - const [start, end] = this.#peerMissing[run]; - const rest: [bigint, bigint][] = []; - if (start < requestId) rest.push([start, requestId]); - if (requestId + 2n < end) rest.push([requestId + 2n, end]); - this.#peerMissing.splice(run, 1, ...rest); - } else if (requestId > this.#peerNext) { - this.#peerMissing.push([this.#peerNext, requestId]); - } - } + 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); } - /** The index of the skipped draft-16 run holding `requestId`, or -1. */ - #missingRun(requestId: bigint): number { - const runs = this.#peerMissing; - if (!runs) return -1; - let lo = 0; - let hi = runs.length; - while (lo < hi) { - const mid = (lo + hi) >>> 1; - if (runs[mid][0] <= requestId) lo = mid + 1; - else hi = mid; - } - return lo > 0 && requestId < runs[lo - 1][1] ? lo - 1 : -1; - } - /** * An update spends its own request ID like any request but keeps no stream, so the * slot is retired at once. The ID is spent even when the target is already gone.