From d727db64d999d6fe66030d2182fd4a83a0f63c75 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 17:33:25 -0800 Subject: [PATCH 1/2] chore(quest): claim quest/m1/precut-fixes/js-net-leaks.md Quest-Claim: 9212c53e3061b71a From 4afcc970d122a49aaf51231ce7ac3d7c20c8d270 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 17:45:29 -0800 Subject: [PATCH 2/2] fix(net): js/net leaks nothing on a long listing or an early subscription end - Front's listing races the event against the wait flat, so a won event leaves no registrations behind per event. - The lite publisher closes a group held for the upstream START when the subscription ends first. - The ietf publisher sends END_OF_TRACK only when the subscription's range reaches the track's end. Co-Authored-By: Claude Sonnet 5.5 --- js/net/src/ietf/publisher.test.ts | 46 +++++++++++++++++++----- js/net/src/ietf/publisher.ts | 16 +++++++-- js/net/src/lite/publisher.test.ts | 28 ++++++++++++++- js/net/src/lite/publisher.ts | 2 ++ js/net/src/resume.test.ts | 50 +++++++++++++++++++++++++++ js/net/src/resume.ts | 6 ++-- quest/m1/precut-fixes/js-net-leaks.md | 30 ---------------- 7 files changed, 134 insertions(+), 44 deletions(-) delete mode 100644 quest/m1/precut-fixes/js-net-leaks.md diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 6672b116e9f..b3cde83b131 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -3239,11 +3239,9 @@ test("draft-22: a clean close past a bounded filter's end still sends PUBLISH_DO expect(await client.reader.u53()).toBe(PublishDone.id); const done = await PublishDone.decode(client.reader, V22); expect(done.statusCode).toBe(TRACK_ENDED_STATUS); - expect(done.streamCount).toBe(2n); - - // Only the in-range group was ever served; the other stream marks the track's end. - const end = await nextUni(fx.uni); - expect(await readEndOfTrack(end)).toBe(2n); + // Only the in-range group was ever served: the range stopped short of the track's end, + // so no END_OF_TRACK claims it. + expect(done.streamCount).toBe(1n); expect(await opens(fx.uni.read(), clock)).toBe(false); } finally { fx.close(); @@ -3251,6 +3249,39 @@ test("draft-22: a clean close past a bounded filter's end still sends PUBLISH_DO } }); +test("draft-22: a bounded filter that reaches the track's end still sends END_OF_TRACK", async () => { + const fx = fixture(); + const track = fx.broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null }); + writeGroup(track, 1); // group 0 + writeGroup(track, 1); // group 1, the filter's last and the track's + + const { client } = await runSubscribe( + fx, + new Subscribe({ + requestId: 7n, + trackNamespace: Path.from("test"), + trackName: "video", + subscriberPriority: 0, + filter: { kind: "absolute", startGroup: 0n, startObject: 0n, endGroup: 1n }, + }), + ); + + try { + for (let i = 0; i < 2; i++) await readGroup(await nextUni(fx.uni)); + track.finish(); + track.close(); + + expect(await client.reader.u53()).toBe(PublishDone.id); + const done = await PublishDone.decode(client.reader, V22); + expect(done.statusCode).toBe(TRACK_ENDED_STATUS); + expect(done.streamCount).toBe(3n); + expect(await readEndOfTrack(await nextUni(fx.uni))).toBe(2n); + } finally { + fx.close(); + client.close(); + } +}); + /** * A broadcast served through `requested()` resolves its track on demand, and a dynamic serve * is deliberately one request per peer subscription. Resolving the track again to read the @@ -4275,9 +4306,8 @@ test("draft-22: a range update replaces the live subscription bounds", async () expect(await client.reader.u53()).toBe(PublishDone.id); const done = await PublishDone.decode(client.reader, fx.version); expect(done.statusCode).toBe(TRACK_ENDED_STATUS); - expect(done.streamCount).toBe(3n); - const end = await nextUni(fx.uni); - expect(await readEndOfTrack(end)).toBe(5n); + // The range ends at group 3, short of the track's end, so no END_OF_TRACK. + expect(done.streamCount).toBe(2n); } finally { client.close(); track.finish(); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 09d0f8040ba..85f3104036a 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -22,7 +22,15 @@ import { } from "../error.ts"; import type * as group from "../group.ts"; import type { Hop, WireRoute } from "../hop.ts"; -import { datagramTrackInfo, firstEligible, hiddenBelow, hooks, presented, type ReadGroupFrame } from "../internal.ts"; +import { + datagramTrackInfo, + finalKey, + firstEligible, + hiddenBelow, + hooks, + presented, + type ReadGroupFrame, +} from "../internal.ts"; import type { Consumer as OriginConsumer } from "../origin.ts"; import * as Path from "../path.ts"; import { sendPayload } from "../payload.ts"; @@ -1063,9 +1071,11 @@ export class Publisher { } // PUBLISH_DONE has no end location: an END_OF_TRACK object is what tells the - // subscriber where the track ended. + // subscriber where the track ended. A range that stopped short of the end never saw + // it, and claiming it would end the subscriber's track early. const final = hooks.subscriberEnd(track); - if (!datagramTrack && ended && !publishError && final !== undefined && !streams.terminalSent) { + const reached = final !== undefined && (range.end === undefined || range.end.group + 1n >= finalKey(final)); + if (!datagramTrack && ended && !publishError && final !== undefined && reached && !streams.terminalSent) { await this.#runEndOfTrack({ requestId: msg.requestId, final, diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index cc6ae5c17be..07d1ffd3bad 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -5,7 +5,7 @@ import { Signal } from "@moq/signals"; import type { Grant } from "../auth.ts"; import { Producer as BroadcastProducer } from "../broadcast.ts"; import { fromTransport, ProtocolViolation, SessionCode, StreamCode, StreamError, toStreamCode } from "../error.ts"; -import type { Producer as GroupProducer } from "../group.ts"; +import { Consumer as GroupConsumer, type Producer as GroupProducer } from "../group.ts"; import { randomHop } from "../hop.ts"; import { hooks, NO_MAX_DELAY, staleHole } from "../internal.ts"; import { createMockTransportPair, manual, settlesWithin, textFrame } from "../mock.ts"; @@ -1700,6 +1700,32 @@ for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { } }); + // A subscription that ends while a group waits for the START has no one to read it, so the + // held group is closed, not left for a relay to keep reading upstream. + test(`lite ${version}: a subscription ending before the upstream START closes the group it held`, async () => { + const sub = await servedSubscription({ version, startGroup: 2n }); + const closed: bigint[] = []; + const close = GroupConsumer.prototype.close; + const spy = spyOn(GroupConsumer.prototype, "close").mockImplementation(function (this: GroupConsumer) { + closed.push(this.start); + close.call(this); + }); + try { + hooks.receiveTrack(sub.track); + hooks.startTrack(sub.track, { requested: 2n }); + sub.serve(6n); + await flush(); + expect(closed).not.toContain(6n); + + sub.client.close(); + await sub.serving; + expect(closed).toContain(6n); + } finally { + spy.mockRestore(); + await sub.close(); + } + }); + // A track that closes never gets its START, so the held group must not wait for it. test(`lite ${version}: a closed source releases the START it was waiting for`, async () => { const sub = await servedSubscription({ version, startGroup: 2n }); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 71dc9b0bc6c..bedbb056a32 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1865,6 +1865,8 @@ export class Publisher { }); } } finally { + // A group held for the upstream START has no one left to read it. + first?.group.close(); if (!finished) unsubscribe(); // A lite-07 subscriber FINs once it read the finished track's tail, or once it wants // nothing more, even while a reply is still being written. Either way the groups still diff --git a/js/net/src/resume.test.ts b/js/net/src/resume.test.ts index 09a7b97f521..5f27cc6ce0f 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -1,6 +1,7 @@ import { expect, spyOn, test } from "bun:test"; import { Duration, yieldNow } from "@moq/clock"; import { Manual } from "@moq/clock/test"; +import { Signal } from "@moq/signals"; import { Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts"; import { NotFound, SessionCode, SessionError, StreamCode, StreamError } from "./error.ts"; import { type Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; @@ -3789,6 +3790,55 @@ test("a concluded front's listing keeps its closed source's finishes", async () } }); +// Each source event wakes the listing's wait, which must drop the registrations it made on the +// front's signals instead of leaving a set behind per event. +test("a front's listing leaves no registration behind per event", async () => { + const front = new Front(); + const a = new BroadcastProducer(); + const from = a.consume(); + const answer = wireOf(from).lister(); + answer.synced(); + front.serve(from); + const tracks = front.consumer.tracks(); + + // Registrations still pending, by signal: one-shot, so one that fired or was disposed is gone. + const live = new Map, Set>(); + const changed = Signal.prototype.changed; + const spy = spyOn(Signal.prototype, "changed").mockImplementation(function ( + this: Signal, + fn?: (value: unknown) => void, + ) { + const original = changed.bind(this); + if (!fn) return original(); + const entry = {}; + const pending = live.get(this) ?? new Set(); + live.set(this, pending); + pending.add(entry); + const dispose = original((value: unknown) => { + pending.delete(entry); + fn(value); + }); + return () => { + pending.delete(entry); + dispose(); + }; + } as typeof changed); + try { + expect(await tracks.next()).toMatchObject({ kind: "synced" }); + for (let i = 0; i < 20; i++) { + answer.add(`track${i}`, { info: INFO }); + expect(await tracks.next()).toMatchObject({ kind: "added", name: `track${i}` }); + } + expect(Math.max(0, ...[...live.values()].map((pending) => pending.size))).toBeLessThanOrEqual(2); + } finally { + spy.mockRestore(); + tracks.close(); + answer.close(); + front.consumer.close(); + a.close(); + } +}); + test("only delivery codes are a route failing", () => { // Mirrors `only_delivery_codes_are_a_route_failing` in rs/moq-net/src/model/resume.rs. const delivery = [0x0, 0x1, 0x3, 0x4, 0x31, 0x36]; diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 16fe67f5b60..a20d591635f 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -1953,8 +1953,10 @@ export class Front { continue; } listing.next ??= read(reader); - const result = await race([listing.next, wait().then(() => undefined)]); - if (!result) continue; + // One flat race: nested, the wait's registrations stay pending whenever the event wins. + // What a change or the close settles with is no event, so the loop looks again. + const result = await race([listing.next, Signal.race(lister.wanted, this.#changed), closed]); + if (typeof result !== "object" || result === null) continue; listing.next = undefined; // The source was replaced as its answer arrived: the new source's listing supersedes it. if (listing.source !== this.#source) continue; diff --git a/quest/m1/precut-fixes/js-net-leaks.md b/quest/m1/precut-fixes/js-net-leaks.md deleted file mode 100644 index 64d3ced46cb..00000000000 --- a/quest/m1/precut-fixes/js-net-leaks.md +++ /dev/null @@ -1,30 +0,0 @@ -# [S] js/net leaks nothing on a long listing or an early subscription end - -## Goal - -`@moq/net` holds no listener, resolver, or group past its use: a front's -track listing does not grow per TRACKS event, a lite publisher closes a held -first group when its subscription ends first, and the ietf publisher sends -END_OF_TRACK only when the subscription's range reaches the track's end, as -Rust does. - -## Plan - -Found by the 2026-11-17 pre-cut correctness audit, from source; reproduce -first. - -- `js/net/src/resume.ts` `Front.#list`: `race([listing.next, wait()...])` - leaves the inner race's three registrations pending each time - `listing.next` wins. The same pattern is in `lite/subscriber.ts` - (`reoffers.changed()`). Race the change waits directly. From #6158 and - #8476. -- `js/net/src/lite/publisher.ts`: `first = { group, largest }` is held while - the source start is pending, and the early exits and the `finally` never - close it, so a relayed filled group keeps reading upstream for nobody. Close - it in the `finally`. From #6206. -- `js/net/src/ietf/publisher.ts`: `#runEndOfTrack` is sent unclipped to a - bounded subscription's range; port Rust's `reached` check from - `TrackServe::end` ([moq-dev/moq#5283](https://github.com/moq-dev/moq/pull/5283)). - On `release` too. - -Public API: none. Wire: none.