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
2 changes: 2 additions & 0 deletions drafts/draft-lcurley-moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ AUTH_ERROR Message {

**Error Code**:
A code from the [session error registry](#session-error-codes), such as UNAUTHORIZED.
A code that does not fit in 32 bits is a PROTOCOL_VIOLATION that closes the session.
Comment thread
kixelated marked this conversation as resolved.
Resolved

**Reason Phrase**:
A human-readable reason, at most 8,192 bytes.
Expand Down Expand Up @@ -1443,6 +1444,7 @@ The `Message Length` describes the payload size on the wire.
- The Subscribe Stream FIN now follows once every counted Group Stream has finished or been reset.
- Added announce compression: ANNOUNCE_START gains `Path Base` and `Path Keep` to copy the head of a live advertisement's suffix, and ANNOUNCE_START and ANNOUNCE_UPDATE gain `Hop Base` and `Hop Keep` to copy the tail of a live advertisement's Hop ID list.
- Capped the SETUP Message Length at 65,536 bytes.
- An AUTH_ERROR code that does not fit in 32 bits is a PROTOCOL_VIOLATION.

## moq-lite-06

Expand Down
31 changes: 31 additions & 0 deletions js/net/src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Producer as OriginProducer } from "./origin.ts";
import * as Path from "./path.ts";
import { Writer } from "./stream.ts";
import { Timestamp } from "./time.ts";
import { withTimeout } from "./util/timeout.ts";
import { wireOf } from "./wire.ts";

const url = new URL("https://localhost:4443/test");
Expand Down Expand Up @@ -158,6 +159,36 @@ describe.each([Lite.ALPN_06, Ietf.ALPN.DRAFT_17, Ietf.ALPN.DRAFT_22])("%s", (pro
server.close();
});

test("closing the session drops the grant", async () => {
const { client, server } = await connect({ publish: new OriginProducer(), protocol });
await waitFor(client.auth.grant, (g) => g !== undefined && g.publish.size > 0);

// Checked before any stream notices the transport closing.
client.close();
const closed = client.auth.grant.peek();
expect(closed?.publish.size).toBe(0);
expect(closed?.subscribe.size).toBe(0);
server.close();
});

test("an acceptor that ends a grant sees its stream close", async () => {
const { client, server } = await connect({ publish: new OriginProducer(), protocol });
try {
const requests = server.auth.requests();
const request = await requests.next();
if (!request) throw new Error("the setup token never arrived");
const issued = request.accept(grant(["a"], []));
await waitFor(client.auth.grant, (g) => g !== undefined && g.publish.size > 0);

issued.close();
// A regression leaves `closed` pending, so bound the wait rather than hang the runner.
expect(await withTimeout(Promise.resolve(issued.closed), 1000, "the grant stream never closed")).toBeNull();
} finally {
client.close();
server.close();
}
});

test("a refused setup token grants nothing rather than everything", async () => {
const { client, server } = await connect({ publish: new OriginProducer(), protocol });
const requests = server.auth.requests();
Expand Down
4 changes: 3 additions & 1 deletion js/net/src/auth_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ export class AuthSession implements AuthApi {
this.#closed = true;
for (const token of this.#tokens) token.end(new Error("session closed"));
this.#tokens.clear();
this.#recompute();
if (this.#acceptor instanceof RequestQueue) this.#acceptor.close();
}

Expand All @@ -346,8 +347,9 @@ export class AuthSession implements AuthApi {
const reply = await wire.read(stream);
if (!reply) {
// The peer ended the grant without revoking it, or closed without ever
// answering.
// answering. Close our side too, which is what settles its Issued.closed.
result = token.isAnswered ? null : new Unsupported();
stream.close();
break;
}
if ("grant" in reply) {
Expand Down
1 change: 0 additions & 1 deletion quest/m1/auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ existing lite-06 ALPN.

## Required

- [AUTH endings](/quest/m1/auth/error-codes.md) - an out-of-range AUTH_ERROR code is refused, and both sides settle and recompute grants when a stream ends
- [Lite NOT_SUPPORTED](/quest/m1/auth/not-supported.md) - a lite acceptor answers AUTH_ERROR NOT_SUPPORTED after a grant too, and lite messages cap at moq-transport's 65,535 bytes
- [AUTH violations](/quest/m1/auth/violations.md) - every AUTH protocol violation closes the session in Rust and JS, lite and IETF
- [JS fetch grant watch](/quest/m1/auth/js-fetch-watch.md) - a JS `fetchGroup` ends `Unauthorized` when its path leaves the grant
Expand Down
33 changes: 0 additions & 33 deletions quest/m1/auth/error-codes.md

This file was deleted.

2 changes: 1 addition & 1 deletion quest/m1/auth/not-supported.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ code, and a lower lite message ceiling.

## Related

- [AUTH endings](/quest/m1/auth/error-codes.md) - a malformed or out-of-range AUTH_ERROR code is refused on the same path
- [AUTH violations](/quest/m1/auth/violations.md) - AUTH protocol violations close the session on the same path (AUTH endings landed in #4550)
6 changes: 1 addition & 5 deletions quest/m1/auth/violations.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ decode, such as a bad pattern or length.

## Plan

[AUTH endings](/quest/m1/auth/error-codes.md) (#4550) closes the session only
AUTH endings (#4550) closes the session only
on an explicit `ProtocolViolation` in Rust lite. These gaps remain:

- JS reports the exact oversized code but doesn't close the session. Let the
Expand Down Expand Up @@ -39,7 +39,3 @@ regression test per case, and check the lite and IETF draft text agrees. Run
`just test interop --all`.

Public API: none. Wire: none beyond draft wording.

## Related

- [AUTH endings](/quest/m1/auth/error-codes.md) - the Rust lite case this generalizes
32 changes: 28 additions & 4 deletions rs/moq-net/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,10 +738,34 @@ pub(crate) struct Issue {
pub peer: Option<Error>,
}

impl Issue {
/// A fresh stream's shared state.
pub(crate) fn shared() -> kio::Shared<Self> {
kio::Shared::default()
/// The serving task's hold on one peer token's [`Issue`].
///
/// Dropping it settles [`Issued::closed`] with the session's error, unless the task
/// already recorded why the stream ended, so no exit path leaves the acceptor waiting.
pub(crate) struct Serving {
pub issue: kio::Shared<Issue>,
handle: Handle,
}

impl Serving {
/// A fresh stream's shared state, settled by the session `handle` if the task drops.
pub(crate) fn new(handle: Handle) -> Self {
Self {
issue: kio::Shared::default(),
handle,
}
}

/// Record why the stream ended on the peer's side; the first reason wins.
pub(crate) fn end(&self, err: Error) {
self.issue.lock().peer.get_or_insert(err);
}
}

impl Drop for Serving {
fn drop(&mut self) {
let err = self.handle.state.read().closed().unwrap_or(Error::Cancel);
self.end(err);
Comment on lines +765 to +768

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Settle lite grants when the driver completes

When a lite session driver reaches a terminal result but its caller retains the Driver, Driver::State::poll caches the completed Protocol::Lite instead of dropping it. The Publisher and its AuthServe children therefore remain parked, so this drop-only guard never records the session error and Issued::closed() can still wait indefinitely after the session has ended. Settle the issues when the lite driver completes, rather than relying on the public driver being dropped.

AGENTS.md reference: rs/AGENTS.md:L13-L15

Useful? React with 👍 / 👎.

}
}

Expand Down
9 changes: 5 additions & 4 deletions rs/moq-net/src/ietf/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::time::Duration;

use bytes::Bytes;

use crate::auth::{Grant, Handle, Issue, Reply, Request};
use crate::auth::{Grant, Handle, Issue, Reply, Request, Serving};
use crate::coding::{Decode, DecodeError, Encode, EncodeError, Sizer, Stream};
use crate::{Error, Path, Pattern, Patterns, SessionError};

Expand Down Expand Up @@ -394,7 +394,8 @@ impl Serve {
msg: Auth,
version: Version,
) {
let issue = Issue::shared();
let serving = Serving::new(self.handle.clone());
let issue = &serving.issue;
let mut default = None;
match self.handle.acceptor() {
Some(requests) => {
Expand All @@ -417,7 +418,7 @@ impl Serve {
None => default = Some(crate::auth::DefaultGrant::new(self.handle.clone(), self.peer_grant)),
}

let err = match serve_issue(&self.runtime, &issue, default.as_mut(), &mut stream, version).await {
let err = match serve_issue(&self.runtime, issue, default.as_mut(), &mut stream, version).await {
Ok(()) => Error::Cancel,
Err(err) => {
match &err {
Expand All @@ -429,7 +430,7 @@ impl Serve {
err
}
};
issue.lock().peer.get_or_insert(err);
serving.end(err);
}
}

Expand Down
158 changes: 94 additions & 64 deletions rs/moq-net/src/ietf/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,12 @@ where
Poll::Pending
})
.await;
// Before this arm's Auth serve tasks drop, so each settles with the
// session's error rather than a bare cancel.
auth.close(match &res {
Ok(()) => Error::Cancel,
Err(err) => err.clone(),
});
if let Err(err) = &res {
// Every track this session was receiving ends with its error.
subscriber.abort(err);
Expand Down Expand Up @@ -850,74 +856,98 @@ where
let declared = subscriber.solicit().await;

let mut tasks = TaskSet::owned();
let mut accept = session.clone();
loop {
let mut stream = tasks
.drive(|waiter| {
let mut cx = std::task::Context::from_waker(waiter.waker());
Stream::poll_accept(&mut accept, version, &mut cx)
})
.await?;

// The intermediate results live outside the poll closure, so a Pending
// mid-header resumes where it left off.
let mut hdr_id: Option<u64> = None;
let mut hdr_size: Option<u16> = None;
let header = tasks
.drive(|waiter| {
let mut cx = std::task::Context::from_waker(waiter.waker());
let id = match hdr_id {
Some(id) => id,
None => *hdr_id.insert(std::task::ready!(stream.reader.poll_decode(&mut cx))?),
};
let size = match hdr_size {
Some(size) => size,
None => *hdr_size.insert(std::task::ready!(stream.reader.poll_decode(&mut cx))?),
};
let data = std::task::ready!(stream.reader.poll_read_exact(&mut cx, size as usize))?;
std::task::Poll::Ready(Ok::<_, Error>((id, data)))
})
.await;
// Same tolerance as `run_unis`: a request stream that dies before its header
// is the peer abandoning that request, not the session. Anything else, a
// header that does not parse included, still fails the session.
let (id, data) = match header {
Ok(header) => header,
Err(err @ (Error::Cancel | Error::Stream(_) | Error::Remote(_) | Error::Decode(DecodeError::Short))) => {
tracing::debug!(%err, "dropping bidi stream that died before its header");
continue;
}
Err(err) => return Err(err),
};

match id {
// Publisher handles: Subscribe, Fetch, SubscribeNamespace (0x50 modern /
// 0x11 legacy), TrackStatus
ietf::Subscribe::ID
| ietf::Fetch::ID
| ietf::SubscribeNamespace::ID
| ietf::SubscribeNamespaceLegacy::ID
| ietf::TrackStatus::ID => {
tasks.push(publisher.handle_stream(id, data, stream)?);
}
// Subscriber handles: Publish, PublishNamespace
ietf::Publish::ID | ietf::PublishNamespace::ID => {
tasks.push(subscriber.handle_stream(id, data, stream, peer, declared)?);
}
auth::Auth::ID if let Some(serve) = &serve => {
let mut data = data;
let msg = auth::Auth::decode_msg(&mut data, version)?;
if !data.is_empty() {
return Err(Error::WrongSize);
// Each AUTH serve task settles `Issued::closed` from whatever the auth handle
// holds when it drops, and the first reason wins. So the error that ends the
// session has to reach the handle while `tasks` is still alive, or every grant
// reports a bare cancel instead. `None` when the peer never negotiated AUTH,
// which leaves no serve task to settle.
let handle = serve.as_ref().map(|serve| serve.handle.clone());

// Scoped so `tasks` outlives the close below: the loop borrows it, so it only
// drops once this block's future is done.
let res: Result<(), Error> = async {
let mut accept = session.clone();
loop {
let mut stream = tasks
.drive(|waiter| {
let mut cx = std::task::Context::from_waker(waiter.waker());
Stream::poll_accept(&mut accept, version, &mut cx)
})
.await?;

// The intermediate results live outside the poll closure, so a Pending
// mid-header resumes where it left off.
let mut hdr_id: Option<u64> = None;
let mut hdr_size: Option<u16> = None;
let header = tasks
.drive(|waiter| {
let mut cx = std::task::Context::from_waker(waiter.waker());
let id = match hdr_id {
Some(id) => id,
None => *hdr_id.insert(std::task::ready!(stream.reader.poll_decode(&mut cx))?),
};
let size = match hdr_size {
Some(size) => size,
None => *hdr_size.insert(std::task::ready!(stream.reader.poll_decode(&mut cx))?),
};
let data = std::task::ready!(stream.reader.poll_read_exact(&mut cx, size as usize))?;
std::task::Poll::Ready(Ok::<_, Error>((id, data)))
})
.await;
// Same tolerance as `run_unis`: a request stream that dies before its header
// is the peer abandoning that request, not the session. Anything else, a
// header that does not parse included, still fails the session.
let (id, data) = match header {
Ok(header) => header,
Err(
err @ (Error::Cancel | Error::Stream(_) | Error::Remote(_) | Error::Decode(DecodeError::Short)),
) => {
tracing::debug!(%err, "dropping bidi stream that died before its header");
continue;
}
Err(err) => return Err(err),
};

match id {
// Publisher handles: Subscribe, Fetch, SubscribeNamespace (0x50 modern /
// 0x11 legacy), TrackStatus
ietf::Subscribe::ID
| ietf::Fetch::ID
| ietf::SubscribeNamespace::ID
| ietf::SubscribeNamespaceLegacy::ID
| ietf::TrackStatus::ID => {
tasks.push(publisher.handle_stream(id, data, stream)?);
}
// Subscriber handles: Publish, PublishNamespace
ietf::Publish::ID | ietf::PublishNamespace::ID => {
tasks.push(subscriber.handle_stream(id, data, stream, peer, declared)?);
}
auth::Auth::ID if let Some(serve) = &serve => {
let mut data = data;
let msg = auth::Auth::decode_msg(&mut data, version)?;
if !data.is_empty() {
return Err(Error::WrongSize);
}
tasks.push(serve.clone().run(stream, msg, version));
}
_ => {
tracing::warn!(id, "unexpected bidi stream type");
return Err(Error::UnexpectedStream);
}
tasks.push(serve.clone().run(stream, msg, version));
}
_ => {
tracing::warn!(id, "unexpected bidi stream type");
return Err(Error::UnexpectedStream);
}
}
}
.await;

// Every error exit above drops the AUTH serve tasks in `tasks`, so the session's
// error has to land on the handle first. The driver closes it again with the same
// error, which then does nothing.
if let Some(handle) = &handle
&& let Err(err) = &res
{
handle.close(err.clone());
}
res
}

/// Monitor the peer's SETUP stream for a GOAWAY, surfacing it through
Expand Down
Loading
Loading