Repository navigation
fix(moq-net): send registered moq-transport request error codes #3531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5e6ec6a
6cbbad1
8980206
900afba
104d644
1e47491
dfb9c39
2a2bbd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| * @module | ||
| */ | ||
|
|
||
| import { sharedStreamCode } from "./ietf/error.ts"; | ||
| import type { IetfVersion } from "./ietf/version.ts"; | ||
| import { TimeoutError } from "./util/timeout.ts"; | ||
|
|
||
|
|
@@ -269,16 +270,20 @@ interface TransportErrorOptions { | |
| * rather than inventing one. A {@link SessionError} lands there too: the two registries are | ||
| * disjoint, so forwarding its code onto a stream would mistranslate it. | ||
| * | ||
| * On IETF streams only cancellation is mapped; other failures use INTERNAL_ERROR. | ||
| * On an IETF stream the code has to be one the negotiated draft assigns the same meaning to, | ||
| * so a condition it does not register degrades to INTERNAL_ERROR as well. See | ||
| * {@link sharedStreamCode}. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function toStreamCode(err: unknown, options?: TransportErrorOptions): StreamCode { | ||
| if (options?.version !== undefined) { | ||
| // Match Rust's IETF mapping: a local timeout is not a negotiated delivery timeout, | ||
| // and losing one group does not establish that the subscription is terminating. | ||
| return err instanceof StreamError && err.code === StreamCode.Cancel ? StreamCode.Cancel : StreamCode.Internal; | ||
| } | ||
| const code = localStreamCode(err); | ||
| if (options?.version === undefined) return code; | ||
| return sharedStreamCode(code, options.version) ? code : StreamCode.Internal; | ||
| } | ||
|
|
||
| /** The moq-lite code for a local failure, before any draft has a say. */ | ||
| function localStreamCode(err: unknown): StreamCode { | ||
| if (err instanceof StreamError) return err.code; | ||
| if (err instanceof TimeoutError) return StreamCode.DeliveryTimeout; | ||
| // Session-scoped: the peer learns which rule it broke from the session close, not from here. | ||
|
|
@@ -298,15 +303,38 @@ export function toStreamCode(err: unknown, options?: TransportErrorOptions): Str | |
| * the same `instanceof` as one raised here. Only the registered codes do: the reserved 32-63 | ||
| * placeholders carry no meaning the draft assigns, so they stay a plain {@link StreamError}. | ||
| * | ||
| * On an IETF stream a code keeps its value unless moq-lite claims that number for something | ||
| * the draft does not: `0x4` is GOING_AWAY here but UNKNOWN_OBJECT_STATUS on draft-16 and 17, | ||
| * and reading one back as the other would retire a session that is not going anywhere. Those | ||
| * read as {@link StreamCode.Internal} with the wire value kept in the message, since | ||
| * {@link StreamCode} is one numeric space and cannot hold a foreign code without lying about | ||
| * it. A code moq-lite names nothing for survives intact: nothing can misread it. | ||
| * | ||
| * @internal Called at the transport boundary so the raw error never reaches an application. | ||
| */ | ||
| export function fromTransport(err: unknown, options?: TransportErrorOptions): Error { | ||
| const code = streamCode(err); | ||
| if (code === undefined) return error(err); | ||
| if (options?.version === undefined && code === StreamCode.TooFarBehind) return new Lagged({ cause: err }); | ||
| if (options?.version !== undefined && !sharedStreamCode(code, options.version) && claimedLocally(code)) { | ||
| return new StreamError(StreamCode.Internal, { cause: err, message: `remote error: ${code}` }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an IETF peer sends a registered code that has no matching moq-lite meaning, such as Useful? React with 👍 / 👎.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, and taken partway. A code moq-lite names nothing for now keeps its value: The flattening stays only where moq-lite claims the number for something the draft does not, which is the case you named: (written by claude-opus-5) |
||
| } | ||
| if (code === StreamCode.TooFarBehind) return new Lagged({ cause: err }); | ||
| return new StreamError(code, { cause: err }); | ||
| } | ||
|
|
||
| /** The codes moq-lite names, which a foreign one must not borrow. */ | ||
| const NAMED_CODES: ReadonlySet<number> = new Set(Object.values(StreamCode)); | ||
|
|
||
| /** | ||
| * Whether moq-lite could give `code` a meaning of its own: one of its named codes, or | ||
| * anywhere in the 64+ range, where `StreamCode(code)` mints an application code and an | ||
| * application compares against its own. moq-transport registers nothing above 0x12 and has | ||
| * no application range at all, so a foreign code landing there is never what it looks like. | ||
| */ | ||
| function claimedLocally(code: number): boolean { | ||
| return code >= 64 || NAMED_CODES.has(code); | ||
| } | ||
|
|
||
| const legacyWebTransportErrors = new WeakSet<object>(); | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { StreamCode } from "../error.ts"; | ||
| import { fromRequestCode, type RequestCondition, type RequestKind, sharedStreamCode, toRequestCode } from "./error.ts"; | ||
| import { type IetfVersion, Version } from "./version.ts"; | ||
|
|
||
| const ALL: IetfVersion[] = [ | ||
| Version.DRAFT_14, | ||
| Version.DRAFT_15, | ||
| Version.DRAFT_16, | ||
| Version.DRAFT_17, | ||
| Version.DRAFT_18, | ||
| Version.DRAFT_19, | ||
| Version.DRAFT_20, | ||
| ]; | ||
|
|
||
| const KINDS: RequestKind[] = ["subscribe", "fetch", "publish", "publish_namespace", "subscribe_namespace"]; | ||
|
|
||
| /** Every condition a rejection can carry. A new one belongs here. */ | ||
| const CONDITIONS: RequestCondition[] = [ | ||
| "internal", | ||
| "unauthorized", | ||
| "timeout", | ||
| "not_supported", | ||
| "does_not_exist", | ||
| "uninterested", | ||
| "malformed_track", | ||
| "going_away", | ||
| ]; | ||
|
|
||
| /** | ||
| * Every code we put in a rejection has to be one the negotiated draft registers for that | ||
| * request. The table is transcribed from the drafts (draft-14 section 13.1 through draft-20 | ||
| * section 15.11.2), not derived from the mapping, so a mistake in the mapping cannot talk | ||
| * the assertion into agreeing with it. | ||
| */ | ||
| function registered(kind: RequestKind, version: IetfVersion): number[] { | ||
| if (version === Version.DRAFT_14) { | ||
| switch (kind) { | ||
| case "subscribe": | ||
| return [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x10, 0x12]; | ||
| case "fetch": | ||
| return [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10, 0x12]; | ||
| case "publish": | ||
| return [0x0, 0x1, 0x2, 0x3, 0x4]; | ||
| case "publish_namespace": | ||
| return [0x0, 0x1, 0x2, 0x3, 0x4, 0x10, 0x12]; | ||
| case "subscribe_namespace": | ||
| return [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x10, 0x12]; | ||
| } | ||
| } | ||
| if (version === Version.DRAFT_15) return [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x10, 0x11, 0x12, 0x20, 0x30, 0x32, 0x33]; | ||
| if (version === Version.DRAFT_16) return [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x10, 0x11, 0x12, 0x19, 0x20, 0x30, 0x32]; | ||
| if (version === Version.DRAFT_17) { | ||
| return [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x9, 0x10, 0x11, 0x12, 0x19, 0x20, 0x30, 0x31, 0x32]; | ||
| } | ||
| return [ | ||
| 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x9, 0x10, 0x11, 0x12, 0x19, 0x20, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, | ||
| ]; | ||
| } | ||
|
|
||
| test("only registered codes reach the wire", () => { | ||
| for (const version of ALL) { | ||
| for (const kind of KINDS) { | ||
| for (const condition of CONDITIONS) { | ||
| const code = toRequestCode(condition, kind, version); | ||
| expect(registered(kind, version)).toContain(code); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("every emitted code round trips", () => { | ||
| for (const version of ALL) { | ||
| for (const kind of KINDS) { | ||
| for (const condition of CONDITIONS) { | ||
| const code = toRequestCode(condition, kind, version); | ||
| const decoded = fromRequestCode(code, kind, version) ?? "internal"; | ||
| expect(toRequestCode(decoded, kind, version)).toBe(code); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| /** | ||
| * Draft-14 numbers a missing track 0x4 and draft-15 moved it to 0x10, which is draft-14's | ||
| * MALFORMED_AUTH_TOKEN. Getting this backwards tells a peer its token is broken when the | ||
| * broadcast simply is not here, so it re-authenticates instead of waiting. | ||
| */ | ||
| test("a missing broadcast uses the draft's own number", () => { | ||
| for (const kind of ["subscribe", "fetch"] as const) { | ||
| expect(toRequestCode("does_not_exist", kind, Version.DRAFT_14)).toBe(0x4); | ||
| expect(fromRequestCode(0x4, kind, Version.DRAFT_14)).toBe("does_not_exist"); | ||
|
|
||
| for (const version of ALL.slice(1)) { | ||
| expect(toRequestCode("does_not_exist", kind, version)).toBe(0x10); | ||
| expect(fromRequestCode(0x10, kind, version)).toBe("does_not_exist"); | ||
| // 0x4 is MALFORMED_AUTH_TOKEN from draft-15 on, which is not ours to claim. | ||
| expect(fromRequestCode(0x4, kind, version)).toBeUndefined(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| /** | ||
| * Draft-14 gives 0x4 to UNINTERESTED on the requests that offer content and to | ||
| * TRACK_DOES_NOT_EXIST on the ones that ask for it, so the same integer on the same draft | ||
| * says two different things. | ||
| */ | ||
| test("draft-14 reads 0x4 per request", () => { | ||
| for (const kind of ["publish", "publish_namespace"] as const) { | ||
| expect(toRequestCode("uninterested", kind, Version.DRAFT_14)).toBe(0x4); | ||
| expect(fromRequestCode(0x4, kind, Version.DRAFT_14)).toBe("uninterested"); | ||
| // Those requests offer content, so "it is not here" is not a rejection they carry. | ||
| expect(toRequestCode("does_not_exist", kind, Version.DRAFT_14)).toBe(0x0); | ||
| } | ||
|
|
||
| expect(fromRequestCode(0x4, "subscribe_namespace", Version.DRAFT_14)).toBeUndefined(); | ||
| }); | ||
|
|
||
| /** GOING_AWAY arrived in draft-17; an earlier peer is told INTERNAL_ERROR instead. */ | ||
| test("going away only exists from draft-17", () => { | ||
| for (const kind of KINDS) { | ||
| for (const version of [Version.DRAFT_14, Version.DRAFT_15, Version.DRAFT_16] as const) { | ||
| expect(toRequestCode("going_away", kind, version)).toBe(0x0); | ||
| expect(fromRequestCode(0x6, kind, version)).toBeUndefined(); | ||
| } | ||
|
|
||
| for (const version of [Version.DRAFT_17, Version.DRAFT_18, Version.DRAFT_19, Version.DRAFT_20] as const) { | ||
| expect(toRequestCode("going_away", kind, version)).toBe(0x6); | ||
| expect(fromRequestCode(0x6, kind, version)).toBe("going_away"); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| /** | ||
| * Draft-18 took 0x4 for GOING_AWAY from UNKNOWN_OBJECT_STATUS, so the same integer means | ||
| * different things on two drafts we both negotiate. Sending it to draft-17 would claim the | ||
| * next object's status is unknowable; reading theirs as GOING_AWAY would start draining a | ||
| * session that is not going anywhere. | ||
| */ | ||
| test("stream reset codes are shared only where the draft agrees", () => { | ||
| for (const version of ALL) { | ||
| for (const code of [ | ||
| StreamCode.Internal, | ||
| StreamCode.Cancel, | ||
| StreamCode.DeliveryTimeout, | ||
| StreamCode.SessionClosed, | ||
| ]) { | ||
| expect(sharedStreamCode(code, version)).toBe(true); | ||
| } | ||
|
|
||
| // moq-lite's reserved 32-63 placeholders are not in this registry at all. | ||
| for (const code of [StreamCode.NotFound, StreamCode.Old, StreamCode.Evicted, StreamCode.FrameTooLarge]) { | ||
| expect(sharedStreamCode(code, version)).toBe(false); | ||
| } | ||
| } | ||
|
|
||
| for (const version of [Version.DRAFT_14, Version.DRAFT_15, Version.DRAFT_16, Version.DRAFT_17] as const) { | ||
| expect(sharedStreamCode(StreamCode.GoingAway, version)).toBe(false); | ||
| } | ||
| for (const version of [Version.DRAFT_18, Version.DRAFT_19, Version.DRAFT_20] as const) { | ||
| expect(sharedStreamCode(StreamCode.GoingAway, version)).toBe(true); | ||
| } | ||
|
|
||
| expect(sharedStreamCode(StreamCode.TooFarBehind, Version.DRAFT_16)).toBe(false); | ||
| expect(sharedStreamCode(StreamCode.TooFarBehind, Version.DRAFT_17)).toBe(true); | ||
| expect(sharedStreamCode(StreamCode.MalformedTrack, Version.DRAFT_15)).toBe(false); | ||
| expect(sharedStreamCode(StreamCode.MalformedTrack, Version.DRAFT_16)).toBe(true); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This maps every
TimeoutErrorto the IETFDELIVERY_TIMEOUTreset even when the timeout is unrelated to media delivery. For example, the SUBSCRIBE_OK timer inietf/subscriber.tsand the PUBLISH_NAMESPACE response timer inietf/publisher.tsboth abort their request streams with that error, so a silent peer is told that content missed a delivery deadline although only a control response timed out. Keep generic IETF timeouts asINTERNAL_ERROR, or introduce a distinct error used only when an actual delivery deadline expires.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The observation is right, but the fix is not this PR's.
toStreamCodemapped everyTimeoutErrortoDeliveryTimeoutbefore this branch, and Rust does the same throughFrom<&Error> for StreamError(Error::Timeout => StreamError::DeliveryTimeout). Making the IETF branch alone say INTERNAL_ERROR would leave the two languages and the two wires disagreeing about the same condition, which is exactly what this PR set out to stop.The real question is whether "you never answered" deserves a condition separate from "your content was late", in both languages, on both wires. Filed as quest/m1/control-timeout-code.md in 104d644, including the option of dropping the code rather than adding one.
(written by claude-opus-5)