From d50350be0eed15db3e81070bb7f729350769ce96 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 14:36:00 -0800 Subject: [PATCH 1/4] chore(quest): claim quest/m0/test-flakes-2/lite07-reply-fin.md Quest-Claim: f88fe2bbf58f1203 From 98c20a1f9b7a2d80f96bc2aceac01cf3322288f9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 14:48:39 -0800 Subject: [PATCH 2/4] fix(net): a write yields before handing its bytes over A lite-07 subscriber FIN that landed while a reply write yielded on the shared write budget reset the stream with Cancel, though the reply's bytes had already gone out. Yielding before the handover means a write still pending is one the transport has not taken, for every Writer caller. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 37 ++++++++++++---------- js/net/src/stream.ts | 12 ++++--- quest/m0/test-flakes-2/README.md | 1 - quest/m0/test-flakes-2/lite07-reply-fin.md | 34 -------------------- 4 files changed, 28 insertions(+), 56 deletions(-) delete mode 100644 quest/m0/test-flakes-2/lite07-reply-fin.md diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 74e53073fff..c0602b3dc2c 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -13,7 +13,7 @@ import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; import { sendOrder } from "../priority.ts"; import { Front } from "../resume.ts"; -import { Reader, Stream, Writer } from "../stream.ts"; +import { Reader, Stream, Writer, writeBudget } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; import { Subscriber as TrackSubscriber } from "../track.ts"; import { Budget } from "../util/yield.ts"; @@ -1056,25 +1056,28 @@ test("lite-07 refuses an update retiring an unknown Range ID", async () => { // A bounded window that resolves empty has no group to answer it, so the publisher answers it // at once. The stream stays open for an update until the subscriber FINs, and the publisher -// FINs in reply. -// Quarantined: a subscriber FIN during the reply write's budget yield resets the stream. -// Investigation: quest/m0/test-flakes-2/lite07-reply-fin.md. -test.skip("lite-07 answers a bounded window that resolves empty and FINs in reply", async () => { +// FINs in reply, even when the FIN lands while the reply's write yields to the host. +test("lite-07 answers a bounded window that resolves empty and FINs in reply", async () => { const version = Version.DRAFT_07; - const sub = await servedSubscription({ - version, - prefill: [0n, 1n, 2n, 3n, 4n, 5n], - maxAge: Milli(3_600_000), - ranges: [{ start: { relative: 0n }, end: { absolute: 3n } }], - }); + const yielding = spyOn(writeBudget, "poll").mockImplementation(() => yieldNow()); try { - const resp = await decodeSubscribeResponse(sub.client.reader, version); - if (!("start" in resp)) throw new Error("expected SUBSCRIBE_START"); - expect(resp.start.starts).toEqual([undefined]); - sub.client.writer.close(); - expect(await decodeSubscribeResponseMaybe(sub.client.reader, version)).toBeUndefined(); + const sub = await servedSubscription({ + version, + prefill: [0n, 1n, 2n, 3n, 4n, 5n], + maxAge: Milli(3_600_000), + ranges: [{ start: { relative: 0n }, end: { absolute: 3n } }], + }); + try { + const resp = await decodeSubscribeResponse(sub.client.reader, version); + if (!("start" in resp)) throw new Error("expected SUBSCRIBE_START"); + expect(resp.start.starts).toEqual([undefined]); + sub.client.writer.close(); + expect(await decodeSubscribeResponseMaybe(sub.client.reader, version)).toBeUndefined(); + } finally { + await sub.close(); + } } finally { - await sub.close(); + yielding.mockRestore(); } }); diff --git a/js/net/src/stream.ts b/js/net/src/stream.ts index 05d1068f69a..be8aa4533b1 100644 --- a/js/net/src/stream.ts +++ b/js/net/src/stream.ts @@ -20,8 +20,10 @@ import { } from "./util/varint.ts"; import { Budget } from "./util/yield.ts"; -// Sharing the slice across writers bounds concurrent groups and subscriber fanout too. -const writeBudget = new Budget(); +/** Sharing the slice across writers bounds concurrent groups and subscriber fanout too. + * @internal + */ +export const writeBudget = new Budget(); /** Stream credit alone lets a peer keep thousands of streams ready, each accepted in a * microtask, so accept loops share a slice that lets the host's other tasks run. @@ -809,14 +811,16 @@ export class Writer implements OpenStream { } async write(v: Uint8Array) { + // Yield before handing the bytes over, so a write still pending is one the transport has + // not taken: a caller racing it against the peer's end never abandons bytes already sent. + const pause = writeBudget.poll(); + if (pause !== undefined) await pause; this.#written += v.byteLength; // Mirrors Reader.#fill: every write funnels through here, so a STOP_SENDING from the // peer surfaces as a typed code rather than the transport's own error shape. await this.#writer.write(v).catch((err: unknown) => { throw fromTransport(err, { version: asIetf(this.version) }); }); - const pause = writeBudget.poll(); - if (pause !== undefined) await pause; } async string(str: string) { diff --git a/quest/m0/test-flakes-2/README.md b/quest/m0/test-flakes-2/README.md index c428ee10b0c..aa9d978f151 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -171,4 +171,3 @@ Public API: none. Wire: none. - [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 - [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 - [A lite-07 relative start resolves to the publisher's start every time](/quest/m0/test-flakes-2/lite07-relative-start.md) - `js/net`'s frameless-newest-group integration test stops reading a start of 0, and its `test.skip` is removed -- [A lite-07 reply FINs instead of resetting when the subscriber FINs mid-write](/quest/m0/test-flakes-2/lite07-reply-fin.md) - `js/net`'s empty-window reply test passes under `--rerun-each 200`, fixed in the publisher diff --git a/quest/m0/test-flakes-2/lite07-reply-fin.md b/quest/m0/test-flakes-2/lite07-reply-fin.md deleted file mode 100644 index 66e7450c50e..00000000000 --- a/quest/m0/test-flakes-2/lite07-reply-fin.md +++ /dev/null @@ -1,34 +0,0 @@ -# [S] A lite-07 reply FINs instead of resetting when the subscriber FINs mid-write - -## Goal - -When a lite-07 subscriber FINs its subscription stream while the publisher's -reply write is yielding on the shared write budget, the publisher still FINs -in reply instead of resetting with Cancel. `js/net/src/lite/publisher.test.ts`'s -"lite-07 answers a bounded window that resolves empty and FINs in reply" -passes under `--rerun-each 200`, and its `test.skip` is removed. - -## Plan - -Planned 2026-11-07 (maintainer, /quest-complete) from -[moq-dev/moq#7670](https://github.com/moq-dev/moq/pull/7670)'s validation: -the test fails on main 12 times in 200 under `bun test --rerun-each 200`. It -is quarantined with `test.skip` until this quest fixes it, per AGENTS.md. - -The bug is in the product, not the test. `Writer.write` -(`js/net/src/stream.ts`) yields on the shared write budget after its bytes -are already queued. During that yield, `SubscriptionControls.response` -(`js/net/src/lite/publisher.ts`) takes the subscriber's clean FIN as -overriding the blocked write, so the publisher resets the stream. -[moq-dev/moq#8047](https://github.com/moq-dev/moq/pull/8047) fixed the same -yield's race for opened group streams only. - -Candidate fixes: - -- a clean FIN no longer overrides a reply write, and only an error does; -- `Writer.write` yields before handing its bytes over, not after. - -Prefer the one that also covers every other `Writer.write` caller whose peer -can FIN during the yield. Check whether the Rust publisher has the same -ordering. Prove the fix with a test that forces the yield, not with repeated -runs. From ca121933ffcb805fb15a4d510f70da27ec825475 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 15:57:57 -0800 Subject: [PATCH 3/4] fix(net): count a write's bytes before its budget yield Co-Authored-By: Claude Opus 5.5 --- js/net/src/stream.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/js/net/src/stream.ts b/js/net/src/stream.ts index be8aa4533b1..cd0a1e35e41 100644 --- a/js/net/src/stream.ts +++ b/js/net/src/stream.ts @@ -813,9 +813,10 @@ export class Writer implements OpenStream { async write(v: Uint8Array) { // Yield before handing the bytes over, so a write still pending is one the transport has // not taken: a caller racing it against the peer's end never abandons bytes already sent. + // Count it synchronously, so acknowledged() never treats a pending write as acknowledged. + this.#written += v.byteLength; const pause = writeBudget.poll(); if (pause !== undefined) await pause; - this.#written += v.byteLength; // Mirrors Reader.#fill: every write funnels through here, so a STOP_SENDING from the // peer surfaces as a typed code rather than the transport's own error shape. await this.#writer.write(v).catch((err: unknown) => { From 3e6f887ba075d7d1e22ee2044ee2ac1e49d507c7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 18:11:30 -0800 Subject: [PATCH 4/4] test(net): the ticker test disposes its last subscriber Co-Authored-By: Claude Opus 5.5 --- js/net/src/connection/ticker.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/net/src/connection/ticker.test.ts b/js/net/src/connection/ticker.test.ts index c87600b4f76..11deb27cf50 100644 --- a/js/net/src/connection/ticker.test.ts +++ b/js/net/src/connection/ticker.test.ts @@ -25,9 +25,11 @@ test("subscribers share one tick and the loop stops with the last", async () => expect(ticks).toEqual(["a", "b", "b"]); // A stopped loop restarts for a later subscriber, rather than leaving the page unsampled. - tick(period, () => ticks.push("c")); + const c = tick(period, () => ticks.push("c")); await clock.advance(period); expect(ticks).toEqual(["a", "b", "b", "c"]); + // The ticker is page-wide: a leaked subscriber keeps its loop asleep on this test's clock. + c(); } finally { installed.close(); }