Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3239,18 +3239,49 @@ 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();
client.close();
}
});

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
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 13 additions & 3 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 27 additions & 1 deletion js/net/src/lite/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 });
Expand Down
2 changes: 2 additions & 0 deletions js/net/src/lite/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions js/net/src/resume.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<Signal<unknown>, Set<object>>();
const changed = Signal.prototype.changed;
const spy = spyOn(Signal.prototype, "changed").mockImplementation(function (
this: Signal<unknown>,
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];
Expand Down
6 changes: 4 additions & 2 deletions js/net/src/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 0 additions & 30 deletions quest/m1/precut-fixes/js-net-leaks.md

This file was deleted.