From f1067b323e2690e41b1fa3455cf33a1348c8d6bf Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 8 Oct 2026 07:59:25 -0700 Subject: [PATCH 1/3] fix(spf): serve relay pulls whose fill cannot be reset reliably MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moq-relay (0.14.17 through 0.17.0) subscribes upstream with Next Object plus FILL_PARAMETERS for the current group. Once a track has content that fill range is nonempty, and the publish session refused the whole SUBSCRIBE with NOT_SUPPORTED when the transport's writers lack WebTransport's commit() (RESET_STREAM_AT). Chromium 147 has no commit(), so in the browser a relay's second pull of a live track always failed: the real subscribe after a cross-relay TRACK_INFO probe, or any re-subscribe after the last viewer left. Players on another relay never got a catalog. Serve the subscription, and fail the fill as before: write the FETCH_HEADER, commit it where the writer can, and reset. Without commit() the reset may discard the header, and the subscriber sees no fill, which leaves its join at the next group boundary, as a failed fill does. REQUEST_UPDATE fills get the same treatment instead of ending the subscription. Stacked on #62, which ends tracks with PUBLISH_DONE and an exact Stream Count. There the fill counted only past the commit() gate this change removes. It now counts wherever the session opens one, whether or not its header survives the reset: §10.12 counts the streams the publisher opened, fill fetch streams included. A subscriber waits out a timeout for a stream reset before its header, but one that receives more streams than the count may close the session. moq-relay 0.17.0 never closes on either: it ends the track once the counted streams and the fill have arrived, or after a 1 s grace. A lost fill header leaves its fill outstanding, so that track end waits out the grace whatever the count says. The relay hub can now model a transport without commit(). The session tests cover the relay's probe-then-subscribe sequence, and the Stream Count of a fill reset without commit(), with its header delivered and lost. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Ts6nhCDi2pBF9h5ooBNbss --- packages/spf/src/network/moqt/session.ts | 6 +- .../src/publish/session/publish-session.ts | 42 ++--- .../session/tests/publish-session.test.ts | 178 +++++++++++++++--- .../src/publish/tests/helpers/relay-hub.ts | 8 +- .../spf/src/publish/tests/relay-hub.test.ts | 95 +++++----- 5 files changed, 234 insertions(+), 95 deletions(-) diff --git a/packages/spf/src/network/moqt/session.ts b/packages/spf/src/network/moqt/session.ts index 105b4f1d74..e849db2543 100644 --- a/packages/spf/src/network/moqt/session.ts +++ b/packages/spf/src/network/moqt/session.ts @@ -60,9 +60,9 @@ import { type BidirectionalStreamLike, openRequestStream, type RequestStream } f * Structural subset of `WebTransport` the session driver needs. A real `WebTransport` instance satisfies this; tests * provide an in-memory fake (the same seam pattern as SPF's fetch injection). * - * Publish-side fill failure signaling requires writers with WebTransport's `commit()` method, which preserves the - * written prefix across a reset. Support is detected on the control stream's writer and must be consistent across - * outgoing streams. Without it, nonempty fill requests are rejected with NOT_SUPPORTED. + * Publish-side fill failure signaling uses WebTransport's `commit()` writer method where it exists, which preserves the + * written FETCH_HEADER across the reset. Without it the subscription is still served, and the reset that fails the fill + * may discard its header. */ export interface MoqtTransport { readonly incomingUnidirectionalStreams: ReadableStream>; diff --git a/packages/spf/src/publish/session/publish-session.ts b/packages/spf/src/publish/session/publish-session.ts index 5bcf0b210d..458b36e44e 100644 --- a/packages/spf/src/publish/session/publish-session.ts +++ b/packages/spf/src/publish/session/publish-session.ts @@ -643,8 +643,15 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { * Answer a nonempty fill request (see `#fillRequested`). This origin serves no fills, so it meets the §5.1.3.1 * requirement the honest way: open a uni stream, write the FETCH_HEADER carrying the initiating Request ID so the * subscriber can correlate the failure, then reset it. A reset is the fill-failure signal — a FIN would falsely claim - * the fill range was delivered in full. Moq-relay 0.14.17 requests the current group this way; the reliable reset - * keeps that join from waiting on a fill that never arrives. + * the fill range was delivered in full. Moq-relay requests the current group this way (0.14.17 through 0.17.0) on + * every upstream SUBSCRIBE once the track has content. + * + * The header survives the reset only where the writer has WebTransport's `commit()` (RESET_STREAM_AT's reliable + * prefix). Without it (Chromium as of 147) the subscription is still served: the reset may discard the header, and + * the subscriber then sees no fill at all — which, like a failed one, leaves its join at the next group boundary. + * Refusing the SUBSCRIBE instead would fail every relay's second pull of a live track (the real subscribe after a + * cross-relay TRACK_INFO probe, or any re-subscribe after the last viewer left), so nothing would play across a relay + * mesh. */ #openAndResetFill(requestId: number, subscriber: SubscriberStream): void { const fill: FillStream = {}; @@ -660,20 +667,20 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { if (!subscriber.fills.has(fill) || subscriber.finished) return; - // All WebTransport writers expose the same capabilities, probed - // on the control writer before accepting a nonempty fill. Check - // this stream too so an inconsistent injected transport never - // writes a header it cannot preserve through the reset. - if (!hasMethods(writer, ['commit'])) return; - + // PUBLISH_DONE's Stream Count is the streams this side opened, + // fill fetch streams included (§10.12), so count this one even + // when the reset may discard its header. The subscriber waits + // out a timeout for a stream reset before its header; a count + // short of the streams it does receive may close the session. subscriber.fillStreams++; await writer.write(encodeFetchHeader(requestId)); if (!subscriber.fills.has(fill) || subscriber.finished) return; // write() only hands bytes to the transport. commit() includes - // them in the reliable prefix of RESET_STREAM_AT. - writer.commit(); + // them in the reliable prefix of RESET_STREAM_AT; without it the + // reset below is best-effort for the header. + if (hasMethods(writer, ['commit'])) writer.commit(); } catch { // Session closing or the peer went away before the header landed. } finally { @@ -1044,18 +1051,6 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { subscribe.parameters.fillParameters ); - if (fillRequested && !hasMethods(this.#controlWriter, ['commit'])) { - try { - await writer.write(encodeRequestError(REQUEST_ERROR_CODE.NOT_SUPPORTED, 'fill requires reliable stream reset')); - await writer.close(); - } catch { - // Peer cancelled. - } - - await reader.cancel(); - return; - } - track.subscribers.push(subscriber); // STOP_SENDING on the response direction cancels the subscription @@ -1166,7 +1161,6 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { // here, immediately resets) a fill fetch stream keyed by the // update's own Request ID (§5.1.3.1); an empty range opens none. const fillRequested = this.#fillRequested(largestObject, subscriber, subscribe.parameters, fillParameters); - const unsupportedFill = fillRequested && !hasMethods(this.#controlWriter, ['commit']); this.#callbacks.onRequestUpdate?.({ requestId: subscribe.requestId, @@ -1175,7 +1169,7 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { }); if (!subscriber.finished) { - if (Object.keys(unsupported).length > 0 || unsupportedFill) { + if (Object.keys(unsupported).length > 0) { // Acknowledging an update we did not apply would leave the // peer serving stale expectations (a filter or range it // believes is in effect). Forward State is applied above diff --git a/packages/spf/src/publish/session/tests/publish-session.test.ts b/packages/spf/src/publish/session/tests/publish-session.test.ts index c9e4fdb77d..8c080c3d9a 100644 --- a/packages/spf/src/publish/session/tests/publish-session.test.ts +++ b/packages/spf/src/publish/session/tests/publish-session.test.ts @@ -80,9 +80,11 @@ function collectTrack( async function makeFillHarness({ reliableReset = true, openFillStream, + callbacks, }: { reliableReset?: boolean; openFillStream?: () => Promise>; + callbacks?: MoqtPublishSessionCallbacks; } = {}) { const pair = createTransportPair(); const openUniStream = pair.client.createUnidirectionalStream.bind(pair.client); @@ -102,7 +104,7 @@ async function makeFillHarness({ return stream; }; - const session = createMoqtPublishSession(pair.client); + const session = createMoqtPublishSession(pair.client, { callbacks }); const fills: { requestId: number; reset: boolean }[] = []; const streams = pair.server.incomingUnidirectionalStreams.getReader(); @@ -146,8 +148,11 @@ async function makeFillHarness({ }; } -/** A buffered send stream: reset discards everything except the explicitly committed prefix. */ -function makeBufferedFillStream() { +/** + * A buffered send stream: reset discards everything except the explicitly committed prefix. Without `reliableReset` the + * writer has no `commit()` — a browser WebTransport today — so a reset discards everything still unread. + */ +function makeBufferedFillStream({ reliableReset = true }: { reliableReset?: boolean } = {}) { const chunks: Uint8Array[] = []; let committed = 0; let readIndex = 0; @@ -188,12 +193,14 @@ function makeBufferedFillStream() { }); const getWriter = writable.getWriter.bind(writable); - writable.getWriter = () => - Object.assign(getWriter(), { - commit() { - committed = chunks.length; - }, - }); + if (reliableReset) { + writable.getWriter = () => + Object.assign(getWriter(), { + commit() { + committed = chunks.length; + }, + }); + } return { writable, readable, reset: () => reset }; } @@ -1320,39 +1327,164 @@ describe('createMoqtPublishSession', () => { await finish(); }); - it('rejects nonempty fills without reliable reset support while allowing ordinary subscriptions', async () => { - const { pair, session, fills, finish } = await makeFillHarness({ reliableReset: false }); + it("serves a relay's second pull of a live track without reliable reset support", async () => { + const bindings: (number | undefined)[] = []; + const { pair, session, fills, finish } = await makeFillHarness({ + reliableReset: false, + callbacks: { onTrackBinding: ({ trackAlias }) => bindings.push(trackAlias) }, + }); + let largest: { group: number; object: number } | undefined; + + session.registerTrack({ trackNamespace: NAMESPACE, trackName: 'catalog', getLargestObject: () => largest }); + + // Moq-relay's upstream SUBSCRIBE (0.14.17 through 0.17.0): Next Object, + // plus a fill of the current group's head. + const relayPull = (requestId: number) => + openRawRequest( + pair.server, + encodeSubscribe({ + requestId, + trackNamespace: NAMESPACE, + trackName: 'catalog', + parameters: { + forward: 1, + subscriberPriority: 128, + locationFilter: { type: 'next-object' }, + fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, + groupOrder: 'descending', + includeProperties: 1, + }, + }) + ); + + // A cross-relay viewer's first pull is the relay's TRACK_INFO probe: + // nothing published yet, so there is nothing to fill. + const probe = await relayPull(235); + + await vi.waitFor(() => expect(probe.received.map((m) => m.kind)).toEqual(['subscribe-ok'])); + + // The probe's bind put the catalog on the wire, then the relay + // cancelled it and pulled again for the viewer — now with a nonempty + // fill this transport cannot fail reliably. The pull is served anyway. + largest = { group: 0, object: 0 }; + await probe.reset(); + const pull = await relayPull(237); + + await vi.waitFor(() => { + expect(pull.received.map((m) => m.kind)).toEqual(['subscribe-ok']); + expect(bindings.at(-1)).toBe(237); + expect(fills).toEqual([{ requestId: 237, reset: true }]); + }); + expect(pull.ended()).toBe(false); + + // A fill requested by REQUEST_UPDATE is acknowledged and failed the same way. + await pull.send( + encodeRequestUpdate(239, { fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } } }) + ); + await vi.waitFor(() => { + expect(pull.received.map((m) => m.kind)).toEqual(['subscribe-ok', 'request-ok']); + expect(fills).toEqual([ + { requestId: 237, reset: true }, + { requestId: 239, reset: true }, + ]); + }); + expect(pull.ended()).toBe(false); + await finish(); + }); + + it('still resets the fill when the reset may discard its header (no reliable reset)', async () => { + const fill = makeBufferedFillStream({ reliableReset: false }); + const { pair, session, finish } = await makeFillHarness({ + reliableReset: false, + openFillStream: async () => fill.writable, + }); session.registerTrack({ trackNamespace: NAMESPACE, trackName: 'video', getLargestObject: () => ({ group: 4, object: 2 }), }); - const rejected = await openRawRequest( + const sub = await openRawRequest( pair.server, encodeSubscribe({ - requestId: 235, + requestId: 241, trackNamespace: NAMESPACE, trackName: 'video', parameters: { fillParameters: {} }, }) ); - await vi.waitFor(() => expect(rejected.ended()).toBe(true)); - expect(rejected.received).toMatchObject([{ kind: 'request-error', errorCode: REQUEST_ERROR_CODE.NOT_SUPPORTED }]); + await vi.waitFor(() => { + expect(sub.received.map((m) => m.kind)).toEqual(['subscribe-ok']); + expect(fill.reset()).toBeDefined(); + }); - const sub = await rawSubscribe(pair.server, 'video', 237); + // Nothing preserved the header through the reset: the peer sees only + // the reset, and the subscription is unaffected. + const reader = new StreamReader(fill.readable); - await vi.waitFor(() => expect(sub.received[0]?.kind).toBe('subscribe-ok')); - await sub.send(encodeRequestUpdate(239, { fillParameters: { locationFilter: { type: 'next-object' } } })); - await vi.waitFor(() => expect(sub.received[1]?.kind).toBe('request-ok')); - await sub.send(encodeRequestUpdate(241, { fillParameters: {} })); - await vi.waitFor(() => expect(sub.ended()).toBe(true)); - expect(sub.received[2]).toMatchObject({ kind: 'request-error', errorCode: REQUEST_ERROR_CODE.NOT_SUPPORTED }); - expect(fills).toEqual([]); + await expect(reader.readVarint()).rejects.toThrow('fill fetch streams are not served'); + expect(sub.ended()).toBe(false); await finish(); }); + it.each([{ headerSurvives: true }, { headerSurvives: false }])( + 'counts a fill reset without reliable reset in the PUBLISH_DONE Stream Count (header survives: $headerSurvives)', + async ({ headerSurvives }) => { + const buffered = makeBufferedFillStream({ reliableReset: false }); + const { pair, session, fills, finish } = await makeFillHarness({ + reliableReset: false, + openFillStream: headerSurvives ? undefined : async () => buffered.writable, + }); + const track = session.registerTrack({ + trackNamespace: NAMESPACE, + trackName: 'video', + getLargestObject: () => ({ group: 4, object: 2 }), + getStreamCount: () => 3, + }); + const sub = await openRawRequest( + pair.server, + encodeSubscribe({ + requestId: 243, + trackNamespace: NAMESPACE, + trackName: 'video', + parameters: { fillParameters: {} }, + }) + ); + + await vi.waitFor(() => { + expect(sub.received.map((m) => m.kind)).toEqual(['subscribe-ok']); + + if (headerSurvives) expect(fills).toEqual([{ requestId: 243, reset: true }]); + else expect(buffered.reset()).toBeDefined(); + }); + + if (!headerSurvives) { + // The peer saw only the reset, so it cannot tell the stream + // belonged to this subscription. + await expect(new StreamReader(buffered.readable).readVarint()).rejects.toThrow( + 'fill fetch streams are not served' + ); + } + + track.end(); + await vi.waitFor(() => { + expect(sub.ended()).toBe(true); + }); + // §10.12 counts the streams the publisher opened, fill fetch + // streams included: three from the track publisher plus the fill, + // whether or not its header outlived the reset. A subscriber waits + // out a timeout for a stream reset before its header, but one that + // receives more streams than the count may close the session. + expect(sub.received.at(-1)).toMatchObject({ + kind: 'publish-done', + statusCode: PUBLISH_DONE_STATUS.TRACK_ENDED, + streamCount: 4, + }); + await finish(); + } + ); + it('rejects a subscription update it cannot apply instead of acknowledging it', async () => { const endedRequests: number[] = []; const bindings: (number | undefined)[] = []; diff --git a/packages/spf/src/publish/tests/helpers/relay-hub.ts b/packages/spf/src/publish/tests/helpers/relay-hub.ts index 271e8c61a4..bca23d1027 100644 --- a/packages/spf/src/publish/tests/helpers/relay-hub.ts +++ b/packages/spf/src/publish/tests/helpers/relay-hub.ts @@ -178,6 +178,11 @@ export function createRelayHub( options: { /** Delay individual downstream object streams to exercise jitter and late arrivals. */ deliveryDelayMs?: (trackName: string, object: Readonly) => number; + /** + * Give the publisher's stream writers WebTransport's `commit()` (RESET_STREAM_AT). Default `true`; `false` models + * the browsers that ship WebTransport without it (Chromium 147). + */ + reliableReset?: boolean; } = {} ): RelayHub { let destroyed = false; @@ -254,7 +259,8 @@ export function createRelayHub( // These zero-buffer pipes settle writes only after the peer reads // them, so the reliable prefix is already delivered at commit time. - stream.getWriter = () => Object.assign(getWriter(), { commit() {} }); + if (options.reliableReset !== false) stream.getWriter = () => Object.assign(getWriter(), { commit() {} }); + return stream; }; diff --git a/packages/spf/src/publish/tests/relay-hub.test.ts b/packages/spf/src/publish/tests/relay-hub.test.ts index d90ee928c5..e0033621a3 100644 --- a/packages/spf/src/publish/tests/relay-hub.test.ts +++ b/packages/spf/src/publish/tests/relay-hub.test.ts @@ -10,54 +10,61 @@ import { createRelayHub } from './helpers/relay-hub'; * cross-engine suites are too coarse to pin down. */ describe('createRelayHub', () => { - it.each([false, true])('follows live after a relay 0.14.17 pull (existing content: %s)', async (hasContent) => { - const hub = createRelayHub(); - const { transport } = hub.connectPublisher({ url: 'https://relay.test/moq', namespace: ['live'] }); - const onSubscribe = vi.fn(); - const onTrackBinding = vi.fn(); - const onClosed = vi.fn(); - const session = createMoqtPublishSession(transport, { callbacks: { onSubscribe, onTrackBinding, onClosed } }); + it.each([ + { hasContent: false, reliableReset: true }, + { hasContent: true, reliableReset: true }, + { hasContent: true, reliableReset: false }, + ])( + 'follows live after a relay 0.14.17 pull (existing content: $hasContent, reliable reset: $reliableReset)', + async ({ hasContent, reliableReset }) => { + const hub = createRelayHub({ reliableReset }); + const { transport } = hub.connectPublisher({ url: 'https://relay.test/moq', namespace: ['live'] }); + const onSubscribe = vi.fn(); + const onTrackBinding = vi.fn(); + const onClosed = vi.fn(); + const session = createMoqtPublishSession(transport, { callbacks: { onSubscribe, onTrackBinding, onClosed } }); - try { - await session.ready; - session.announce(['live']); - session.registerTrack({ - trackNamespace: ['live'], - trackName: 'video', - getLargestObject: () => (hasContent ? { group: 255, object: 128 } : undefined), - }); - hub.subscribeUpstream('video'); - await vi.waitFor(() => { - expect(onSubscribe).toHaveBeenCalledWith( - expect.objectContaining({ - parameters: expect.objectContaining({ - subscriberPriority: 255, - locationFilter: { type: 'next-object' }, - fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, - }), - }) - ); - expect(onTrackBinding).toHaveBeenCalledWith({ trackName: 'video', trackAlias: expect.any(Number) }); - expect(hub.fills).toEqual(hasContent ? [{ requestId: 3, reset: true }] : []); - }); + try { + await session.ready; + session.announce(['live']); + session.registerTrack({ + trackNamespace: ['live'], + trackName: 'video', + getLargestObject: () => (hasContent ? { group: 255, object: 128 } : undefined), + }); + hub.subscribeUpstream('video'); + await vi.waitFor(() => { + expect(onSubscribe).toHaveBeenCalledWith( + expect.objectContaining({ + parameters: expect.objectContaining({ + subscriberPriority: 255, + locationFilter: { type: 'next-object' }, + fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, + }), + }) + ); + expect(onTrackBinding).toHaveBeenCalledWith({ trackName: 'video', trackAlias: expect.any(Number) }); + expect(hub.fills).toEqual(hasContent ? [{ requestId: 3, reset: true }] : []); + }); - const trackAlias = onTrackBinding.mock.calls[0]![0].trackAlias; - const stream = createSubgroupWriter(await session.openUniStream(), { - trackAlias, - groupId: 256, - endOfGroup: true, - }); + const trackAlias = onTrackBinding.mock.calls[0]![0].trackAlias; + const stream = createSubgroupWriter(await session.openUniStream(), { + trackAlias, + groupId: 256, + endOfGroup: true, + }); - await stream.writeObject({ objectId: 0, payload: Uint8Array.of(1, 2, 3) }); - await stream.fin(); - await vi.waitFor(() => expect(hub.objectCount('video')).toBe(1)); - expect(hub.fills).toEqual(hasContent ? [{ requestId: 3, reset: true }] : []); - expect(onClosed).not.toHaveBeenCalled(); - } finally { - session.destroy(); - hub.destroy(); + await stream.writeObject({ objectId: 0, payload: Uint8Array.of(1, 2, 3) }); + await stream.fin(); + await vi.waitFor(() => expect(hub.objectCount('video')).toBe(1)); + expect(hub.fills).toEqual(hasContent ? [{ requestId: 3, reset: true }] : []); + expect(onClosed).not.toHaveBeenCalled(); + } finally { + session.destroy(); + hub.destroy(); + } } - }); + ); it('re-pulls a track the publisher ended and re-registered while demand stands', async () => { const hub = createRelayHub(); From 8f3e84fe5a61215378d47cadbecfd32f0b36135a Mon Sep 17 00:00:00 2001 From: "mux-hermoq[bot]" <332784920+mux-hermoq[bot]@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:06:37 +0000 Subject: [PATCH 2/3] test(spf): state the fill Stream Count rule and pin a fill opening at end MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review (cubic, #63) read the fill counter's comment as "count every stream this side opens" and flagged a fill whose stream is still opening when the track ends: PUBLISH_DONE goes out without it. That is the intended rule, the one the track publisher applies to subgroup streams (#62): a stream counts in the step that commits it to its header, so a stream reset before its header, which the subscriber cannot attribute to the subscription, is left out. The late fill is reset with no header, so the count can never fall short of the streams the subscriber can tie to the subscription. Reword the comment to say so, including the over-count side (a counted fill whose reset discards its header, or whose write rejects), which §10.12 covers with the subscriber's timeout. Add a test that ends the track while the fill's open is pending, with and without reliable reset: Stream Count 3, the fill stream gets no write and is aborted. --- .../src/publish/session/publish-session.ts | 16 +++++-- .../session/tests/publish-session.test.ts | 48 +++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/packages/spf/src/publish/session/publish-session.ts b/packages/spf/src/publish/session/publish-session.ts index 458b36e44e..9424d1f32f 100644 --- a/packages/spf/src/publish/session/publish-session.ts +++ b/packages/spf/src/publish/session/publish-session.ts @@ -667,11 +667,17 @@ class MoqtPublishSessionImpl implements MoqtPublishSession { if (!subscriber.fills.has(fill) || subscriber.finished) return; - // PUBLISH_DONE's Stream Count is the streams this side opened, - // fill fetch streams included (§10.12), so count this one even - // when the reset may discard its header. The subscriber waits - // out a timeout for a stream reset before its header; a count - // short of the streams it does receive may close the session. + // PUBLISH_DONE's Stream Count includes fill fetch streams + // (§10.12). Count in the same synchronous step that commits this + // stream to its FETCH_HEADER — the track publisher's rule for + // subgroup streams — so any PUBLISH_DONE covers every fill that + // carries a header. A fill still opening when the track ended + // was stopped by the guard above: it is reset with no header, so + // the subscriber cannot attribute it to this subscription and + // leaving it uncounted cannot put the subscriber over the count. + // A counted fill whose reset discards its header (no commit()) + // or whose write rejects over-counts by one; for that §10.12 has + // the subscriber fall back to a timeout. subscriber.fillStreams++; await writer.write(encodeFetchHeader(requestId)); diff --git a/packages/spf/src/publish/session/tests/publish-session.test.ts b/packages/spf/src/publish/session/tests/publish-session.test.ts index 8c080c3d9a..e1ad95f29c 100644 --- a/packages/spf/src/publish/session/tests/publish-session.test.ts +++ b/packages/spf/src/publish/session/tests/publish-session.test.ts @@ -1327,6 +1327,54 @@ describe('createMoqtPublishSession', () => { await finish(); }); + it.each([{ reliableReset: true }, { reliableReset: false }])( + 'leaves a fill still opening when the track ends out of the Stream Count, headerless (reliable reset: $reliableReset)', + async ({ reliableReset }) => { + let resolveOpen!: (stream: WritableStream) => void; + const openFillStream = vi.fn( + () => + new Promise>((resolve) => { + resolveOpen = resolve; + }) + ); + const { pair, session, finish } = await makeFillHarness({ reliableReset, openFillStream }); + const track = session.registerTrack({ + trackNamespace: NAMESPACE, + trackName: 'video', + getLargestObject: () => ({ group: 4, object: 2 }), + getStreamCount: () => 3, + }); + const sub = await rawSubscribe(pair.server, 'video', 245); + + await vi.waitFor(() => expect(sub.received[0]?.kind).toBe('subscribe-ok')); + await sub.send(encodeRequestUpdate(247, { fillParameters: {} })); + await vi.waitFor(() => expect(openFillStream).toHaveBeenCalledOnce()); + + // The track ends while the fill's stream is still opening. + track.end(); + await vi.waitFor(() => expect(sub.ended()).toBe(true)); + + const write = vi.fn(); + const abort = vi.fn(); + + resolveOpen(new WritableStream({ write, abort })); + await vi.waitFor(() => expect(abort).toHaveBeenCalledOnce()); + + // A fill is counted in the same step that commits it to its + // FETCH_HEADER, as the track publisher counts subgroup streams. + // This one opened after PUBLISH_DONE, so it gets no header and is + // reset bare: the peer cannot attribute it to the subscription, so + // leaving it out of the count cannot put the peer over it. + expect(write).not.toHaveBeenCalled(); + expect(sub.received.at(-1)).toMatchObject({ + kind: 'publish-done', + statusCode: PUBLISH_DONE_STATUS.TRACK_ENDED, + streamCount: 3, + }); + await finish(); + } + ); + it("serves a relay's second pull of a live track without reliable reset support", async () => { const bindings: (number | undefined)[] = []; const { pair, session, fills, finish } = await makeFillHarness({ From f0577b432d4ea0dbad691cb310aca9e2c24c959c Mon Sep 17 00:00:00 2001 From: "mux-hermoq[bot]" <332784920+mux-hermoq[bot]@users.noreply.github.com> Date: Sun, 11 Oct 2026 16:13:54 +0000 Subject: [PATCH 3/3] test(spf): cover a relay probe whose end lands after the second pull Review (cubic, #63) suspected the probe's unbind could land after pull 237's bind and leave the binding undefined. It cannot: a subscription's end reports the newest live binding, not an unbind, and pull 237 is already accepted. Run the relay test both ways, with the probe cancelled before and after the second pull, and check the binding is still 237 once the probe's end has been handled. Reporting an unbind there fails the new case. --- .../session/tests/publish-session.test.ts | 130 ++++++++++-------- 1 file changed, 74 insertions(+), 56 deletions(-) diff --git a/packages/spf/src/publish/session/tests/publish-session.test.ts b/packages/spf/src/publish/session/tests/publish-session.test.ts index e1ad95f29c..099e38f975 100644 --- a/packages/spf/src/publish/session/tests/publish-session.test.ts +++ b/packages/spf/src/publish/session/tests/publish-session.test.ts @@ -1375,70 +1375,88 @@ describe('createMoqtPublishSession', () => { } ); - it("serves a relay's second pull of a live track without reliable reset support", async () => { - const bindings: (number | undefined)[] = []; - const { pair, session, fills, finish } = await makeFillHarness({ - reliableReset: false, - callbacks: { onTrackBinding: ({ trackAlias }) => bindings.push(trackAlias) }, - }); - let largest: { group: number; object: number } | undefined; + it.each([{ probeEnds: 'before' }, { probeEnds: 'after' }])( + "serves a relay's second pull of a live track without reliable reset support (probe ends $probeEnds the pull)", + async ({ probeEnds }) => { + const bindings: (number | undefined)[] = []; + const ended: number[] = []; + const { pair, session, fills, finish } = await makeFillHarness({ + reliableReset: false, + callbacks: { + onTrackBinding: ({ trackAlias }) => bindings.push(trackAlias), + onSubscribeEnd: ({ requestId }) => ended.push(requestId), + }, + }); + let largest: { group: number; object: number } | undefined; + + session.registerTrack({ trackNamespace: NAMESPACE, trackName: 'catalog', getLargestObject: () => largest }); + + // Moq-relay's upstream SUBSCRIBE (0.14.17 through 0.17.0): Next Object, + // plus a fill of the current group's head. + const relayPull = (requestId: number) => + openRawRequest( + pair.server, + encodeSubscribe({ + requestId, + trackNamespace: NAMESPACE, + trackName: 'catalog', + parameters: { + forward: 1, + subscriberPriority: 128, + locationFilter: { type: 'next-object' }, + fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, + groupOrder: 'descending', + includeProperties: 1, + }, + }) + ); - session.registerTrack({ trackNamespace: NAMESPACE, trackName: 'catalog', getLargestObject: () => largest }); + // A cross-relay viewer's first pull is the relay's TRACK_INFO probe: + // nothing published yet, so there is nothing to fill. + const probe = await relayPull(235); - // Moq-relay's upstream SUBSCRIBE (0.14.17 through 0.17.0): Next Object, - // plus a fill of the current group's head. - const relayPull = (requestId: number) => - openRawRequest( - pair.server, - encodeSubscribe({ - requestId, - trackNamespace: NAMESPACE, - trackName: 'catalog', - parameters: { - forward: 1, - subscriberPriority: 128, - locationFilter: { type: 'next-object' }, - fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, - groupOrder: 'descending', - includeProperties: 1, - }, - }) - ); + await vi.waitFor(() => expect(probe.received.map((m) => m.kind)).toEqual(['subscribe-ok'])); - // A cross-relay viewer's first pull is the relay's TRACK_INFO probe: - // nothing published yet, so there is nothing to fill. - const probe = await relayPull(235); + // The probe's bind put the catalog on the wire, then the relay + // cancelled it and pulled again for the viewer — now with a nonempty + // fill this transport cannot fail reliably. The pull is served anyway. + largest = { group: 0, object: 0 }; - await vi.waitFor(() => expect(probe.received.map((m) => m.kind)).toEqual(['subscribe-ok'])); + if (probeEnds === 'before') await probe.reset(); - // The probe's bind put the catalog on the wire, then the relay - // cancelled it and pulled again for the viewer — now with a nonempty - // fill this transport cannot fail reliably. The pull is served anyway. - largest = { group: 0, object: 0 }; - await probe.reset(); - const pull = await relayPull(237); + const pull = await relayPull(237); - await vi.waitFor(() => { - expect(pull.received.map((m) => m.kind)).toEqual(['subscribe-ok']); + await vi.waitFor(() => { + expect(pull.received.map((m) => m.kind)).toEqual(['subscribe-ok']); + expect(bindings.at(-1)).toBe(237); + expect(fills).toEqual([{ requestId: 237, reset: true }]); + }); + + // The probe's end may be handled after the pull is bound: it reports + // the newest live binding, not an unbind, so the pull keeps it. + if (probeEnds === 'after') await probe.reset(); + + await vi.waitFor(() => expect(ended).toContain(235)); expect(bindings.at(-1)).toBe(237); - expect(fills).toEqual([{ requestId: 237, reset: true }]); - }); - expect(pull.ended()).toBe(false); + expect(pull.ended()).toBe(false); - // A fill requested by REQUEST_UPDATE is acknowledged and failed the same way. - await pull.send( - encodeRequestUpdate(239, { fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } } }) - ); - await vi.waitFor(() => { - expect(pull.received.map((m) => m.kind)).toEqual(['subscribe-ok', 'request-ok']); - expect(fills).toEqual([ - { requestId: 237, reset: true }, - { requestId: 239, reset: true }, - ]); - }); - expect(pull.ended()).toBe(false); - await finish(); - }); + // A fill requested by REQUEST_UPDATE is acknowledged and failed the same way. + await pull.send( + encodeRequestUpdate(239, { + fillParameters: { locationFilter: { type: 'relative-group', groupsBeforeNext: 1 } }, + }) + ); + await vi.waitFor(() => { + expect(pull.received.map((m) => m.kind)).toEqual(['subscribe-ok', 'request-ok']); + expect(fills).toEqual([ + { requestId: 237, reset: true }, + { requestId: 239, reset: true }, + ]); + }); + expect(pull.ended()).toBe(false); + await finish(); + } + ); it('still resets the fill when the reset may discard its header (no reliable reset)', async () => { const fill = makeBufferedFillStream({ reliableReset: false });