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
7 changes: 3 additions & 4 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { Timescale } from "../time.ts";
import type { Groups, Info, Position as TrackPosition, Subscriber as TrackSubscriber } from "../track.ts";
import { untilAborted } from "../util/abort.ts";
import { nextChange } from "../util/change.ts";
import { Deadline, REQUEST_TIMEOUT_MS, requestOptions } from "../util/deadline.ts";
import { Deadline, REQUEST_TIMEOUT_MS } from "../util/deadline.ts";
import { untilGivenUp } from "../util/given_up.ts";
import { TimeoutError, withTimeout } from "../util/timeout.ts";
import * as Varint from "../varint.ts";
Expand Down Expand Up @@ -1881,13 +1881,12 @@ export class Publisher {
const deadline = new Deadline();
const answer = await deadline.wait(
(async (): Promise<Answer> => {
const stream = await deadline.open(this.#session.openBi(requestOptions));
const opened = new RequestStream(stream, this.#session, this.#quic);
const opened = await RequestStream.open(this.#session, deadline, this.#quic);
request = opened;
const version = this.#session.version;
const token = this.#auth?.carried(auth);
const msg = new PublishNamespace({ requestId, trackNamespace: path, params, token });
await deadline.send(stream, async () => {
await deadline.send(opened, async () => {
await Message.write(opened.writer, PublishNamespace.id, msg, version);
});

Expand Down
36 changes: 34 additions & 2 deletions js/net/src/ietf/request_stream.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect, test } from "bun:test";
import { ProtocolViolation, SessionCode } from "../error.ts";
import { expect, spyOn, test } from "bun:test";
import { Manual, Milli, Nano } from "@moq/clock";
import { ProtocolViolation, SessionCode, StreamCode, StreamError } from "../error.ts";
import { Reader, Stream } from "../stream.ts";
import { Deadline, REQUEST_TIMEOUT_MS } from "../util/deadline.ts";
import * as Varint from "../varint.ts";
import { RequestError } from "./request.ts";
import { namespaceUpdate, nextMessage, RequestStream, refuse } from "./request_stream.ts";
Expand Down Expand Up @@ -164,3 +166,33 @@ test("a refusal carries its reason", async () => {
expect(err.reasonPhrase).toBe("no");
}
});

test("a request's deadline ends it through the request, so later ends do nothing", async () => {
const clock = new Manual();
const installed = clock.install();
try {
const stream = new Stream({
readable: new ReadableStream<Uint8Array>(),
writable: new WritableStream<Uint8Array>(),
version: Version.DRAFT_18,
});
const aborts = spyOn(stream, "abort");
const session = { version: Version.DRAFT_18, client: true, openBi: () => stream } as unknown as Session;
const deadline = new Deadline();
const request = await RequestStream.open(session, deadline);
const answer = deadline.wait(new Promise<never>(() => {})).catch((e: unknown) => e);
await clock.advance(Nano.fromMilli(Milli(REQUEST_TIMEOUT_MS)));
const err = await answer;
expect(err).toBeInstanceOf(StreamError);
expect((err as StreamError).code).toBe(StreamCode.ControlTimeout);
await request.ended;

// The caller's own cleanup after the deadline.
request.abort(new Error("abandoned"));
request.close();
expect(aborts).toHaveBeenCalledTimes(1);
expect(aborts.mock.calls[0]?.[0]).toBe(err as StreamError);
} finally {
installed.close();
}
});
9 changes: 9 additions & 0 deletions js/net/src/ietf/request_stream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { error, ProtocolViolation, StreamCode, StreamError } from "../error.ts";
import type * as Path from "../path.ts";
import { Reader, type Stream, type Writer } from "../stream.ts";
import { type Deadline, requestOptions } from "../util/deadline.ts";
import { type RequestCondition, type RequestKind, toRequestCode } from "./error.ts";
import { GoAway } from "./goaway.ts";
import * as Message from "./message.ts";
Expand Down Expand Up @@ -165,6 +166,14 @@ export class RequestStream {
});
}

/**
* Open a request's stream under `deadline`, whose failure then resets the request through
* {@link abort} like any other end, so the request ends once whatever ends it.
*/
static open(session: Session, deadline: Deadline, quic?: WebTransport): Promise<RequestStream> {
return deadline.open(session.openBi(requestOptions), (stream) => new RequestStream(stream, session, quic));
}

/**
* Settles once the peer ends the request, by FIN where that cancels or by resetting either
* direction, or once it closes here or over a violation. Never rejects.
Expand Down
54 changes: 12 additions & 42 deletions js/net/src/ietf/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,9 @@ type SubscribeSetupState = {
* gone quiet, and the cleanup that fired first saw no stream to tear down.
*/
cancelled?: boolean;
/**
* Whether the SUBSCRIBE reached the wire. From that moment the publisher may be serving,
* even before it answers, so abandoning owes it a cancellation regardless of whether we
* ever saw the SUBSCRIBE_OK.
*/
sent?: boolean;
/**
* Whether the publisher answered with an error. It has torn the request down already, so
* there is nothing left to cancel and naming the dead request id at it risks being read
* as a protocol violation.
* there is nothing left to cancel and ours ends cleanly.
*/
rejected?: boolean;
/** Hands a draft-22 fill stream that overtook SUBSCRIBE_OK the subscription it heads, or nothing. */
Expand Down Expand Up @@ -531,11 +524,7 @@ export class Subscriber {

try {
const deadline = new Deadline();
const request = new RequestStream(
await deadline.wait(deadline.open(this.#session.openBi(requestOptions))),
this.#session,
this.#quic,
);
const request = await deadline.wait(RequestStream.open(this.#session, deadline, this.#quic));

const run = async () => {
const msg = new SubscribeNamespace({ namespace: prefix, requestId, hidden });
Expand Down Expand Up @@ -833,9 +822,9 @@ export class Subscriber {
);
// Runs now for whatever is already open, and again if `setup` settles late.
// Retirement is repeated because a late SUBSCRIBE_OK can register an alias after
// the first pass; the stream is torn down once.
let torn = false;
const cleanup = async (afterSetup: boolean) => {
// the first pass. The request ends once: a deadline failure already reset it through
// the same stream, so a second end here does nothing.
const cleanup = () => {
state.cancelled = true;

// A fill stream waiting on SUBSCRIBE_OK gets nothing, and a head already started goes.
Expand All @@ -851,31 +840,18 @@ export class Subscriber {
this.#timescales.delete(state.registeredAlias);
}

if (!state.stream || torn) return;

// A SUBSCRIBE still being written must not be torn down from here. Aborting a
// Web Stream waits for the in-flight write, so the request can still reach the
// peer while the abort destroys the stream we would have cancelled on. Setup
// unwinds on the cancelled flag once that write lands, and its pass below has
// the finished picture.
if (!state.sent && !afterSetup) return;
torn = true;

// A refusal already ended the request, so end ours cleanly: a reset would tell the
// publisher we failed.
if (state.rejected) state.stream.close();
else state.stream.abort(e);
if (state.rejected) state.stream?.close();
else state.stream?.abort(e);
};

// Tear down what is already open rather than waiting on `setup`. A peer that
// opened the stream and then went quiet never settles it, and deferring would
// leave the request outstanding for the life of the session -- which is the
// timeout case, not a rare one.
void cleanup(false);
setup.then(
() => cleanup(true),
() => cleanup(true),
);
cleanup();
setup.then(cleanup, cleanup);
return;
}

Expand Down Expand Up @@ -1209,11 +1185,7 @@ export class Subscriber {
}> {
const version = this.#session.version;

state.stream = new RequestStream(
await deadline.open(this.#session.openBi(requestOptions)),
this.#session,
this.#quic,
);
state.stream = await RequestStream.open(this.#session, deadline, this.#quic);

// The timeout can fire while the open is still in flight, in which case cleanup ran
// with nothing to release. Bail now that the stream is recorded, so settling this
Expand Down Expand Up @@ -1241,11 +1213,9 @@ export class Subscriber {
state.fill = accepted.resolve;
}
await Message.write(state.stream.writer, Subscribe.id, msg, version);
state.sent = true;

// The caller may have given up while that write was in flight. It deliberately left
// the stream alone, so unwind here and let its cleanup send the cancellation now that
// the SUBSCRIBE has actually reached the peer.
// The caller may have given up while that write was in flight, resetting the request
// under it.
if (state.cancelled) throw new Error("subscribe cancelled while it was being sent");
console.debug(`subscribe written: id=${requestId} broadcast=${broadcast} track=${request.name}`);

Expand Down
7 changes: 5 additions & 2 deletions js/net/src/lite/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,8 +1522,11 @@ export class Publisher {
void stream.reader.done().then(
(fin) => {
hold.abort();
// TRACK is the requester's only message.
if (!fin) stream.abort(new ProtocolViolation("data after TRACK"));
if (fin) return;
// TRACK is the requester's only message, so more breaks the session, as in Rust.
const violation = new ProtocolViolation("data after TRACK");
closeViolation(this.#quic, violation);
stream.abort(violation);
},
() => hold.abort(),
);
Expand Down
26 changes: 25 additions & 1 deletion js/net/src/lite/track_stream.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// An open Track Stream is interest in the track: the subscriber keeps it open until its
// SUBSCRIBE is answered, and the publisher keeps the track wanted until the subscriber closes it.

import { describe, expect, test } from "bun:test";
import { describe, expect, spyOn, test } from "bun:test";
import { Elapsed, Milli, Nano, timeout } from "@moq/clock";
import { SessionCode } from "../error.ts";
import { randomHop } from "../hop.ts";
import { createMockTransportPair } from "../mock.ts";
import { Producer as OriginProducer } from "../origin.ts";
Expand Down Expand Up @@ -149,6 +150,29 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver
},
);

test("data after a TRACK request closes the session", async () => {
const pair = createMockTransportPair(ALPN_05);
const closing = spyOn(pair.server, "close");
const origin = new OriginProducer();
const publisher = new Publisher(pair.server, version, randomHop(), origin.consume());
const broadcastPublication = origin.createBroadcast(Path.from("room"));
const broadcast = broadcastPublication.broadcast;
broadcastPublication.announce();

const client = await Stream.open(pair.client, { version });
const server = await Stream.accept(pair.server, version);
if (!server) throw new Error("the publisher never accepted the TRACK stream");
void publisher.runTrackInfo(new TrackMessage(Path.from("room"), "video"), server);

await client.writer.u53(0);
await expect(client.reader.done()).rejects.toThrow();
expect(closing).toHaveBeenCalledWith(expect.objectContaining({ closeCode: SessionCode.ProtocolViolation }));

publisher.close();
broadcast.close();
origin.close();
});

test.each(["FIN", "reset"] as const)(
"a requester's %s while TRACK_INFO is blocked lets the track go",
async (close) => {
Expand Down
32 changes: 23 additions & 9 deletions js/net/src/util/deadline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,28 @@ function sessionFailure(err: unknown): boolean {
return source === "session" || name === "InvalidStateError";
}

/** What a deadline failure resets: a stream, or whatever owns one. @internal */
export interface Abortable {
abort(err: Error): void;
}

/** One fatal deadline for a request, including every stream its setup creates. @internal */
export class Deadline {
/** Creates still waiting on credit: a fetch opens its TRACK stream alongside. */
#opening = 0;
#streams: Stream[] = [];
#owners: Abortable[] = [];
#failure?: Error;

/** Record a create so deadline failures reset its stream even if it opens late. */
async open(opening: Stream | Promise<Stream>): Promise<Stream> {
/**
* Record a create so deadline failures reset its stream even if it opens late. `own` wraps
* the opened stream in its owner, which the failure then resets instead.
*/
open(opening: Stream | Promise<Stream>): Promise<Stream>;
open<T extends Abortable>(opening: Stream | Promise<Stream>, own: (stream: Stream) => T): Promise<T>;
async open(
opening: Stream | Promise<Stream>,
own: (stream: Stream) => Abortable = (stream) => stream,
): Promise<Abortable> {
this.#opening++;
let stream: Stream;
try {
Expand All @@ -35,18 +48,19 @@ export class Deadline {
} finally {
this.#opening--;
}
const owner = own(stream);
if (this.#failure) {
stream.abort(this.#failure);
owner.abort(this.#failure);
throw this.#failure;
}
this.#streams.push(stream);
return stream;
this.#owners.push(owner);
return owner;
}

/** Write the request on an opened stream, which a deadline failure after resets. */
async send(stream: Stream, write: () => Promise<void>): Promise<void> {
async send(stream: Abortable, write: () => Promise<void>): Promise<void> {
if (this.#failure) throw this.#failure;
if (!this.#streams.includes(stream)) throw new Error("request sent on a stream the deadline did not open");
if (!this.#owners.includes(stream)) throw new Error("request sent on a stream the deadline did not open");
await write();
}

Expand All @@ -68,7 +82,7 @@ export class Deadline {
this.#failure = failure;
// Other failures keep the caller's cancellation ordering.
if (cause instanceof TimeoutError || cause instanceof NoStreamCredit) {
for (const stream of this.#streams) stream.abort(failure);
for (const owner of this.#owners) owner.abort(failure);
}
throw failure;
}
Expand Down
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ lite-07; bitrate enforcement is not part of this release.
- [Auth serve bound address](/quest/m2/auth-serve-bound-addr.md) - `moq auth serve` logs the port it actually bound
- [H.264 parsing with maintained crates](/quest/m2/h264-reader.md) - moq-mux parses SPS VUI with h264-reader and shares one bit reader across codec config parsers
- [Relay metrics through prometheus-client](/quest/m2/relay-prometheus-client.md) - `/metrics` renders through the maintained client with the same series
- [JS IETF request streams](/quest/m2/js-request-stream.md) - `RequestStream` owns deadlines and cancellation, and TRACK trailing bytes close the session like Rust
- [moq-transport finish interop](/quest/m2/ietf-finish.md) - the interop `finish` case runs on every even moq-transport draft instead of being skipped
- [Join after a quick finish](/quest/m2/ietf-join-after-finish.md) - a pre-draft-22 joining FETCH that arrives after its subscription finished is still answered, within a bound the peer already negotiates
- [JS joining FETCH](/quest/m2/ietf-js-joining-fetch.md) - the JS publisher answers a joining FETCH that arrives after a quick finish, mirroring the Rust bound
Expand Down
21 changes: 0 additions & 21 deletions quest/m2/js-request-stream.md

This file was deleted.