diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 66b6b7874d4..760d4657ba0 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -1,7 +1,6 @@ -import { expect, jest, mock, spyOn, test } from "bun:test"; -import { Signal } from "@moq/signals"; +import { expect, jest, spyOn, test } from "bun:test"; import type { Producer as BroadcastProducer } from "../broadcast.ts"; -import { error } from "../error.ts"; +import { error, StreamCode } from "../error.ts"; import { Producer as GroupProducer, MAX_GROUP_FRAMES } from "../group.ts"; import { type Hop, HopSchema } from "../hop.ts"; import { createMockTransportPair } from "../mock.ts"; @@ -386,89 +385,6 @@ test("a group that goes stale while its stream opens writes nothing", async () = } }); -test.each(["acknowledged", "rejected"] as const)( - "a replacement waits until its predecessor's FIN is %s", - async (result) => { - const pair = createMockTransportPair(ALPN.DRAFT_19); - const open = pair.server.createBidirectionalStream.bind(pair.server); - const closing = Promise.withResolvers(); - const acknowledged = Promise.withResolvers(); - let opened = 0; - pair.server.createBidirectionalStream = async (options) => { - const stream = await open(options); - if (++opened !== 1) return stream; - const writer = stream.writable.getWriter(); - return { - readable: stream.readable, - writable: new WritableStream({ - write: (chunk) => writer.write(chunk), - async close() { - await writer.close(); - closing.resolve(); - await acknowledged.promise; - }, - abort: (reason) => writer.abort(reason), - }), - } as WebTransportBidirectionalStream; - }; - const { pub, origin } = publisher(pair.server); - let first: BroadcastProducer | undefined; - let second: BroadcastProducer | undefined; - const changed = Signal.prototype.changed; - const disposed = mock(() => {}); - let registration: ReturnType> | undefined; - const loop = pub.runPublishNamespaces(); - try { - first = publish(origin, Path.from("replacement")); - const old = await nextStream(pair.client); - if (!old) throw new Error("missing initial advertisement"); - expect(await readPublishNamespace(old)).toBe(Path.from("replacement")); - await acceptPublishNamespace(old); - registration = spyOn(Signal.prototype, "changed").mockImplementation(function ( - this: Signal, - fn?: (value: unknown) => void, - ) { - const original = changed.bind(this); - if (!fn) return original(); - const dispose = original(fn); - return () => { - dispose(); - disposed(); - }; - } as typeof changed); - first.close(); - second = publish(origin, Path.from("replacement")); - await closing.promise; - const early = await nextStream(pair.client); - early?.abort(new Error("replacement arrived before acknowledgment")); - expect(early).toBeUndefined(); - expect(opened).toBe(1); - if (result === "rejected") { - expect(disposed).not.toHaveBeenCalled(); - acknowledged.reject(new Error("FIN acknowledgment failed")); - await loop; - expect(opened).toBe(1); - expect(disposed).toHaveBeenCalled(); - return; - } - acknowledged.resolve(); - const replacement = await nextStream(pair.client); - if (!replacement) throw new Error("missing replacement after acknowledgment"); - expect(await readPublishNamespace(replacement)).toBe(Path.from("replacement")); - await acceptPublishNamespace(replacement); - } finally { - registration?.mockRestore(); - acknowledged.resolve(); - first?.close(); - second?.close(); - origin.close(); - await loop; - pair.client.close(); - pair.server.close(); - } - }, -); - /** * Every advertisement waits a round trip for the peer's reply. A broadcast published in * that window has to survive it: the loop is not watching the signal while it waits, so @@ -1224,8 +1140,8 @@ test("a republish withdraws the PUBLISH_NAMESPACE and advertises again", async ( republish(origin, broadcast); - // Draft-17+ withdraws with the FIN alone, and the replacement waits for it. - expect(await old.reader.done()).toBe(true); + // Draft-17+ withdraws by cancelling the request, never a FIN. + expect(await old.reader.done().catch(error)).toMatchObject({ code: StreamCode.Cancel }); const next = await take(streams); expect(await acceptClustered(next)).toMatchObject({ cost: 4n }); } finally { @@ -1285,8 +1201,8 @@ test.each([ await readUpdate(stream); await declinePublishNamespace(stream, retryInterval); - // Our side finishes too, which completes the withdrawal. - expect(await stream.reader.done()).toBe(true); + // Our side cancels too, which completes the withdrawal. + expect(await stream.reader.done().catch(error)).toMatchObject({ code: StreamCode.Cancel }); // Well past our own backoff, but inside the interval the peer asked for. await advance(9_000); @@ -1308,9 +1224,9 @@ test.each([ }); /** - * A peer that refuses an update and then closes its side can make our withdrawal's wait - * on the FIN reject. The request is gone either way, so the loop keeps the refusal and - * re-offers the namespace instead of ending. + * A peer that refuses an update and then closes its side races our own withdrawal. The + * request is gone either way, so the loop keeps the refusal and re-offers the namespace + * instead of ending. */ test("a refused update whose peer closes the request keeps the loop running", async () => { const { pub, origin, streams, close } = clustered(); diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 47c2e633f76..337e97cece5 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -1310,41 +1310,37 @@ export class Publisher { return "dropped"; } - // The peer already closed out a refused request, and its STOP_SENDING can reject the - // wait on our FIN. That is the withdrawal finishing, not a reason to end the loop. + // The peer may already have closed out a refused request. Ending it again is the + // withdrawal finishing, not a reason to end the loop. if (answer !== "held") await this.#withdraw(path, requests).catch(() => undefined); return answer; } /** - * Close out a namespace's PUBLISH_NAMESPACE request with PUBLISH_NAMESPACE_DONE. + * End a namespace's PUBLISH_NAMESPACE request: PUBLISH_NAMESPACE_DONE then FIN before + * draft-17, a cancel in both directions after. Neither waits on the peer. */ async #withdraw(path: Path.Valid, requests: Requests) { const request = requests.get(path); if (!request) return; requests.delete(path); - // Draft-17+ removed PUBLISH_NAMESPACE_DONE: the close below is the whole - // withdrawal. Sending it anyway puts the type on the wire before the body throws, - // and a receiver reading 0x09 there has no choice but to treat it as a protocol - // violation. + // Draft-17+ withdraws by cancelling the request in both directions; a FIN is only + // allowed to end SUBSCRIBE_NAMESPACE and SUBSCRIBE_TRACKS. const version = this.#session.version; - if (version === Version.DRAFT_14 || version === Version.DRAFT_15 || version === Version.DRAFT_16) { - 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); - } catch { - // Stream might already be closed - } - request.stream.close(); + if (version !== Version.DRAFT_14 && version !== Version.DRAFT_15 && version !== Version.DRAFT_16) { + request.stream.abort(new StreamError(StreamCode.Cancel, { message: "withdrawn" })); return; } + + 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); + } catch { + // Stream might already be closed + } request.stream.close(); - // Wait for transport acknowledgment before opening a replacement request. - // This assumes the peer processes the withdrawal by then; FIN acknowledgment - // does not itself acknowledge application processing across streams. - await request.stream.writer.closed; } /** diff --git a/js/net/src/ietf/subscriber.test.ts b/js/net/src/ietf/subscriber.test.ts index 8d34fdd969d..fd672c81a50 100644 --- a/js/net/src/ietf/subscriber.test.ts +++ b/js/net/src/ietf/subscriber.test.ts @@ -101,6 +101,53 @@ test("an unsolicited announcement lands", async () => { }); }); +/** + * Drafts 17+ withdraw a PUBLISH_NAMESPACE by resetting it, so the reset is a routine end: + * the handler retracts and returns rather than failing the bidi stream. + */ +test("a reset PUBLISH_NAMESPACE is withdrawn without an error", async () => { + const pair = createMockTransportPair(ALPN.DRAFT_19); + const session = new NativeSession(pair.server, VERSION, true); + const subscriber = new Subscriber({ session }); + + const announced = subscriber.announced(); + expect(await nextStream(pair.client)).toBeDefined(); + + const stream = await Stream.open(pair.server, { version: VERSION }); + const handler = subscriber.runPublishNamespace( + new PublishNamespace({ requestId: 0n, trackNamespace: Path.from("reset") }), + stream, + ); + expect(await announced.next()).toMatchObject({ prefix: Path.from("reset"), kind: "announced" }); + + const peer = await nextStream(pair.client); + if (!peer) throw new Error("no PUBLISH_NAMESPACE stream to reset"); + peer.abort(new StreamError(StreamCode.Cancel, { message: "withdrawn" })); + await handler; + expect(await announced.next()).toMatchObject({ prefix: Path.from("reset"), kind: "retracted" }); +}); + +/** Any other reset is the peer failing, and still fails the request. */ +test("a PUBLISH_NAMESPACE reset with an error fails the request", async () => { + const pair = createMockTransportPair(ALPN.DRAFT_19); + const session = new NativeSession(pair.server, VERSION, true); + const subscriber = new Subscriber({ session }); + + subscriber.announced(); + expect(await nextStream(pair.client)).toBeDefined(); + + const stream = await Stream.open(pair.server, { version: VERSION }); + const handler = subscriber.runPublishNamespace( + new PublishNamespace({ requestId: 0n, trackNamespace: Path.from("failed") }), + stream, + ); + + const peer = await nextStream(pair.client); + if (!peer) throw new Error("no PUBLISH_NAMESPACE stream to reset"); + peer.abort(new StreamError(StreamCode.Internal, { message: "broken" })); + await expect(handler).rejects.toMatchObject({ code: StreamCode.Internal }); +}); + /** * A session without the Cluster extension names no publisher, so each connection stamps its * own random Hop ID in front of a 0: a publisher that reconnects reads as a new one, and the 0 diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index f8784cf637c..b96f1b014fd 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -2,7 +2,16 @@ import { race, Signal } from "@moq/signals"; import * as announce from "../announced.ts"; import * as broadcast from "../broadcast.ts"; import { BroadcastCache } from "../consume.ts"; -import { closeError, controlTimeout, error, ProtocolViolation, reason, sessionCause } from "../error.ts"; +import { + closeError, + controlTimeout, + error, + ProtocolViolation, + reason, + StreamCode, + StreamError, + sessionCause, +} from "../error.ts"; import * as netGroup from "../group.ts"; import { Cost, type Route, randomHop, routesEqual, stampHops, UNKNOWN_HOP } from "../hop.ts"; import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts"; @@ -893,7 +902,13 @@ export class Subscriber { let held = msg.cluster; const done = version === Version.DRAFT_16 || legacy; for (;;) { - if (await stream.reader.done()) break; + // Drafts 17+ withdraw by resetting the request with CANCELLED, which ends it like a + // FIN. Any other reset is a failure. + const ended = await stream.reader.done().catch((err: unknown) => { + if (!done && err instanceof StreamError && err.code === StreamCode.Cancel) return true; + throw err; + }); + if (ended) break; const typeId = await stream.reader.u53(); if (done && typeId === PublishNamespaceDone.id) { diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index 5a91fda6f95..e0374aa1e86 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -1864,6 +1864,14 @@ test("integration: ietf republish on one session swaps the handle every generati await runRepublishCycle("", Ietf.Version.DRAFT_14); }); +// Draft-17+ withdraws by reset, which the replacement request does not wait behind. +test.each([Ietf.Version.DRAFT_17, Ietf.Version.DRAFT_19])( + "integration: ietf republish withdrawn by reset swaps the handle (%#)", + async (version) => { + await runRepublishCycle("", version); + }, +); + test("integration: a blind handle picks up a publisher that arrives late", async () => { const pair = createMockTransportPair(Lite.ALPN_06); const origin = new OriginProducer();