diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 8c7d801b580..a357008169e 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -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 diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 41d41bf2c78..6465ca3a994 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -1754,16 +1754,22 @@ export class Publisher { const deadline = new Deadline(); const answer = await deadline.wait( (async (): Promise => { - 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(); @@ -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 @@ -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. @@ -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; } /** diff --git a/js/net/src/ietf/subscriber.test.ts b/js/net/src/ietf/subscriber.test.ts index 3301ee2cc07..c9f960a3adc 100644 --- a/js/net/src/ietf/subscriber.test.ts +++ b/js/net/src/ietf/subscriber.test.ts @@ -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 diff --git a/js/net/src/util/deadline.ts b/js/net/src/util/deadline.ts index 8c074820fc3..7b1f0fdd0b5 100644 --- a/js/net/src/util/deadline.ts +++ b/js/net/src/util/deadline.ts @@ -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; + withdraw?: () => Promise; +} + /** 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. */ @@ -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, withdraw?: () => Promise): Promise { + 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(answer: Promise): Promise { try { @@ -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); + stream.abort(failure); + } } diff --git a/quest/m1/README.md b/quest/m1/README.md index 872f4f36c37..21220f265b8 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -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 diff --git a/quest/m1/legacy-request-timeout-cleanup.md b/quest/m1/legacy-request-timeout-cleanup.md deleted file mode 100644 index 47f9e210ca5..00000000000 --- a/quest/m1/legacy-request-timeout-cleanup.md +++ /dev/null @@ -1,39 +0,0 @@ -# [S] Timed-out requests clean up on drafts 14-16 - -## Goal - -When a request on moq-transport drafts 14-16 times out after it reached the -peer, Rust and JS both send the legacy cleanup message before the local -reset: PUBLISH_NAMESPACE_DONE for an advertisement, and UNSUBSCRIBE_NAMESPACE -for a namespace subscription on drafts 14-15. Unanswered advertisements stay -terminal, as #5741 decided; only the cleanup changes. - -On drafts 14-16 a request rides a virtual control-stream stream whose reset -never reaches the peer, so today a timed-out PUBLISH_NAMESPACE is never -withdrawn and the peer can hold the namespace until the session closes: - -- JS: `Deadline` (`js/net/src/util/deadline.ts`) aborts every stream it - recorded when the deadline fires, before the caller can write the cleanup. -- Rust: when `read_response` in `rs/moq-net/src/ietf/publisher.rs` gives up - on an advertisement, the request is dropped without PUBLISH_NAMESPACE_DONE. - -## Plan - -Decided with the maintainer on 2026-10-17: - -- The goal comes from the review of - [#5741](https://github.com/moq-dev/moq/pull/5741), which put the JS fix in - `Deadline`'s timeout path rather than in each caller. -- Widened to Rust and JS in one quest, rather than a separate Rust quest, - since both have the same gap on the same drafts. - -The JS legacy SUBSCRIBE path opens its virtual stream outside `Deadline`, and -its timeout cleanup already sends UNSUBSCRIBE (`#cancelSubscribe`); keep that -covered by a test while moving the mechanism. Check every other timeout on -these drafts, in both languages, for the same gap. Draft 17 and later keep -the stream reset alone. - -Test with mocked time on drafts 14, 15 and 16, in Rust and JS: a timed-out -PUBLISH_NAMESPACE whose message was sent is followed by PUBLISH_NAMESPACE_DONE -on the control stream, and the same for UNSUBSCRIBE_NAMESPACE on drafts -14-15. Wire: no new messages. diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 61f61d9cd60..fd837c6c910 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -2992,7 +2992,7 @@ where .advertise_namespace(requests, watch, path, suffix.clone(), advert.params(), auth) .await?; if let Some(old) = old { - self.end_request(old).await; + self.end_request(old.stream, &old.path, old.request_id).await; } } (true, Some(_)) => { @@ -3138,6 +3138,9 @@ where Ok(Answer::Message(framed)) => framed, Ok(Answer::Late) => { tracing::debug!(broadcast = %absolute, "no answer to the advertisement"); + // The peer may hold the request it never answered. Dropping one riding the + // draft-14-16 control stream tells it nothing, so it is withdrawn instead. + self.end_request(request, path, request_id).await; return Ok(Refused::No); } Ok(Answer::Ended) => { @@ -3576,7 +3579,8 @@ where match target { Target::Requests(_) => { if let Some(request) = requests.remove(&suffix) { - self.end_request(request).await; + self.end_request(request.stream, &request.path, request.request_id) + .await; } } Target::Inline(stream) => { @@ -3593,27 +3597,26 @@ where } /// Withdraw one PUBLISH_NAMESPACE request, best effort: the peer may already be gone. - async fn end_request(&self, mut request: NamespaceRequest) { + async fn end_request(&self, mut stream: Stream, path: &crate::PathOwned, request_id: RequestId) { // Draft-17+ withdraws by cancelling the request in both directions; a FIN is only // allowed to end SUBSCRIBE_NAMESPACE and SUBSCRIBE_TRACKS. if !matches!(self.version, Version::Draft14 | Version::Draft15 | Version::Draft16) { - request.stream.reader.abort(&Error::Cancel); - request.stream.writer.abort(&Error::Cancel); + stream.reader.abort(&Error::Cancel); + stream.writer.abort(&Error::Cancel); return; } - let _ = request - .stream + let _ = stream .writer .encode_message(&ietf::PublishNamespaceDone { - track_namespace: request.path.as_path(), - request_id: request.request_id, + track_namespace: path.as_path(), + request_id, }) .await; // The withdrawal rides this request's own stream, which drops with it, so it needs // the acknowledgement before the drop-time reset can discard it. - let _ = request.stream.writer.close().await; + let _ = stream.writer.close().await; } /// Close out every open PUBLISH_NAMESPACE request. A no-op for a loop whose entries @@ -14872,7 +14875,7 @@ mod tests { let mut ok = bytes::Bytes::from(publish_namespace_ok(version).await); held.writer.write_all(&mut ok).await.unwrap(); - let (_again, name) = offered().await; + let (mut again, name) = offered().await; assert_eq!( name, first, "{version} fin={fin}: the failed namespace was not offered again" @@ -14882,6 +14885,9 @@ mod tests { "{version} fin={fin}: re-offered after {:?}", ended.elapsed() ); + // Answered, so the loop moves on now rather than withdrawing it at its deadline. + let mut ok = bytes::Bytes::from(publish_namespace_ok(version).await); + again.writer.write_all(&mut ok).await.unwrap(); cams.remove(second); match version { @@ -15271,6 +15277,52 @@ mod tests { ); } + /// An advertisement the peer never answers is given up at its deadline. On drafts 14-16 + /// it rides the control stream, where dropping it tells the peer nothing, so it is + /// withdrawn with PUBLISH_NAMESPACE_DONE; from draft 17 its own stream's reset ends it. + #[moq_clock::test] + async fn an_unanswered_advertisement_is_withdrawn() { + for version in [Version::Draft14, Version::Draft15, Version::Draft16, Version::Draft17] { + let origin = unheld_origin(); + let _cam = origin.announce("cam", crate::origin::Route::default()).unwrap(); + settle().await; + + // The peer takes the request and answers nothing. + let session = crate::lite::test_transport::ScriptedSession::per_stream(vec![Vec::new()]); + let log = session.log.clone(); + let publisher = Publisher::new( + crate::time::sim(), + session, + origin.consume(), + Control::new(None, false), + None, + declared(Some(false)), + version, + ); + let mut run = std::pin::pin!(publisher.run_publish_namespaces()); + poll_until(&mut run, &log, b"cam", 1).await; + + let done = match version { + Version::Draft17 => None, + _ => Some(publish_namespace_done(version).await), + }; + let withdrawn = || match &done { + Some(done) => occurrences(&log, done) > 0, + None => !log.resets().is_empty(), + }; + moq_clock::sleep(ADVERTISE_TIMEOUT - Duration::from_millis(10)).await; + assert!(futures::poll!(run.as_mut()).is_pending()); + assert!(!withdrawn(), "{version}: withdrawn before its deadline"); + + moq_clock::sleep(Duration::from_millis(20)).await; + assert!(futures::poll!(run.as_mut()).is_pending()); + assert!( + withdrawn(), + "{version}: the unanswered advertisement was never withdrawn" + ); + } + } + /// Credit returning raises no signal of its own: no announce, no route change, nothing /// the loop is watching. Only a retry brings the namespace back, and without one it /// stays undiscoverable for the life of the session.