From 9a04fa59139a5f3e23c972b63700c288c3335d67 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 08:14:32 -0800 Subject: [PATCH 1/2] chore(quest): claim quest/m0/test-flakes-2/datagram-end-close.md Quest-Claim: 87c4c5e73eb3fc53 From 8ffcffcef3b8929348f040ca7b3d7feda96a46b9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 08:31:25 -0800 Subject: [PATCH 2/2] fix(net): a lite-05/06 datagram track keeps its accepted end through a session failure A peer's protocol violation, such as a duplicate SETUP, fails the session without the drain a close gets. A datagram track whose SUBSCRIBE_FINISH arrived but whose FIN had not yet finalized the subscription aborted with the session's error. Subscriber.close now finishes it whenever the stream track's settled end survives the abort, as Rust ends shared datagrams off the stream track's finish. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/datagram-tracks.test.ts | 71 +++++++++++++++++- js/net/src/lite/subscriber.ts | 5 +- quest/m0/test-flakes-2/README.md | 6 +- quest/m0/test-flakes-2/datagram-end-close.md | 79 -------------------- quest/m1/ts-reliability/pcr-jump-flag.md | 2 +- 5 files changed, 80 insertions(+), 83 deletions(-) delete mode 100644 quest/m0/test-flakes-2/datagram-end-close.md diff --git a/js/net/src/lite/datagram-tracks.test.ts b/js/net/src/lite/datagram-tracks.test.ts index 22df152a2ea..4b874b14726 100644 --- a/js/net/src/lite/datagram-tracks.test.ts +++ b/js/net/src/lite/datagram-tracks.test.ts @@ -3,14 +3,18 @@ import { yieldNow } from "@moq/clock"; import { hostTime } from "@moq/clock/test"; import { Signal } from "@moq/signals"; import { accept, connect } from "../connection/index.ts"; +import { SessionCode } from "../error.ts"; +import { hooks } from "../internal.ts"; import { createMockTransportPair, type MockTransport } from "../mock.ts"; import { Producer as Origin } from "../origin.ts"; import * as Path from "../path.ts"; import { sendOrder } from "../priority.ts"; +import { Writer } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; import { Budget } from "../util/yield.ts"; import { wireOf } from "../wire.ts"; -import { ALPN_04, ALPN_05, ALPN_06, ALPN_07_WIP } from "./version.ts"; +import { DataType } from "./stream.ts"; +import { ALPN_04, ALPN_05, ALPN_06, ALPN_07_WIP, Version } from "./version.ts"; const url = new URL("https://localhost/datagrams"); const path = Path.from("live"); @@ -492,6 +496,71 @@ for (const alpn of [ALPN_05, ALPN_06, ALPN_07_WIP]) { }); } +// A peer's protocol violation fails the session at once, without the drain a close gets. A lite-05/06 +// datagram track whose SUBSCRIBE_FINISH was accepted keeps that end, even while its FIN lags. +for (const [alpn, version] of [ + [ALPN_05, Version.DRAFT_05], + [ALPN_06, Version.DRAFT_06], +] as const) { + test(`${alpn}: a duplicate SETUP after a datagram track's end keeps the end`, async () => { + const source = new Origin(); + const publication = source.route(path, { hidden: true }); + const datagrams = publication.broadcast.createDatagrams("audio"); + publication.announce(); + const pair = createMockTransportPair(alpn); + // Once armed, the publisher's FINs on the request streams it accepts never leave. + let holding = false; + const holdFin = (writable: WritableStream) => { + const writer = writable.getWriter(); + return new WritableStream({ + write: (bytes) => writer.write(bytes), + abort: (reason) => writer.abort(reason), + close: () => (holding ? new Promise(() => {}) : writer.close()), + }); + }; + Object.defineProperty(pair.server, "incomingBidirectionalStreams", { + value: pair.server.incomingBidirectionalStreams.pipeThrough( + new TransformStream({ + transform: (bidi, controller) => + controller.enqueue({ readable: bidi.readable, writable: holdFin(bidi.writable) }), + }), + ), + }); + const [client, server] = await Promise.all([ + connect({ url, transport: pair.client }), + accept({ url, transport: pair.server, publish: source.consume() }), + ]); + const remote = wireOf(client).consume(path); + const reader = remote.datagrams("audio"); + reader.setPriority(5); + const accepted = Promise.withResolvers(); + const endTrack = hooks.endTrack; + const spy = spyOn(hooks, "endTrack").mockImplementation((...args) => { + endTrack(...args); + accepted.resolve(); + }); + try { + await reader.info(); + while (datagrams.priority.peek() !== 5) await datagrams.priority.changed(); + holding = true; + datagrams.finish(); + await accepted.promise; + const writer = await Writer.open(pair.server, { version }); + // The client claims the Setup Stream on its type, so the type alone is the duplicate. + await writer.u53(DataType.Setup); + expect((await pair.client.closed).closeCode).toBe(SessionCode.ProtocolViolation); + expect(await reader.recv()).toBeUndefined(); + } finally { + spy.mockRestore(); + reader.close(); + remote.close(); + client.abort(); + server.abort(); + source.close(); + } + }); +} + // Holds back what the publisher writes on the next request stream it accepts once armed, until // released: that subscription's answer stays outstanding while everything else flows. function holdNextAnswer(transport: MockTransport): { arm(): void; release(): void } { diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index c5efe95780d..b9779958995 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2693,9 +2693,12 @@ export class Subscriber { close(err: Error = new SessionError(SessionCode.Cancel)) { this.#closed.abort(err); - for (const { track, finished } of this.#subscribes.values()) { + for (const { track, finished, end, datagrams } of this.#subscribes.values()) { if (finished) track.finish(); track.abort(err); + // Lite-05/06: the stream track's end is its datagrams' end. An abort after the declared + // end settled ends it cleanly, even while the FIN that would finalize it is in flight. + if (datagrams && end !== undefined && hooks.trackReleased(track).peek() === null) datagrams.ended = true; } this.#subscribes.clear(); diff --git a/quest/m0/test-flakes-2/README.md b/quest/m0/test-flakes-2/README.md index f448acb0bc2..b7cc766a6a8 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -139,6 +139,11 @@ publisher's write budget yielded under load while writing SUBSCRIBE_FINISH, past the case's microtask drain. [moq-dev/moq#8000](https://github.com/moq-dev/moq/pull/8000) stubbed that budget, merging after the quarantine was written. The case was re-enabled in [moq-dev/moq#8110](https://github.com/moq-dev/moq/pull/8110). +Its follow-up: a peer's protocol violation, such as a duplicate SETUP, fails the +session without the drain a close gets, so a lite-05/06 datagram track whose +SUBSCRIBE_FINISH arrived before its FIN aborted with the session's error. Its +datagrams now finish whenever the stream track's settled end survives the abort, +fixed by [moq-dev/moq#8157](https://github.com/moq-dev/moq/pull/8157). `sh/dispatch-query.test.py`'s terminate test was never load-bound. A TERM landing just after the dispatcher's `wait -n` returned left Bash marking the @@ -153,6 +158,5 @@ Public API: none. Wire: none. ## Required - [Fallback playback](/quest/m0/test-flakes-2/ietf22-fallback.md) - a browser viewer follows a fallback within the media lane's bound without dropping its session -- [The datagram-end close follow-up](/quest/m0/test-flakes-2/datagram-end-close.md) - investigate whether duplicate SETUP can override an accepted lite-06 SUBSCRIBE_END during shutdown - [moq-transcode's idle-rung cancel test survives load](/quest/m0/test-flakes-2/transcode-idle-rung-cancel.md) - `cancelling_an_idle_rung_reports_cancel` stops seeing `serve` return before its abort lands - [moq-gst's sink-restart pad test survives load](/quest/m0/test-flakes-2/gst-sink-restart-pads.md) - `a_moqsink_restart_keeps_pads_when_caps_arrive_at_different_times` keeps its pads under load diff --git a/quest/m0/test-flakes-2/datagram-end-close.md b/quest/m0/test-flakes-2/datagram-end-close.md deleted file mode 100644 index 656e632d966..00000000000 --- a/quest/m0/test-flakes-2/datagram-end-close.md +++ /dev/null @@ -1,79 +0,0 @@ -# [S] The datagram-end close test survives load - -## Goal - -`js/net/src/lite/datagram-tracks.test.ts`'s -`a session close right after a datagram track's end keeps the end` passes -reliably in loaded runs, fixed at its cause under the -[epic](/quest/m0/test-flakes-2/README.md)'s rules, and its `test.skip` is -removed. - -## Plan - -The original end/close case was re-enabled by PR #8110 after PR #8000 -stubbed the publisher write budget. The additional duplicate-SETUP -investigation below remains open. - -Added 2026-11-06 (maintainer's standing rule that flakes join this epic). -[moq-dev/moq#7986](https://github.com/moq-dev/moq/pull/7986)'s Check failed -in this case, whose diff touches only `bench/`, `justfile`, and quest prose. -It reproduces on unchanged `origin/main`, so the flake came from -[moq-dev/moq#7951](https://github.com/moq-dev/moq/pull/7951)'s new case; -the PR quarantines it with every assertion kept. - -Reproduction 2026-11-06 on this head and on `git archive origin/main`, from -`js/net`, with six CPU loops on four cores: - -- `bun test src/lite/datagram-tracks.test.ts` failed 3 of 4 loaded runs here - and 3 of 3 on `origin/main`; only the lite-06 draft failed, though the - case loops over lite-05, lite-06, and lite-07-wip. -- The same filter `-t "a session close right after a datagram track's end - keeps the end"` passed 10 of 10 idle runs and 8 of 8 loaded runs, so the - whole file (its 37 other cases) is what makes the window miss. -- The failure rejects with `Session: remote error: 0` at `error.ts`'s - `closeError` from `connection.ts`'s `abort`, i.e. `reader.recv()` - rejected instead of ending. - -Likely cause, unverified: the case finishes the datagram track and then -drains only microtasks (`for (i < 1000) await Promise.resolve()`) before -`client.abort()`, assuming "the mock delivers the end in microtasks, all -within this host task". The FIN handler awaits `#datagramsRouted`, which -starts with a host-task `yieldNow`; `Subscriber.close` finishes only -datagram entries whose `ended` is already set and aborts the rest. Under -load the FIN is not read before the abort, so `ended` is still unset and -the producer aborts. Check the delivery point before choosing between -waiting on the end's arrival and yielding to the host in the drain. - -Reproduce first: temporarily remove the case's `test.skip`, then run the -whole file beside CPU loops, as above. Keep the failing output before making -other changes to the test or the end path. Restore the skip until the fix. - -### Additional lite-06 investigation - -Planned 2026-11-06 under the maintainer's rule that a known flaky test is -quarantined immediately and its investigation planned. `Check` failed on this -case in -[run 42200239263](https://github.com/moq-dev/moq/actions/runs/42200239263/job/127833434287) -on the unrelated [moq-dev/moq#7981](https://github.com/moq-dev/moq/pull/7981), -where it was the only failing case in `js/net`. The unchanged module reproduced -it once in eight runs: `cd js/net && bun test src/lite/datagram-tracks.test.ts`. - -The case spins 1000 microtasks after `datagrams.finish()`, then aborts the -client, expecting the reader's `recv()` to return `undefined` because the end -already arrived. On failure the reader surfaces the session close instead -(`Session: remote error: 0`). The spin assumes the mock delivers the end in -microtasks, but the subscriber's response loop yields through `@moq/clock`'s -`yieldNow` (a host task) once its budget's 4 ms slice expires, which a loaded -runner can reach before the end is read. Whether that is only the fixture's -wait or a real gap between receiving the end and delivering it is not -established: diagnose before changing the wait or production code, and do not -keep the case passing by loosening the assertion. Base subsequently quarantined -all three protocol versions, as described above. - -Decided 2026-11-06 (maintainer, /quest-unblock): investigate the additional -[CodeRabbit finding](https://github.com/moq-dev/moq/pull/7981#discussion_r4491539836) -here: an accepted lite-06 `SUBSCRIBE_END` followed by duplicate SETUP can be -overridden by session failure before subscription finalization. Reproduce this -ordering deterministically and preserve the accepted end during shutdown. - -Public API: none. Wire: none. diff --git a/quest/m1/ts-reliability/pcr-jump-flag.md b/quest/m1/ts-reliability/pcr-jump-flag.md index 382181145c8..53893ff678e 100644 --- a/quest/m1/ts-reliability/pcr-jump-flag.md +++ b/quest/m1/ts-reliability/pcr-jump-flag.md @@ -8,6 +8,6 @@ Validate actual-PCR-jump flags in the remaining TS recovery interop coverage. Decided 2026-11-06 (maintainer, /quest-unblock): ✅ Continue the actual-PCR-jump approach. Not chosen: rethink; drop the PR. -Decided 2026-11-06 (maintainer, /quest-unblock): ✅ Reproduce and fix the Rust `with_delay` schedule replacement in this PR. Hand the unrelated JS decoded-end/session-failure finding to [datagram-end-close](/quest/m0/test-flakes-2/datagram-end-close.md). Not chosen: fixing both findings here. +Decided 2026-11-06 (maintainer, /quest-unblock): ✅ Reproduce and fix the Rust `with_delay` schedule replacement in this PR. Hand the unrelated JS decoded-end/session-failure finding to the datagram-end close quest, fixed by [moq-dev/moq#8157](https://github.com/moq-dev/moq/pull/8157). Not chosen: fixing both findings here. Decided 2026-11-06 (maintainer, /quest-unblock): ✅ Construction-only delay supersedes runtime updates. TS Export and Release take delay when constructed, so retained state never changes its delay basis. Not chosen: full runtime retiming; landing without the fix.