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
24 changes: 15 additions & 9 deletions js/net/src/ietf/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ import { type IetfVersion, Version, versionName } from "./version.ts";
// The PADDING stream type (draft-18+): bytes a peer sends to probe for bandwidth.
const PADDING = 0x132b3e28n;

// A handler owns its request stream, and ending the request (closing the session over a
// violation) is its job: closing again here could only send a competing code.
function handed(task: Promise<void>): Promise<void> {
return task.catch((err: unknown) => console.error("error processing bidi stream", err));
}

/**
* Represents a connection to a MoQ server using moq-transport protocol.
*
Expand Down Expand Up @@ -373,7 +379,7 @@ export class Connection implements Established {
case BigInt(SubscribeNamespace.id): {
const decoded = SubscribeNamespace.decode(stream.reader, this.#session.version);
const msg = await this.#request(stream, "subscribe_namespace", decoded);
if (msg) await this.#publisher.runSubscribeNamespace(msg, stream);
if (msg) await handed(this.#publisher.runSubscribeNamespace(msg, stream));
break;
}
case BigInt(SubscribeNamespaceLegacy.id): {
Expand Down Expand Up @@ -403,14 +409,14 @@ export class Connection implements Established {
this.#spend(legacy.requestId);
try {
if (legacy.subscribeOptions === SubscribeOptions.PUBLISH) {
await this.#publisher.refuseSubscribeNamespace(legacy.requestId, stream);
await handed(this.#publisher.refuseSubscribeNamespace(legacy.requestId, stream));
} else {
const msg = new SubscribeNamespace({
requestId: legacy.requestId,
namespace: legacy.namespace,
hidden: legacy.hidden,
});
await this.#publisher.runSubscribeNamespace(msg, stream);
await handed(this.#publisher.runSubscribeNamespace(msg, stream));
}
} finally {
adapter?.releaseRequest(legacy.requestId);
Expand All @@ -426,7 +432,7 @@ export class Connection implements Established {
case BigInt(Subscribe.id): {
const decoded = Subscribe.decode(stream.reader, this.#session.version);
const msg = await this.#request(stream, "subscribe", decoded);
if (msg) await this.#publisher.runSubscribe(msg, stream);
if (msg) await handed(this.#publisher.runSubscribe(msg, stream));
break;
}
case BigInt(SUBSCRIBE_TRACKS_ID): {
Expand All @@ -443,19 +449,19 @@ export class Connection implements Established {
throw malformed(err, "subscribe_tracks");
});
this.#session.spendRequest(request.requestId);
await this.#publisher.runSubscribeTracks(stream, request.namespace);
await handed(this.#publisher.runSubscribeTracks(stream, request.namespace));
break;
}
case BigInt(TrackStatusRequest.id): {
const decoded = TrackStatusRequest.decode(stream.reader, this.#session.version);
const msg = await this.#request(stream, "track_status", decoded);
if (msg) await this.#publisher.runTrackStatusRequest(msg, stream);
if (msg) await handed(this.#publisher.runTrackStatusRequest(msg, stream));
break;
}
case BigInt(Fetch.id): {
const decoded = Fetch.decode(stream.reader, this.#session.version);
const msg = await this.#request(stream, "fetch", decoded);
if (msg) await this.#publisher.runFetch(msg, stream);
if (msg) await handed(this.#publisher.runFetch(msg, stream));
break;
}

Expand Down Expand Up @@ -488,7 +494,7 @@ export class Connection implements Established {
break;
}

await this.#subscriber.runPublishNamespace(msg, stream);
await handed(this.#subscriber.runPublishNamespace(msg, stream));
break;
}
case BigInt(AuthMessage.id): {
Expand All @@ -500,7 +506,7 @@ export class Connection implements Established {
case BigInt(Publish.id): {
const decoded = Publish.decode(stream.reader, this.#session.version);
const msg = await this.#request(stream, "publish", decoded);
if (msg) await this.#subscriber.runPublish(msg, stream);
if (msg) await handed(this.#subscriber.runPublish(msg, stream));
break;
}

Expand Down
37 changes: 37 additions & 0 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ function requestStream(version: IetfVersion) {
stream,
written,
wire: () => new Reader(undefined, Uint8Array.from(written), version),
send: (bytes: Uint8Array) => input.enqueue(bytes),
cancel: () => input.error(new Error("request cancelled")),
};
}
Expand Down Expand Up @@ -319,6 +320,42 @@ test("TRACK_STATUS outside the grant is refused on every draft", async () => {
}
});

// Nothing answers a SUBSCRIBE while its broadcast resolves, yet the request stream is still
// read: a second GOAWAY then closes the session at once, not once an answer goes out.
test("a second GOAWAY on an unanswered SUBSCRIBE closes the session", async () => {
for (const version of [Version.DRAFT_18, Version.DRAFT_21]) {
const pair = createMockTransportPair(ALPNS[version]);
const session = new NativeSession(pair.server, version, true);
const { pub, origin } = publisher(pair.server, { session });
const requests = origin.dynamic(Path.from("room")).requested();

const held = requestStream(version);
const task = pub.runSubscribe(
new Subscribe({
requestId: 0n,
trackNamespace: Path.from("room"),
trackName: "video",
subscriberPriority: 0,
}),
held.stream,
);
expect((await requests.next()).value?.path).toBe(Path.from("room"));
// GOAWAY with no new session and no timeout, twice.
held.send(Uint8Array.of(0x10, 0, 2, 0, 0, 0x10, 0, 2, 0, 0));

const closed = await Promise.race([
pair.server.closed,
new Promise<never>((_resolve, reject) => setTimeout(() => reject(new Error("session stayed up")), 500)),
]);
expect(closed.closeCode).toBe(SessionCode.ProtocolViolation);
await task;
expect(held.written).toEqual([]);

await requests.return?.();
origin.close();
}
});

test("TRACK_STATUS for a missing broadcast or track is refused DOES_NOT_EXIST", async () => {
for (const version of IETF_VERSIONS) {
const missing = toRequestCode("does_not_exist", "track_status", version);
Expand Down
Loading
Loading