diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 2ce25ec94e..81dc71b81c 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -453,6 +453,11 @@ export const hooks: { * so it names no object; assigned by `track.ts`. */ reportedGroup: (producer: Producer, group: number) => void; + /** + * How long a session's tail waits for a late group: the subscribers' max delay, bounded by + * the track's max age; assigned by `track.ts`. + */ + tailGrace: (producer: Producer) => Milli; /** Snapshot the latest group sequence for wire positioning; assigned by `track.ts`. */ latestSequence: (subscriber: Subscriber) => number | undefined; /** @@ -671,6 +676,9 @@ export const hooks: { reportedGroup: () => { throw new Error("track.ts not loaded"); }, + tailGrace: () => { + throw new Error("track.ts not loaded"); + }, latestSequence: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 7f46c64c05..6f24dd615b 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -15,6 +15,7 @@ import { Front } from "../resume.ts"; import { Reader, Stream, Writer } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; import { Subscriber as TrackSubscriber } from "../track.ts"; +import { wireOf } from "../wire.ts"; import { type AnnounceBroadcast, AnnounceOk, AnnounceRequest, decodeAnnounceBroadcast } from "./announce.ts"; import { Datagram as DatagramMessage } from "./datagram.ts"; import { Fetch } from "./fetch.ts"; @@ -696,6 +697,8 @@ async function servedSubscription( // Answer the subscription with a front's broadcast, as an application forwarding a // routed one does, so the track reads through the front's serving route. routed?: boolean; + // Leave the track unresolved until `accept`, as an on-demand track the SUBSCRIBE beats. + pending?: boolean; } = {}, ) { const version = options.version ?? Version.DRAFT_05; @@ -720,7 +723,10 @@ async function servedSubscription( } else { broadcast = publish(origin, Path.from("test")); } - const track = broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: options.maxAge }); + const info = { timescale: Timescale.MILLI, maxAge: options.maxAge }; + let track = options.pending ? undefined : broadcast.createTrack("video", info); + // Pulled before the SUBSCRIBE arrives: a broadcast nobody pulls refuses an unknown track. + const request = options.pending ? wireOf(broadcast).requested() : undefined; const client = await Stream.open(pair.client, { version: version }); @@ -752,16 +758,29 @@ async function servedSubscription( const opened = pair.client.incomingUnidirectionalStreams.getReader(); + const resolved = () => { + if (!track) throw new Error("the track is still pending"); + return track; + }; + return { client, datagrams: pair.client.datagrams.readable, - track, + get track() { + return resolved(); + }, serving, parked: gate.parked, release: gate.release, + // Resolve a pending track with the publisher's request for it. + async accept() { + const pulled = await request; + if (!pulled) throw new Error("the publisher never requested the track"); + track = pulled.accept(info); + }, // Stamped by sequence, so a group served out of order still starts in order. serve(sequence: number) { - const group = track.createGroup(sequence); + const group = resolved().createGroup(sequence); for (const frame of frames) group.writeFrame(textFrame(frame, Timestamp.fromMillis(sequence))); group.finish(); }, @@ -1330,6 +1349,26 @@ test("lite draft-05: an update dropping the floor re-pins to the latest group", } }); +// A SUBSCRIBE can beat its track resolving, as one a relay serves from a copy still fetching +// upstream. Groups the track holds once it does are older than the join, so an absent Group +// Start is still the latest group. +test.each([Version.DRAFT_01, Version.DRAFT_03, Version.DRAFT_05])( + "lite %s: a subscribe that beats its track resolving joins at the latest group", + async (version) => { + const sub = await servedSubscription({ version, pending: true }); + try { + await sub.accept(); + sub.serve(0); + sub.serve(1); + sub.serve(2); + expect(await sub.servedHeader()).toBe(2); + expect(await sub.servedSequence()).toBeUndefined(); + } finally { + await sub.close(); + } + }, +); + // On an empty track the re-pin has no latest group to name, so the first group the // track produces is served, even below the dropped floor. test("lite draft-05: an update dropping the floor on an empty track serves the next group", async () => { diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index dca9694158..5ec1cff30d 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -892,7 +892,8 @@ export class Publisher { }, }); serving(track); - const floor = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track)); + const latest = hooks.latestSequence(track); + let floor = cursorFloor(this.version, msg.startGroup, latest); track.withRange({ start: { included: floor }, end: endGroup === undefined ? undefined : { excluded: endGroup }, @@ -926,6 +927,18 @@ export class Publisher { // priority. const info = await untilAborted(track.info(), left.signal); const publisher = info.priority; + // A SUBSCRIBE can arrive before its track resolves, such as one a relay serves from a + // copy still fetching upstream, or on lite-05 one sent alongside its TRACK, when no + // group is known yet: the newest once it resolves stands in, as it would for one + // sent after. + const resolved = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track)); + if (latest === undefined && resolved !== floor) { + floor = resolved; + track.withRange({ + start: { included: floor }, + end: endGroup === undefined ? undefined : { excluded: endGroup }, + }); + } // Older drafts carry no timestamps. let stamp: Stamp | undefined; let compress = false; diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index a70ee88597..a465e35745 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -31,6 +31,7 @@ import { Probe } from "./probe.ts"; import { StreamId } from "./stream.ts"; import { encodeSubscribeResponse, + Subscribe, SubscribeDrop, SubscribeEnd, SubscribeOk, @@ -1162,7 +1163,7 @@ test.each([ }); // A setup that outlived its deadline is over: its TRACK stream is reset, even one still waiting for a -// slot, and the subscription is neither registered again nor sent as a SUBSCRIBE. +// slot, along with the SUBSCRIBE sent beside it, and the subscription is not registered again. test.each([ ["lite-05 subscribe waiting on the TRACK_INFO", Version.DRAFT_05, false], ["lite-06 subscribe waiting on the TRACK_INFO", Version.DRAFT_06, false], @@ -1184,7 +1185,8 @@ test.each([ if (parked) streams[0].release(); await streams[0].aborted; - expect(streams.length).toBe(1); + await streams[1].aborted; + expect(streams.length).toBe(2); // A GROUP for a forgotten id is ignored without touching its stream. const touched: PropertyKey[] = []; @@ -1258,6 +1260,104 @@ test("a lite subscribe whose SUBSCRIBE write stalls past the deadline closes its } }); +// On lite-05 and later a SUBSCRIBE goes out beside its TRACK, before the TRACK_INFO, and accepting +// the info sends no SUBSCRIBE_UPDATE even when the publisher keeps groups for less than the +// subscriber waits. +test.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])( + "lite %s: a subscribe goes out with its track", + async (version) => { + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const track = subscriber + .consume(Path.from("room")) + .track("video") + .subscribe({ priority: 3, maxDelay: Milli(10_000) }); + try { + await opened(0); + await streams[0].reading; + await (await opened(1)).writing; + const sent = new Reader(undefined, new Uint8Array(Buffer.concat(streams[0].written)), version); + expect(await sent.u53()).toBe(StreamId.Track); + await drainReactions(); + const wire = () => new Reader(undefined, new Uint8Array(Buffer.concat(streams[1].written)), version); + let subscribe = wire(); + expect(await subscribe.u53()).toBe(StreamId.Subscribe); + const msg = await Subscribe.decode(subscribe, version); + expect([msg.id, msg.track, msg.priority, msg.maxDelay]).toEqual([0n, "video", 3, 10_000]); + + const chunks: Uint8Array[] = []; + const writer = new Writer( + new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), + version, + ); + await new TrackInfo({ timescale: 1000, maxAge: 1000 }).encode(writer, version); + for (const chunk of chunks) streams[0].inbound.enqueue(chunk); + await track.info(); + await drainReactions(); + subscribe = wire(); + await subscribe.u53(); + await Subscribe.decode(subscribe, version); + expect(await subscribe.done()).toBe(true); + } finally { + track.close(); + subscriber.close(); + } + }, +); + +// A GROUP that beats its subscription's TRACK_INFO waits unread for it, then is read with it. +test("a group that beats its track info waits for it", async () => { + const version = Version.DRAFT_07; + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const track = subscriber.consume(Path.from("room")).track("video").subscribe(); + try { + await opened(0); + await (await opened(1)).writing; + const touched: PropertyKey[] = []; + const stream = await groupStream(0, version); + const reader = new Proxy(stream, { + get: (target, key) => { + touched.push(key); + const value = Reflect.get(target, key, target); + return typeof value === "function" ? value.bind(target) : value; + }, + }); + const receiving = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0 }), reader); + await drainReactions(); + expect(touched).toEqual([]); + + await answerTrackInfo(streams[0], version, 1000); + await receiving; + const group = await track.recvGroup(); + expect(group?.sequence).toBe(0); + expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([0])); + } finally { + track.close(); + subscriber.close(); + } +}); + +// A TRACK reset while its SUBSCRIBE is live fails the subscription, so the origin can try another +// route, and resets the SUBSCRIBE. +test("a reset track rejects the subscription and resets its subscribe", async () => { + const warn = spyOn(console, "warn").mockImplementation(() => {}); + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); + const track = subscriber.consume(Path.from("room")).track("video").subscribe(); + try { + await opened(0); + await (await opened(1)).writing; + streams[0].inbound.error(new Error("peer reset")); + expect(await track.closed).toBeInstanceOf(Error); + await streams[1].aborted; + } finally { + track.close(); + subscriber.close(); + warn.mockRestore(); + } +}); + test("a fetch started after the subscriber closes rejects without opening a stream", async () => { const { quic, streams } = fakeSession(); const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n)); @@ -1269,7 +1369,7 @@ test("a fetch started after the subscriber closes rejects without opening a stre }); // The grant watch is armed before the first check, so a shrink while TRACK_INFO is still in -// flight refuses the subscription rather than opening a SUBSCRIBE the grant no longer covers. +// flight refuses the subscription and resets the SUBSCRIBE sent beside its TRACK. test("a grant that shrinks while the subscription sets up refuses it", async () => { const { quic, streams, opened } = fakeSession(); const scoped = (prefix: string): Grant => ({ @@ -1298,8 +1398,8 @@ test("a grant that shrinks while the subscription sets up refuses it", async () const err = await next; expect(err).toBeInstanceOf(StreamError); expect((err as StreamError).code).toBe(StreamCode.Unauthorized); - // Nothing past the TRACK stream reached the wire. - expect(streams.length).toBe(1); + await streams[1].aborted; + expect(streams.length).toBe(2); track.close(); }); @@ -1805,6 +1905,42 @@ test.each([ } }); +// A TRACK answered while its SUBSCRIBE still waits for a stream belongs to the subscription, so +// a withdrawal then closes it rather than leaving it to a create that may never open. +test("a withdrawal while the subscribe opens closes the answered track", async () => { + const version = Version.DRAFT_07; + const warn = spyOn(console, "warn").mockImplementation(() => {}); + const { quic, streams, opened } = fakeSession([2]); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const announced = subscriber.announced(); + try { + await opened(0); + const pool = await epochlessPool(version, streams, announced); + const held = subscriber.consume(Path.from("pool/job")); + const track = held.track("video").subscribe(); + const info = await opened(1); + await opened(2); + await info.reading; + await answerTrackInfo(info, version); + await drainReactions(); + + let finished = false; + void info.finished.then(() => { + finished = true; + }); + await pool.withdraw(); + expect(await track.closed).toBeInstanceOf(Error); + await drainReactions(); + expect(finished).toBe(true); + track.close(); + held.close(); + } finally { + announced.close(); + subscriber.close(); + warn.mockRestore(); + } +}); + // A TRACKS request without an epoch lists whichever instance won the path when it arrived, so // another instance taking the path refuses one still unanswered. test("a listing of an epochless handle replaced before its answer is refused", async () => { @@ -2684,8 +2820,9 @@ test("closing a broadcast while TRACK_INFO is pending leaves no unhandled reject broadcast.close(); track.close(); await streams[0].aborted; + await streams[1].aborted; await drainReactions(); - expect(streams).toHaveLength(1); + expect(streams).toHaveLength(2); expect(unhandled).toEqual([]); } finally { process.off("unhandledRejection", onUnhandled); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 79f7db3df4..9d804d3888 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -23,7 +23,6 @@ import { import * as netGroup from "../group.ts"; import { Cost, type Hop, MAX_HOPS, type Route, routesEqual, UNKNOWN_HOP } from "../hop.ts"; import { groupBounds, hiddenBelow, hooks, maxDelayOf, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts"; -import { grace } from "../ledger.ts"; import type { Lister } from "../listing.ts"; import * as Path from "../path.ts"; import { receiveFrame } from "../payload.ts"; @@ -176,6 +175,9 @@ export class Subscriber { // Registered only after immutable TRACK_INFO or the older drafts' defaults resolve, // so every incoming group and datagram already knows its timestamp framing. #subscribes = new Map(); + // A SUBSCRIBE sent alongside its TRACK, until its TRACK_INFO lands: settles with the + // entry, or `undefined` if it never does. + #pending = new Map>(); #subscribeNext = 0n; // Dedup consumed broadcasts per path: repeat consume() calls share one subscription. @@ -1067,16 +1069,16 @@ export class Subscriber { } } - // Determine the track's immutable properties, accept the request (so the - // application's track.Subscriber resolves and incoming groups have a producer to - // write into), register it, then open the subscribe stream. `state.stream` and - // `state.track` are populated as soon as each stream opens so the caller can clean - // them up on timeout even before this promise settles. + // Open the subscribe stream and accept the request (so the application's track.Subscriber + // resolves and incoming groups have a producer to write into) with the track's immutable + // properties. `state.stream` is populated as soon as it opens, and `state.track` once + // answered, so the caller can clean them up on timeout even before this promise settles. // - // On lite-05+ the properties come from a TRACK stream opened first, held open until - // the SUBSCRIBE is answered, and the SUBSCRIBE is accepted implicitly (no - // SUBSCRIBE_OK). Older drafts carry no per-track properties, so they resolve to - // defaults and just drain SUBSCRIBE_OK. + // On lite-05+ the properties come from a TRACK stream, held open until the SUBSCRIBE is + // answered, and the SUBSCRIBE goes out alongside it rather than a round trip after: it is + // accepted implicitly (no SUBSCRIBE_OK), and needs the info only to read what it receives. + // A group stream that beats the info waits on `#pending`, unread. Older drafts carry no + // per-track properties, so they resolve to defaults and just drain SUBSCRIBE_OK. async #openSubscribe( state: SubscribeSetup, msg: Subscribe, @@ -1085,46 +1087,94 @@ export class Subscriber { deadline: Deadline, ): Promise<{ stream: Stream; entry: SubscribeEntry }> { state.cancel.signal.throwIfAborted(); - let producer: track.Producer; - let drainOk = false; - let compress = false; - let timescale = 0; - - if (hasTrackStream(this.version)) { - // Fetch the immutable properties once via the TRACK stream. - const { info, stream } = await this.#trackInfo( - msg.broadcast, - msg.epoch, - msg.track, - state.cancel.signal, - deadline, - ); - state.track = stream; + + if (!hasTrackStream(this.version)) { + // Older drafts negotiate nothing per-track: verbatim frames with no timeline. + const entry = this.#register(id, request.accept(), { compress: false, timescale: 0 }, msg, request, state); + const stream = await this.#sendSubscribe(state, msg, deadline); + // The first response MUST be a SUBSCRIBE_OK (older drafts only). + const resp = await decodeSubscribeResponse(stream.reader, this.version); + if (!("ok" in resp)) { + throw new Error("first subscribe response must be SUBSCRIBE_OK"); + } + return { stream, entry }; + } + + // The TRACK opens first, then the SUBSCRIBE, which is waiting on before it is written. + // The answered TRACK is held at once, so a cancellation while the SUBSCRIBE still opens + // closes it rather than waiting on an open that may never finish. + const info = this.#trackInfo(msg.broadcast, msg.epoch, msg.track, state.cancel.signal, deadline).then( + (answered) => { + state.track = answered.stream; + // Cancelled as it was answered: the caller may already have left. + if (state.cancel.signal.aborted) answered.stream.close(); + return answered; + }, + ); + let settle: (entry: SubscribeEntry | undefined) => void = () => {}; + this.#pending.set( + id, + new Promise((resolve) => { + settle = resolve; + }), + ); + const sending = this.#sendSubscribe(state, msg, deadline); + try { + // Either failing fails the subscription: a reset TRACK rejects the request, so the + // origin can try another route, and the caller resets the SUBSCRIBE. + const [answered, stream] = await Promise.all([info, sending]); // The deadline passed as TRACK_INFO landed: the request is already rejected, so don't - // register it again or send its SUBSCRIBE. + // accept it again. state.cancel.signal.throwIfAborted(); - producer = request.accept(this.#toModelInfo(info)); - compress = info.compress; - timescale = info.timescale ?? 0; - } else { - // Older drafts negotiate nothing per-track: verbatim frames with no timeline. - producer = request.accept(); - drainOk = true; + const producer = request.accept(this.#toModelInfo(answered.info)); + const entry = this.#register( + id, + producer, + { compress: answered.info.compress, timescale: answered.info.timescale ?? 0 }, + msg, + request, + state, + ); + settle(entry); + return { stream, entry }; + } catch (err) { + settle(undefined); + // Whichever exchange outlived the other is no longer wanted. + void info.then( + ({ stream }) => stream.abort(error(err)), + () => {}, + ); + void sending.then( + (stream) => stream.abort(error(err)), + () => {}, + ); + throw err; + } finally { + this.#pending.delete(id); } + } + + // Register a subscription's accepted copy, so incoming groups and datagrams find it. + #register( + id: bigint, + producer: track.Producer, + framing: { compress: boolean; timescale: number }, + msg: Subscribe, + request: track.Request, + state: SubscribeSetup, + ): SubscribeEntry { // Lite-06+ resolves where the feed starts only with SUBSCRIBE_START, so a relay serving // this copy holds its first group until the answer arrives. if (resolvesStart(this.version)) hooks.startTrack(producer, { requested: msg.startGroup }); - // Register before opening SUBSCRIBE so a racing GROUP stream finds the entry. const entry: SubscribeEntry = { track: producer, - compress, - timescale, + ...framing, // The effective max delay is the stopgap grace: the wrong clock (it bounds // presentation-time drift), but it is how long the subscriber was willing to wait - // for a late group anyway. Already the smaller of the subscriber's and the track's. + // for a late group anyway, and no longer than the publisher keeps one. tail: new Tail({ - grace: () => grace(producer.subscription.peek()?.maxDelay ?? Milli.zero), + grace: () => hooks.tailGrace(producer), }), requested: msg.startGroup, bounded: msg.endGroup !== undefined, @@ -1133,23 +1183,19 @@ export class Subscriber { }; this.#subscribes.set(id, entry); state.producer = producer; + return entry; + } - state.stream = await deadline.open(Stream.open(this.#quic, { version: this.version, ...requestOptions })); + // Open the subscribe stream and write the SUBSCRIBE on it. + async #sendSubscribe(state: SubscribeSetup, msg: Subscribe, deadline: Deadline): Promise { + const stream = await deadline.open(Stream.open(this.#quic, { version: this.version, ...requestOptions })); + state.stream = stream; // The deadline passed while the open waited: the late-setup handler resets the stream, so // don't send a SUBSCRIBE on it first. state.cancel.signal.throwIfAborted(); - await state.stream.writer.u53(StreamId.Subscribe); - await msg.encode(state.stream.writer, this.version); - - if (drainOk) { - // The first response MUST be a SUBSCRIBE_OK (older drafts only). - const resp = await decodeSubscribeResponse(state.stream.reader, this.version); - if (!("ok" in resp)) { - throw new Error("first subscribe response must be SUBSCRIBE_OK"); - } - } - - return { stream: state.stream, entry }; + await stream.writer.u53(StreamId.Subscribe); + await msg.encode(stream.writer, this.version); + return stream; } // Opens a TRACK stream and reads the single TRACK_INFO. Lite-05+ only. The stream @@ -1716,7 +1762,9 @@ export class Subscriber { * @internal */ async runGroup(group: GroupMessage, stream: Reader) { - const entry = this.#subscribes.get(group.subscribe); + // One that beats its subscription's TRACK_INFO waits for it unread, which flow control + // bounds. Waiters resume in the order they arrived. + const entry = this.#subscribes.get(group.subscribe) ?? (await this.#pending.get(group.subscribe)); if (!entry) { if (group.subscribe >= this.#subscribeNext) { throw new Error(`unknown subscription: id=${group.subscribe}`); @@ -1813,15 +1861,17 @@ export class Subscriber { // datagram (best-effort) if the subscription is unknown or closed. async #routeDatagram(payload: Uint8Array): Promise { if (payload.byteLength > DatagramMessage.MAX_SIZE) return; - // An unknown subscription's datagram is dropped below, however it parses. - const dg = await DatagramMessage.decode(payload, this.version, (id) => ({ - timed: !!this.#subscribes.get(id)?.timescale, - compressed: this.#subscribes.get(id)?.compress ?? false, - })); + // Read with the entry it was decoded with: one registered while the decode awaited would + // read a datagram parsed with the defaults. An unknown subscription's datagram, or one + // whose TRACK_INFO had not landed, which a datagram cannot wait for, is dropped below. + let entry: SubscribeEntry | undefined; + const dg = await DatagramMessage.decode(payload, this.version, (id) => { + entry = this.#subscribes.get(id); + return { timed: !!entry?.timescale, compressed: entry?.compress ?? false }; + }); if (!dg) return; - - const entry = this.#subscribes.get(dg.subscribe); - if (!entry) return; // Unknown or already-closed subscription. + // Gone since, too: the subscription ended while the decode awaited. + if (!entry || this.#subscribes.get(dg.subscribe) !== entry) return; const scale = entry.timescale; diff --git a/js/net/src/request-deadline.test.ts b/js/net/src/request-deadline.test.ts index 3ec2007281..9636f4d47d 100644 --- a/js/net/src/request-deadline.test.ts +++ b/js/net/src/request-deadline.test.ts @@ -18,7 +18,7 @@ function limited(mode: "refuse" | "queue" | "open") { const cancelled = Promise.withResolvers(); const written: Uint8Array[] = []; const options: unknown[] = []; - // Each create gets its own stream: a fetch opens its TRACK stream alongside. + // Each open gets its own stream; the first abort and cancel among them resolve. const stream = () => ({ readable: new ReadableStream({ cancel: cancelled.resolve }), writable: new WritableStream({ @@ -74,7 +74,11 @@ for (const protocol of ["lite", "ietf"] as const) { } else { expect(failure?.name).toBe("NoStreamCredit"); } - expect(transport.options).toEqual([{ sendOrder: undefined, waitUntilAvailable: false }]); + // A lite subscription opens its SUBSCRIBE alongside its TRACK, under the one deadline. + const opened = protocol === "lite" ? 2 : 1; + expect(transport.options).toEqual( + Array.from({ length: opened }, () => ({ sendOrder: undefined, waitUntilAvailable: false })), + ); if (mode === "queue") { transport.release(); await transport.aborted; @@ -86,7 +90,7 @@ for (const protocol of ["lite", "ietf"] as const) { await transport.cancelled; } await clock.advance(Nano.fromMilli(Milli(30_000))); - expect(transport.options).toHaveLength(1); + expect(transport.options).toHaveLength(opened); } finally { track.close(); broadcast.close(); diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index a539967061..25a2d0602a 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -564,15 +564,20 @@ test("an unfloored subscriber clears a resume floor above the live edge", () => expect(producer.subscription.peek()?.groups?.start).toBeUndefined(); }); -test("the producer aggregate is clamped without changing subscriber options", async () => { +test("the producer aggregate is not clamped to the track's maxAge", () => { + // The publisher applies its own, so a subscriber asking upstream before the info lands + // asks for the same demand after it. const producer = new TrackProducer("test"); const track = producer.subscribe({ maxDelay: Milli(10_000) }); - const clamped = producer.subscription.changed(); + const before = producer.subscription.peek(); producer.accept({ timescale: Timescale.MILLI, maxAge: Milli(2_000) }); - expect((await clamped)?.maxDelay).toBe(Milli(2_000)); + expect(producer.subscription.peek()).toEqual(before); + expect(producer.subscription.peek()?.maxDelay).toBe(Milli(10_000)); expect(track.subscription.peek()?.maxDelay).toBe(Milli(10_000)); + // A late group is waited for no longer than the publisher keeps it. + expect(hooks.tailGrace(producer)).toBe(Milli(2_000)); }); test("nextGroup skips late arrivals", async () => { @@ -847,7 +852,8 @@ test("an omitted maxDelay is the default zero, and null sets no limit", async () // An explicit null leaves the publisher's retention as the only bound. const unlimited = producer.subscribe({ maxDelay: null }); for (const sequence of [0, 1, 2]) expect((await unlimited.recvGroup())?.sequence).toBe(sequence); - expect(producer.subscription.peek()?.maxDelay).toBe(Milli(5000)); + expect(producer.subscription.peek()?.maxDelay).toBe(Milli(Number.MAX_SAFE_INTEGER)); + expect(hooks.tailGrace(producer)).toBe(Milli(5000)); producer.finish(); producer.close(); }); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 51079eb74d..00644fb663 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -810,6 +810,7 @@ export class Producer { hooks.producerLargest = (producer) => producer.#state.largest; hooks.joinProducer = (producer) => producer.#join(); hooks.endTrack = (producer, end, error) => producer.#endPending(end, error); + hooks.tailGrace = (producer) => producer.#grace(); hooks.releaseTrack = (producer) => producer.#close(undefined); hooks.trackReleased = (producer) => producer.#state.closed; hooks.trackFinished = (producer) => producer.#state.end.peek() !== undefined; @@ -1100,12 +1101,13 @@ export class Producer { // Recompute from every live sink because an update or close can narrow as well as widen // the aggregate. The wire layer observes this signal and emits SUBSCRIBE_UPDATE. + // + // The max delay is the subscribers' own, not clamped to the track's maxAge: the publisher + // applies its own, as each reader's budget does, so the demand asked upstream is the same + // before and after the info lands. #updateSubscription(): void { const combined = combineSubscriptions(this.#sinks); this.#settle(); - const retained = this.#state.info.peek()?.maxAge; - if (combined && retained !== undefined) - combined.maxDelay = Milli(Math.min(combined.maxDelay ?? Milli.zero, retained)); this.#state.update.set(combined); // The requested floor bounds what a pending tail owes. this.#owe(); @@ -1611,9 +1613,12 @@ export class Producer { this.#owe(); } - // The grace the session's tail uses, from the max delay the track's subscribers ask for. + // The grace the session's tail uses, from the max delay the track's subscribers ask for, + // bounded by how long the publisher keeps a group. The demand asked upstream is not. #grace(): Milli { - return grace(this.#state.update.peek()?.maxDelay ?? Milli.zero); + const maxDelay = this.#state.update.peek()?.maxDelay ?? Milli.zero; + const maxAge = this.#state.info.peek()?.maxAge; + return grace(maxAge === undefined ? maxDelay : Milli(Math.min(maxDelay, maxAge))); } // Tell every reader whether the pending tail still owes a group below the end. diff --git a/quest/m1/README.md b/quest/m1/README.md index dfde045a69..a6ae010d43 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -176,7 +176,6 @@ removed). - [HLS export follows a resolution change](/quest/m1/hls-reinit-same-codec.md) - a decoder-identity change under the same codec string starts a new init and segment tag - [A JS reader finishes after a clean close](/quest/m1/js-close-range-finish.md) - a range ending below the cache delivers what's held and then ends - [fMP4 decode times keep increasing](/quest/m1/fmp4-bframe-decode-time.md) - fragments shorter than a GOP of B-frame video never step `tfdt` backwards -- [A JS relay floors an unresolved copy at its newest group](/quest/m1/js-early-latest.md) - lite-01..05 subscribes without a start don't replay the cache from group 0 - [moq-quic deletes its remaining 0-RTT code](/quest/m1/quic-zero-rtt-leftovers.md) - only the code that drops a 0-RTT packet remains, with a test - [Gate the no-em-dash rule](/quest/m1/em-dash-gate.md) - `just check` fails on an em dash in any tracked text file diff --git a/quest/m1/js-early-latest.md b/quest/m1/js-early-latest.md deleted file mode 100644 index b005bd2dec..0000000000 --- a/quest/m1/js-early-latest.md +++ /dev/null @@ -1,13 +0,0 @@ -# [S] A JS relay starts an unresolved copy's subscription at its newest group - -## Goal - -On lite-01..05, a SUBSCRIBE with no start group served by a JS publisher from a track copy that has no groups yet starts at the newest group once the copy resolves, instead of replaying the whole cache from group 0, matching the Rust fix in [moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328). - -## Plan - -Found by the agent on [moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328). `cursorFloor` (`js/net/src/lite/publisher.ts:419-423`) returns `latest ?? 0` and is called once right after subscribe (`:890`) with `hooks.latestSequence(track)` (`js/net/src/track.ts:2249`); a related fallback is at `:1290`. Rust's fix re-reads `latest_sequence()` once the track resolves. - -Decided 2026-10-30 (maintainer, follow-up planning after the /quest-afk run). Port the Rust rule rather than invent a JS one: an unresolved copy defers the floor until it resolves, then floors at the newest group. Test a JS relay serving a copy that resolves after the SUBSCRIBE arrives. - -Public API: none. Wire: none. diff --git a/quest/m2/README.md b/quest/m2/README.md index 34114fba7a..066c83369f 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -60,7 +60,6 @@ lite-07; bitrate enforcement is not part of this release. - [Drop test-only cut requests](/quest/m2/video-cut-test-only.md) - moq-video's encode producer loses code only its tests use -- [Pipelined requests](/quest/m2/pipeline-requests/README.md) - SUBSCRIBE and the first FETCH go out with the track-info request at every hop, so first data arrives a round trip sooner per hop - [OBS native codecs](/quest/m2/obs-moq-video/README.md) - replace FFmpeg video and audio decoding with moq-video and moq-audio, deliver GPU frames, and use native audio/video encoders - [MoQ in obs-studio](/quest/m2/obs-studio/README.md) - a native MoQ output and service merged into obs-studio, once its maintainers agree; the plugin carries it until then - [CMake qlog option for OBS](/quest/m2/obs-qlog.md) - an OBS build can opt into qlog trace capture through moq-ffi's `qlog` feature diff --git a/quest/m2/pipeline-requests/README.md b/quest/m2/pipeline-requests/README.md deleted file mode 100644 index 2cd01ff1c0..0000000000 --- a/quest/m2/pipeline-requests/README.md +++ /dev/null @@ -1,27 +0,0 @@ -# Pipelined requests - -## Goal - -A track request never waits a round trip for the track's info before the -request that carries data goes out: SUBSCRIBE and the first FETCH travel with -TRACK (lite) or TRACK_STATUS (moq-transport) at every hop, so first data -arrives one round trip sooner per hop. Peers that send them serially keep -working unchanged. - -## Plan - -Decided 2026-10-08 in a `/quest-plan` interview: the extra round trip is not -worth keeping, for SUBSCRIBE and FETCH alike. Shared rules for the SUBSCRIBE -and FETCH children: - -- Streamed data that arrives before the info stays unread in QUIC until the - info lands. Datagrams can't wait unread, so they are dropped until then. -- A failed or reset info request fails the data request. -- Legacy serial peers keep working, proven by mixed-mode tests in Rust and JS. - -End to end: measure time to first frame and first fetched group across one -and two relay hops, before and after the line. - -## Required - -- [SUBSCRIBE goes out with TRACK](/quest/m2/pipeline-requests/subscribe.md) - subscribers open TRACK and SUBSCRIBE together in Rust and JS diff --git a/quest/m2/pipeline-requests/subscribe.md b/quest/m2/pipeline-requests/subscribe.md deleted file mode 100644 index a32494a4a3..0000000000 --- a/quest/m2/pipeline-requests/subscribe.md +++ /dev/null @@ -1,92 +0,0 @@ -# [L] SUBSCRIBE goes out with TRACK - -## Goal - -On moq-lite 05 and later, a subscriber opens its TRACK and SUBSCRIBE streams -together instead of waiting for TRACK_INFO before sending SUBSCRIBE, in Rust -and JS, at every hop: the viewer's session and each relay's upstream session. -First data arrives one round trip sooner per hop. A peer that still sends -them serially, and lite-03/04 with no track stream, keep working unchanged. - -## Plan - -Decided 2026-10-08 in a `/quest-plan` interview (paper trail in the PR that -added this quest). It builds on #5053 (merged) and reverses that PR's "not -pipelining" decision: the draft already allows it -(`drafts/draft-lcurley-moq-lite.md`, Track Stream: the subscriber "MAY open -the Track and Subscribe streams concurrently"), and the round trip is not -worth keeping. - -Facts (2026-10-08, `origin/main` 11ee77d5d, after #5053): - -- Rust: `TrackServeRun::poll` waits on `info.poll_fetch` before building its - `ServeLoop` (`rs/moq-net/src/lite/subscriber.rs:4144`), and only then does - `begin_subscription` and `Establish` write SUBSCRIBE. -- JS: `#openSubscribe` awaits `#trackInfo` before `Stream.open` and the - SUBSCRIBE write (`js/net/src/lite/subscriber.ts:704`). The receive path - already waits for the timescale (`runGroup`) and drops datagrams that beat - TRACK_INFO. -- No SUBSCRIBE field needs TRACK_INFO, and neither publisher needs a TRACK - before a SUBSCRIBE (`SubscribeServe::start` subscribes the model track - directly). -- The relay front already hands demand to a copy before splicing it - (`model/origin.rs` `Action::Query`), so the gap is in the session. -- Both stacks route groups only to an accepted subscription. Rust's - `ServeLoop::new` accepts the request with the info, and - `begin_subscription` registers a `TrackEntry` holding that producer and its - timescale; a group whose id has no entry is cancelled, not left unread - (`subscriber.rs:816`). JS `runGroup` (`subscriber.ts:1145`) drops a group - for a past id with no registered subscription (an id it has not allocated - yet is a protocol error), and writes the group before it - waits on the timescale. - -Decisions: - -- Both streams open at once. Group streams that arrive before TRACK_INFO stay - unread in QUIC until it lands (flow control bounds them; no buffering, no - copies). Datagrams that beat it are dropped, as JS does today. Rejected: - buffering decoded bytes in memory. -- Each subscription gets a pending state before accept, in both stacks: - SUBSCRIBE is built from the request's subscription before the info is - known, and its entry is registered under the id before SUBSCRIBE is written, - with the producer and timescale deferred. A group stream that arrives early - parks after its header and resumes on accept. -- A TRACK that fails or is reset while its SUBSCRIBE is live rejects the - request, so the origin fails over as today, and resets the SUBSCRIBE - stream. Rejected: keeping the subscription and learning the info another - way. -- Only the max age SUBSCRIBE and SUBSCRIBE_UPDATE carry on the wire stops - being clamped by TRACK_INFO (`max_age_bound()`, `model/track.rs`): it is the - subscribers' own, still capped by the local cache ceiling (which needs no - info), and the publisher enforces its own. The per-reader lateness budget - (`is_expired`) and the drift check (`poll_drift`) keep `max_age_bound()`. - Accepting TRACK_INFO never emits a SUBSCRIBE_UPDATE. Reword the docs that - promise a clamped aggregate (`track::Producer::subscription`, - `max_age_bounds_the_budget`). Rejected: a SUBSCRIBE_UPDATE once TRACK_INFO - lands. Narrowed 2026-10-08 from review. This agrees with - the shared staleness rule (`Subscription::max_delay`): both budgets apply one - staleness rule, so a group is stale under the smaller budget exactly when it - is stale under either. -- Publishers send a track's TRACK_INFO at a higher stream priority than that - track's groups, in Rust and JS, as the draft's SHOULD asks, so the unread - wait stays one round trip. -- Keep #5053's held TRACK stream: demand still has to hold across either - arrival order and for serial peers. -- Legacy: mixed-mode integration tests in Rust and JS, a serial subscriber - against a pipelining publisher and relay and the reverse, on lite-05/06/07, - with lite-03/04 unchanged. `just test interop --all` covers the - cross-language pairs. - -Tests also: SUBSCRIBE is on the wire before TRACK_INFO; groups that arrive -first are read only after it; a reset TRACK rejects the request and the -origin fails over; accepting TRACK_INFO sends no SUBSCRIBE_UPDATE; TRACK_INFO -outranks the track's groups. Measure time to first frame across one and two -relay hops before and after. - -Public API: none. Wire: none (ordering only; the draft already permits it). -Decided 2026-10-08: pipelining is ordering-only, so it neither waits on nor -blocks the Related quests below; whichever lands second rebases. - -## Related - -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - replaces the SUBSCRIBE floor with ranges diff --git a/rs/moq-net/src/coding/stream.rs b/rs/moq-net/src/coding/stream.rs index f158991473..4393a6b18a 100644 --- a/rs/moq-net/src/coding/stream.rs +++ b/rs/moq-net/src/coding/stream.rs @@ -19,6 +19,11 @@ use crate::coding::{Reader, StreamCodes, Writer}; /// round-robin share: a packet or two, which is all it needs. const CONTROL_SEND_ORDER: u8 = u8::MAX; +/// The send order a TRACK_INFO answer goes at: ahead of every group stream, unlike other +/// control. A subscriber that sent its SUBSCRIBE alongside the TRACK holds the groups that +/// beat the info unread, so the info is all that stands between it and them. +pub(crate) const TRACK_INFO_SEND_ORDER: i64 = CONTROL_SEND_ORDER as i64 + 1; + /// A [Writer] and [Reader] pair for a single stream. pub struct Stream { pub writer: Writer, @@ -176,7 +181,7 @@ mod tests { assert_eq!( log.priorities(), - vec![CONTROL_SEND_ORDER], + vec![i64::from(CONTROL_SEND_ORDER)], "a control stream left at the transport default loses to every group", ); } @@ -194,7 +199,7 @@ mod tests { assert_eq!( log.priorities(), - vec![CONTROL_SEND_ORDER], + vec![i64::from(CONTROL_SEND_ORDER)], "an accepted control stream left at the transport default loses to every group", ); } diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index 3050ef8b1e..3faa622715 100644 --- a/rs/moq-net/src/coding/writer.rs +++ b/rs/moq-net/src/coding/writer.rs @@ -375,7 +375,7 @@ mod tests { writer.set_priority(send_order.into()); } - assert_eq!(log.priorities(), (0u8..=255).collect::>()); + assert_eq!(log.priorities(), (0i64..=255).collect::>()); } /// A message the stream has not taken whole cannot be finished, so closing resets rather diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index bc81892ffd..4f9e02231c 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -13358,12 +13358,23 @@ mod stitch_tests { original.push(datagram); } track.finish().unwrap(); - let (served, _sessions) = match hop { + let (served, _sessions, _copy) = match hop { true => { let (served, sessions) = crate::lite::ietf::test_hop(&origin, "room", [true, true]).await; - (served, Some(sessions)) + // The hop learns the track's info first, and holds the copy for the SUBSCRIBE + // below: a SUBSCRIBE sent alongside the TRACK has the cache replayed as + // datagrams before the TRACK_INFO they are read with, and they are dropped. + let copy = served + .consume() + .request("room", None) + .await + .unwrap() + .track("video") + .unwrap(); + copy.info().await.unwrap(); + (served, Some(sessions), Some(copy)) } - false => (origin, None), + false => (origin, None, None), }; let downstream = ScriptedSession::per_stream(vec![vec![]]).with_datagrams(65535); let peer = peer::PeerSetup::default(); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index ebc033bc06..1393f68957 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -119,7 +119,9 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option /// must not read as a request to replay the whole cache on join. /// /// `latest` is the newest group when the SUBSCRIBE arrived, not once the subscription -/// resolved: anything written in between is newer, so it is delivered. +/// resolved: anything written in between is newer, so it is delivered. On lite-05, a +/// SUBSCRIBE sent alongside its TRACK can arrive before the track resolves, when no group +/// is known yet: the newest once it resolves stands in, as it would for one sent after. fn cursor_floor(version: Version, start_group: Option, latest: Option) -> u64 { if version.resolves_start() || start_group.is_some() { return 0; @@ -1907,6 +1909,8 @@ impl Request for TrackInfoServe { let info = ready!(self.querying.poll_ok(waiter))?; crate::ietf::flate::egress(&info, shared.version.has_compression())?; + // Ahead of the track's groups, which a subscriber holds unread until it lands. + writer.set_priority(crate::coding::TRACK_INFO_SEND_ORDER); // TRACK_INFO only flows on Lite05+ (the encode errors otherwise). writer.buffer(&lite::TrackInfo::serve(&info, shared.version))?; if ietf { @@ -2167,6 +2171,10 @@ impl Request for SubscribeServe { ietf, }; + let latest = match shared.version.has_track_stream() { + true => latest.or_else(|| track.latest_sequence()), + false => latest, + }; let floor = cursor_floor(shared.version, bounds.groups().start(), latest); let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); if let Some(update) = update { @@ -8235,6 +8243,59 @@ mod tests { } } + /// TRACK_INFO goes out ahead of every group stream, the most urgent included: a subscriber + /// that sent its SUBSCRIBE alongside holds the groups that beat it unread. + #[kio::test] + async fn track_info_outranks_the_track_groups() { + let version = Version::Lite07; + let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); + let broadcast = origin.create_broadcast("room").unwrap(); + let _track = broadcast.create_track("video", crate::track::Info::timed()).unwrap(); + broadcast.announce(Default::default()).unwrap(); + let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); + peer_setup.set(crate::lite::Setup::default()); + let (_, goaway) = crate::goaway::Handle::new(true); + let publisher = Publisher::new(PublisherConfig { + estimate: kio::Producer::default().weak(), + runtime: crate::time::sim(), + session: crate::driver::Transport::new(ScriptedSession::new(Vec::new())), + origin: origin.consume(), + version, + peer_setup, + goaway, + peer_hop: None, + subscriptions: Default::default(), + auth: crate::auth::Link::new(false), + peer_grant: Default::default(), + client: false, + }); + + let mut script = Vec::new(); + lite::Track { + epoch: None, + broadcast: crate::Path::from_static("room"), + track: "video".into(), + } + .encode(&mut crate::coding::Encoder::new(&mut script, version.into()), version) + .unwrap(); + let session = ScriptedSession::new(script); + let (send, recv) = kio::wait(|waiter| { + ::poll_open_bi(&session, waiter, Default::default()) + }) + .await + .unwrap(); + let stream = Stream:: { + writer: Writer::new(crate::transport::boxed::SendStream::new(send), version), + reader: crate::coding::Reader::new(crate::transport::boxed::RecvStream::new(recv), version), + }; + let mut serve = RequestServe::::new(publisher.shared.clone(), stream); + assert!(!drive(&mut serve).await, "the TRACK stream is held"); + + let top = i64::from(PriorityHandle::send_order_of(0)); + assert_eq!(session.log.priorities(), [crate::coding::TRACK_INFO_SEND_ORDER]); + assert!(crate::coding::TRACK_INFO_SEND_ORDER > top); + } + /// An answered TRACK stream holds its track until the requester closes its side, by /// FIN or reset, and the hold owes nothing a draining close would wait for. A requester /// that leaves while the reply is still blocked on flow control lets the track go too. diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 13ae5939b2..1ddef7bac5 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -71,8 +71,8 @@ pub(super) struct Subscriber { // local and is never written into a hop chain. session_origin: crate::Hop, subscribes: Lock>, - /// Why this session ended, once it has. A track still waiting on TRACK_INFO is - /// not in [`Self::subscribes`], so dropping its request reads this instead of + /// Why this session ended, once it has. A track still waiting on TRACK_INFO has no + /// copy in [`Self::subscribes`], so dropping its request reads this instead of /// becoming [`Error::Dropped`]. ended: Lock>, next_id: Arc, @@ -81,6 +81,8 @@ pub(super) struct Subscriber { peer_setup: super::PeerSetup, /// GROUP streams parked until the peer's SETUP says how to read their header. setup_waits: Arc, + /// GROUP streams parked until their subscription's TRACK_INFO says how to read them. + park_waits: Arc, /// Local policy overriding the peer's declared egress price. See `poll_link_cost`. cost: Option, /// Sources minted by the announce half, drained by the driver into @@ -92,12 +94,12 @@ pub(super) struct Subscriber { pub(super) announces: crate::session::Slots, } -#[derive(Clone)] struct TrackEntry { - producer: track::Producer, - /// How to read the track, from its TRACK_INFO, known before the SUBSCRIBE is even - /// opened, so group streams decode frames without blocking. - framing: lite::Framing, + /// Where the subscription's groups go, once its TRACK_INFO says how to read them. A + /// SUBSCRIBE sent alongside its TRACK registers before. + copy: Option, + /// The group streams that arrived before the copy, while any wait. + parked: Option>, /// The groups received so far, so the subscription's end can wait for the ones owed. tail: kio::Producer, /// Lite-05/06: where the feed starts, from SUBSCRIBE_START. @@ -114,12 +116,203 @@ impl TrackEntry { fn answered(&mut self, start: Option, first: Option) { self.start = self.start.or(start); self.first = self.first.or(first); - if let (Some(start), Some(first)) = (self.start, self.first) { - self.producer.set_live(Some(first), Some(start)); + if let (Some(start), Some(first), Some(copy)) = (self.start, self.first, &mut self.copy) { + copy.producer.set_live(Some(first), Some(start)); } } } +/// A subscription's accepted copy of its track. +struct Accepted { + producer: track::Producer, + /// How to read the track, from its TRACK_INFO. + framing: lite::Framing, +} + +/// Group streams that beat their subscription's TRACK_INFO, each parked unread after its +/// header, which flow control bounds. The accept opens their groups at once, in the order +/// they arrived, so the copy receives them as it would have without the wait, and each +/// stream then takes its own. +#[derive(Default)] +struct Parked { + /// The headers still waiting on the accept, by ticket, in arrival order. + waiting: Vec<(u64, lite::Group)>, + /// What the accept opened for each, until its stream takes it. + opened: HashMap>, + next: u64, +} + +/// A group stream's group, opened in its subscription's copy for the stream to fill. +struct Opened { + group: group::Producer, + track: track::Producer, + framing: lite::Framing, + reading: Reading, +} + +impl Drop for TrackEntry { + fn drop(&mut self) { + // Groups the accept opened for streams that never took them end as cancelled with + // the subscription, rather than dropped unfinished. + let Some(parked) = &self.parked else { + return; + }; + let opened = match parked.write() { + Ok(mut state) => std::mem::take(&mut state.opened), + Err(_) => return, + }; + for opened in opened.into_values().flatten() { + let _ = opened.group.abort(Error::Cancel); + } + } +} + +/// Counts one GROUP stream parked on its subscription for as long as it waits. +struct ParkWait(Arc); + +impl ParkWait { + fn new(waits: &Arc) -> Self { + waits.fetch_add(1, atomic::Ordering::Relaxed); + Self(waits.clone()) + } +} + +impl Drop for ParkWait { + fn drop(&mut self) { + self.0.fetch_sub(1, atomic::Ordering::Relaxed); + } +} + +impl TrackEntry { + fn new(copy: Option, tail: kio::Producer) -> Self { + Self { + copy, + parked: None, + tail, + start: None, + first: None, + } + } + + #[cfg(test)] + fn accepted(producer: track::Producer, framing: lite::Framing, tail: kio::Producer) -> Self { + Self::new(Some(Accepted { producer, framing }), tail) + } + + /// Park a group stream whose `hdr` arrived before the copy, returning its ticket. + fn park(&mut self, hdr: &lite::Group) -> (kio::Consumer, u64) { + let parked = self.parked.get_or_insert_with(Default::default); + let ticket = match parked.write() { + Ok(mut state) => { + let ticket = state.next; + state.next += 1; + state.waiting.push((ticket, hdr.clone())); + ticket + } + Err(_) => unreachable!("open while the entry holds it"), + }; + (parked.consume(), ticket) + } + + /// Give the copy to the subscription, opening the groups of the streams parked on it. + fn accept(&mut self, copy: Accepted, version: Version, now: crate::time::Instant) { + self.copy = Some(copy); + let Some(parked) = self.parked.clone() else { + return; + }; + let waiting = match parked.write() { + Ok(mut state) => std::mem::take(&mut state.waiting), + Err(_) => return, + }; + let opened: Vec<_> = waiting + .into_iter() + .map(|(ticket, hdr)| (ticket, self.open(&hdr, version, now))) + .collect(); + if let Ok(mut state) = parked.write() { + state.opened.extend(opened); + } + } + + /// Take what the accept opened for the parked stream `ticket`. + fn unpark(&mut self, ticket: u64) -> Result { + let Some(parked) = &self.parked else { + return Err(Error::Cancel); + }; + let (opened, done) = match parked.write() { + Ok(mut state) => { + let opened = state.opened.remove(&ticket).unwrap_or(Err(Error::Cancel)); + (opened, state.opened.is_empty() && state.waiting.is_empty()) + } + Err(_) => (Err(Error::Cancel), false), + }; + if done { + self.parked = None; + } + opened + } + + /// Open the group of a stream whose header is `hdr` in the copy. + fn open(&mut self, hdr: &lite::Group, version: Version, now: crate::time::Instant) -> Result { + let Some(Accepted { producer, framing }) = &mut self.copy else { + return Err(Error::Cancel); + }; + let group_info = group::Info { sequence: hdr.sequence }; + // Stats (groups/frames/bytes) are counted in the model as the group + // is written, through the tagged `track::Producer`. The track counts + // it as content once its first frame lands. + let received = producer.create_group_at(group_info, hdr.frame_start); + // The subscription's end waits until this stream is read. Counted + // whatever the cache said, since the publisher counts every stream it + // opens (a resubscribe resends a cached group), and only after the + // cache holds the group, so a fetch held for the feed that sees it + // accounted also finds it. + let reading = Reading::open(&self.tail, Some(hdr.sequence), now); + // A route's first group says where its live feed is, when its answer does + // not: even one the cache already holds, as an idle copy asked from its + // head gets back. The copy goes live once the cache shows that group. + // Lite-07's answer does, and a group can overtake it: on a descending + // track the first to land is the newest, not where the feed starts. + if !producer.is_live() { + let first = Position { + group: hdr.sequence, + frame: hdr.frame_start, + }; + if !version.has_track_stream() { + producer.set_live(Some(first), None); + } else if !version.has_largest() { + self.first = self.first.or(Some(first)); + if let (Some(start), Some(first)) = (self.start, self.first) { + producer.set_live(Some(first), Some(start)); + } + } + } + let group = match received { + Ok(group) => group, + // The group is at or past the end the publisher declared, which no + // later stream can repair. Before lite-05 only a local finish sets + // an end, and lite-05 specified an inclusive one, so its last + // group lands on it: drop only that stream there. + Err(Error::Closed) + if !matches!( + version, + Version::Lite01 | Version::Lite02 | Version::Lite03 | Version::Lite04 | Version::Lite05 + ) => + { + tracing::warn!(group = hdr.sequence, "group past the declared end of track"); + let _ = producer.clone().abort(Error::ProtocolViolation); + return Err(Error::ProtocolViolation); + } + Err(err) => return Err(err), + }; + Ok(Opened { + group, + track: producer.clone(), + framing: *framing, + reading, + }) + } +} + impl Subscriber { pub fn new(config: SubscriberConfig) -> Self { // Identity for incoming-hop loop detection. Derived from the local @@ -141,6 +334,7 @@ impl Subscriber { version: config.version, peer_setup: config.peer_setup, setup_waits: Default::default(), + park_waits: Default::default(), cost: config.cost, sources: kio::Queue::new(), going_away: config.going_away, @@ -579,30 +773,40 @@ impl Subscriber { let mut subscribes = self.subscribes.lock(); // An unknown subscription's datagram is dropped below, however it parses. let Some(dg) = lite::Datagram::decode(payload, self.version, ietf, |id| { - subscribes.get(&id).map(|entry| entry.framing).unwrap_or_default() + subscribes + .get(&id) + .and_then(|entry| entry.copy.as_ref()) + .map(|copy| copy.framing) + .unwrap_or_default() })? else { return Ok(()); }; - let Some(entry) = subscribes.get_mut(&dg.subscribe) else { - // Unknown or already-closed subscription: drop the datagram. + // Unknown or already-closed subscription, or one whose TRACK_INFO has not landed + // yet, which a datagram cannot wait for: drop the datagram. + let Some(TrackEntry { + copy: Some(Accepted { producer, framing }), + tail, + .. + }) = subscribes.get_mut(&dg.subscribe) + else { return Ok(()); }; let timestamp = dg .timestamp - .zip(entry.framing.timescale) + .zip(framing.timescale) .map(|(value, scale)| Timestamp::new(value, scale)) .transpose() .map_err(|_| Error::BoundsExceeded(crate::coding::BoundsExceeded))?; // A datagram is never owed a stream, so it never holds the subscription's end open. - if let Ok(mut tail) = entry.tail.write() { + if let Ok(mut tail) = tail.write() { tail.account(dg.sequence..dg.sequence.saturating_add(1), self.runtime.now()); } // A datagram says where the live feed is as well as a group does. Inserted first, so // the copy goes live with it already showing. - let live = entry.producer.is_live(); + let live = producer.is_live(); let datagram = crate::model::datagram::Stored { datagram: crate::Datagram { sequence: dg.sequence, @@ -613,12 +817,12 @@ impl Subscriber { deflated: None, }; match dg.inflated { - Some(size) => entry.producer.insert_deflated_datagram(datagram, size)?, - None => entry.producer.insert_wire_datagram(datagram)?, + Some(size) => producer.insert_deflated_datagram(datagram, size)?, + None => producer.insert_wire_datagram(datagram)?, } // A datagram-only feed may never answer with SUBSCRIBE_START, which waits for a group. if !live { - entry.producer.set_live(Some(Position::group(dg.sequence)), None); + producer.set_live(Some(Position::group(dg.sequence)), None); } Ok(()) } @@ -645,11 +849,16 @@ impl SubscriptionCleanup { /// [`track::Producer::abort`] ends its readers cleanly instead. Before lite-05 the FIN /// is that end, declared here at the live edge since nothing more can arrive. fn abort(&self, err: &Error) { - for (_, entry) in self.0.lock().drain() { + for (_, mut entry) in self.0.lock().drain() { + // A track still waiting on TRACK_INFO has no copy yet: its request rejects with + // the session's end instead. + let Some(Accepted { producer, .. }) = entry.copy.take() else { + continue; + }; if entry.tail.read().is_ended() { - let _ = entry.producer.finish(); + let _ = producer.finish(); } - let _ = entry.producer.abort(err.clone()); + let _ = producer.abort(err.clone()); } } } @@ -719,6 +928,13 @@ impl SubscriberDriver { pub fn poll_drain(&mut self, waiter: &kio::Waiter) -> Poll<()> { let closed = self.uni.poll(waiter).is_ready(); let _ = self.uni.children.poll(waiter); + // Streams parked on a TRACK_INFO wait for their source to read it, which accepts them + // or ends their subscription. Either wakes this. + let parked = self.subscriber.park_waits.load(atomic::Ordering::Relaxed); + if closed && parked > 0 && self.uni.children.len() == parked { + let _ = self.sources.poll(waiter); + return Poll::Pending; + } // Once every remaining stream waits on a SETUP that never arrived, none of // them can deliver it: the peer can open no more. let stuck = self.subscriber.peer_setup.ietf().is_none() @@ -996,6 +1212,8 @@ struct GroupRecv { state: GroupRecvState, /// Held while parked on the peer's SETUP, so a drain can tell it never arrives. setup_wait: Option, + /// Held while parked on the subscription's TRACK_INFO, so a drain reads it first. + park_wait: Option, } /// Counts one GROUP stream parked on the peer's SETUP for as long as it lives. @@ -1022,11 +1240,12 @@ impl GroupRecv { reader, state: GroupRecvState::Start, setup_wait: None, + park_wait: None, } } fn poll_serve(&mut self, waiter: &kio::Waiter) -> Poll> { - let (ietf, hdr) = wait!(Header => { + let header: (bool, lite::Group) = wait!(Header => { // Before the header, which a wait after it would lose. let Poll::Ready(ietf) = self.subscriber.peer_setup.poll_ietf(waiter) else { let waits = &self.subscriber.setup_waits; @@ -1037,56 +1256,47 @@ impl GroupRecv { self.reader.poll_decode::(waiter).map_ok(|hdr| (ietf, hdr)) })?; - let (group, track, framing, reading) = { + // A SUBSCRIBE sent alongside its TRACK may be served before the TRACK_INFO that says + // how to read it: the stream parks, unread, until the accept opens its group. + let parked: Option<(kio::Consumer, u64)> = { let mut subs = self.subscriber.subscribes.lock(); - let entry = subs.get_mut(&hdr.subscribe).ok_or(Error::Cancel)?; - - let group_info = group::Info { sequence: hdr.sequence }; - // Stats (groups/frames/bytes) are counted in the model as the group - // is written, through the tagged `track::Producer`. The track counts - // it as content once its first frame lands. - let received = entry.producer.create_group_at(group_info, hdr.frame_start); - // The subscription's end waits until this stream is read. Counted - // whatever the cache said, since the publisher counts every stream it - // opens (a resubscribe resends a cached group), and only after the - // cache holds the group, so a fetch held for the feed that sees it - // accounted also finds it. - let reading = Reading::open(&entry.tail, Some(hdr.sequence), self.subscriber.runtime.now()); - // A route's first group says where its live feed is, when its answer does - // not: even one the cache already holds, as an idle copy asked from its - // head gets back. The copy goes live once the cache shows that group. - // Lite-07's answer does, and a group can overtake it: on a descending - // track the first to land is the newest, not where the feed starts. - if !entry.producer.is_live() { - let first = Position { - group: hdr.sequence, - frame: hdr.frame_start, - }; - if !self.subscriber.version.has_track_stream() { - entry.producer.set_live(Some(first), None); - } else if !self.subscriber.version.has_largest() { - entry.answered(None, Some(first)); + match subs.get_mut(&header.1.subscribe) { + Some(entry) if entry.copy.is_none() => { + self.park_wait = Some(ParkWait::new(&self.subscriber.park_waits)); + Some(entry.park(&header.1)) } + _ => None, } - let group = match received { - Ok(group) => group, - // The group is at or past the end the publisher declared, which no - // later stream can repair. Before lite-05 only a local finish sets - // an end, and lite-05 specified an inclusive one, so its last - // group lands on it: drop only that stream there. - Err(Error::Closed) - if !matches!( - self.subscriber.version, - Version::Lite01 | Version::Lite02 | Version::Lite03 | Version::Lite04 | Version::Lite05 - ) => - { - tracing::warn!(group = hdr.sequence, "group past the declared end of track"); - let _ = entry.producer.clone().abort(Error::ProtocolViolation); - return Poll::Ready(Err(Error::ProtocolViolation)); - } - Err(err) => return Poll::Ready(Err(err)), - }; - (group, entry.producer.clone(), entry.framing, reading) + }; + // Closed once the subscription went, which the lookup below reports. + let _ = wait!(Accept => match parked { + Some((parked, ticket)) => parked + .poll(waiter, |parked| match parked.opened.contains_key(ticket) { + true => Poll::Ready(()), + false => Poll::Pending, + }) + .map(|res| res.is_ok()), + None => Poll::Ready(true), + }); + self.park_wait = None; + let (ietf, hdr) = header; + + let opened = { + let mut subs = self.subscriber.subscribes.lock(); + let entry = subs.get_mut(&hdr.subscribe).ok_or(Error::Cancel)?; + match &parked { + Some((_, ticket)) => entry.unpark(*ticket), + None => entry.open(&hdr, self.subscriber.version, self.subscriber.runtime.now()), + } + }; + let Opened { + group, + track, + framing, + reading, + } = match opened { + Ok(opened) => opened, + Err(err) => return Poll::Ready(Err(err)), }; let (priority, order) = track .subscription() @@ -1094,9 +1304,8 @@ impl GroupRecv { self.reader .set_priority(crate::priority::recv_group(priority, hdr.sequence, order)); - // The timescale came from TRACK_INFO (read before this subscription was - // even registered), so frames decode immediately. No SUBSCRIBE_OK to - // wait on. + // The timescale came from TRACK_INFO, so frames decode immediately. No + // SUBSCRIBE_OK to wait on. // Guarded: dropping this machine mid-group is a cancellation, not a clean end. let group: crate::recv::Group = crate::recv::Group::new(group); let track: track::Producer = track; @@ -3047,17 +3256,15 @@ mod tests { let mut consumer = track.consume().subscribe(None).await.unwrap(); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track, - framing: lite::Framing { + TrackEntry::accepted( + track, + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - start: None, - first: None, - }, + Default::default(), + ), ); let mut accept = UniAccept::new(subscriber, early); assert!(accept.poll(&kio::Waiter::noop()).is_pending()); @@ -3109,17 +3316,15 @@ mod tests { track.finish_at(Position::group(3), None).unwrap(); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track.clone(), - framing: lite::Framing { + TrackEntry::accepted( + track.clone(), + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - start: None, - first: None, - }, + Default::default(), + ), ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); @@ -3212,16 +3417,10 @@ mod tests { "video", info, ); - subscriber.subscribes.lock().insert( - 7, - TrackEntry { - producer: track.clone(), - framing, - tail: Default::default(), - start: None, - first: None, - }, - ); + subscriber + .subscribes + .lock() + .insert(7, TrackEntry::accepted(track.clone(), framing, Default::default())); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); let mut group = GroupRecv::new( @@ -3398,17 +3597,15 @@ mod tests { before.finish().unwrap(); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track.clone(), - framing: lite::Framing { + TrackEntry::accepted( + track.clone(), + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - start: None, - first: None, - }, + Default::default(), + ), ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); @@ -3481,13 +3678,7 @@ mod tests { cached.write_frame(Timestamp::ZERO, &b"0"[..]).unwrap(); cached.finish().unwrap(); track.set_idle(); - let mut entry = TrackEntry { - producer: track.clone(), - framing: Default::default(), - tail: Default::default(), - start: None, - first: None, - }; + let mut entry = TrackEntry::accepted(track.clone(), Default::default(), Default::default()); // The route resends from the cached head, newest first. if header_first { @@ -3561,17 +3752,15 @@ mod tests { let tail = kio::Producer::new(Tail::default()); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track.clone(), - framing: lite::Framing { + TrackEntry::accepted( + track.clone(), + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: tail.clone(), - start: None, - first: None, - }, + tail.clone(), + ), ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); @@ -4085,6 +4274,96 @@ mod tests { } } + /// A failed subscription reads a stream whose header arrived to its end before it gives + /// the track back, but only while someone holds the copy: once nobody does, a stream the + /// publisher never finishes cannot keep the subscription. + #[kio::test] + async fn a_failed_subscription_drains_only_while_wanted() { + for wanted in [true, false] { + let version = Version::Lite06; + let mut responses = Vec::new(); + lite::SubscribeResponse::Start(lite::SubscribeStart { + largest: None, + starts: vec![Some(0)], + }) + .encode( + &mut crate::coding::Encoder::new(&mut responses, version.into()), + version, + ) + .unwrap(); + let session = crate::lite::test_transport::ScriptedSession::new(responses); + let subscriber = Subscriber::new(SubscriberConfig { + runtime: crate::time::sim(), + session: crate::driver::Transport::new(session.clone()), + origin: origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + recv_bandwidth: None, + version, + peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Address::Setup), + peer_hop: None, + cost: None, + going_away: Default::default(), + auth: crate::auth::Link::new(false), + }); + let serve = TrackServe { + validity: Default::default(), + route: None, + subscriber, + path: Path::from_static("room"), + epoch: None, + name: "video".to_string(), + }; + let broadcast = crate::broadcast::Info::new().produce(); + let serving = ServeLoop::new( + &serve, + broadcast.reserve_track("video").unwrap(), + crate::track::Info::timed(), + ); + let mut reader = broadcast + .consume() + .track("video") + .unwrap() + .subscribe(None) + .await + .unwrap(); + let gate = serve.gate(); + let mut running = TrackServeRun { + serve, + state: TrackRunState::Serve(serving), + gate, + }; + assert!(kio::Task::poll(&mut running, &kio::Waiter::noop()).is_pending()); + + // A group stream whose header arrived, still being read. + let reading = { + let subscribes = running.serve.subscriber.subscribes.lock(); + let (_, entry) = subscribes.iter().next().expect("subscribed"); + Reading::open(&entry.tail, Some(0), running.serve.subscriber.runtime.now()) + }; + session.close(crate::lite::test_transport::Close::ResetStream(64 + 7)); + assert!( + kio::Task::poll(&mut running, &kio::Waiter::noop()).is_pending(), + "wanted={wanted}: the stream being read holds the track" + ); + + match wanted { + true => { + drop(reading); + assert!(kio::Task::poll(&mut running, &kio::Waiter::noop()).is_ready()); + assert!(reader.recv_group().await.is_err(), "the track is given back failed"); + } + false => { + drop(reader); + assert!( + kio::Task::poll(&mut running, &kio::Waiter::noop()).is_ready(), + "nobody holds the copy" + ); + assert!(running.serve.subscriber.subscribes.lock().is_empty()); + drop(reading); + } + } + } + } + /// Both runtimes abort a track whose tail the other's publisher still owed when the /// session died. #[kio::test] @@ -4127,17 +4406,15 @@ mod tests { let mut received = producer.subscribe(None); subscriber.subscribes.lock().insert( 1, - TrackEntry { + TrackEntry::accepted( producer, - framing: lite::Framing { + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - start: None, - first: None, - }, + Default::default(), + ), ); if version.has_padding() { @@ -4145,7 +4422,18 @@ mod tests { for body in [vec![1], vec![1; 1200], vec![1; 1201]] { subscriber.route_datagram(body.into()).unwrap(); assert!(received.recv_datagram().now_or_never().is_none()); - assert!(!subscriber.subscribes.lock().get(&1).unwrap().producer.is_live()); + assert!( + !subscriber + .subscribes + .lock() + .get(&1) + .unwrap() + .copy + .as_ref() + .unwrap() + .producer + .is_live() + ); } } @@ -4397,6 +4685,225 @@ mod tests { } } + /// A subscriber on `version` over `session`, whose peer's SETUP was already read. + fn pipelining(session: crate::driver::Transport, version: Version) -> Subscriber { + let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); + peer_setup.set(lite::Setup::default()); + Subscriber::new(SubscriberConfig { + runtime: crate::time::sim(), + session, + origin: origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + recv_bandwidth: None, + version, + peer_setup, + peer_hop: None, + cost: None, + going_away: Default::default(), + auth: crate::auth::Link::new(false), + }) + } + + /// A subscription to `video` over `subscriber`, waiting on the run that serves it. + fn subscribing( + subscriber: &Subscriber, + subscription: Subscription, + ) -> (kio::Pending, TrackServeRun) { + let broadcast = crate::broadcast::Info::new().produce(); + let mut dynamic = broadcast.requests(); + let mut waiting = broadcast.consume().track("video").unwrap().subscribe(subscription); + assert!((&mut waiting).now_or_never().is_none(), "waiting on the publisher"); + let request = dynamic.requested_track().now_or_never().unwrap().expect("request"); + let serve = TrackServe { + validity: Default::default(), + route: None, + subscriber: subscriber.clone(), + path: Path::from_static("room"), + epoch: None, + name: "video".to_string(), + }; + (waiting, TrackServeRun::new(serve, request)) + } + + /// What `session` wrote on its `stream`th send stream. + fn sent(log: &crate::lite::test_transport::Log, stream: usize) -> Vec { + log.trail() + .into_iter() + .filter(|(id, _)| *id == stream) + .filter_map(|(_, sent)| match sent { + crate::lite::test_transport::Sent::Write(bytes) => Some(bytes), + _ => None, + }) + .flatten() + .collect() + } + + /// The SUBSCRIBE a request stream carried, and whether anything followed it. + fn sent_subscribe(wire: &[u8], version: Version) -> (lite::Subscribe<'static>, bool) { + let mut wire = wire; + assert_eq!( + crate::coding::decode_buf(&mut wire, version, |r, v| lite::ControlType::decode(r, v)).unwrap(), + lite::ControlType::Subscribe + ); + let msg = crate::coding::decode_from(&mut wire, version, lite::Subscribe::decode).unwrap(); + (msg.into_owned(), !wire.is_empty()) + } + + /// On lite-05 and later the SUBSCRIBE goes out beside its TRACK, registered before the + /// TRACK_INFO its copy is accepted with. + #[kio::test] + async fn a_subscribe_goes_out_with_its_track() { + for version in [Version::Lite05, Version::Lite06, Version::Lite07] { + let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![Vec::new(), Vec::new()]); + let subscriber = pipelining(crate::driver::Transport::new(session.clone()), version); + let (mut waiting, mut run) = subscribing(&subscriber, Subscription::default().with_priority(3)); + assert!(kio::Task::poll(&mut run, &kio::Waiter::noop()).is_pending()); + + let track = sent(&session.log, 0); + let mut track = track.as_slice(); + assert_eq!( + crate::coding::decode_buf(&mut track, version, |r, v| lite::ControlType::decode(r, v)).unwrap(), + lite::ControlType::Track, + "{version}: the TRACK opens first" + ); + let (msg, trailing) = sent_subscribe(&sent(&session.log, 1), version); + assert_eq!((msg.id, msg.track.as_ref(), msg.priority), (0, "video", 3), "{version}"); + assert!(!trailing, "{version}"); + assert!( + subscriber + .subscribes + .lock() + .get(&0) + .is_some_and(|entry| entry.copy.is_none()), + "{version}: registered with its copy pending" + ); + assert!((&mut waiting).now_or_never().is_none(), "{version}: no TRACK_INFO yet"); + } + } + + /// Accepting the TRACK_INFO sends no SUBSCRIBE_UPDATE, even when the publisher keeps + /// groups for less than the subscriber waits: the publisher applies its own max age. + #[kio::test] + async fn accepting_track_info_sends_no_subscribe_update() { + for version in [Version::Lite05, Version::Lite06, Version::Lite07] { + let mut info = Vec::new(); + lite::TrackInfo { + refresh: None, + priority: 0, + max_age: Some(Duration::from_secs(1)), + timescale: Some(Timescale::MILLI), + compress: false, + } + .encode(&mut crate::coding::Encoder::new(&mut info, version.into()), version) + .unwrap(); + let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![info, Vec::new()]); + let subscriber = pipelining(crate::driver::Transport::new(session.clone()), version); + let wait = Duration::from_secs(10); + let (waiting, mut run) = subscribing(&subscriber, Subscription::default().with_max_delay(wait)); + for _ in 0..4 { + let _ = kio::Task::poll(&mut run, &kio::Waiter::noop()); + } + waiting.now_or_never().expect("accepted").unwrap(); + + let (msg, trailing) = sent_subscribe(&sent(&session.log, 1), version); + assert_eq!(msg.max_delay, wait, "{version}: the subscriber's own max delay"); + assert!(!trailing, "{version}: a SUBSCRIBE_UPDATE followed the accept"); + } + } + + /// A TRACK reset while its SUBSCRIBE is live rejects the request, so the origin can try + /// another route, and resets the SUBSCRIBE. + #[kio::test] + async fn a_reset_track_rejects_the_request_and_resets_its_subscribe() { + for version in [Version::Lite05, Version::Lite06, Version::Lite07] { + let session = crate::lite::test_transport::ScriptedSession::per_stream_reset(vec![Vec::new(), Vec::new()]); + let subscriber = pipelining(crate::driver::Transport::new(session.clone()), version); + let (waiting, mut run) = subscribing(&subscriber, Subscription::default()); + assert!(kio::Task::poll(&mut run, &kio::Waiter::noop()).is_ready()); + + assert!(waiting.now_or_never().expect("rejected").is_err(), "{version}"); + assert!( + session + .log + .trail() + .iter() + .any(|(stream, sent)| *stream == 1 && matches!(sent, crate::lite::test_transport::Sent::Reset(_))), + "{version}: the SUBSCRIBE was not reset" + ); + assert!(subscriber.subscribes.lock().is_empty(), "{version}"); + } + } + + /// A group stream that beats its subscription's TRACK_INFO waits after its header, + /// unread, and the accept opens the groups that waited in the order they arrived. + #[kio::test] + async fn a_group_that_beats_its_track_info_waits_for_it() { + let version = Version::Lite07; + let group = |sequence: u64, payload: u8| { + let mut bytes = Vec::new(); + let mut w = crate::coding::Encoder::new(&mut bytes, version.into()); + lite::DataType::Group.encode(&mut w, version).unwrap(); + lite::Group { + subscribe: 0, + sequence, + frame_start: 0, + } + .encode(&mut w, version) + .unwrap(); + w.varint(0).unwrap(); + w.varint(1).unwrap(); + w.u8(payload); + bytes + }; + let session = crate::lite::test_transport::ScriptedSession::eof(Vec::new()) + .with_incoming_unis(vec![group(3, 30), group(2, 20)]); + let subscriber = pipelining(crate::driver::Transport::new(session.clone()), version); + subscriber + .subscribes + .lock() + .insert(0, TrackEntry::new(None, Default::default())); + let mut accept = UniAccept::new(subscriber.clone(), Vec::new()); + assert!(accept.poll(&kio::Waiter::noop()).is_pending()); + assert_eq!(subscriber.park_waits.load(atomic::Ordering::Relaxed), 2); + assert!(session.log.stops().is_empty(), "a parked stream is kept"); + + let track = crate::track::Producer::new( + Arc::new(crate::broadcast::Info::default()), + "video", + crate::track::Info::default().with_timescale(Timescale::MILLI), + ); + // Every group, oldest first, however late. + let every = Subscription::default() + .with_order(crate::track::Order::Ascending) + .with_start(0) + .with_max_delay(Duration::from_secs(60)); + let mut consumer = track.consume().subscribe(every).await.unwrap(); + let framing = lite::Framing::new(track.info(), version); + subscriber.subscribes.lock().get_mut(&0).unwrap().accept( + Accepted { + producer: track.clone(), + framing, + }, + version, + subscriber.runtime.now(), + ); + // Both opened at the accept, the first to arrive first. + assert_eq!(track.latest_sequence(), Some(3)); + + assert!(accept.poll(&kio::Waiter::noop()).is_pending()); + assert_eq!(subscriber.park_waits.load(atomic::Ordering::Relaxed), 0); + let mut read = Vec::new(); + for _ in 0..2 { + let mut group = consumer.recv_group().await.unwrap().unwrap(); + read.push((group.sequence, group.read_frame().await.unwrap().unwrap().payload[0])); + } + read.sort_unstable(); + assert_eq!(read, [(2, 20), (3, 30)]); + assert!( + subscriber.subscribes.lock().get(&0).unwrap().parked.is_none(), + "released once every stream took its group" + ); + } + /// TRACK opens its stream before the fetches sent alongside it: TRACK fails fast without /// stream credit, where a fetch waits for it, so fetches opened first could leave TRACK /// none and fail the track with every fetch. @@ -6625,17 +7132,15 @@ mod tests { ); subscriber.subscribes.lock().insert( 7, - TrackEntry { - producer: track.clone(), - framing: lite::Framing { + TrackEntry::accepted( + track.clone(), + lite::Framing { timescale: Some(Timescale::MILLI), ietf: None, compressed: false, }, - tail: Default::default(), - start: None, - first: None, - }, + Default::default(), + ), ); let mut held = Vec::new(); for start in (1..=8).rev() { @@ -7221,39 +7726,8 @@ impl TrackServe { pref: Option, supports_update: bool, ) -> Result { - // An empty half-open range asks for nothing, and the wire cannot say that: its - // bounds are inclusive, so the nearest encoding either hands back the position - // the caller excluded or inverts the range outright once the two bounds meet. No - // demand at all is the faithful translation. An absent start is the live edge, wherever that lands, so the only end that is - // certainly empty is the very first position, which is what `Position::default()` - // stands in for. - let pref = pref.filter(|sub| sub.end().is_none_or(|end| end > sub.start().unwrap_or_default())); - let ranged = self.subscriber.version.has_ranges(); - let request = match pref { - Some(subscription) => { - // Lite-07 carries every range asked for; an older wire one span covering them. - let mut subscription = match ranged { - true => subscription, - false => wire_span(subscription), - }; - // An idle copy is asked from the head of the newest group it cached when - // the subscriber has no start of its own: a quiet track (a catalog) sends - // that group again, which says the cache is current. A lite-07 list keeps - // any range of its own, so only one asking just for the live or oldest group - // counts. - let idle = subscription.start().is_none() - && (!ranged - || subscription - .ranges - .iter() - .all(|range| range.end.is_none() && matches!(range.start, Anchor::Relative(0 | u64::MAX)))); - if matches!(sub, Sub::None) && idle { - subscription = subscription.with_start(producer.idle_newest()); - } - wire_ranges(&subscription, producer.info().timescale)?.map(|ranges| (subscription, ranges)) - } - None => None, - }; + let opening = matches!(sub, Sub::None); + let request = self.wire_request(pref, opening, producer.idle_newest(), producer.info().timescale)?; match request { Some((subscription, ranges)) => { @@ -7275,7 +7749,7 @@ impl TrackServe { start..floor.unwrap_or(u64::MAX) }); if let Ok(mut tail) = active.tail.write() { - tail.set_grace(tail::grace(subscription.budget())); + tail.set_grace(producer.tail_grace(subscription.budget())); if let Some(groups) = lowered { tail.demand(groups, self.subscriber.runtime.now()); } @@ -7319,15 +7793,71 @@ impl TrackServe { } } + /// What a SUBSCRIBE carries for the demand `pref`: the subscription and its ranges, or + /// `None` when it asks for nothing. + /// + /// `opening` when this opens a subscription for a copy whose newest cached group, while + /// idle, is `newest`. `timescale` is the track's, which only a time anchor needs. + fn wire_request( + &self, + pref: Option, + opening: bool, + newest: Option, + timescale: Option, + ) -> Result)>, Error> { + // An empty half-open range asks for nothing, and the wire cannot say that: its + // bounds are inclusive, so the nearest encoding either hands back the position + // the caller excluded or inverts the range outright once the two bounds meet. No + // demand at all is the faithful translation. An absent start is the live edge, wherever that lands, so the only end that is + // certainly empty is the very first position, which is what `Position::default()` + // stands in for. + let Some(subscription) = pref.filter(|sub| sub.end().is_none_or(|end| end > sub.start().unwrap_or_default())) + else { + return Ok(None); + }; + let ranged = self.subscriber.version.has_ranges(); + // Lite-07 carries every range asked for; an older wire one span covering them. + let mut subscription = match ranged { + true => subscription, + false => wire_span(subscription), + }; + // An idle copy is asked from the head of the newest group it cached when + // the subscriber has no start of its own: a quiet track (a catalog) sends + // that group again, which says the cache is current. A lite-07 list keeps + // any range of its own, so only one asking just for the live or oldest group + // counts. + let idle = subscription.start().is_none() + && (!ranged + || subscription + .ranges + .iter() + .all(|range| range.end.is_none() && matches!(range.start, Anchor::Relative(0 | u64::MAX)))); + if opening && idle { + subscription = subscription.with_start(newest); + } + Ok(wire_ranges(&subscription, timescale)?.map(|ranges| (subscription, ranges))) + } + + /// Open the SUBSCRIBE for `pref` alongside the TRACK still waiting on its TRACK_INFO, + /// registered with its copy pending. `None` when it asks for nothing, or names a time + /// that needs the track's timescale, so it waits for the info. + fn pipeline(&self, pref: Subscription) -> Result, Error> { + // A copy not accepted yet caches nothing. + let (subscription, ranges) = match self.wire_request(Some(pref.clone()), true, None, None) { + Ok(Some(request)) => request, + Ok(None) | Err(Error::TimestampMismatch) => return Ok(None), + Err(err) => return Err(err), + }; + let establish = self.register(subscription, ranges, None); + Ok(Some(Pipelined { + pref, + state: PipelinedState::Opening(establish), + })) + } + /// Allocate the id, set the demand floor, and register the subscription, so the /// returned [`Establish`] can put the SUBSCRIBE on the wire. /// - /// Registration happens here, before any of it reaches the transport: `id` is - /// live the moment the peer reads it, and a publisher may serve its first group - /// immediately, so a late insert races the group stream (a group whose id isn't - /// in the map yet is dropped, stalling the track forever). The caller - /// deregisters `id` if the establish fails. - /// /// The subscription's bounds come straight from the demand aggregate. fn prepare_establish( &self, @@ -7335,32 +7865,68 @@ impl TrackServe { subscription: Subscription, ranges: Vec, ) -> Establish { - let id = self.subscriber.next_id.fetch_add(1, atomic::Ordering::Relaxed); + self.request_floor(producer, subscription.start()); + let framing = lite::Framing::new(producer.info(), self.subscriber.version); + self.register( + subscription, + ranges, + Some(Accepted { + producer: producer.clone(), + framing, + }), + ) + } - // The floor tracks the requested start until this subscription's own - // SUBSCRIBE_START refines it: the peer never serves below the request, a - // previous subscription's declaration must not outlive its demand, and - // live-edge demand (None) starts with no floor at all. Lite-06+ only resolves - // the start with its SUBSCRIBE_START, so until then the floor is just a request. - let floor = subscription.start(); + /// The floor tracks the requested start until this subscription's own + /// SUBSCRIBE_START refines it: the peer never serves below the request, a + /// previous subscription's declaration must not outlive its demand, and + /// live-edge demand (None) starts with no floor at all. Lite-06+ only resolves + /// the start with its SUBSCRIBE_START, so until then the floor is just a request. + fn request_floor(&self, producer: &mut track::Producer, floor: Option) { let _ = match self.subscriber.version.resolves_start() { true => producer.request_start(floor), false => producer.start_at(floor), }; + } + + /// Give a pipelined subscription its accepted copy, the target its parked group streams + /// resume into. + fn accept_pipelined(&self, id: u64, producer: &track::Producer) { + let framing = lite::Framing::new(producer.info(), self.subscriber.version); + let mut subscribes = self.subscriber.subscribes.lock(); + let Some(entry) = subscribes.get_mut(&id) else { + return; + }; + let copy = Accepted { + producer: producer.clone(), + framing, + }; + entry.accept(copy, self.subscriber.version, self.subscriber.runtime.now()); + } + + /// Allocate the id and register the subscription, so the returned [`Establish`] can + /// put the SUBSCRIBE on the wire. + /// + /// Registration happens here, before any of it reaches the transport: `id` is + /// live the moment the peer reads it, and a publisher may serve its first group + /// immediately, so a late insert races the group stream (a group whose id isn't + /// in the map yet is dropped, stalling the track forever). The caller + /// deregisters `id` if the establish fails. + fn register(&self, subscription: Subscription, ranges: Vec, copy: Option) -> Establish { + let id = self.subscriber.next_id.fetch_add(1, atomic::Ordering::Relaxed); tracing::debug!(id, broadcast = %self.subscriber.log_path(&self.path), track = %self.name, "subscribe started"); - let tail = kio::Producer::new(Tail::new(tail::grace(subscription.budget()))); - self.subscriber.subscribes.lock().insert( - id, - TrackEntry { - producer: producer.clone(), - framing: lite::Framing::new(producer.info(), self.subscriber.version), - tail: tail.clone(), - start: None, - first: None, - }, - ); + // A pipelined SUBSCRIBE has no copy, so no max age, until it is adopted. + let grace = match © { + Some(copy) => copy.producer.tail_grace(subscription.budget()), + None => tail::grace(subscription.budget()), + }; + let tail = kio::Producer::new(Tail::new(grace)); + self.subscriber + .subscribes + .lock() + .insert(id, TrackEntry::new(copy, tail.clone())); let session = self.subscriber.session.clone(); Establish { @@ -7564,6 +8130,57 @@ impl Establish { } } +/// A SUBSCRIBE sent alongside its TRACK, before the TRACK_INFO its copy is accepted with. +struct Pipelined { + /// The demand it asked for, which later demand changes are measured against. + pref: Subscription, + state: PipelinedState, +} + +#[expect( + clippy::large_enum_variant, + reason = "a state machine's enum is its storage: one transient instance per stream, so the big variant is the working state, not padding held in bulk" +)] +enum PipelinedState { + Opening(Establish), + Open(SubStream), +} + +impl Pipelined { + fn id(&self) -> u64 { + match &self.state { + PipelinedState::Opening(establish) => establish.id, + PipelinedState::Open(active) => active.id, + } + } + + /// Drive the SUBSCRIBE onto the wire; its responses wait for the accept. Only a failure + /// to send it is ready. + fn poll(&mut self, waiter: &kio::Waiter) -> Poll { + let PipelinedState::Opening(establish) = &mut self.state else { + return Poll::Pending; + }; + match ready!(establish.poll(waiter)) { + Ok(active) => { + self.state = PipelinedState::Open(active); + Poll::Pending + } + Err(err) => Poll::Ready(err), + } + } + + /// Give up on the subscription with its track, resetting it with `err`. + fn abort(self, serve: &TrackServe, err: &Error) { + match self.state { + PipelinedState::Opening(establish) => establish.abort(err), + PipelinedState::Open(active) => { + serve.subscriber.remove_subscribe(active.id); + active.stream.writer.abort(err); + } + } + } +} + /// Drives one [`TrackServe`]: the TRACK_INFO fetch, then the serve loop, then /// the teardown that decides how the origin sees this copy end. struct TrackServeRun { @@ -7579,12 +8196,14 @@ struct TrackServeRun { )] enum TrackRunState { /// Lite05+ learns the track's immutable properties once, up front, via a - /// TRACK stream. The timescale then flows into every SUBSCRIBE and FETCH - /// without a per-response header. Fetches go out meanwhile, and read their - /// response only once the info lands. + /// TRACK stream. The timescale then flows into every FETCH without a + /// per-response header. A SUBSCRIBE and any fetches go out alongside, rather + /// than a round trip later: they need the info only to read what they receive. Info { request: Option, info: TrackInfoFetch, + /// The SUBSCRIBE sent alongside, once anyone subscribes. + pipelined: Option, dynamic: track::Requests, fetches: kio::Tasks, known: crate::util::Learning, @@ -7610,6 +8229,7 @@ impl TrackServeRun { dynamic: request.requests(), request: Some(request), info: TrackInfoFetch::new(&serve), + pipelined: None, fetches: kio::Tasks::new(), known: crate::util::Learning::new(), _working: serve.subscriber.session.work().start(), @@ -7629,12 +8249,21 @@ impl TrackServeRun { fn revoke(&mut self) { tracing::info!(broadcast = %self.serve.subscriber.log_path(&self.serve.path), track = %self.serve.name, "subscription no longer authorized"); match std::mem::replace(&mut self.state, TrackRunState::Done) { - TrackRunState::Info { request, known, .. } => { + TrackRunState::Info { + request, + pipelined, + known, + .. + } => { // Each fetch sent alongside resets itself as it drops, seeing the denial. known.resolve(Err(Error::Unauthorized)); if let Some(request) = request { request.reject(Error::Unauthorized); } + // Reset like a live one. + if let Some(pipelined) = pipelined { + pipelined.abort(&self.serve, &Error::Unauthorized); + } } TrackRunState::Serve(serve_loop) => { let _ = serve_loop.serving.abort(Error::Unauthorized); @@ -7667,8 +8296,13 @@ impl TrackServeRun { let pending = request.as_ref().expect("request pending"); ready!(crate::util::poll_unwanted(&mut pending.demand(), waiter)); if pending.reject_unwanted(Error::Cancel) { - if let TrackRunState::Info { known, .. } = std::mem::replace(&mut self.state, TrackRunState::Done) { + if let TrackRunState::Info { pipelined, known, .. } = + std::mem::replace(&mut self.state, TrackRunState::Done) + { known.resolve(Err(Error::Cancel)); + if let Some(pipelined) = pipelined { + pipelined.abort(&self.serve, &Error::Cancel); + } } return Poll::Ready(()); } @@ -7700,8 +8334,9 @@ impl kio::Task for TrackServeRun { loop { match &mut self.state { TrackRunState::Info { - request: _, + request, info, + pipelined, dynamic, fetches, known, @@ -7712,15 +8347,33 @@ impl kio::Task for TrackServeRun { fetches.push(FillServeRun::new(self.serve.clone(), fill, known.later())); } // Abandonment was checked at the top of this poll, arming its wait. - let res = info.poll_fetch(&self.serve, waiter); + let pending = request.as_mut().expect("request pending"); + // Demand that arrives before the info goes upstream with it, the TRACK + // first. One that needs the info to say what it asks for waits for it. + let mut started = Ok(()); + if pipelined.is_none() + && let Poll::Ready(Some(pref)) = pending.poll_subscription_changed(waiter) + { + started = self.serve.pipeline(pref).map(|subscribing| *pipelined = subscribing); + } // TRACK opens first: its stream fails fast without credit, where a fetch's // waits for it, so fetches opened ahead could starve their own prerequisite. - let Poll::Ready(res) = res else { - let _ = fetches.poll(waiter); - return Poll::Pending; + let fetched = info.poll_fetch(&self.serve, waiter); + let failed = match pipelined { + Some(subscribing) => subscribing.poll(waiter), + None => Poll::Pending, + }; + let res = match (started, failed, fetched) { + (Err(err), _, _) | (_, Poll::Ready(err), _) => Err(err), + (_, _, Poll::Ready(res)) => res, + (_, _, Poll::Pending) => { + let _ = fetches.poll(waiter); + return Poll::Pending; + } }; let TrackRunState::Info { request, + pipelined, dynamic, fetches, known, @@ -7737,6 +8390,9 @@ impl kio::Task for TrackServeRun { info.unchecked = !self.serve.subscriber.version.has_media_time(); let mut serve_loop = ServeLoop::adopt(&self.serve, request, info.clone(), dynamic, fetches); serve_loop.held = Some(held); + if let Some(pipelined) = pipelined { + serve_loop.adopt_pipelined(&self.serve, pipelined); + } // Only once accepted: a fetch answered now caches into the accepted copy. known.resolve(Ok(info)); self.state = TrackRunState::Serve(serve_loop); @@ -7756,7 +8412,10 @@ impl kio::Task for TrackServeRun { drop(fetches); // Rejecting the request lets the origin retry (bounded) on // another source; waiting subscribers stall rather than error - // meanwhile. + // meanwhile. The SUBSCRIBE sent alongside ends with it. + if let Some(pipelined) = pipelined { + pipelined.abort(&self.serve, &err); + } request.reject(err); return Poll::Ready(()); } @@ -7817,9 +8476,15 @@ impl kio::Task for TrackServeRun { impl Drop for TrackServeRun { fn drop(&mut self) { - // Still waiting on TRACK_INFO: the request never reached the subscribe map, - // so the driver's abort cannot see it. Reject with the session's error. - let TrackRunState::Info { request, known, .. } = &mut self.state else { + // Still waiting on TRACK_INFO: the request has no copy for the driver's abort to + // end. Reject with the session's error. + let TrackRunState::Info { + request, + pipelined, + known, + .. + } = &mut self.state + else { return; }; let Some(request) = request.take() else { @@ -7827,6 +8492,9 @@ impl Drop for TrackServeRun { }; let err = self.serve.subscriber.end_reason(); known.resolve(Err(err.clone())); + if let Some(pipelined) = pipelined.take() { + pipelined.abort(&self.serve, &err); + } request.reject(err); } } @@ -7861,12 +8529,10 @@ impl TrackInfoFetch { ) -> Poll> { #[every_poll] self.validity.check(waiter)?; + // A withdrawn route fails an info still owed. One already delivered stays usable, like + // one read before the withdrawal: a SUBSCRIBE sent alongside may already be served. #[every_poll] - if serve - .route - .as_ref() - .is_some_and(|route| route.poll_closed(waiter).is_ready()) - { + if !matches!(self.state, TrackInfoState::Read(_)) && withdrawn(serve, waiter) { return Poll::Ready(Err(Error::Unroutable)); } @@ -7885,8 +8551,14 @@ impl TrackInfoFetch { if let Poll::Ready(err) = self.closed.poll_closed(waiter) { return Poll::Ready(Err(crate::lite::error::from_transport(err))); } - let ietf = ready!(serve.subscriber.peer_setup.poll_ietf(waiter)); - stream.reader.poll_decode::(waiter).map_ok(|info| (ietf, info)) + let read = match serve.subscriber.peer_setup.poll_ietf(waiter) { + Poll::Ready(ietf) => stream.reader.poll_decode::(waiter).map_ok(|info| (ietf, info)), + Poll::Pending => Poll::Pending, + }; + if read.is_pending() && withdrawn(serve, waiter) { + return Poll::Ready(Err(Error::Unroutable)); + } + read })?; // Publisher Max Age rides on the wire, so the local retention @@ -7914,6 +8586,28 @@ impl TrackInfoFetch { } } +/// Ready once no group stream that beat `sub`'s accept still waits to be read: they arrived +/// before whatever ends the subscription, so they are delivered first. +fn poll_unparked(sub: &Sub, serve: &TrackServe, waiter: &kio::Waiter) -> Poll<()> { + let Sub::Active(active) = sub else { + return Poll::Ready(()); + }; + match serve.subscriber.subscribes.lock().get(&active.id) { + Some(TrackEntry { + parked: Some(parked), .. + }) => parked.poll_closed(waiter), + _ => Poll::Ready(()), + } +} + +/// Whether the advertisement that minted `serve`'s source was withdrawn. +fn withdrawn(serve: &TrackServe, waiter: &kio::Waiter) -> bool { + serve + .route + .as_ref() + .is_some_and(|route| route.poll_closed(waiter).is_ready()) +} + /// An answered TRACK stream, kept open as interest in the track: the publisher holds the /// track for us until we FIN it, which dropping this does. Held until the SUBSCRIBE that /// follows has its first response, or until nobody wants this copy, so demand never @@ -7963,6 +8657,10 @@ struct ServeLoop { /// The ranges of a bounded lite-07 subscription the publisher covered, while the /// demand still asks for just them: subscribing again would only resend them. covered: Option>, + /// A pipelined subscription whose group streams stay parked until the responses that + /// beat the accept are read, as they would have been without the wait: a + /// SUBSCRIBE_START says where the feed starts before its first group does. + parked: Option, /// Our side of the covered subscription, finished and held until the publisher reads /// the FIN, since dropping it would reset it. The subscription keeps the session busy /// until then. @@ -7993,6 +8691,14 @@ enum ServeMode { /// subscription, after which the copy stays for its readers. track: bool, }, + /// The upstream subscription failed with `err`. Every group stream whose header arrived + /// before it, parked on the accept or not, is read to its end first, as a group ends on + /// its own stream's FIN or reset: then the track is given back. + Failed { + err: Error, + /// The subscription's tail, whose readers say which streams are still being read. + tail: kio::Consumer, + }, } /// Fills a pre-lite-05 upstream has no FETCH for, held while the live subscription may @@ -8242,6 +8948,38 @@ impl ServeLoop { held: None, covered: None, closing: None, + parked: None, + } + } + + /// Resume the parked group streams into the accepted copy. + fn unpark(&mut self, serve: &TrackServe) { + if let Some(id) = self.parked.take() { + serve.accept_pipelined(id, &self.serving); + } + } + + /// Take over the SUBSCRIBE sent alongside the TRACK, now that its copy is accepted. Its + /// demand counts as asked for, so only a change since goes out as an update. + fn adopt_pipelined(&mut self, serve: &TrackServe, pipelined: Pipelined) { + let floor = match &pipelined.state { + PipelinedState::Opening(establish) => establish.subscription.start(), + PipelinedState::Open(active) => active.requested, + }; + serve.request_floor(&mut self.serving, floor); + // The tail's grace waited on the max age the accepted copy now carries. + let tail = match &pipelined.state { + PipelinedState::Opening(establish) => &establish.tail, + PipelinedState::Open(active) => &active.tail, + }; + if let Ok(mut tail) = tail.write() { + tail.set_grace(self.serving.tail_grace(pipelined.pref.budget())); + } + self.parked = Some(pipelined.id()); + self.serving.seen_subscription(pipelined.pref); + match pipelined.state { + PipelinedState::Opening(establish) => self.mode = ServeMode::Establish(establish), + PipelinedState::Open(active) => self.sub = Sub::Active(active), } } @@ -8271,12 +9009,7 @@ impl ServeLoop { fn poll(&mut self, serve: &TrackServe, waiter: &kio::Waiter) -> Poll { // Immutable TRACK_INFO stays usable locally after withdrawal. Its wire hold // is only a bridge to SUBSCRIBE while this source is still advertised. - if self.held.is_some() - && serve - .route - .as_ref() - .is_some_and(|route| route.poll_closed(waiter).is_ready()) - { + if self.held.is_some() && withdrawn(serve, waiter) { self.held = None; } let mut budget = kio::coop::Budget::new(32); @@ -8296,7 +9029,40 @@ impl ServeLoop { } } } + ServeMode::Failed { .. } => { + // The accept counts each parked stream as read until its group settles, so + // the wait below covers the ones it opens. + self.unpark(serve); + let ServeMode::Failed { tail, .. } = &self.mode else { + unreachable!() + }; + let drained = tail + .poll(waiter, |tail| match tail.is_reading() { + true => Poll::Pending, + false => Poll::Ready(()), + }) + .is_ready(); + let _ = self.fetches.poll(waiter); + self.feed_fetches + .poll(&self.sub, serve.subscriber.runtime.now(), waiter); + if !drained { + // The groups are drained only for a reader: once nobody holds the + // copy, a stream the publisher never finishes must not hold it. + if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() { + return Poll::Ready(ServeEnd::Idle); + } + return Poll::Pending; + } + let ServeMode::Failed { err, .. } = std::mem::replace(&mut self.mode, ServeMode::Select) else { + unreachable!() + }; + return Poll::Ready(ServeEnd::GiveBack(err)); + } ServeMode::Tail { settle, owed, track } => { + // The tail waits for groups, parked ones included. + if let Some(id) = self.parked.take() { + serve.accept_pipelined(id, &self.serving); + } let _ = self.fetches.poll(waiter); self.feed_fetches .poll(&self.sub, serve.subscriber.runtime.now(), waiter); @@ -8341,6 +9107,9 @@ impl ServeLoop { // once the declared end settled. Before lite-05 the FIN is that end, as in // `SubscriptionCleanup`. if let Poll::Ready(err) = self.closed.poll_closed(waiter) { + if poll_unparked(&self.sub, serve, waiter).is_pending() { + return Poll::Pending; + } if *track && !serve.subscriber.version.has_track_stream() { let _ = self.serving.finish(); } @@ -8601,7 +9370,7 @@ impl ServeLoop { if !serve.subscriber.version.has_track_stream() { tail.end(); } - tail.set_grace(tail::grace(budget.unwrap_or_default())); + tail.set_grace(self.serving.tail_grace(budget.unwrap_or_default())); tail.expire(serve.subscriber.runtime.now()); } self.mode = ServeMode::Tail { @@ -8632,9 +9401,10 @@ impl ServeLoop { if !serve.subscriber.version.has_track_stream() { tail.end(); } - tail.set_grace(tail::grace( - subscription.map(|sub| sub.budget()).unwrap_or_default(), - )); + tail.set_grace( + self.serving + .tail_grace(subscription.map(|sub| sub.budget()).unwrap_or_default()), + ); tail.expire(serve.subscriber.runtime.now()); } self.mode = ServeMode::Tail { @@ -8646,14 +9416,24 @@ impl ServeLoop { } Err(err) => { tracing::warn!(broadcast = %serve.subscriber.log_path(&serve.path), track = %serve.name, %err, "subscribe error"); - return Poll::Ready(ServeEnd::GiveBack(err)); + self.mode = ServeMode::Failed { + err, + tail: active.tail.consume(), + }; + continue; } } } + // Every response that beat the accept is read. + self.unpark(serve); + // (5) The session died: hand the track back for another route, with // the session's error in case none takes it. if let Poll::Ready(err) = self.closed.poll_closed(waiter) { + if poll_unparked(&self.sub, serve, waiter).is_pending() { + return Poll::Pending; + } return Poll::Ready(ServeEnd::GiveBack(crate::lite::error::from_transport(err))); } diff --git a/rs/moq-net/src/lite/test_transport.rs b/rs/moq-net/src/lite/test_transport.rs index 6c0c7ac700..60455c595e 100644 --- a/rs/moq-net/src/lite/test_transport.rs +++ b/rs/moq-net/src/lite/test_transport.rs @@ -72,7 +72,7 @@ pub struct Log { closed_at: Arc>>, bi_opens: Arc, pub request_attempts: Arc, - priorities: Arc>>, + priorities: Arc>>, /// Hands each send group its id. group_ids: Arc, groups: Arc>>, @@ -137,7 +137,7 @@ impl Log { /// Every value handed to the transport's `set_priority`, in call order. These are /// trait-contract send orders (higher = transmitted first), recorded so tests can /// verify priority conversions at their protocol boundaries. - pub fn priorities(&self) -> Vec { + pub fn priorities(&self) -> Vec { self.priorities.lock().unwrap().clone() } @@ -342,7 +342,10 @@ impl poll::SendStream for SinkSend { self.log.orders.lock().unwrap().push((self.id, order)); return; } - let order = u8::try_from(order).expect("moq-net sends u8 send orders outside a group"); + assert!( + (0..=crate::coding::TRACK_INFO_SEND_ORDER).contains(&order), + "moq-net sends u8 send orders outside a group, and TRACK_INFO just above them" + ); self.log.priorities.lock().unwrap().push(order); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 0cc48e09f1..360679c30a 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1346,17 +1346,24 @@ impl TrackState { self.lookup.get(&sequence)?.copies().find(|group| group.covers(frames)) } - /// The local retention bound, or `None` when unknown or unlimited. - /// Bounds the aggregate subscription without changing the publisher's metadata. + /// The local retention bound, or `None` when unknown or unlimited: how long a reader + /// may wait for a late group, without changing the publisher's metadata. fn max_age_bound(&self) -> Option { let published = self.info.as_ref()?.max_age; - let ceiling = self.broadcast.cache_duration; match published { - Some(age) => Some(age.min(ceiling)), - None => (ceiling != Duration::MAX).then_some(ceiling), + Some(age) => Some(age.min(self.broadcast.cache_duration)), + None => self.demand_bound(), } } + /// The bound on the aggregate subscription: the local cache ceiling alone, or `None` + /// when unlimited. It needs no info, so a track asks upstream for the same demand + /// before and after its info lands, and the publisher enforces its own max age. + fn demand_bound(&self) -> Option { + let ceiling = self.broadcast.cache_duration; + (ceiling != Duration::MAX).then_some(ceiling) + } + /// The live edge a subscription served `servable` measures drift against: the /// highest-sequence group in it that has presented a frame, or `None` while nothing /// stamped is servable (an unstamped track drives no media-time expiry; the wall @@ -3910,12 +3917,13 @@ impl Producer { /// when there are no live subscribers. Unlike [`Self::subscription`], this /// doesn't wait for a change or advance the change cursor. /// - /// The aggregate's [`Subscription::max_delay`] is clamped to this track's - /// [`Info::max_age`]: no subscriber can wait for a late group longer than the - /// publisher keeps it. + /// The aggregate's [`Subscription::max_delay`] is the subscribers' own, capped only by + /// the local cache ceiling ([`broadcast::Info::cache_duration`]). The publisher + /// enforces its own [`Info::max_age`], which each reader's lateness budget also + /// applies, so the demand asked upstream is the same before and after the info lands. pub fn subscription(&self) -> Option { let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); snapshot_subscription(&subs, bound) } @@ -3933,12 +3941,27 @@ impl Producer { // Read the bound before locking `subs`, so the aggregation never nests the two locks. let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); poll_combined_changed(&subs, bound, &mut self.prev_subscription, waiter).map(Ok) } + /// Count `seen` as the aggregate already acted on, so + /// [`Self::poll_subscription_changed`] reports only a change from it: the demand a + /// subscriber session asked upstream for before accepting the track. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn seen_subscription(&mut self, seen: Subscription) { + self.prev_subscription = Some(seen); + } + + /// How long a subscriber session's tail waits for a late group: `budget`, bounded by how + /// long the publisher keeps one. The aggregate asked upstream is not bounded by it. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn tail_grace(&self, budget: Duration) -> Duration { + ledger::grace(clamp_max_delay(budget, self.state.read().max_age_bound())) + } + /// Create a [`Requests`] handle that serves on-demand fetches of uncached /// (old) groups. Most producers never need this; a relay creates one to fetch /// past groups from upstream. @@ -4173,15 +4196,18 @@ fn snapshot_subscription(subs: &kio::Lock, bound: Option) -> Duration { if let Some(bound) = bound { max_delay = max_delay.min(bound); @@ -4189,7 +4215,7 @@ fn clamp_max_delay(mut max_delay: Duration, bound: Option) -> Duration max_delay } -/// Clamp the aggregate's max delay budget to the publisher's window; see [`clamp_max_delay`]. +/// Clamp the aggregate's max delay budget to `bound`; see [`clamp_max_delay`]. fn clamp_combined(combined: Option, bound: Option) -> Option { let mut combined = combined?; if bound.is_some() { @@ -4275,7 +4301,7 @@ impl TrackWeak { /// The readers' aggregate demand and contribution count, or `None` without one. pub(super) fn subscription(&self) -> Option<(Subscription, usize)> { let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); let subs = subs.lock(); Some((clamp_combined(subs.combined(), bound)?, subs.len())) @@ -8527,7 +8553,7 @@ impl Request { /// or `None` if nobody is waiting. Useful for sizing the track before accepting. pub fn subscription(&self) -> Option { let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); snapshot_subscription(&subs, bound) } @@ -8542,7 +8568,7 @@ impl Request { /// Poll for a changed subscription. pub fn poll_subscription_changed(&mut self, waiter: &kio::Waiter) -> Poll> { let state = self.state.read(); - let (subs, bound) = (state.subscriptions.clone(), state.max_age_bound()); + let (subs, bound) = (state.subscriptions.clone(), state.demand_bound()); drop(state); poll_combined_changed(&subs, bound, &mut self.prev_subscription, waiter) @@ -10982,10 +11008,10 @@ mod test { #[test] fn max_delay_clamped_to_cache() { - let producer = track_producer("test", Info::timed().with_max_age(Duration::from_secs(2))); + let producer = track_producer_capped("test", Info::timed(), Duration::from_secs(2)); // A max delay budget beyond the cache is capped in the aggregate; a group can't be - // waited for longer than the publisher keeps it. The subscriber's own preference + // waited for longer than the cache keeps it. The subscriber's own preference // is stored verbatim, so what it asked for stays readable. let mut subscriber = producer.subscribe(Subscription::default().with_max_delay(Duration::from_secs(10))); assert_eq!(subscriber.subscription().max_delay, Some(Duration::from_secs(10))); @@ -11074,7 +11100,7 @@ mod test { #[test] fn max_delay_clamped_via_every_update_path() { - let producer = track_producer("test", Info::timed().with_max_age(Duration::from_secs(2))); + let producer = track_producer_capped("test", Info::timed(), Duration::from_secs(2)); let over = Subscription::default().with_max_delay(Duration::from_secs(10)); // The clamp lives in the aggregation, so it applies no matter which entry point @@ -11091,7 +11117,7 @@ mod test { #[test] fn max_delay_aggregate_clamps_across_subscribers() { - let producer = track_producer("test", Info::timed().with_max_age(Duration::from_secs(2))); + let producer = track_producer_capped("test", Info::timed(), Duration::from_secs(2)); // The aggregate takes the max, then clamps once. Equivalent to clamping each // subscriber first, since `min` distributes over `max`. @@ -11101,6 +11127,40 @@ mod test { assert_eq!(producer.subscription().unwrap().max_delay, Some(Duration::from_secs(2))); } + /// The publisher enforces its own max age, so the demand asked of it is the subscribers' + /// own, and a request asks for the same before and after its info lands. + #[kio::test] + #[cfg(feature = "async")] + async fn the_aggregate_is_not_clamped_to_the_publisher_max_age() { + let broadcast = broadcast::Info { + cache_duration: Duration::from_secs(30), + ..Default::default() + } + .produce(); + let mut dynamic = broadcast.requests(); + let consumer = broadcast.consume(); + let mut waiting = std::pin::pin!( + consumer + .track("test") + .unwrap() + .subscribe(Subscription::default().with_max_delay(Duration::from_secs(60))) + ); + assert!(futures::poll!(waiting.as_mut()).is_pending()); + let request = dynamic.requested_track().await.unwrap(); + // Capped by the local ceiling alone. + let before = request.subscription().unwrap().max_delay; + assert_eq!(before, Some(Duration::from_secs(30))); + + let producer = request.accept(Info::timed().with_max_age(Duration::from_secs(2))); + assert_eq!(producer.subscription().unwrap().max_delay, before); + assert_eq!(producer.state.read().max_age_bound(), Some(Duration::from_secs(2))); + // A session's tail still waits for a late group no longer than the publisher keeps it, + // even for a subscriber that defers to the max age alone. + assert_eq!(producer.tail_grace(Duration::from_secs(60)), Duration::from_secs(2)); + assert_eq!(producer.tail_grace(Duration::MAX), Duration::from_secs(2)); + waiting.await.unwrap(); + } + #[test] fn churned_subscribers_do_not_accumulate() { let producer = track_producer("test", Info::timed()); @@ -12294,8 +12354,8 @@ mod test { #[cfg(feature = "async")] fn max_age_bounds_the_budget() { // The publisher only keeps a group around for 500ms, so a subscriber asking to - // wait ten seconds for one still gives up at 500ms: the same clamp the aggregate - // applies, on the subscriber's own side of it. + // wait ten seconds for one still gives up at 500ms: the window the publisher + // applies to the aggregate, on the subscriber's own side of it. let mut producer = track_producer("test", Info::timed().with_max_age(Duration::from_millis(500))); append_at(&mut producer, 0); append_at(&mut producer, 1000); diff --git a/rs/moq-net/src/tail.rs b/rs/moq-net/src/tail.rs index 07b74137da..4eeb653279 100644 --- a/rs/moq-net/src/tail.rs +++ b/rs/moq-net/src/tail.rs @@ -92,6 +92,11 @@ impl Tail { self.ledger.folds_at(group, self.ledger.grace()) } + /// Whether a data stream whose header arrived is still being read. + pub fn is_reading(&self) -> bool { + self.active > 0 + } + /// Data streams received, whether they finished or were reset. pub fn streams(&self) -> u64 { self.streams diff --git a/rs/moq-net/tests/integration/errored_end.rs b/rs/moq-net/tests/integration/errored_end.rs index 0eaefbee34..fd5501e3f0 100644 --- a/rs/moq-net/tests/integration/errored_end.rs +++ b/rs/moq-net/tests/integration/errored_end.rs @@ -14,6 +14,7 @@ use support::harness::{MockConnectOptions, connect_mock}; const TIMEOUT: Duration = Duration::from_secs(10); const GROUPS: u64 = 2; +const FRAMES: [&[u8]; 2] = [b"first", b"second"]; fn produce_origin(hop: u64) -> moq_net::origin::Producer { let (producer, driver) = moq_net::origin::Producer::new(moq_net::origin::Config::new(Hop::new(hop).unwrap())); @@ -21,8 +22,9 @@ fn produce_origin(hop: u64) -> moq_net::origin::Producer { producer } -/// Read every group from the first, returning their sequences and how the read ended. -async fn read(broadcast: &moq_net::broadcast::Consumer) -> (Vec, Option) { +/// Read every group from the first, frame by frame, returning each group's sequence and +/// payloads, and how the read ended. +async fn read(broadcast: &moq_net::broadcast::Consumer) -> (Vec<(u64, Vec>)>, Option) { let subscription = Subscription::default() .with_range(0..) .with_max_delay(Duration::from_secs(30)); @@ -36,16 +38,43 @@ async fn read(broadcast: &moq_net::broadcast::Consumer) -> (Vec, Option got.push(group.sequence), + Ok(Some(mut group)) => { + let mut frames = Vec::new(); + while let Some(frame) = kio::clock::timeout(TIMEOUT, group.read_frame()) + .await + .expect("frame timeout") + .unwrap_or_else(|err| panic!("group {} cut short: {err:?}", group.sequence)) + { + frames.push(frame.payload.to_vec()); + } + got.push((group.sequence, frames)); + } Ok(None) => return (got, None), Err(err) => return (got, Some(err)), } } } +/// What [`read`] returns for the whole history. +fn history() -> Vec<(u64, Vec>)> { + (0..GROUPS) + .map(|sequence| (sequence, FRAMES.iter().map(|frame| frame.to_vec()).collect())) + .collect() +} + +/// Each group stream's header lands, but its frames and FIN only after the subscription's +/// end, as QUIC does not order streams. A stream whose header arrived still belongs to the +/// track, so the hop reads it to its end. With the SUBSCRIBE first, the publisher's groups +/// also beat its TRACK_INFO, so they park on it and the end lands as the accept unparks them. #[kio::test] async fn an_errored_end_keeps_its_history_across_a_hop() { - for name in ["moq-lite-06", "moq-lite-07-wip"] { + for (name, subscribe_first) in [ + ("moq-lite-06", false), + ("moq-lite-06", true), + ("moq-lite-07-wip", false), + ("moq-lite-07-wip", true), + ] { + let label = format!("{name} subscribe_first={subscribe_first}"); let version: Version = name.parse().unwrap(); let publisher = produce_origin(1); let broadcast = publisher.create_broadcast("bcast").unwrap(); @@ -54,7 +83,9 @@ async fn an_errored_end_keeps_its_history_across_a_hop() { broadcast.announce(Default::default()).unwrap(); for _ in 0..GROUPS { let mut group = track.append_group().unwrap(); - group.write_frame(Timestamp::ZERO, &b"frame"[..]).unwrap(); + for frame in FRAMES { + group.write_frame(Timestamp::ZERO, frame).unwrap(); + } group.finish().unwrap(); } track.finish_at(Position::group(GROUPS), Some(Error::App(7))).unwrap(); @@ -69,16 +100,34 @@ async fn an_errored_end_keeps_its_history_across_a_hop() { .expect("resolve timeout") .expect("broadcast resolves"); - let first = read(&remote).await; + session.server_transport.split_unis(); + if subscribe_first { + session.client_transport.hold_bidis(); + } + let release = async { + if subscribe_first { + kio::clock::sleep(Duration::from_millis(1)).await; + session.client_transport.release_bidis_reversed(); + } + kio::clock::sleep(Duration::from_millis(100)).await; + if name != "moq-lite-07-wip" { + assert!( + session.server_transport.resets().contains(&(64 + 7)), + "{label}: the reset was not sent before the frames" + ); + } + session.server_transport.release_split(); + }; + let (first, ()) = futures::join!(read(&remote), release); let later = read(&remote).await; for (what, (got, err)) in [("first", first), ("later", later)] { - assert_eq!(got, (0..GROUPS).collect::>(), "{name} {what}"); + assert_eq!(got, history(), "{label} {what}"); match name == "moq-lite-07-wip" { true => assert!( matches!(err, Some(Error::Stream(StreamError::App(7)))), - "{name} {what}: {err:?}" + "{label} {what}: {err:?}" ), - false => assert!(err.is_some(), "{name} {what}: ended cleanly"), + false => assert!(err.is_some(), "{label} {what}: ended cleanly"), } } // Lite-07 ends the subscription after SUBSCRIBE_END rather than resetting it. @@ -86,7 +135,7 @@ async fn an_errored_end_keeps_its_history_across_a_hop() { assert_eq!( reset, name != "moq-lite-07-wip", - "{name}: resets {:?}", + "{label}: resets {:?}", session.server_transport.resets() ); drop((session, track, broadcast, publisher, subscriber)); diff --git a/rs/moq-net/tests/integration/fetch_pipeline.rs b/rs/moq-net/tests/integration/fetch_pipeline.rs index d89e188a0c..5cb165927a 100644 --- a/rs/moq-net/tests/integration/fetch_pipeline.rs +++ b/rs/moq-net/tests/integration/fetch_pipeline.rs @@ -178,7 +178,7 @@ async fn first_fetch(version: &str, relays: u64, rtt: Duration) -> Duration { async fn the_first_fetch_takes_one_round_trip_per_hop() { const RTT: Duration = Duration::from_millis(100); for version in VERSIONS { - for relays in [0, 1] { + for relays in [0, 1, 2] { let elapsed = first_fetch(version, relays, RTT).await; let hops = u32::try_from(relays + 1).unwrap(); assert!(elapsed <= RTT * hops, "{version}, {relays} relays: {elapsed:?}"); diff --git a/rs/moq-net/tests/integration/finished_broadcast_mock.rs b/rs/moq-net/tests/integration/finished_broadcast_mock.rs index 4170eba1b3..7b96336041 100644 --- a/rs/moq-net/tests/integration/finished_broadcast_mock.rs +++ b/rs/moq-net/tests/integration/finished_broadcast_mock.rs @@ -55,14 +55,14 @@ async fn round(finish_broadcast: bool) -> (Vec>, Option) .expect("resolve timeout") .expect("broadcast resolves"); + // Resolved before the ending starts: a SUBSCRIBE sent alongside its TRACK reaches the + // publisher before the subscriber has the TRACK_INFO it accepts the track with. + let subscription = moq_net::track::Subscription::default().with_range(0..); + let mut sub = kio::clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(subscription)) + .await + .expect("subscribe timeout") + .expect("subscribe"); let reader = kio::clock::sim::spawn(async move { - let subscription = moq_net::track::Subscription::default().with_range(0..); - let mut sub = remote - .track("video") - .unwrap() - .subscribe(subscription) - .await - .expect("subscribe"); let mut got = Vec::new(); loop { let mut group = match sub.recv_group().await { diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 3374488ac7..6c4c2ad082 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -63,6 +63,7 @@ mod stream_credit; mod subscribe_priority; mod subscribe_ranges; mod subscribe_tracks; +mod subscribe_with_track; mod subscriber_max_age; mod subscription_end_integrity; #[cfg(feature = "async")] diff --git a/rs/moq-net/tests/integration/subscribe_with_track.rs b/rs/moq-net/tests/integration/subscribe_with_track.rs new file mode 100644 index 0000000000..8e1eeea42c --- /dev/null +++ b/rs/moq-net/tests/integration/subscribe_with_track.rs @@ -0,0 +1,159 @@ +#![cfg(feature = "async")] + +//! On moq-lite 05 and later a subscriber opens its TRACK and SUBSCRIBE streams together, +//! so first data arrives one round trip after asking, per hop, rather than two. +//! +//! QUIC does not order streams, so either may reach the publisher first, or the SUBSCRIBE a +//! round trip after the TRACK, as from a subscriber that waited for TRACK_INFO. Whichever +//! it is, the reader gets the track, including when the SUBSCRIBE lands first and the +//! publisher's groups beat the TRACK_INFO they are read with. Lite-03/04 carry no TRACK +//! stream and are unchanged. + +use crate::support; + +use std::time::Duration; + +use moq_net::{Hop, Timestamp, Version, origin}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(10); +const LATENCY: Duration = Duration::from_millis(10); +const RTT: Duration = Duration::from_millis(20); +const VERSIONS: [&str; 5] = [ + "moq-lite-03", + "moq-lite-04", + "moq-lite-05", + "moq-lite-06", + "moq-lite-07-wip", +]; + +/// How the reader's TRACK and SUBSCRIBE reach the first hop. +#[derive(Debug, Clone, Copy)] +enum Arrival { + /// As sent: together. + Together, + /// The SUBSCRIBE a round trip after the TRACK, once its TRACK_INFO is back. + Serial, + /// The SUBSCRIBE first, so the publisher's groups head back before its TRACK_INFO. + SubscribeFirst, +} + +fn produce_origin(hop: u64) -> origin::Producer { + let (producer, driver) = origin::Producer::new(origin::Config::new(Hop::new(hop).unwrap())); + support::harness::spawn(driver); + producer +} + +/// `subscriber` reads what `publisher` serves, over a link with [`LATENCY`] each way. +async fn link(version: Version, publisher: &origin::Producer, subscriber: &origin::Producer) -> MockPair { + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(subscriber.clone()); + options.latency = LATENCY; + connect_mock(options).await +} + +/// A reader subscribes through `relays` relays and reads a cached group, then a live one. +/// Returns how long the cached group's first frame took. +async fn first_frame(version: &str, relays: usize, arrival: Arrival) -> Duration { + let version: Version = version.parse().unwrap(); + let label = format!("{version} over {relays} relays, {arrival:?}"); + + let nodes: Vec<_> = (1..=relays as u64 + 2).map(produce_origin).collect(); + let mut links = Vec::new(); + for pair in nodes.windows(2) { + links.push(link(version, &pair[0], &pair[1]).await); + } + let reader = &links.last().unwrap().client_transport; + + let broadcast = nodes[0].create_broadcast("room").unwrap(); + let track = broadcast + .create_track( + "video", + moq_net::track::Info::default().with_timescale(moq_net::Timescale::MILLI), + ) + .unwrap(); + let mut cached = track.append_group().unwrap(); + cached.write_frame(Timestamp::ZERO, b"cached".as_ref()).unwrap(); + cached.finish().unwrap(); + broadcast.announce(Default::default()).unwrap(); + + let consumer = nodes.last().unwrap().consume(); + let remote = consumer.follow("room", None).unwrap().available().await.unwrap(); + + if !matches!(arrival, Arrival::Together) { + reader.hold_bidis(); + } + let start = kio::clock::now(); + let subscribing = kio::clock::sim::spawn(remote.track("video").unwrap().subscribe(None)); + match arrival { + Arrival::Together => {} + Arrival::Serial => { + kio::clock::sleep(Duration::from_millis(1)).await; + reader.release_oldest_bidi(); + kio::clock::sleep(RTT).await; + reader.release_bidis(); + } + Arrival::SubscribeFirst => { + kio::clock::sleep(Duration::from_millis(1)).await; + reader.release_bidis_reversed(); + } + } + let mut subscriber = kio::clock::timeout(TIMEOUT, subscribing) + .await + .unwrap_or_else(|_| panic!("{label}: subscribe stalled")) + .unwrap() + .unwrap_or_else(|err| panic!("{label}: subscribe failed: {err}")); + + let mut group = kio::clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .unwrap_or_else(|_| panic!("{label}: no group arrived")) + .unwrap() + .expect("the track is live"); + let frame = group.read_frame().await.unwrap().expect("the group has a frame"); + let elapsed = kio::clock::now() - start; + assert_eq!(frame.payload.as_ref(), b"cached", "{label}"); + + let mut live = track.append_group().unwrap(); + live.write_frame(Timestamp::from_millis(1).unwrap(), b"live".as_ref()) + .unwrap(); + live.finish().unwrap(); + let mut group = kio::clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .unwrap_or_else(|_| panic!("{label}: the live group did not arrive")) + .unwrap() + .expect("the track is live"); + let frame = group.read_frame().await.unwrap().expect("the group has a frame"); + assert_eq!(frame.payload.as_ref(), b"live", "{label}"); + elapsed +} + +#[kio::test] +async fn every_arrival_order_delivers() { + for version in VERSIONS { + let pipelined = !matches!(version, "moq-lite-03" | "moq-lite-04"); + for relays in 0..=2 { + first_frame(version, relays, Arrival::Together).await; + // Without a TRACK stream there is nothing to reorder. + if pipelined { + first_frame(version, relays, Arrival::Serial).await; + first_frame(version, relays, Arrival::SubscribeFirst).await; + } + } + } +} + +/// The first frame arrives one round trip after asking, per hop. +#[kio::test] +async fn first_frame_takes_one_round_trip_per_hop() { + for version in ["moq-lite-05", "moq-lite-06", "moq-lite-07-wip"] { + for relays in 0..=2 { + let elapsed = first_frame(version, relays, Arrival::Together).await; + let hops = relays as u32 + 1; + assert!( + elapsed <= RTT * hops, + "{version} over {relays} relays: first frame after {elapsed:?}" + ); + } + } +} diff --git a/rs/moq-net/tests/integration/support/mock.rs b/rs/moq-net/tests/integration/support/mock.rs index d811138ab0..8f870f3d42 100644 --- a/rs/moq-net/tests/integration/support/mock.rs +++ b/rs/moq-net/tests/integration/support/mock.rs @@ -777,6 +777,34 @@ impl MockSession { } } + /// Deliver only the oldest held bidi stream, and keep holding the rest. + pub fn release_oldest_bidi(&self) { + let link = &self.side.out; + link.settle(); + let mut held = link.held_bi.lock().unwrap(); + let held = held.as_mut().expect("not holding"); + if !held.is_empty() { + let _ = link.ready_bi.try_push(held.remove(0)); + } + } + + /// Deliver the held bidi streams newest first, and stop holding. + pub fn release_bidis_reversed(&self) { + let link = &self.side.out; + link.settle(); + for stream in link + .held_bi + .lock() + .unwrap() + .take() + .unwrap_or_default() + .into_iter() + .rev() + { + let _ = link.ready_bi.try_push(stream); + } + } + /// Park every uni stream this side opens from now on, like a peer that has granted /// no more stream credit. pub fn withhold_unis(&self) { diff --git a/rs/moq-relay/tests/integration/auth_lifetime.rs b/rs/moq-relay/tests/integration/auth_lifetime.rs index 18893a09ee..d41040861c 100644 --- a/rs/moq-relay/tests/integration/auth_lifetime.rs +++ b/rs/moq-relay/tests/integration/auth_lifetime.rs @@ -883,12 +883,17 @@ async fn a_rechecked_grant_resizes_live_sessions() { .await .expect("subscribe"); send(0, 1); - let group = kio::clock::timeout(TIMEOUT, audio.recv_group()) - .await - .expect("recv_group timeout") - .expect("recv_group failed") - .expect("the undeafened track closed"); - assert!(group.sequence >= 1, "{scheme}"); + // The SUBSCRIBE went out with the TRACK, so the latest group when it arrived may come first. + loop { + let group = kio::clock::timeout(TIMEOUT, audio.recv_group()) + .await + .expect("recv_group timeout") + .expect("recv_group failed") + .expect("the undeafened track closed"); + if group.sequence >= 1 { + break; + } + } for (session, what) in [(pub_session, "publisher"), (sub_session, "subscriber")] { assert!(