From aad0be1b182608b2f51b5521b3c10790bebfdb32 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 15 Oct 2026 12:28:17 -0700 Subject: [PATCH] fix(moq-tokio): grant qmux sessions the QUIC stream limit qmux grants a peer 100 concurrent streams by default, and moq-lite holds one open per subscription, so a WebSocket, tcp://, tls:// or unix:// peer past 100 subscriptions waits on stream credit forever with nothing erroring. Configure every qmux builder with the same 10,000 streams QUIC grants, through the session config qmux 0.6.3 added to its builders. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- rs/moq-tokio/src/lib.rs | 13 +++++++++++ rs/moq-tokio/src/tcp.rs | 43 ++++++++++++++++++++++++++++++++--- rs/moq-tokio/src/unix.rs | 5 +++- rs/moq-tokio/src/websocket.rs | 13 ++++++++--- 6 files changed, 70 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3bf788a746d..573153b9cdb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7102,9 +7102,9 @@ dependencies = [ [[package]] name = "qmux" -version = "0.6.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5813506ee76dc0d4ae9870a23105603af88552df45fe262ca776fad6dc0f94aa" +checksum = "542edbed299cac6d624d3964c423c2b2a94b19ffcde4eef5fde09b74c64799e4" dependencies = [ "bytes", "futures", diff --git a/Cargo.toml b/Cargo.toml index 6070495c7e9..00a081d389e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -209,7 +209,7 @@ objc2-screen-capture-kit = "0.3.2" object_store = { version = "0.14", default-features = false } percent-encoding = "2" pollster = "1.0" -qmux = { version = "0.6", default-features = false } +qmux = { version = "0.6.3", default-features = false } rand = "0.10.1" # default-features off so each consumer picks its own TLS backend and body # features; nothing here wants the default native-tls stack. diff --git a/rs/moq-tokio/src/lib.rs b/rs/moq-tokio/src/lib.rs index 05802f59969..29a9b356e95 100644 --- a/rs/moq-tokio/src/lib.rs +++ b/rs/moq-tokio/src/lib.rs @@ -67,6 +67,19 @@ pub mod watch; #[cfg(feature = "websocket")] pub mod websocket; +/// Session settings for every qmux transport (WebSocket, `tcp://`, `tls://`, `unix://`). +/// +/// moq-lite holds a stream open per subscription, so a qmux session grants the same +/// stream count as QUIC rather than qmux's default of 100, past which a peer's opens +/// wait on credit forever. +#[cfg(any(feature = "websocket", feature = "tcp"))] +fn qmux_config() -> qmux::Config { + let mut config = qmux::Config::default(); + config.max_streams_bidi = quic::DEFAULT_MAX_STREAMS; + config.max_streams_uni = quic::DEFAULT_MAX_STREAMS; + config +} + // Enumerated rather than globbed, so the root surface is a deliberate list and a // new `pub` item in these modules doesn't silently join it. pub use client::Client; diff --git a/rs/moq-tokio/src/tcp.rs b/rs/moq-tokio/src/tcp.rs index adbbb91ee41..1c19e4310f5 100644 --- a/rs/moq-tokio/src/tcp.rs +++ b/rs/moq-tokio/src/tcp.rs @@ -192,7 +192,8 @@ pub(crate) async fn connect_tls( crate::tls::server_name(&name)?; let client = qmux::tls::Client::new(std::sync::Arc::new(tls.build()?)) .with_protocols(protocols.iter().map(|&alpn| (alpn, &[WIRE_VERSION][..]))) - .require_protocol(); + .require_protocol() + .with_session_config(crate::qmux_config()); tracing::debug!(peer = %crate::connect::Endpoint(&url), "connecting via TLS over TCP"); let candidates = crate::resolve::Candidates::resolve(host, port, resolution_delay); @@ -221,6 +222,7 @@ async fn connect_addrs( async move { qmux::tcp::Config::new(WIRE_VERSION) .protocols(protocols.iter().map(String::as_str)) + .session_config(crate::qmux_config()) .connect(addr) .await .map_err(|err| Error::Connect(crate::error::message(err))) @@ -253,7 +255,7 @@ impl Listener { /// Serve TLS with `config`, whose ALPN list already names each /// `qmux-01.` pair to accept. pub(crate) fn with_tls(mut self, config: std::sync::Arc) -> Self { - self.tls = Some(qmux::tls::Server::new(config)); + self.tls = Some(qmux::tls::Server::new(config).with_session_config(crate::qmux_config())); self } @@ -310,7 +312,9 @@ impl Listener { ) -> impl Future> + use<> { let (stream, addr) = self.accept_socket().await; tracing::debug!(%addr, "accepted TCP connection"); - let config = qmux::tcp::Config::new(WIRE_VERSION).protocols(self.protocols.iter().map(String::as_str)); + let config = qmux::tcp::Config::new(WIRE_VERSION) + .protocols(self.protocols.iter().map(String::as_str)) + .session_config(crate::qmux_config()); let tls = self.tls.clone(); async move { let session = match tls { @@ -459,6 +463,39 @@ mod tests { accept.await.expect("accept task panicked"); } + /// moq-lite holds a stream open per subscription, so each end of a session must + /// let its peer hold far more than qmux's default of 100 open at once. + #[tokio::test] + async fn sessions_grant_a_thousand_open_streams() { + use futures::FutureExt as _; + + let listener = Listener::bind("127.0.0.1:0".parse().unwrap()) + .await + .expect("bind listener") + .with_protocols(["moq-test"]); + let addr = listener.local_addr().expect("local addr"); + let accept = tokio::spawn(async move { listener.accept().await.expect("listener gone").expect("accept") }); + let candidates = crate::resolve::Candidates::fixed([addr]); + let client = connect_addrs(candidates, &["moq-test"], Duration::ZERO) + .await + .expect("connect failed"); + let server = accept.await.expect("accept task panicked"); + + // Both handshakes are done, so each end knows its peer's grant, and an open + // with credit left completes without waiting. + for (end, session) in [("client", &client), ("server", &server)] { + let mut held = Vec::new(); + for opened in 0..1_000 { + let bi = session.open_bi().now_or_never(); + let uni = session.open_uni().now_or_never(); + let (Some(bi), Some(uni)) = (bi, uni) else { + panic!("{end} stalled on stream credit after opening {opened} stream pairs"); + }; + held.push((bi.expect("open bi"), uni.expect("open uni"))); + } + } + } + #[tokio::test] async fn connect_addrs_rejects_empty() { let candidates = crate::resolve::Candidates::fixed([]); diff --git a/rs/moq-tokio/src/unix.rs b/rs/moq-tokio/src/unix.rs index 6b857cb051c..1c10499e3a7 100644 --- a/rs/moq-tokio/src/unix.rs +++ b/rs/moq-tokio/src/unix.rs @@ -247,6 +247,7 @@ pub(crate) async fn connect(url: Url, protocols: &[&str]) -> Result impl Future> + use<> { let stream = self.accept_socket().await; - let config = qmux::uds::Config::new(WIRE_VERSION).protocols(self.protocols.iter().map(String::as_str)); + let config = qmux::uds::Config::new(WIRE_VERSION) + .protocols(self.protocols.iter().map(String::as_str)) + .session_config(crate::qmux_config()); async move { let cred = stream.peer_cred()?; let cred = PeerCred { diff --git a/rs/moq-tokio/src/websocket.rs b/rs/moq-tokio/src/websocket.rs index 37c11d83bbb..609de2be9f0 100644 --- a/rs/moq-tokio/src/websocket.rs +++ b/rs/moq-tokio/src/websocket.rs @@ -154,7 +154,9 @@ where .and_then(|timeout| tokio::time::Instant::now().checked_add(timeout)); // A vanished peer sends no FIN. Ping and timeout bound the same stale-session // lifetime that QUIC's idle timeout bounds on the native path. - let mut upgraded = qmux::ws::Upgraded::new(socket).with_keep_alive(qmux::ws::KeepAlive::default()); + let mut upgraded = qmux::ws::Upgraded::new(socket) + .with_keep_alive(qmux::ws::KeepAlive::default()) + .with_session_config(crate::qmux_config()); if let Some(stats) = &options.socket_stats { upgraded = upgraded.with_socket_stats(stats.clone()); } @@ -455,6 +457,7 @@ pub(crate) async fn connect( .with_protocols(alpns.iter().map(|&a| (a, qmux_versions_for(a)))) .with_connector(connector) .with_keep_alive(qmux::ws::KeepAlive::default()) // 5s ping / 30s deadline; TCP backs off retransmits too far for QUIC's 10s + .with_session_config(crate::qmux_config()) .connect(url.as_str()) .await .map_err(Error::connect)? @@ -513,7 +516,9 @@ async fn connect_tls_override( .headers() .get(http::header::SEC_WEBSOCKET_PROTOCOL) .and_then(|value| value.to_str().ok()); - let upgraded = qmux::ws::Upgraded::new(websocket).with_keep_alive(qmux::ws::KeepAlive::default()); + let upgraded = qmux::ws::Upgraded::new(websocket) + .with_keep_alive(qmux::ws::KeepAlive::default()) + .with_session_config(crate::qmux_config()); Ok(match negotiated { Some(protocol) => upgraded.with_alpn(protocol).connect(), None => upgraded.connect(), @@ -719,7 +724,9 @@ impl Listener { .unwrap() .take() .expect("successful upgrade selected a protocol"); - let upgraded = qmux::ws::Upgraded::new(websocket).with_keep_alive(qmux::ws::KeepAlive::default()); + let upgraded = qmux::ws::Upgraded::new(websocket) + .with_keep_alive(qmux::ws::KeepAlive::default()) + .with_session_config(crate::qmux_config()); let session = match &protocol { Some(protocol) => upgraded.with_alpn(protocol).accept(), None => upgraded.accept(),