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
20 changes: 0 additions & 20 deletions quest/m2/api-shape/README.md

This file was deleted.

26 changes: 0 additions & 26 deletions quest/m2/api-shape/request-expired.md

This file was deleted.

2 changes: 1 addition & 1 deletion quest/m3/release/api-review.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ consumers in this repository and its downstream applications. Mean 6.9; three
clusters were restrictive (the native playout clock, JS duration units, and
moq-video's environment knobs). Its approved changes are m2 quests
(route-roles, js-time-types, mux-sync, no-sentinels, video-devices,
gateway-ingest (landed), and the [api-shape epic](/quest/m2/api-shape/README.md)),
gateway-ingest (landed), and the api-shape epic (landed)),
two m1 quests, and plan edits. Check first, next run: surfaces `just
api-diff` cannot see (GStreamer properties, CLI flags and env, MIGRATE drift)
and publish versions of packages exempt from the cut's version. `just
Expand Down
14 changes: 9 additions & 5 deletions rs/moq-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,16 @@ impl Serving {
let origin = &self.cluster.origin;
let directions = self.directions;
let auth_request = moq_relay::auth::request_for(&self.auth, &request);
// The listener's handshake deadline covers admission; `within` closes a session
// The listener's handshake deadline covers admission; `expired` closes a session
// it gives up on with the timeout code.
let lease = match request.within(self.auth.admit(auth_request)).await {
Err(err) => return Err(anyhow::Error::new(err).context("admission timed out")),
Ok(Ok(lease)) => lease,
Ok(Err(err)) => {
let admitted = tokio::select! {
biased;
admitted = self.auth.admit(auth_request) => admitted,
() = request.expired() => return Err(anyhow::Error::new(moq_net::Error::Timeout).context("admission timed out")),
};
let lease = match admitted {
Ok(lease) => lease,
Err(err) => {
request.reject(moq_net::SessionError::from(&err)).await.ok();
return Err(anyhow::Error::new(err).context("session refused"));
}
Expand Down
2 changes: 1 addition & 1 deletion rs/moq-relay/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ impl Auth {
///
/// Waits on the decider for as long as the caller does. The caller owns the
/// deadline: a listener bounds it with the connection's handshake deadline
/// ([`moq_tokio::server::Request::within`]), and dropping the future withdraws
/// ([`moq_tokio::server::Request::expired`]), and dropping the future withdraws
/// the admission from the decider.
///
/// A path naming a `.` or `..` segment is refused before the decider sees it. A
Expand Down
13 changes: 8 additions & 5 deletions rs/moq-relay/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ impl<R: Incoming> Connection<R> {
// socket like it bounds a session, and at the handshake deadline, so a peer
// cannot hold the connection open behind a decision that never comes.
// Boxed: the admission future is large, and nesting it inline overflows debug stacks.
let admitted = request
.within(Box::pin(lifecycle.on(shared.as_ref(), admit)))
.await
.inspect_err(|_| lifecycle.cluster.refusals.record(RefusalKind::Unavailable))
.map_err(|err| err.context("admission timed out"))??;
let admitted = tokio::select! {
biased;
admitted = Box::pin(lifecycle.on(shared.as_ref(), admit)) => admitted?,
() = request.expired() => {
lifecycle.cluster.refusals.record(RefusalKind::Unavailable);
return Err(anyhow::Error::new(moq_net::Error::Timeout).context("admission timed out"));
}
};
let (admitted, registration) = match admitted {
Ok(admitted) => admitted,
Err(err) => {
Expand Down
26 changes: 7 additions & 19 deletions rs/moq-relay/src/incoming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ pub trait Sealed: Sized {
/// What the auth decider is asked about this session.
fn admission(&self, auth: &auth::Auth) -> moq_auth::Request;

/// Finish `work` before the handshake deadline, closing the session if it passes.
async fn within<T>(&mut self, work: impl std::future::Future<Output = T>) -> anyhow::Result<T>;
/// Resolve once the handshake deadline passes, having closed the session with the
/// timeout code; never, without a deadline.
async fn expired(&mut self);

/// Refuse the session, closing it with `code`.
async fn reject(self, code: moq_net::SessionError);
Expand Down Expand Up @@ -113,8 +114,8 @@ impl Sealed for moq_tokio::server::Request {
auth::request_for(auth, self)
}

async fn within<T>(&mut self, work: impl std::future::Future<Output = T>) -> anyhow::Result<T> {
Ok(moq_tokio::server::Request::within(self, work).await?)
async fn expired(&mut self) {
moq_tokio::server::Request::expired(self).await
}

async fn reject(self, code: moq_net::SessionError) {
Expand Down Expand Up @@ -198,8 +199,8 @@ impl Sealed for Accepted {
auth::request_for_setup(auth, &self.handshake, self.transport)
}

async fn within<T>(&mut self, work: impl std::future::Future<Output = T>) -> anyhow::Result<T> {
within(&mut self.handshake, work).await
async fn expired(&mut self) {
self.handshake.expired().await
}

async fn reject(self, code: moq_net::SessionError) {
Expand Down Expand Up @@ -253,16 +254,3 @@ impl Sealed for Accepted {
Ok(())
}
}

/// Finish `work` before `handshake`'s accept deadline, which refuses the session if it passes.
#[cfg(any(feature = "test-support", all(target_os = "linux", feature = "_uring")))]
pub(crate) async fn within<T>(
handshake: &mut moq_net::server::Handshake,
work: impl std::future::Future<Output = T>,
) -> anyhow::Result<T> {
let expired = handshake.expired();
match futures::future::select(std::pin::pin!(work), std::pin::pin!(expired)).await {
futures::future::Either::Left((done, _)) => Ok(done),
futures::future::Either::Right(_) => Err(moq_net::Error::Timeout.into()),
}
}
4 changes: 2 additions & 2 deletions rs/moq-relay/src/uring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,8 @@ impl crate::incoming::Sealed for Request {
request
}

async fn within<T>(&mut self, work: impl Future<Output = T>) -> anyhow::Result<T> {
crate::incoming::within(&mut self.handshake, work).await
async fn expired(&mut self) {
self.handshake.expired().await
}

async fn reject(self, code: moq_net::SessionError) {
Expand Down
2 changes: 1 addition & 1 deletion rs/moq-tokio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `server::Request::with_quic_limits` sets the QUIC limits `ok` applies before answering SETUP. A server QUIC connection holds only `moq_net::transport::SETUP_RECEIVE_WINDOW` until `ok` opens `[quic] receive_window`, so a client cannot bank credit before it is admitted.

- `server::Request::within` finishes a future, such as admitting the session, within what remains of the handshake deadline, closing the session with the timeout code otherwise.
- `server::Request::poll_expired` and `expired` resolve once the handshake deadline passes, having closed the session with the timeout code, so a caller races admission against them.

## [0.19.23](https://github.com/moq-dev/moq/compare/moq-tokio-v0.19.22...moq-tokio-v0.19.23) - 2026-10-06

Expand Down
70 changes: 54 additions & 16 deletions rs/moq-tokio/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1633,23 +1633,18 @@ impl Request {
Ok(self)
}

/// Finish `work` before the accept deadline, or refuse the session with the timeout code
/// and return [`moq_net::Error::Timeout`]; see [`moq_net::server::Handshake::expired`].
/// Resolve once the accept deadline passes, having refused the session with the timeout
/// code; never, without a deadline. See [`moq_net::server::Handshake::poll_expired`].
///
/// Admitting the session is the caller's part of the handshake, so the same
/// `listen.timeout` that bounds the transport and SETUP bounds it too; a peer
/// cannot hold a connection open behind a decision that never comes. Without a
/// deadline, waits forever.
pub async fn within<T>(&mut self, work: impl Future<Output = T>) -> crate::Result<T> {
let handshake = &mut self.handshake;
crate::runtime::scope(async move {
tokio::select! {
biased;
done = work => Ok(done),
() = handshake.expired() => Err(moq_net::Error::Timeout.into()),
}
})
.await
/// Race admission against it, so the same `listen.timeout` that bounds the transport and
/// SETUP bounds the caller's decision too: a peer cannot hold a connection open behind a
/// decision that never comes. Once it resolves, [`ok`](Self::ok) fails with the timeout.
#[kio::wrapper]
pub fn poll_expired(&mut self, waiter: &kio::Waiter) -> std::task::Poll<()> {
// The deadline's timer may start here, which needs a tokio runtime even when an
// embedder polls from outside one.
let _runtime = crate::runtime::enter();
self.handshake.poll_expired(waiter)
}

/// The receive window of the qmux session under this request.
Expand Down Expand Up @@ -2238,6 +2233,49 @@ mod tests {
assert!(admission.close().now_or_never().is_some());
}

/// An embedder may poll the deadline from outside any tokio runtime. A SETUP that is
/// already buffered resolves the handshake before its timer starts, so the timer
/// first starts here and still needs a runtime.
#[cfg(feature = "websocket")]
#[test]
fn expired_polls_outside_a_runtime() {
use moq_net::transport::poll::{SendOptions, SendStream as _, Session as _};
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let admission = moq_net::server::Admission::default();
let (mut request, _client) = runtime.block_on(async {
let (client, server) = session_pair().await;
let mut setup = client.open_uni(SendOptions::default()).await.unwrap();
assert_eq!(setup.write(&[1, 1, 0]).await.unwrap(), 3);
setup.finish().unwrap();
// Let the in-memory pumps deliver the SETUP before the handshake's first poll.
for _ in 0..100 {
tokio::task::yield_now().await;
}
let handshake = moq_net::Server::new()
.with_timeout(Some(std::time::Duration::from_secs(60)))
.accept_request(moq_net::time::Instant::now(), server)
.await
.expect("a buffered SETUP");
let request = Request {
active: admission.enter().unwrap(),
transport: Transport::WebSocket,
url: None,
authority: None,
identity: None,
link: Link::default(),
handshake: Box::new(handshake),
under: Under::Opaque,
quic_limits: Default::default(),
};
(request, client)
});
assert!(tokio::runtime::Handle::try_current().is_err());
assert!(request.poll_expired(&kio::Waiter::noop()).is_pending());
}

#[cfg(feature = "websocket")]
#[tokio::test(start_paused = true)]
async fn upgraded_websocket_refuses_setup_path_presence() {
Expand Down