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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,52 @@ test("an unanswered draft 16 update withdraws its request before offering it aga
}
});

/**
* A draft-14-16 request rides the control stream, where the deadline's reset tells the peer
* nothing, so an advertisement the peer never answers is withdrawn with PUBLISH_NAMESPACE_DONE.
* It stays terminal: nothing is offered again.
*/
test.each([
["14", Version.DRAFT_14],
["15", Version.DRAFT_15],
["16", Version.DRAFT_16],
] as const)("an unanswered draft %s advertisement is withdrawn at its deadline", async (_, version) => {
jest.useFakeTimers();
const pair = createMockTransportPair(ALPNS[version]);
const control = await Stream.open(pair.server, { version });
const adapter = new ControlStreamAdapter(pair.server, control, version, 100n, false);
void adapter.run().catch(() => undefined);
const peer = await Stream.accept(pair.client, version);
if (!peer) throw new Error("no control stream");
const { pub, origin } = publisher(pair.server, { session: adapter });
const warn = spyOn(console, "warn").mockImplementation(() => void 0);
try {
origin.createBroadcast(Path.from("mine")).announce();
void pub.runPublishNamespaces();

expect(await peer.reader.u53()).toBe(PublishNamespace.id);
const advertised = await PublishNamespace.decode(peer.reader, version);

const next = watch(peer.reader.u53());
await advance(9_950);
expect(next.state).toBe("pending");
await advanceUntil(() => next.state !== "pending", 100);
expect(next.value).toBe(PublishNamespaceDone.id);
const done = await PublishNamespaceDone.decode(peer.reader, version);
if (version === Version.DRAFT_16) expect(done.requestId).toBe(advertised.requestId);
else expect(done.trackNamespace).toBe(advertised.trackNamespace);

const more = watch(peer.reader.u53());
await advance(30_000);
expect(more.state).toBe("pending");
} finally {
warn.mockRestore();
origin.close();
adapter.close();
jest.useRealTimers();
}
});

/**
* A draft-16 PUBLISH_NAMESPACE_CANCEL can beat the answer to our update. The peer already let
* the namespace go, and draft 16 sends no PUBLISH_NAMESPACE_DONE after a cancel, so the next
Expand Down
44 changes: 27 additions & 17 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1754,16 +1754,22 @@ export class Publisher {
const deadline = new Deadline();
const answer = await deadline.wait(
(async (): Promise<Answer> => {
const opened = new RequestStream(
await deadline.open(this.#session.openBi(requestOptions)),
this.#session,
this.#quic,
);
const stream = await deadline.open(this.#session.openBi(requestOptions));
const opened = new RequestStream(stream, this.#session, this.#quic);
request = opened;
await opened.writer.u53(PublishNamespace.id);
const version = this.#session.version;
const token = this.#auth?.carried(auth);
const msg = new PublishNamespace({ requestId, trackNamespace: path, params, token });
await msg.encode(opened.writer, this.#session.version);
const legacy =
version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16;
await deadline.send(
stream,
async () => {
await opened.writer.u53(PublishNamespace.id);
await msg.encode(opened.writer, version);
},
legacy ? () => this.#done(opened, path, requestId) : undefined,
);

// Read response (RequestOk and PublishNamespaceOk share 0x07)
const response = await opened.next();
Expand Down Expand Up @@ -1945,10 +1951,6 @@ export class Publisher {
return answer;
}

/**
* End a namespace's PUBLISH_NAMESPACE request: PUBLISH_NAMESPACE_DONE then FIN before
* draft-17, a cancel in both directions after.
*/
/**
* Replace the PUBLISH_NAMESPACE request for `path` with one for another instance. The two
* streams are unordered, so the old one is withdrawn only once the peer answered the new
Expand All @@ -1969,7 +1971,10 @@ export class Publisher {
await this.#end(request);
}

/** Withdraw one PUBLISH_NAMESPACE request, already out of its map. */
/**
* Withdraw one PUBLISH_NAMESPACE request, already out of its map: PUBLISH_NAMESPACE_DONE
* then FIN before draft-17, a cancel in both directions after.
*/
async #end(request: NamespaceRequest) {
// Draft-17+ withdraws by cancelling the request in both directions; a FIN is only
// allowed to end SUBSCRIBE_NAMESPACE and SUBSCRIBE_TRACKS.
Expand All @@ -1979,15 +1984,20 @@ export class Publisher {
return;
}

await this.#done(request.stream, request.path, request.requestId);
request.stream.close();
await request.stream.writer.closed;
}

/** Write the PUBLISH_NAMESPACE_DONE that withdraws a draft-14-16 request. */
async #done(request: RequestStream, path: Path.Valid, requestId: bigint) {
try {
await request.stream.writer.u53(PublishNamespaceDone.id);
const done = new PublishNamespaceDone({ trackNamespace: request.path, requestId: request.requestId });
await done.encode(request.stream.writer, version);
await request.writer.u53(PublishNamespaceDone.id);
const done = new PublishNamespaceDone({ trackNamespace: path, requestId });
await done.encode(request.writer, this.#session.version);
} catch {
// Stream might already be closed
}
request.stream.close();
await request.stream.writer.closed;
}

/**
Expand Down
38 changes: 38 additions & 0 deletions js/net/src/ietf/subscriber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,44 @@ test("a legacy cancel reaches the control stream", async () => {
expect(seen).toContain(BigInt(Unsubscribe.id));
});

/**
* A draft-14-16 SUBSCRIBE rides the control stream, where a reset tells the peer nothing, so
* one the peer never answers is cancelled with UNSUBSCRIBE at its deadline.
*/
test.each([
["14", Version.DRAFT_14, ALPN.DRAFT_14],
["15", Version.DRAFT_15, ALPN.DRAFT_15],
["16", Version.DRAFT_16, ALPN.DRAFT_16],
] as const)("an unanswered draft %s subscribe is unsubscribed at its deadline", async (_, version, alpn) => {
jest.useFakeTimers();
const pair = createMockTransportPair(alpn);
const control = await Stream.open(pair.server, { version });
const session = new ControlStreamAdapter(pair.server, control, version, 100n, true);
void session.run().catch(() => undefined);
const peer = await Stream.accept(pair.client, version);
if (!peer) throw new Error("no control stream");
const subscriber = new Subscriber({ session });
const broadcast = subscriber.consume(Path.from("room"));
const track = broadcast.track("video").subscribe();
const warn = spyOn(console, "warn").mockImplementation(() => void 0);
try {
expect(await peer.reader.u53()).toBe(Subscribe.id);
const request = await Subscribe.decode(peer.reader, version);

const next = peer.reader.u53();
jest.advanceTimersByTime(10_000);
expect(await next).toBe(Unsubscribe.id);
expect((await Unsubscribe.decode(peer.reader, version)).requestId).toBe(request.requestId);
expect(((await track.closed) as Error).message).toContain("request timed out after 10000ms");
} finally {
warn.mockRestore();
track.close();
broadcast.close();
session.close();
jest.useRealTimers();
}
});

/**
* A publisher that rejects a SUBSCRIBE has torn the request down before answering, so there
* is nothing left to cancel. Naming a dead request id back at it is what a strict peer can
Expand Down
44 changes: 39 additions & 5 deletions js/net/src/util/deadline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ function sessionFailure(err: unknown): boolean {
return source === "session" || name === "InvalidStateError";
}

// A stream the deadline resets, with the request written on it and the message withdrawing it.
interface Recorded {
stream: Stream;
written?: Promise<boolean>;
withdraw?: () => Promise<void>;
}

/** One fatal deadline for a request, including every stream its setup creates. @internal */
export class Deadline {
#opening = false;
#streams: Stream[] = [];
#streams: Recorded[] = [];
#failure?: Error;

/** Record a create so deadline failures reset its stream even if it opens late. */
Expand All @@ -38,10 +45,29 @@ export class Deadline {
stream.abort(this.#failure);
throw this.#failure;
}
this.#streams.push(stream);
this.#streams.push({ stream });
return stream;
}

/**
* Write the request on an opened stream, and what withdraws it if the deadline fails after.
*
* A draft-14-16 request rides the control stream, where a reset tells the peer nothing, so
* one that reached the peer is withdrawn by message first. Without `withdraw` the reset alone ends it.
*/
async send(stream: Stream, write: () => Promise<void>, withdraw?: () => Promise<void>): Promise<void> {
if (this.#failure) throw this.#failure;
const recorded = this.#streams.find((recorded) => recorded.stream === stream);
if (!recorded) throw new Error("request sent on a stream the deadline did not open");
const sending = write();
recorded.written = sending.then(
() => true,
() => false,
);
recorded.withdraw = withdraw;
await sending;
}

/** Wait for the first answer under the request's single deadline. */
async wait<T>(answer: Promise<T>): Promise<T> {
try {
Expand All @@ -51,17 +77,25 @@ export class Deadline {
`request timed out after ${REQUEST_TIMEOUT_MS}ms`,
);
} catch (cause) {
this.#failure =
const failure =
cause instanceof TimeoutError
? this.#opening
? new NoStreamCredit({ cause })
: controlTimeout(cause)
: error(cause);
this.#failure = failure;
// Other failures keep the caller's cancellation ordering, including legacy UNSUBSCRIBE.
if (cause instanceof TimeoutError || cause instanceof NoStreamCredit) {
for (const stream of this.#streams) stream.abort(this.#failure);
await Promise.all(this.#streams.map((recorded) => Deadline.#reset(recorded, failure)));
}
throw this.#failure;
throw failure;
}
}

static async #reset({ stream, written, withdraw }: Recorded, failure: Error) {
// The withdrawal names a request the peer must already have, so a write still in
// flight finishes first, and one that never landed is not withdrawn.
if (withdraw && written && (await written)) await withdraw().catch(() => undefined);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the deadline while waiting for cleanup

When a draft-14–16 peer stops reading the shared control stream while the initial PUBLISH_NAMESPACE write is backpressured, written never resolves, so the timeout path now waits forever here instead of aborting at 10 seconds; the subsequent withdrawal can likewise block without a bound. This wedges the single publish-namespace loop and prevents every queued advertisement or withdrawal from progressing, so cleanup must remain bounded even when the peer is not consuming control data.

AGENTS.md reference: AGENTS.md:L18-L18

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declining: the wait is already bounded by the session, and a deadline here would not unwedge anything.

  • On drafts 14-16 every request shares one control-stream writer behind a single write mutex. A peer that stops reading it blocks every later control write, including the next advertisement and every withdrawal. Aborting at 10 seconds would let this loop move on only to queue its next write behind the same mutex.
  • The loop's normal withdrawal already waits the same way: #withdraw → #end awaits #done and then writer.closed with no deadline. Rust's end_request also awaits the DONE encode and the close.
  • The cost to us is one parked task with no state growth. Waiting actually removes growth: before this change, each timed-out advertisement left its write queued and the loop went on to queue more.
  • The wait ends with the session. When the transport or the adapter closes, the queued write rejects, so written resolves false and the reset goes through.
  • Aborting without waiting would be wrong. A write already in the mutex queue still reaches the peer after the virtual stream is aborted, so a request the peer later sees would never be withdrawn. That stale state is what this PR fixes.

Waiting before throwing was a maintainer decision on this PR (Decisions, item 2).

(Written by Claude Opus 5.5)

stream.abort(failure);
}
}
1 change: 0 additions & 1 deletion quest/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ lands before the cut.
- [Rust request credit](/quest/m1/rs-request-credit.md) - a moq-net request fails at once without stream credit instead of waiting
- [A stalled subscriber holds bounded state](/quest/m1/egress-held-state.md) - group machines and bytes a credit-withholding downstream makes a publisher hold plateau at what its credit and the latency budget allow, on lite and moq-transport
- [qmux no-wait opens](/quest/m1/qmux-no-wait.md) - the in-tree @moq/qmux rejects an over-limit create when waitUntilAvailable is false, like Chrome
- [Timed-out requests clean up](/quest/m1/legacy-request-timeout-cleanup.md) - on drafts 14-16, Rust and JS send PUBLISH_NAMESPACE_DONE or UNSUBSCRIBE_NAMESPACE for a timed-out request before its local reset
- [TS fidelity and recovery](/quest/m1/ts-reliability/README.md) - both modes have explicit profiles, automated validation, loss tests, and a dedicated guide
- [A widened max age re-offers cached groups](/quest/m1/resolved-demand.md) - a warm relay serves a reader with a wider max age the cached groups a cold relay would, with no wire change
- [Drop set](/quest/m1/drop-set.md) - each cursor judges missing groups against its own budget, so a narrow reader drops promptly and a widening re-offers groups already reported dropped on lite-07
Expand Down
39 changes: 0 additions & 39 deletions quest/m1/legacy-request-timeout-cleanup.md

This file was deleted.

Loading
Loading