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
36 changes: 0 additions & 36 deletions quest/m1/webtransport-close-reason.md

This file was deleted.

3 changes: 2 additions & 1 deletion rs/moq-tokio/src/quic/driver/recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ impl Reader {
{
use moq_quic::recv::Error::{Blocked, Reset};

if self.all_data_read {
// A reset stays the answer to every later read, never a clean end.
if self.all_data_read && self.reset.is_none() {
return Poll::Ready(Ok(None));
}

Expand Down
14 changes: 14 additions & 0 deletions rs/moq-tokio/src/quic/driver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,20 @@ async fn reads_after_a_received_reset_fail_with_it() {
let mut recv = server.accept_uni().await.unwrap();
send.reset(VarInt::from_u32(7));

// A read that hit the reset reads it again, never a clean end.
let mut buf = Vec::with_capacity(8);
loop {
match timeout(Duration::from_secs(10), recv.read(&mut buf)).await.unwrap() {
Ok(Some(_)) => {}
res => {
assert!(matches!(res, Err(recv::Error::Reset(_))), "{res:?}");
break;
}
}
}
let read = recv.read(&mut Vec::with_capacity(8)).await;
assert!(matches!(read, Err(recv::Error::Reset(_))), "{read:?}");

let reset = timeout(Duration::from_secs(10), recv.received_reset()).await.unwrap();
assert_eq!(reset.unwrap(), Some(VarInt::from_u32(7)));
let lease = recv.lease(1).await.err();
Expand Down
89 changes: 65 additions & 24 deletions rs/moq-tokio/src/quic/web/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::OnceLock;
use std::{sync::Mutex, task::Poll};

/// Why a WebTransport (or raw QUIC) session failed: its handshake, its connection, or a close.
#[derive(thiserror::Error, Debug, Clone)]
Expand All @@ -12,10 +12,6 @@ pub enum Error {
#[error("closed: code={0} reason={1}")]
Closed(u32, String),

/// The peer tore down the session's streams before its close reason arrived.
#[error("session gone")]
SessionGone,

/// A stream named a different session.
#[error("unknown session")]
UnknownSession,
Expand Down Expand Up @@ -97,22 +93,37 @@ const WT_SESSION_GONE: u64 = 0x170d7b68;
pub(crate) struct CloseReason {
// Raw QUIC carries the application code as is; HTTP/3 maps it into its own code space.
raw: bool,
reason: OnceLock<Error>,
reason: Mutex<Reason>,
}

#[derive(Debug, Default)]
struct Reason {
err: Option<Error>,
/// Readers that saw WT_SESSION_GONE before the reason was recorded.
waiters: kio::WaiterList,
}

impl CloseReason {
pub(crate) fn new(raw: bool) -> Self {
Self {
raw,
reason: OnceLock::new(),
reason: Mutex::default(),
}
}

/// The QUIC code to reset or stop a stream with.
fn get(&self) -> Option<Error> {
self.reason.lock().unwrap().err.clone()
}

/// The QUIC code to reset or stop a stream with: WT_SESSION_GONE once the session closed,
/// which tells the peer to read the session's close reason instead (draft-ietf-webtrans-http3 §4.4).
pub(crate) fn encode_stream_code(&self, code: u32) -> crate::quic::driver::VarInt {
if self.raw {
return code.into();
}
if self.reason.lock().unwrap().err.is_some() {
return crate::quic::driver::VarInt::from_u64(WT_SESSION_GONE).unwrap();
}
crate::quic::driver::VarInt::try_from(web_transport_proto::error_to_http3(code)).unwrap()
}

Expand All @@ -128,22 +139,42 @@ impl CloseReason {
}
}

/// The session's close reason when the peer reset or stopped a stream with
/// Poll the session's close reason when the peer reset or stopped a stream with
/// WT_SESSION_GONE, which tears down the streams of a closed session
/// (draft-ietf-webtrans-http3 §4.4), or None for any other code.
///
/// The reset can beat the close capsule, so until it lands the code and reason are
/// unknown: [`Error::SessionGone`], never a guessed clean close.
pub(crate) fn session_gone(&self, code: crate::quic::driver::VarInt) -> Option<Error> {
/// The reset can beat the close capsule, so this is pending until the reason is recorded,
/// which the capsule or the connection's close always does.
pub(crate) fn poll_session_gone(
&self,
waiter: &kio::Waiter,
code: crate::quic::driver::VarInt,
) -> Option<Poll<Error>> {
if self.raw || code.into_inner() != WT_SESSION_GONE {
return None;
}
Some(self.reason.get().cloned().unwrap_or(Error::SessionGone))
let mut reason = self.reason.lock().unwrap();
Some(match &reason.err {
Some(err) => Poll::Ready(err.clone()),
None => {
reason.waiters.register(waiter);
Poll::Pending
}
})
}

/// Record the close reason, returning false if one was already recorded.
pub(crate) fn set(&self, err: Error) -> bool {
self.reason.set(err).is_ok()
let mut waiters = {
let mut reason = self.reason.lock().unwrap();
if reason.err.is_some() {
return false;
}
reason.err = Some(err);
reason.waiters.take()
};
waiters.wake();
true
}

/// Replace an error caused by the connection closing with the recorded reason, or
Expand All @@ -155,12 +186,12 @@ impl CloseReason {
Error::Connection(conn)
| Error::Read(recv::Error::Connection(conn))
| Error::Write(send::Error::Connection(conn)) => Some(conn),
Error::Closed(..) | Error::SessionGone | Error::Http3 { .. } => None,
Error::Closed(..) | Error::Http3 { .. } => None,
_ => return err,
};

if let Some(reason) = self.reason.get() {
return reason.clone();
if let Some(reason) = self.get() {
return reason;
}

let Some(connection::Error::ApplicationClosed(close)) = conn else {
Expand Down Expand Up @@ -276,22 +307,32 @@ mod tests {
assert_eq!(close.decode_stream_code(invalid), None);
}

/// WT_SESSION_GONE reports the session's close, before and after its capsule arrives.
/// WT_SESSION_GONE waits for the session's close reason, and a closed session sends it.
#[test]
fn session_gone_reads_as_the_close() {
fn session_gone_waits_for_the_close() {
let gone = crate::quic::driver::VarInt::from_u64(WT_SESSION_GONE).unwrap();
let close = CloseReason::new(false);
assert!(matches!(close.session_gone(gone), Some(Error::SessionGone)));
struct Flag(std::sync::atomic::AtomicBool);
impl std::task::Wake for Flag {
fn wake(self: std::sync::Arc<Self>) {
self.0.store(true, std::sync::atomic::Ordering::SeqCst);
}
}
let flag = std::sync::Arc::new(Flag(Default::default()));
let waiter = kio::Waiter::new(flag.clone().into());
assert!(matches!(close.poll_session_gone(&waiter, gone), Some(Poll::Pending)));
assert!(close.poll_session_gone(&waiter, close.encode_stream_code(7)).is_none());

close.set(Error::Closed(7, "bye".into()));
assert!(flag.0.load(std::sync::atomic::Ordering::SeqCst));
assert!(matches!(
close.session_gone(gone),
Some(Error::Closed(7, reason)) if reason == "bye"
close.poll_session_gone(&waiter, gone),
Some(Poll::Ready(Error::Closed(7, reason))) if reason == "bye"
));
assert!(close.session_gone(close.encode_stream_code(7)).is_none());
assert_eq!(close.encode_stream_code(7), gone);

// Raw QUIC has no HTTP/3 code space, so the same number is an application code.
assert!(CloseReason::new(true).session_gone(gone).is_none());
assert!(CloseReason::new(true).poll_session_gone(&waiter, gone).is_none());
}

/// A peer close maps into WebTransport code space, or is HTTP/3's own failure when its
Expand Down
80 changes: 53 additions & 27 deletions rs/moq-tokio/src/quic/web/recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::quic::web::CloseReason;

/// A stream that can be used to receive bytes. See [`crate::quic::driver::recv::Stream`].
///
/// Dropping it before the end stops it with code 0.
/// Dropping it before the end stops it with code 0, or WT_SESSION_GONE once the session closed.
#[derive(Debug)]
pub struct Stream {
/// Taken when moq-net stops through its mutable poll trait.
Expand All @@ -24,9 +24,7 @@ pub struct Stream {
}

impl Stream {
pub(crate) fn new(mut stream: recv::Stream, close: Arc<CloseReason>) -> Self {
// The driver's raw 0 is outside the HTTP/3 range, so a WebTransport peer can't decode it.
stream.set_drop_stop(close.encode_stream_code(0));
pub(crate) fn new(stream: recv::Stream, close: Arc<CloseReason>) -> Self {
Self {
id: stream.id(),
inner: Some(stream),
Expand All @@ -42,7 +40,11 @@ impl Stream {
pub fn poll_lease(&mut self, waiter: &kio::Waiter, want: usize) -> Poll<Result<recv::Lease<'_>, Error>> {
let close = &self.close;
match &mut self.inner {
Some(inner) => inner.poll_lease(waiter, want).map_err(|e| map_error(close, e)),
Some(inner) => match inner.poll_lease(waiter, want) {
Poll::Ready(Ok(lease)) => Poll::Ready(Ok(lease)),
Poll::Ready(Err(e)) => poll_error(close, waiter, e).map(Err),
Poll::Pending => Poll::Pending,
},
None => Poll::Ready(Err(Error::ClosedStream)),
}
}
Expand All @@ -57,8 +59,8 @@ impl Stream {
};
match ready!(inner.poll_received_reset(waiter)) {
Ok(None) => Poll::Pending,
Ok(Some(code)) => Poll::Ready(map_error(close, recv::Error::Reset(code))),
Err(err) => Poll::Ready(map_error(close, err)),
Ok(Some(code)) => poll_error(close, waiter, recv::Error::Reset(code)),
Err(err) => poll_error(close, waiter, err),
}
}

Expand Down Expand Up @@ -92,41 +94,50 @@ impl Stream {
};
match ready!(inner.poll_closed(waiter)) {
Ok(None) => Poll::Ready(Ok(())),
Ok(Some(code)) => Poll::Ready(Err(map_error(close, recv::Error::Reset(code)))),
Err(err) => Poll::Ready(Err(map_error(close, err))),
Ok(Some(code)) => poll_error(close, waiter, recv::Error::Reset(code)).map(Err),
Err(err) => poll_error(close, waiter, err).map(Err),
}
}

/// Poll for some bytes without retaining the destination buffer.
pub fn poll_read(&mut self, waiter: &kio::Waiter, mut buf: &mut [u8]) -> Poll<Result<Option<usize>, Error>> {
let close = self.close.clone();
self.inner()?
.poll_read_buf(waiter, &mut buf)
.map_err(|err| map_error(&close, err))
match ready!(self.inner()?.poll_read_buf(waiter, &mut buf)) {
Ok(n) => Poll::Ready(Ok(n)),
Err(err) => poll_error(&close, waiter, err).map(Err),
}
}

/// Read some data into `buf` and return the amount read. See [`recv::Stream::read`].
pub async fn read(&mut self, buf: &mut impl BufMut) -> Result<Option<usize>, Error> {
let result = self.inner()?.read(buf).await;
result.map_err(|e| map_error(&self.close, e))
match self.inner()?.read(buf).await {
Ok(res) => Ok(res),
Err(e) => Err(error(&self.close, e).await),
}
}

/// Fill the entire buffer with data. See [`recv::Stream::read_exact`].
pub async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error> {
let result = self.inner()?.read_exact(buf).await;
result.map_err(|e| map_error(&self.close, e))
match self.inner()?.read_exact(buf).await {
Ok(res) => Ok(res),
Err(e) => Err(error(&self.close, e).await),
}
}

/// Read a chunk of data from the stream. See [`recv::Stream::read_chunk`].
pub async fn read_chunk(&mut self, max: usize) -> Result<Option<Bytes>, Error> {
let result = self.inner()?.read_chunk(max).await;
result.map_err(|e| map_error(&self.close, e))
match self.inner()?.read_chunk(max).await {
Ok(res) => Ok(res),
Err(e) => Err(error(&self.close, e).await),
}
}

/// Read until the end of the stream or the limit is hit. See [`recv::Stream::read_to_end`].
pub async fn read_to_end(&mut self, limit: usize) -> Result<Vec<u8>, Error> {
let result = self.inner()?.read_to_end(limit).await;
result.map_err(|e| map_error(&self.close, e))
match self.inner()?.read_to_end(limit).await {
Ok(res) => Ok(res),
Err(e) => Err(error(&self.close, e).await),
}
}

/// Block until the stream has been reset and return the error code. See
Expand All @@ -140,11 +151,11 @@ impl Stream {
};
match inner.received_reset().await {
Ok(None) => Ok(None),
Ok(Some(code)) => match map_error(&self.close, recv::Error::Reset(code)) {
Ok(Some(code)) => match error(&self.close, recv::Error::Reset(code)).await {
Error::Reset(code) => Ok(Some(code)),
err => Err(err),
},
Err(err) => Err(map_error(&self.close, err)),
Err(err) => Err(error(&self.close, err).await),
}
}

Expand All @@ -162,20 +173,35 @@ impl Stream {
}

/// Decode the peer's reset code, and report a session error, or a reset for a session
/// that is gone, as the session's close reason.
fn map_error(close: &CloseReason, e: recv::Error) -> Error {
match e {
/// that is gone, as the session's close reason, pending until that reason is known.
fn poll_error(close: &CloseReason, waiter: &kio::Waiter, e: recv::Error) -> Poll<Error> {
Poll::Ready(match e {
recv::Error::Reset(code) => match close.decode_stream_code(code) {
Some(code) => Error::Reset(code),
None => match close.session_gone(code) {
Some(err) => Error::Session(err),
None => match close.poll_session_gone(waiter, code) {
Some(err) => return err.map(Error::Session),
None => Error::InvalidReset(code),
},
},
e => match Error::from(e) {
Error::Session(e) => Error::Session(close.map(e)),
e => e,
},
})
}

/// [`poll_error`] as a future.
async fn error(close: &CloseReason, e: recv::Error) -> Error {
kio::wait(|waiter| poll_error(close, waiter, e.clone())).await
}

impl Drop for Stream {
fn drop(&mut self) {
// Chosen at drop, since the session may have closed since the stream opened. The
// driver's raw 0 is outside the HTTP/3 range, so a WebTransport peer can't decode it.
if let Some(inner) = &mut self.inner {
inner.set_drop_stop(self.close.encode_stream_code(0));
}
}
}

Expand Down
Loading