From 558bedba741514cface609b20b2ec1f065f2c5f4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 6 Nov 2026 23:20:43 -0800 Subject: [PATCH 1/4] chore(quest): claim quest/a0/qmux-ws-handshake-timeout.md Quest-Claim: 01081a0527299566 From b815b4ca63350bd21646c97464e5c1a2fc5dc937 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 00:05:32 -0800 Subject: [PATCH 2/4] fix(qmux): enforce a shared transport-parameter handshake deadline (Written by GPT-6.1 Sol with xhigh) --- quest/a0/README.md | 1 - quest/a0/qmux-ws-handshake-timeout.md | 11 ----------- 2 files changed, 12 deletions(-) delete mode 100644 quest/a0/qmux-ws-handshake-timeout.md diff --git a/quest/a0/README.md b/quest/a0/README.md index 26ce85f5..9bf5a2b2 100644 --- a/quest/a0/README.md +++ b/quest/a0/README.md @@ -13,7 +13,6 @@ The Required list is ordered by priority. Independent children may proceed concu - [Quinn final Session drop](/quest/a0/quinn-session-drop.md) - close the connection and release the capsule reader - [Quiche lifecycle](/quest/a0/quiche/README.md) - deterministic harness and retirement handling - [QMux socket teardown](/quest/a0/qmux-socket-teardown.md) - release retained stats descriptors -- [WebSocket handshake timeout](/quest/a0/qmux-ws-handshake-timeout.md) - close sessions whose peer never sends parameters - [QMux initial stream-count bound](/quest/a0/qmux-max-streams-param.md) - refuse initial_max_streams parameters above 2^60 - [QMux stream-state checks](/quest/a0/qmux-stream-state.md) - refuse MAX_STREAM_DATA and STOP_SENDING for unopened local streams - [Node Server.close()](/quest/a0/node-server-close.md) - close completes while an accept is pending diff --git a/quest/a0/qmux-ws-handshake-timeout.md b/quest/a0/qmux-ws-handshake-timeout.md deleted file mode 100644 index 1b174191..00000000 --- a/quest/a0/qmux-ws-handshake-timeout.md +++ /dev/null @@ -1,11 +0,0 @@ -# [S] Bound a WebSocket QMux handshake - -## Goal - -A QMux session over WebSocket closes with an error when the peer never sends TRANSPORT_PARAMETERS within `Config::handshake_timeout`, for `ws::Client`, `ws::Server` and `ws::Upgraded` alike. Today a peer that completes the WebSocket upgrade and then stays silent holds the session open indefinitely. - -## Plan - -`Session::connect` and `Session::accept` bound the parameter exchange, but every ws builder starts through `Session::new`, which never waits for the peer's parameters. Arm the deadline inside the session started by `Session::new` and close the session with the same error `Session::connect` returns on timeout if the peer's TRANSPORT_PARAMETERS haven't arrived by then. This is the lowest layer that covers all three builders without an API change. Do not make `ws::Upgraded::connect`/`accept` async; that would break the API. Wire formats that exchange no transport parameters arm no deadline. Fold `Session::connect`/`accept`'s own timeout into the same mechanism if that simplifies things. - -Remove the "`handshake_timeout` does not apply" notes from the three `with_session_config` docs. Cover each builder on paused time. Main only; backport to `qmux-v0.5.x` only if moq asks. From 99db51c28a26f9e77c343cda918624481e86687d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 00:06:04 -0800 Subject: [PATCH 3/4] fix(qmux): time out silent transport-parameter handshakes (Written by GPT-6.1 Sol with xhigh) --- rs/qmux/README.md | 7 ++ rs/qmux/src/config.rs | 12 +-- rs/qmux/src/error.rs | 5 +- rs/qmux/src/session.rs | 105 +++++++++++++++------ rs/qmux/src/ws.rs | 206 +++++++++++++++++++++++++++++++++++++++-- 5 files changed, 292 insertions(+), 43 deletions(-) diff --git a/rs/qmux/README.md b/rs/qmux/README.md index 57baf91f..8c37fdbe 100644 --- a/rs/qmux/README.md +++ b/rs/qmux/README.md @@ -55,6 +55,13 @@ builder or the handshake. The default grants a peer 100 concurrent streams in each direction; raise `max_streams_bidi` and `max_streams_uni` if your application holds more streams open at once. +`Config::handshake_timeout` bounds the wait for peer transport parameters after +the transport connects, including WebSocket sessions. A silent peer closes the +session with `Error::HandshakeTimeout`; WebSocket builders return before this +exchange, so observe the error through session operations or `Session::closed`. +The default is 10 seconds, zero disables the deadline, and the legacy +`webtransport` format exchanges no parameters and is exempt. + To run QMux over something the built-in modules don't cover, wrap any `AsyncRead + AsyncWrite` byte stream in `transport::Stream`, or implement `transport::Transport` yourself, then pass it to `Session::connect` / diff --git a/rs/qmux/src/config.rs b/rs/qmux/src/config.rs index 7ff0403d..c77e3001 100644 --- a/rs/qmux/src/config.rs +++ b/rs/qmux/src/config.rs @@ -75,12 +75,12 @@ pub struct Config { /// 16382 bytes. pub max_datagram_frame_size: u64, - /// How long [`Session::connect`](crate::Session::connect) / - /// [`accept`](crate::Session::accept) waits for the peer's transport - /// parameters before giving up. Bounds the handshake so a peer that completes - /// the transport connection but never sends its parameters can't hang - /// establishment forever. Default: 10s; a zero duration disables the timeout - /// (wait indefinitely). + /// How long a session waits for the peer's transport parameters before + /// closing with [`Error::HandshakeTimeout`](crate::Error::HandshakeTimeout). + /// Starts when the session is created, after the transport handshake, + /// including for WebSocket sessions that return before parameters arrive. + /// The legacy `webtransport` format exchanges no parameters and is exempt. + /// Default: 10s; a zero duration disables the timeout (wait indefinitely). pub handshake_timeout: Duration, /// How often to send a `QX_PING` request purely to measure round-trip time, diff --git a/rs/qmux/src/error.rs b/rs/qmux/src/error.rs index 1d10aa77..b12d4c90 100644 --- a/rs/qmux/src/error.rs +++ b/rs/qmux/src/error.rs @@ -114,10 +114,11 @@ impl Error { /// The wire error code to send on a CONNECTION_CLOSE (0x1c) when *we* tear the /// session down because of this error, or `None` when the peer should not (or /// cannot) be told: a graceful close, a close the peer already sent us, an idle - /// timeout (silent, like QUIC), or a dead transport. Every protocol/transport - /// violation we detect maps to 1002, matching the JS QMux polyfill. + /// timeout (silent, like QUIC), or a dead transport. Handshake expiry uses 0; + /// protocol/transport violations map to 1002, matching the JS QMux polyfill. pub(crate) fn transport_close(&self) -> Option { match self { + Error::HandshakeTimeout => Some(0), Error::ProtocolViolation | Error::FrameEncoding | Error::TransportParameter diff --git a/rs/qmux/src/session.rs b/rs/qmux/src/session.rs index 7b9827a9..f8567d8b 100644 --- a/rs/qmux/src/session.rs +++ b/rs/qmux/src/session.rs @@ -2107,32 +2107,12 @@ impl Session { return Ok(()); } - let wait = async { established.wait_for(|&done| done).await.is_ok() }; - let timeout = self.config.handshake_timeout; - // A zero timeout disables the bound (wait indefinitely). - let outcome = if timeout.is_zero() { - Some(wait.await) + if established.wait_for(|&done| done).await.is_ok() { + Ok(()) } else { - tokio::time::timeout(timeout, wait).await.ok() - }; - - match outcome { - // Established. - Some(true) => Ok(()), - // The backend task ended before establishing — surface the close reason. - Some(false) => Err(self.close_reason().await), - // Timed out waiting for the peer's parameters: abort the half-open - // handshake, notifying the peer, and fail rather than hang. - // If the session closed first, report that reason instead. - None => { - // Abnormal: a CONNECTION_CLOSE (0x1c) so the peer's session rejects - // rather than seeing a graceful close. - let frame = ConnectionClose { - code: VarInt::from(0u32), - reason: "handshake timeout".to_string(), - }; - Err(self.close_with(frame.into(), Error::HandshakeTimeout)) - } + // The backend enforces the handshake deadline and records its error + // before dropping the establishment sender. + Err(self.close_reason().await) } } @@ -2323,8 +2303,27 @@ impl Session { tokio::spawn(timer.run()); } + let handshake_deadline = (version.is_qmux() && !config.handshake_timeout.is_zero()) + .then(|| base + config.handshake_timeout); + let mut established = established_rx.clone(); tokio::spawn(async move { - let err = backend.run().await.err().unwrap_or(Error::Closed); + let handshake = async { + if let Some(deadline) = handshake_deadline { + if tokio::time::timeout_at(deadline, established.wait_for(|&done| done)) + .await + .is_err() + { + return Error::HandshakeTimeout; + } + } + std::future::pending().await + }; + // The deadline starts at construction and remains pollable even if + // the reader is parked on application backpressure. + let err = tokio::select! { + result = backend.run() => result.err().unwrap_or(Error::Closed), + err = handshake => err, + }; // If we tore down because of a protocol/transport violation *we* // detected, tell the peer with a CONNECTION_CLOSE (0x1c) so their // session rejects too, rather than seeing a bare drop. Enqueue on the @@ -5007,6 +5006,60 @@ mod teardown_tests { } } + #[tokio::test(start_paused = true)] + async fn handshake_timeout_releases_wedged_writer_with_retained_session() { + use web_transport_trait::Session as _; + + for version in [Version::QMux00, Version::QMux01, Version::QMux02] { + let (entered_tx, mut entered_rx) = mpsc::unbounded_channel(); + let (dropped_tx, mut dropped_rx) = mpsc::unbounded_channel(); + let mut config = Config::new(version); + config.handshake_timeout = Duration::from_secs(1); + let session = Session::new( + WedgedTransport { + entered_send: entered_tx, + dropped: dropped_tx, + }, + false, + config, + ); + entered_rx.recv().await.unwrap(); + let error = tokio::time::timeout(Duration::from_secs(2), session.closed()) + .await + .unwrap(); + assert!(matches!(error, Error::HandshakeTimeout), "{error:?}"); + tokio::time::timeout(Duration::from_secs(1), dropped_rx.recv()) + .await + .unwrap() + .unwrap(); + } + } + + #[tokio::test(start_paused = true)] + async fn async_entrypoints_report_shared_handshake_timeout() { + for is_server in [false, true] { + let (entered_send, _entered_rx) = mpsc::unbounded_channel(); + let (dropped, _dropped_rx) = mpsc::unbounded_channel(); + let transport = WedgedTransport { + entered_send, + dropped, + }; + let mut config = Config::new(Version::QMux02); + config.handshake_timeout = Duration::from_secs(1); + let establish = async { + if is_server { + Session::accept(transport, config).await + } else { + Session::connect(transport, config).await + } + }; + let result = tokio::time::timeout(Duration::from_secs(2), establish) + .await + .expect("establishment stayed pending"); + assert!(matches!(result, Err(Error::HandshakeTimeout))); + } + } + /// A writer parked inside `send()` on a wedged transport must still observe /// teardown when the last `Session` clone drops, cancelling the in-flight /// write instead of staying alive until the transport eventually errors. diff --git a/rs/qmux/src/ws.rs b/rs/qmux/src/ws.rs index 7223458f..b40e8681 100644 --- a/rs/qmux/src/ws.rs +++ b/rs/qmux/src/ws.rs @@ -104,9 +104,8 @@ where /// Use `config` for every session setting except the wire-format version /// and application protocol, which the handshake negotiates. /// - /// [`handshake_timeout`](Config::handshake_timeout) does not apply: the - /// WebSocket handshake already settled the protocol, so the session starts - /// without awaiting the peer's transport parameters. Keep + /// The session closes if the peer's transport parameters do not arrive within + /// [`handshake_timeout`](Config::handshake_timeout). Keep /// [`max_record_size`](Config::max_record_size) within the WebSocket's /// frame limit (16 MiB by default), since each record is one message. pub fn with_session_config(mut self, config: Config) -> Self { @@ -235,9 +234,8 @@ impl Client { /// Use `config` for every session setting except the wire-format version /// and application protocol, which the handshake negotiates. /// - /// [`handshake_timeout`](Config::handshake_timeout) does not apply: the - /// WebSocket handshake already settled the protocol, so the session starts - /// without awaiting the peer's transport parameters. Keep + /// The session closes if the peer's transport parameters do not arrive within + /// [`handshake_timeout`](Config::handshake_timeout). Keep /// [`max_record_size`](Config::max_record_size) within the WebSocket's /// frame limit (16 MiB by default), since each record is one message. pub fn with_session_config(mut self, config: Config) -> Self { @@ -386,9 +384,8 @@ impl Server { /// Use `config` for every session setting except the wire-format version /// and application protocol, which the handshake negotiates. /// - /// [`handshake_timeout`](Config::handshake_timeout) does not apply: the - /// WebSocket handshake already settled the protocol, so the session starts - /// without awaiting the peer's transport parameters. Keep + /// The session closes if the peer's transport parameters do not arrive within + /// [`handshake_timeout`](Config::handshake_timeout). Keep /// [`max_record_size`](Config::max_record_size) within the WebSocket's /// frame limit (16 MiB by default), since each record is one message. pub fn with_session_config(mut self, config: Config) -> Self { @@ -487,3 +484,194 @@ impl Server { Ok(Session::new(transport, true, config)) } } + +#[cfg(test)] +mod tests { + use super::*; + use futures::{Sink, SinkExt, Stream}; + use std::{future::Future, task::Context}; + use tokio::time::{advance, timeout}; + use tokio_tungstenite::{tungstenite::client::IntoClientRequest, WebSocketStream}; + use web_transport_trait::Session as _; + + const DEADLINE: Duration = Duration::from_secs(5); + + trait Peer: + Stream> + + Sink + + Unpin + { + } + + impl Peer for WebSocketStream where T: AsyncRead + AsyncWrite + Unpin {} + + #[derive(Clone, Copy, Debug)] + enum Builder { + Client, + Server, + UpgradedClient, + UpgradedServer, + } + + const BUILDERS: &[Builder] = &[ + Builder::Client, + Builder::Server, + Builder::UpgradedClient, + Builder::UpgradedServer, + ]; + + fn config() -> Config { + Config { + handshake_timeout: DEADLINE, + max_idle_timeout: 0, + rtt_interval: Duration::ZERO, + ..Config::default() + } + } + + async fn start(builder: Builder, version: Version, config: Config) -> (Session, Box) { + use tungstenite::{http::header::SEC_WEBSOCKET_PROTOCOL, protocol::Role}; + + if let Builder::Client = builder { + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("ws://{}", listener.local_addr().unwrap()); + let server = async { + let (socket, _) = listener.accept().await.unwrap(); + #[allow(clippy::result_large_err)] + let callback = + move |request: &tungstenite::handshake::server::Request, + mut response: tungstenite::handshake::server::Response| { + let protocol = if version.is_qmux() { + request.headers()[SEC_WEBSOCKET_PROTOCOL].clone() + } else { + version.alpn().parse().unwrap() + }; + response.headers_mut().insert(SEC_WEBSOCKET_PROTOCOL, protocol); + Ok(response) + }; + tokio_tungstenite::accept_hdr_async(socket, callback) + .await + .unwrap() + }; + let client = Client::new().with_session_config(config); + let client = if version.is_qmux() { + client.with_protocol("test", &[version]).require_protocol() + } else { + client + }; + let (session, peer) = tokio::join!(client.connect(&url), server); + return (session.unwrap(), Box::new(peer)); + } + + let (a, b) = tokio::io::duplex(64 * 1024); + if let Builder::Server = builder { + let server = Server::new().with_session_config(config); + let mut request = "ws://localhost".into_client_request().unwrap(); + request + .headers_mut() + .insert(SEC_WEBSOCKET_PROTOCOL, version.alpn().parse().unwrap()); + let (session, peer) = tokio::join!( + server.accept(a), + tokio_tungstenite::client_async(request, b) + ); + return (session.unwrap(), Box::new(peer.unwrap().0)); + } + + let is_server = matches!(builder, Builder::UpgradedServer); + let (role, peer_role) = if is_server { + (Role::Server, Role::Client) + } else { + (Role::Client, Role::Server) + }; + let socket = WebSocketStream::from_raw_socket(a, role, None).await; + let peer = WebSocketStream::from_raw_socket(b, peer_role, None).await; + let upgraded = Upgraded::new(socket) + .with_alpn(version.alpn()) + .with_session_config(config); + let session = if is_server { + upgraded.accept() + } else { + upgraded.connect() + }; + (session, Box::new(peer)) + } + + fn assert_open(session: &Session) { + let mut closed = std::pin::pin!(session.closed()); + assert!(closed + .as_mut() + .poll(&mut Context::from_waker(std::task::Waker::noop())) + .is_pending()); + } + + #[tokio::test(start_paused = true)] + async fn silent_peer_hits_handshake_deadline() { + for &builder in BUILDERS { + for &version in Version::ALL.iter().filter(|v| v.is_qmux()) { + let (session, _peer) = start(builder, version, config()).await; + advance(DEADLINE - Duration::from_millis(1)).await; + assert_open(&session); + let error = timeout(Duration::from_secs(1), session.closed()) + .await + .unwrap_or_else(|_| panic!("{builder:?} {version:?}: handshake stayed open")); + assert!(matches!(error, Error::HandshakeTimeout), "{error:?}"); + assert!(matches!( + session.open_uni().await, + Err(Error::HandshakeTimeout) + )); + assert!(matches!( + session.accept_uni().await, + Err(Error::HandshakeTimeout) + )); + } + } + } + + #[tokio::test(start_paused = true)] + async fn disabled_handshake_deadline_and_legacy_stay_open() { + for &builder in BUILDERS { + for &version in Version::ALL { + let mut config = config(); + if version.is_qmux() { + config.handshake_timeout = Duration::ZERO; + } + let (session, _peer) = start(builder, version, config).await; + advance(DEADLINE * 10).await; + assert_open(&session); + } + } + } + + #[tokio::test(start_paused = true)] + async fn receiving_parameters_cancels_handshake_deadline() { + for &builder in BUILDERS { + for &version in Version::ALL.iter().filter(|v| v.is_qmux()) { + let (session, mut peer) = start(builder, version, config()).await; + advance(DEADLINE / 2).await; + let mut peer_config = config(); + peer_config.version = version; + let params = crate::Frame::TransportParameters(peer_config.to_transport_params()) + .encode(version) + .unwrap(); + peer.send(tungstenite::Message::Binary(params)) + .await + .unwrap(); + // Opening needs the peer's stream credit, so this acknowledges + // that its parameters were applied before advancing time. + let _stream = session.open_uni().await.unwrap(); + advance(DEADLINE * 2).await; + assert_open(&session); + } + } + } + + #[tokio::test(start_paused = true)] + async fn disconnect_before_parameters_preserves_error() { + for &builder in BUILDERS { + let (session, peer) = start(builder, Version::QMux02, config()).await; + drop(peer); + let error = timeout(DEADLINE / 2, session.closed()).await.unwrap(); + assert!(!matches!(error, Error::HandshakeTimeout), "{error:?}"); + } + } +} From 65ccb7e7d969693d0c66b1968ec0fff82728026b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 00:17:23 -0800 Subject: [PATCH 4/4] fix(qmux): preserve handshake deadline edge cases and review coverage (Written by GPT-6.1 Sol with xhigh) --- rs/qmux/src/session.rs | 54 +++++++++++++++++++++++++++-- rs/qmux/src/ws.rs | 78 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 120 insertions(+), 12 deletions(-) diff --git a/rs/qmux/src/session.rs b/rs/qmux/src/session.rs index f8567d8b..da074d7e 100644 --- a/rs/qmux/src/session.rs +++ b/rs/qmux/src/session.rs @@ -2303,8 +2303,12 @@ impl Session { tokio::spawn(timer.run()); } - let handshake_deadline = (version.is_qmux() && !config.handshake_timeout.is_zero()) - .then(|| base + config.handshake_timeout); + let handshake_deadline = if version.is_qmux() && !config.handshake_timeout.is_zero() { + // A deadline beyond the clock's range cannot expire. + base.checked_add(config.handshake_timeout) + } else { + None + }; let mut established = established_rx.clone(); tokio::spawn(async move { let handshake = async { @@ -2319,8 +2323,10 @@ impl Session { std::future::pending().await }; // The deadline starts at construction and remains pollable even if - // the reader is parked on application backpressure. + // the reader is parked on application backpressure. Already-readable + // peer input wins over an expired deadline. let err = tokio::select! { + biased; result = backend.run() => result.err().unwrap_or(Error::Closed), err = handshake => err, }; @@ -3675,6 +3681,48 @@ mod recv_open_tests { scripted_session_with_config(Config::new(Version::QMux01)) } + #[tokio::test(start_paused = true)] + async fn queued_handshake_frames_win_after_delayed_poll() { + for version in [Version::QMux00, Version::QMux01, Version::QMux02] { + for peer_closed in [false, true] { + let mut config = Config::new(version); + config.handshake_timeout = Duration::from_secs(1); + config.max_idle_timeout = 0; + config.rtt_interval = Duration::ZERO; + let params = config.to_transport_params(); + let (session, incoming) = scripted_session_with_config(config); + incoming + .send(Frame::TransportParameters(params).encode(version).unwrap()) + .unwrap(); + if peer_closed { + let frame = Frame::ConnectionClose(crate::proto::ConnectionClose { + code: VarInt::from_u32(42), + reason: "peer closed".into(), + }); + incoming.send(frame.encode(version).unwrap()).unwrap(); + } + // No spawned session task is polled until the clock has passed + // its deadline, with the peer's frame already readable. + tokio::time::advance(Duration::from_secs(2)).await; + if peer_closed { + let error = tokio::time::timeout(Duration::from_secs(1), session.closed()) + .await + .unwrap(); + assert!( + matches!(error, Error::ConnectionReset { code, .. } if code.into_inner() == 42) + ); + } else { + session.established().await.unwrap(); + assert!( + tokio::time::timeout(Duration::from_secs(1), session.closed()) + .await + .is_err() + ); + } + } + } + } + /// Encode a STREAM frame on a server-initiated uni stream (peer-initiated and /// receivable, since we're the client). fn uni_stream(index: u64, data: &'static [u8], fin: bool) -> Bytes { diff --git a/rs/qmux/src/ws.rs b/rs/qmux/src/ws.rs index b40e8681..7ba4d94c 100644 --- a/rs/qmux/src/ws.rs +++ b/rs/qmux/src/ws.rs @@ -488,7 +488,7 @@ impl Server { #[cfg(test)] mod tests { use super::*; - use futures::{Sink, SinkExt, Stream}; + use futures::{Sink, SinkExt, Stream, StreamExt}; use std::{future::Future, task::Context}; use tokio::time::{advance, timeout}; use tokio_tungstenite::{tungstenite::client::IntoClientRequest, WebSocketStream}; @@ -604,6 +604,19 @@ mod tests { .is_pending()); } + async fn network_io(builder: Builder, io: F) -> F::Output { + // Real socket delivery must not race paused time's automatic advance. + let real_time = matches!(builder, Builder::Client); + if real_time { + tokio::time::resume(); + } + let output = io.await; + if real_time { + tokio::time::pause(); + } + output + } + #[tokio::test(start_paused = true)] async fn silent_peer_hits_handshake_deadline() { for &builder in BUILDERS { @@ -642,6 +655,48 @@ mod tests { } } + #[tokio::test(start_paused = true)] + async fn oversized_handshake_deadline_stays_open() { + for &builder in BUILDERS { + for &version in Version::ALL.iter().filter(|v| v.is_qmux()) { + let mut config = config(); + config.handshake_timeout = Duration::MAX; + let (session, _peer) = start(builder, version, config).await; + advance(DEADLINE * 10).await; + assert_open(&session); + } + } + } + + #[tokio::test(start_paused = true)] + async fn handshake_deadline_sends_connection_close() { + for &version in Version::ALL.iter().filter(|v| v.is_qmux()) { + let (session, mut peer) = start(Builder::UpgradedClient, version, config()).await; + // Drain our parameters so the writer is idle when the deadline fires. + assert!(matches!( + peer.next().await.unwrap().unwrap(), + tungstenite::Message::Binary(_) + )); + let error = timeout(DEADLINE * 2, session.closed()).await.unwrap(); + assert!(matches!(error, Error::HandshakeTimeout)); + let message = timeout(DEADLINE, peer.next()) + .await + .unwrap() + .unwrap() + .unwrap(); + let tungstenite::Message::Binary(data) = message else { + panic!("expected a QMux CONNECTION_CLOSE, got {message:?}"); + }; + let Some(crate::Frame::ConnectionClose(close)) = + crate::Frame::decode(data, version).unwrap() + else { + panic!("expected a QMux CONNECTION_CLOSE"); + }; + assert_eq!(close.code.into_inner(), 0); + assert_eq!(close.reason, Error::HandshakeTimeout.to_string()); + } + } + #[tokio::test(start_paused = true)] async fn receiving_parameters_cancels_handshake_deadline() { for &builder in BUILDERS { @@ -653,12 +708,14 @@ mod tests { let params = crate::Frame::TransportParameters(peer_config.to_transport_params()) .encode(version) .unwrap(); - peer.send(tungstenite::Message::Binary(params)) - .await - .unwrap(); - // Opening needs the peer's stream credit, so this acknowledges - // that its parameters were applied before advancing time. - let _stream = session.open_uni().await.unwrap(); + let _stream = network_io(builder, async { + peer.send(tungstenite::Message::Binary(params)) + .await + .unwrap(); + // Opening acknowledges that the parameters were applied. + session.open_uni().await.unwrap() + }) + .await; advance(DEADLINE * 2).await; assert_open(&session); } @@ -669,8 +726,11 @@ mod tests { async fn disconnect_before_parameters_preserves_error() { for &builder in BUILDERS { let (session, peer) = start(builder, Version::QMux02, config()).await; - drop(peer); - let error = timeout(DEADLINE / 2, session.closed()).await.unwrap(); + let error = network_io(builder, async { + drop(peer); + timeout(DEADLINE / 2, session.closed()).await.unwrap() + }) + .await; assert!(!matches!(error, Error::HandshakeTimeout), "{error:?}"); } }