diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 88ce011cb7b..41c0714bf03 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -12,7 +12,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"; @@ -1051,25 +1051,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..cd0a1e35e41 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,17 @@ 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; // 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 d6542004e4b..926c2707e62 100644 --- a/quest/m0/test-flakes-2/README.md +++ b/quest/m0/test-flakes-2/README.md @@ -204,6 +204,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 -- [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 - [moq-relay's forged-dial test waits for bob's endpoint](/quest/m0/test-flakes-2/forged-dial-announce.md) - `a_forged_dial_never_connects` requests bob's endpoint only after its announce reaches mallory, and its `#[ignore]` is removed - [moq-uring's qlog accounting test waits on the accounting](/quest/m0/test-flakes-2/qlog-staging-spin.md) - `an_open_trace_accounts_for_staging_and_close` waits for the accounting, not 10,000 spins 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.