From 779913f3d059ff133c3850527d2d405c41c61969 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 14:47:41 -0700 Subject: [PATCH 1/7] quest: start immediate-close transport fallback Co-Authored-By: GPT-6 From 4eff7b20c06a938181e110537a2377821a45c3ea Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 15:08:11 -0700 Subject: [PATCH 2/7] fix(tokio): retain QUIC fallback after an early WebSocket close Co-Authored-By: GPT-6 --- rs/moq-tokio/src/client.rs | 2 +- rs/moq-tokio/src/connection.rs | 90 ++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 15 deletions(-) diff --git a/rs/moq-tokio/src/client.rs b/rs/moq-tokio/src/client.rs index 2c217564dee..bfd5ccbae10 100644 --- a/rs/moq-tokio/src/client.rs +++ b/rs/moq-tokio/src/client.rs @@ -875,7 +875,7 @@ where /// answers the fallback with 403 while QUIC is still in flight, and reconnect /// treats is_auth() as terminal, so a mixed pair reports the retryable error. #[cfg(all(feature = "websocket", feature = "noq"))] -fn race_error(quic: Error, websocket: Error) -> Error { +pub(crate) fn race_error(quic: Error, websocket: Error) -> Error { match (quic.is_auth(), websocket.is_auth()) { (false, false) => Error::TransportRace { quic: std::sync::Arc::new(quic), diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index d9915fb07c8..1a751ae86f3 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -857,7 +857,24 @@ impl Connection { // connection, and draining any predecessor left over from a migration. A QUIC // dial that lands after WebSocket won takes over here without a redial. let ended = loop { - match run_session(shared, &session, &mut draining, &mut upgrade).await { + let mut next = run_session(shared, &session, &mut draining, &mut upgrade).await; + #[cfg(all(feature = "websocket", feature = "noq"))] + if connected.elapsed() < initial + && upgrade.is_some() && let Next::Ended(Ended::Closed(result)) = next + { + // A short-lived fallback has not won the race against the QUIC + // attempt we already started. Its existing connect deadline bounds + // this wait, including in one-shot mode. + shared.migrating(); + next = match kio::wait(|waiter| poll_upgrade(shared, &mut upgrade, waiter)).await { + Ok((next, transport)) => Next::Upgraded(next, transport), + Err(quic) => Next::Ended(Ended::Closed(Err(crate::client::race_error( + quic, + result.err().unwrap_or_else(|| moq_net::Error::Cancel.into()), + )))), + }; + } + match next { Next::Ended(ended) => break ended, Next::Upgraded(next, transport) => { tracing::info!(peer = %Endpoint(&url), %transport, "upgraded from WebSocket"); @@ -977,7 +994,7 @@ impl Connection { // specified, so this is the peer telling us these credentials will // never work, not a code we guessed at. if let Ended::Closed(Err(err)) = &ended { - let err = Error::from(err.clone()); + let err = err.clone(); if err.is_auth() { return Err(err); } @@ -988,7 +1005,7 @@ impl Connection { // A GOAWAY already returned above. if !client.reconnect { return match ended { - Ended::Closed(res) => res.map_err(Error::from), + Ended::Closed(res) => res, Ended::Goaway(_) => unreachable!("handled before the migration branch"), }; } @@ -1004,7 +1021,7 @@ impl Connection { // escalate instead of spinning the CPU. This is what bounds a redirect // loop between two peers. let err = match ended { - Ended::Closed(Err(err)) => Some(Error::from(err)), + Ended::Closed(Err(err)) => Some(err), Ended::Closed(Ok(())) => None, // Handled above: a GOAWAY never reaches here. Ended::Goaway(_) => None, @@ -1342,7 +1359,7 @@ enum Next { /// Why a session stopped being the live one. enum Ended { /// The transport closed. - Closed(Result<(), moq_net::Error>), + Closed(crate::Result<()>), /// The peer sent a GOAWAY; the session is still up and serving. Goaway(moq_net::goaway::Goaway), } @@ -1491,10 +1508,18 @@ async fn run_session( } if let Poll::Ready(err) = waiter.poll_future(closed.as_mut()) { - return Poll::Ready(Next::Ended(Ended::Closed(Err(err)))); + return Poll::Ready(Next::Ended(Ended::Closed(Err(err.into())))); } - poll_upgrade(shared, upgrade, waiter).map(|(session, transport)| Next::Upgraded(session, transport)) + match poll_upgrade(shared, upgrade, waiter) { + Poll::Ready(Ok((session, transport))) => Poll::Ready(Next::Upgraded(session, transport)), + Poll::Ready(Err(err)) => { + tracing::debug!(%err, "QUIC upgrade failed; staying on WebSocket"); + shared.stayed(); + Poll::Pending + } + Poll::Pending => Poll::Pending, + } }) .await } @@ -1504,22 +1529,19 @@ fn poll_upgrade( shared: &Shared, upgrade: &mut Option, waiter: &kio::Waiter, -) -> Poll<(moq_net::Session, crate::Transport)> { +) -> Poll> { while let Some(pending) = upgrade.as_mut() { match ready!(pending.poll(waiter)) { Ok(crate::client::Step::Handshaking) => shared.migrating(), Ok(crate::client::Step::Session(session)) => shared.upgrading(Some(&session)), Ok(crate::client::Step::Done(session, transport)) => { *upgrade = None; - return Poll::Ready((session, transport)); + return Poll::Ready(Ok((session, transport))); } Err(err) => { - // Only the Handshaking stage moved the status, but restoring it is harmless - // either way: this session was serving throughout. - tracing::debug!(%err, "QUIC upgrade failed; staying on WebSocket"); - shared.stayed(); shared.upgrading(None); *upgrade = None; + return Poll::Ready(Err(err)); } } } @@ -2506,7 +2528,13 @@ mod tests { assert!(upgrade.is_some()); fail.send(()).unwrap(); - assert!(poll_upgrade(&shared, &mut upgrade, &waiter).is_pending()); + assert!(matches!( + poll_upgrade(&shared, &mut upgrade, &waiter), + Poll::Ready(Err(Error::ConnectFailed)) + )); + // The caller decides whether a live WebSocket stays or an ended arm loses + // the transport race. Polling the upgrade itself only reports the failure. + shared.stayed(); assert_eq!(shared.state.consume().read().status, Some(Status::Connected)); assert!(upgrade.is_none(), "a failed upgrade is not retried"); } @@ -2803,6 +2831,40 @@ mod tests { ); } + /// One-shot proves this reuses the held dial rather than redialing QUIC. + #[cfg(all(feature = "websocket", feature = "noq"))] + #[tokio::test] + async fn closed_websocket_session_falls_back_to_pending_quic() { + for error in [moq_net::Error::Cancel, moq_net::Error::Unauthorized] { + let origin = crate::origin::spawn(); + let mut fallback = Fallback::start(&origin, Quic::Held).await; + let mut config = crate::connect::Config::default(); + config.tls.insecure = Some(true); + let client = config.init(Default::default()).unwrap().with_reconnect(false); + let mut connection = tokio::time::timeout(UPGRADE_WAIT, client.connect(fallback.url.clone()).established()) + .await + .expect("never connected") + .unwrap(); + assert_eq!(connection.transport(), Some(crate::Transport::WebSocket)); + let (_, websocket) = fallback.accept().await; + websocket.abort(error.clone()); + let _ = websocket.closed().await; + fallback.forwarder.open(); + loop { + if connection.transport() == Some(crate::Transport::WebTransport) { + break; + } + tokio::time::timeout(UPGRADE_WAIT, connection.status()) + .await + .expect("pending QUIC dial never took over") + .expect("connection ended before pending QUIC dial"); + } + let (transport, _) = fallback.accept().await; + assert_eq!(transport, crate::Transport::WebTransport); + assert_eq!(connection.epoch(), 2); + } + } + /// When QUIC wins the race nothing changes: one session, over QUIC, and no /// WebSocket session is ever opened. #[cfg(all(feature = "websocket", feature = "noq"))] From eb795b8f1bdef858a00bcfaf2621821e9288847c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 15:10:18 -0700 Subject: [PATCH 3/7] quest: retire closed fallback after validated fix Co-Authored-By: GPT-6 --- quest/m1/transport-upgrade/README.md | 5 +++- quest/m1/transport-upgrade/closed-fallback.md | 30 ------------------- 2 files changed, 4 insertions(+), 31 deletions(-) delete mode 100644 quest/m1/transport-upgrade/closed-fallback.md diff --git a/quest/m1/transport-upgrade/README.md b/quest/m1/transport-upgrade/README.md index b46b5c8f656..9a15fbd2852 100644 --- a/quest/m1/transport-upgrade/README.md +++ b/quest/m1/transport-upgrade/README.md @@ -70,4 +70,7 @@ Shared decisions, which Rust implements and [JavaScript](/quest/m1/transport-upg ## Required - [JavaScript](/quest/m1/transport-upgrade/js.md) - js/net keeps the WebTransport dial after WebSocket wins and migrates through the client-goaway handover -- [Closed fallback](/quest/m1/transport-upgrade/closed-fallback.md) - a WebSocket session that closes right after connecting falls back to the pending QUIC dial instead of redialing + +## Related + +- [Closed fallback](https://github.com/moq-dev/moq/pull/5284) - a WebSocket session that closes right after connecting falls back to the pending QUIC dial instead of redialing diff --git a/quest/m1/transport-upgrade/closed-fallback.md b/quest/m1/transport-upgrade/closed-fallback.md deleted file mode 100644 index 8df94079c1c..00000000000 --- a/quest/m1/transport-upgrade/closed-fallback.md +++ /dev/null @@ -1,30 +0,0 @@ -# [S] Fall back to QUIC when the WebSocket session closes at once - -## Goal - -When WebSocket wins the dial race and its session closes right after -connecting, the `Connection` moves onto the QUIC dial that is still pending -instead of dropping it and redialing. On moq-lite and IETF draft 17+ the -client handshake does not wait for the server, so a server refusing the -WebSocket session looks like this rather than like a handshake failure, which -already falls back. - -## Plan - -- Today `run_session` in `moq-tokio`'s `connection.rs` returns when the - WebSocket session closes, and the pending upgrade is dropped. The redial - starts a fresh QUIC dial and has lost QUIC's head start. -- Decide what counts as "at once". The unhealthy-session threshold that - already feeds the backoff may be the right line. Keep the connect deadline - that bounds the pending dial as the only deadline. -- An auth close on the WebSocket session is probably still terminal, the way - it is for a lone session. Check how the race treats a mixed auth pair and - match it. -- Test with the held-QUIC forwarder in the connection tests, as the handshake - fallback test does, with a default moq-lite client. - -Public API: none. Wire: none. - -## Related - -- [#4296](https://github.com/moq-dev/moq/pull/4296) - the handshake-failure fallback this extends From 7c11538fdbc326865a21f4d8a7bc435d04a3cb89 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 20:16:00 -0700 Subject: [PATCH 4/7] fix(tokio): retire closed fallback state while finishing QUIC Keep the pending upgrade abortable, enforce predecessor drain deadlines, and exclude disconnected dial time from retry health accounting. Make the QUIC-winner fixture deterministic across host load. Co-Authored-By: GPT-6 Astra --- rs/moq-tokio/src/connection.rs | 233 ++++++++++++++++++++++++--------- 1 file changed, 174 insertions(+), 59 deletions(-) diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index 1a751ae86f3..785d637ad0c 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -533,7 +533,12 @@ impl Shared { /// The session in [`State`] keeps serving while its replacement comes up, so /// only the status moves. fn migrating(&self) { - self.status(Status::Migrating); + // A closed fallback stays disconnected while its pending dial finishes. + if let Ok(mut state) = self.state.write() + && state.status != Some(Status::Disconnected) + { + state.status = Some(Status::Migrating); + } } /// The replacement did not come up after all: the session in [`State`] was @@ -550,7 +555,7 @@ impl Shared { /// Nothing is live. Clears the session so a handle can't be handed a closed one, /// and drops the estimates that belonged to it. - fn disconnected(&self) { + fn clear_session(&self) { if let Ok(mut state) = self.state.write() { // Count one close per live session; a second call without an // intervening connect (e.g. Drop after the loop already reported @@ -562,12 +567,17 @@ impl Shared { state.version = None; state.transport = None; state.session = None; - state.upgrade = None; } let _ = self.send_bw.set(None); let _ = self.recv_bw.set(None); } + /// No live session or pending upgrade remains. + fn disconnected(&self) { + self.clear_session(); + self.upgrading(None); + } + /// Record the error the loop gave up with, for [`Connection::closed`]. fn failed(&self, err: Error) { if let Ok(mut state) = self.state.write() { @@ -856,26 +866,29 @@ impl Connection { // persistent producers meanwhile so consumers track the live stats across the // connection, and draining any predecessor left over from a migration. A QUIC // dial that lands after WebSocket won takes over here without a redial. - let ended = loop { - let mut next = run_session(shared, &session, &mut draining, &mut upgrade).await; + let (ended, tenure) = loop { + let next = run_session(shared, &session, &mut draining, &mut upgrade).await; + let tenure = connected.elapsed(); #[cfg(all(feature = "websocket", feature = "noq"))] - if connected.elapsed() < initial + let next = if tenure < initial && upgrade.is_some() && let Next::Ended(Ended::Closed(result)) = next { // A short-lived fallback has not won the race against the QUIC // attempt we already started. Its existing connect deadline bounds // this wait, including in one-shot mode. - shared.migrating(); - next = match kio::wait(|waiter| poll_upgrade(shared, &mut upgrade, waiter)).await { + shared.clear_session(); + match finish_upgrade(shared, &mut upgrade, &mut draining).await { Ok((next, transport)) => Next::Upgraded(next, transport), Err(quic) => Next::Ended(Ended::Closed(Err(crate::client::race_error( quic, result.err().unwrap_or_else(|| moq_net::Error::Cancel.into()), )))), - }; - } + } + } else { + next + }; match next { - Next::Ended(ended) => break ended, + Next::Ended(ended) => break (ended, tenure), Next::Upgraded(next, transport) => { tracing::info!(peer = %Endpoint(&url), %transport, "upgraded from WebSocket"); // UDP gets through after all, so the next dial gives QUIC its head start. @@ -900,7 +913,7 @@ impl Connection { // Score the WebSocket tenure like any session's, so a QUIC session that // drops right away retries in a fresh window, not one that opened // before WebSocket served. - retry.settle(connected, initial); + retry.settle(tenure, initial); connected = tokio::time::Instant::now(); } } @@ -910,7 +923,7 @@ impl Connection { // A session that stayed up past the initial backoff is healthy; one that // ended sooner counts as a failed attempt however it ended. - let healthy = retry.settle(connected, initial); + let healthy = retry.settle(tenure, initial); // The connected target owns the policy, including in one-shot mode. A // refused redirect is terminal: the peer is leaving and named somewhere we @@ -1318,10 +1331,10 @@ impl Retry { } } - /// Score a session live since `connected`: one that outlived `initial` was healthy, - /// so the window starts over. Returns whether it was healthy. - fn settle(&mut self, connected: tokio::time::Instant, initial: Duration) -> bool { - let healthy = connected.elapsed() >= initial; + /// Score captured live time, excluding any wait after the session closed. + /// A tenure past `initial` is healthy and starts a fresh retry window. + fn settle(&mut self, tenure: Duration, initial: Duration) -> bool { + let healthy = tenure >= initial; if healthy { *self = Self::new(initial); } @@ -1474,7 +1487,7 @@ async fn sleep_draining(delay: Duration, draining: &mut Option, shared /// `upgrade` is a QUIC dial still in flight after the WebSocket fallback won. It reports /// [`Status::Migrating`] once the QUIC transport is up and returns [`Next::Upgraded`] once /// the peer's SETUP arrives. A failure leaves this session serving. It is dropped with -/// the session otherwise: the redial races QUIC again. +/// a healthy session otherwise; an early close still finishes the pending dial. async fn run_session( shared: &Shared, session: &moq_net::Session, @@ -1524,6 +1537,20 @@ async fn run_session( .await } +/// Finish a dial after its fallback closes, still enforcing the predecessor's handover cap. +#[cfg(all(feature = "websocket", feature = "noq"))] +async fn finish_upgrade( + shared: &Shared, + upgrade: &mut Option, + draining: &mut Option, +) -> crate::Result<(moq_net::Session, crate::Transport)> { + kio::wait(|waiter| { + poll_draining(draining, waiter); + poll_upgrade(shared, upgrade, waiter) + }) + .await +} + /// Drive a pending upgrade, `Ready` with the QUIC session once the peer's SETUP arrives. fn poll_upgrade( shared: &Shared, @@ -2005,14 +2032,33 @@ mod tests { let websocket = tokio::time::Instant::now(); tokio::time::advance(TIMEOUT * 2).await; - assert!(retry.settle(websocket, INITIAL)); + assert!(retry.settle(websocket.elapsed(), INITIAL)); let quic = tokio::time::Instant::now(); tokio::time::advance(INITIAL / 2).await; - assert!(!retry.settle(quic, INITIAL)); + assert!(!retry.settle(quic.elapsed(), INITIAL)); assert!(retry_wait(retry.delay, retry.start, TIMEOUT).is_some()); } + /// Waiting after a short-lived session closes must not turn it into a healthy tenure. + #[tokio::test(start_paused = true)] + async fn pending_dial_time_does_not_reset_the_retry_window() { + const INITIAL: Duration = Duration::from_secs(1); + const TIMEOUT: Duration = Duration::from_secs(10); + let mut retry = Retry::new(INITIAL); + let connected = tokio::time::Instant::now(); + tokio::time::advance(INITIAL / 2).await; + let tenure = connected.elapsed(); + assert!(tenure < INITIAL); + // The fallback is already closed while the original dial is still pending. + tokio::time::advance(TIMEOUT * 2).await; + assert!( + !retry.settle(tenure, INITIAL), + "pending dial time made a closed session healthy" + ); + assert!(retry_wait(retry.delay, retry.start, TIMEOUT).is_none()); + } + #[test] fn test_backoff_default() { let backoff = Backoff::default(); @@ -2307,6 +2353,8 @@ mod tests { accepted: tokio::sync::mpsc::UnboundedReceiver<(crate::Transport, moq_net::Session)>, /// QUIC requests held unanswered in [`Quic::Refused`] mode. held: tokio::sync::mpsc::UnboundedReceiver, + /// Keep the fallback bound but unaccepted when the test requires QUIC to win. + _websocket: Option, } /// When a [`Fallback`] lets QUIC through. @@ -2351,9 +2399,10 @@ mod tests { // With no MoQ server behind it, the fallback's session gets as far as the MoQ // handshake and no further. - let (websocket, failing) = match quic { - Quic::AfterWebSocketFails => (None, Some(websocket)), - _ => (Some(websocket), None), + let (websocket, failing, held_websocket) = match quic { + Quic::Open => (None, None, Some(websocket)), + Quic::AfterWebSocketFails => (None, Some(websocket), None), + _ => (Some(websocket), None, None), }; let config = crate::server::Config { @@ -2400,6 +2449,7 @@ mod tests { forwarder, accepted, held, + _websocket: held_websocket, } } @@ -2501,6 +2551,38 @@ mod tests { group.sequence } + /// A replacement's pending dial cannot extend an older session's handover cap. + #[cfg(all(feature = "tcp", feature = "websocket", feature = "noq"))] + #[tokio::test] + async fn closed_fallback_keeps_the_predecessor_deadline() { + let (mut server, url) = live().await; + let client = client(); + let (connection, request) = tokio::join!(client.connect(url).established(), server.accept()); + let connection = connection.unwrap(); + let _peer = request.unwrap().ok().await.unwrap(); + let session = connection.state.read().session.clone().unwrap(); + // Real sockets establish the fixture; only the handover timer is under test. + tokio::time::pause(); + let shared = shared(); + let mut draining = Some(Draining::new(session, Duration::from_secs(1), &shared.state)); + let mut upgrade = Some(crate::client::Upgrade::new( + Box::pin(std::future::pending()), + crate::Transport::WebTransport, + )); + let deadline = draining.as_ref().unwrap().deadline.deadline(); + let waiter = kio::Waiter::noop(); + let mut finishing = std::pin::pin!(finish_upgrade(&shared, &mut upgrade, &mut draining)); + assert!(waiter.poll_future(finishing.as_mut()).is_pending()); + assert!(shared.state.consume().read().predecessor.is_some()); + // Await the paused clock's timer driver, not only its clock adjustment. + tokio::time::sleep_until(deadline).await; + assert!(waiter.poll_future(finishing.as_mut()).is_pending()); + assert!( + shared.state.consume().read().predecessor.is_none(), + "pending QUIC kept the predecessor past its cap" + ); + } + /// The upgrade reports [`Status::Migrating`] while the MoQ handshake runs on the /// QUIC session, and a failure there puts the status back: the WebSocket session /// served throughout. @@ -2720,44 +2802,60 @@ mod tests { #[cfg(all(feature = "websocket", feature = "noq"))] #[tokio::test] async fn abort_ends_an_upgrade_in_flight() { - let origin = crate::origin::spawn(); - let mut fallback = Fallback::start(&origin, Quic::Refused).await; - - let mut config = crate::connect::Config::default(); - config.tls.insecure = Some(true); - let client = config - .init(Default::default()) - .unwrap() - .with_subscriber(crate::origin::spawn()); - let mut connection = tokio::time::timeout(UPGRADE_WAIT, client.connect(fallback.url.clone()).established()) - .await - .expect("never connected") - .unwrap(); - assert_eq!(connection.transport(), Some(crate::Transport::WebSocket)); - let _websocket = fallback.accept().await; + for close_fallback in [false, true] { + let origin = crate::origin::spawn(); + let mut fallback = Fallback::start(&origin, Quic::Refused).await; - // The QUIC transport comes up and its handshake waits on the server's answer. - fallback.forwarder.open(); - let request = fallback.held().await; - let status = tokio::time::timeout(UPGRADE_WAIT, connection.status()).await.unwrap(); - assert_eq!(status.unwrap(), Status::Migrating); + let mut config = crate::connect::Config::default(); + config.tls.insecure = Some(true); + let client = config + .init(Default::default()) + .unwrap() + .with_subscriber(crate::origin::spawn()); + let mut connection = tokio::time::timeout(UPGRADE_WAIT, client.connect(fallback.url.clone()).established()) + .await + .expect("never connected") + .unwrap(); + assert_eq!(connection.transport(), Some(crate::Transport::WebSocket)); + let (_, websocket) = fallback.accept().await; - connection.abort(moq_net::Error::GoawayTimeout); - let closed = async { - match request.ok().await { - Ok(session) => session.closed().await, - Err(err) => panic!("the server could not answer the upgrade: {err}"), + // The QUIC transport comes up and its handshake waits on the server's answer. + fallback.forwarder.open(); + let request = fallback.held().await; + let status = tokio::time::timeout(UPGRADE_WAIT, connection.status()).await.unwrap(); + assert_eq!(status.unwrap(), Status::Migrating); + + if close_fallback { + let mut monitor = connection.monitor(); + websocket.abort(moq_net::Error::Cancel); + loop { + let presence = tokio::time::timeout(UPGRADE_WAIT, monitor.presence_changed()) + .await + .expect("closed fallback remains live") + .unwrap(); + if presence.sessions_ended == 1 { + break; + } + } + assert_eq!(connection.transport(), None); } - }; - let err = tokio::time::timeout(UPGRADE_WAIT, closed) - .await - .expect("the upgrade's connection was left open"); - let code = |err: &moq_net::Error| moq_net::SessionError::from(err).to_code(); - assert_eq!( - code(&err), - code(&moq_net::Error::GoawayTimeout), - "the upgrade ended without the abort's error: {err}" - ); + connection.abort(moq_net::Error::GoawayTimeout); + let closed = async { + match request.ok().await { + Ok(session) => session.closed().await, + Err(err) => panic!("the server could not answer the upgrade: {err}"), + } + }; + let err = tokio::time::timeout(UPGRADE_WAIT, closed) + .await + .expect("the upgrade's connection was left open"); + let code = |err: &moq_net::Error| moq_net::SessionError::from(err).to_code(); + assert_eq!( + code(&err), + code(&moq_net::Error::GoawayTimeout), + "the upgrade ended without the abort's error: {err}" + ); + } } /// Closing while a QUIC upgrade is still in its MoQ handshake ends the upgrade too, and @@ -2849,6 +2947,20 @@ mod tests { let (_, websocket) = fallback.accept().await; websocket.abort(error.clone()); let _ = websocket.closed().await; + loop { + let status = tokio::time::timeout(UPGRADE_WAIT, connection.status()) + .await + .expect("closed fallback never reported its pending dial") + .expect("connection ended before pending QUIC dial"); + if status == Status::Disconnected { + break; + } + } + assert_eq!(connection.transport(), None, "the closed fallback is no longer live"); + assert!( + connection.monitor().stats().is_none(), + "closed fallback stats remain visible" + ); fallback.forwarder.open(); loop { if connection.transport() == Some(crate::Transport::WebTransport) { @@ -2880,6 +2992,9 @@ mod tests { let subscriber = crate::origin::spawn(); let mut config = crate::connect::Config::default(); config.tls.insecure = Some(true); + // Both dials start now; the fixture holds the fallback handshake so load + // cannot decide which transport this test exercises. + config.websocket.delay = Duration::ZERO; let client = config .init(Default::default()) .unwrap() @@ -2902,7 +3017,7 @@ mod tests { group.finish().unwrap(); assert_eq!(next_group(&mut sub).await, 0); - // QUIC's win dropped the fallback before it dialed, so there is nothing else to + // QUIC's win dropped the fallback before it handshook, so there is nothing else to // accept and nothing to upgrade from. assert!(fallback.accepted.try_recv().is_err(), "a second session was opened"); assert_eq!(connection.epoch(), 1); From b6836d5e345c6d27497e02db6f10dd8ace700df3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 10:54:16 -0700 Subject: [PATCH 5/7] fix(tokio): install QUIC without draining a closed fallback A closed WebSocket fallback went through the ordinary upgrade handover, which sent it GOAWAY and published it as the predecessor. A close racing the takeover could then return the dead session's error. finish_upgrade now installs the pending QUIC session itself with nothing to drain. Co-Authored-By: Claude Opus 5.5 --- rs/moq-tokio/src/connection.rs | 122 ++++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 31 deletions(-) diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index 785d637ad0c..3d693cecfc3 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -869,32 +869,22 @@ impl Connection { let (ended, tenure) = loop { let next = run_session(shared, &session, &mut draining, &mut upgrade).await; let tenure = connected.elapsed(); - #[cfg(all(feature = "websocket", feature = "noq"))] - let next = if tenure < initial - && upgrade.is_some() && let Next::Ended(Ended::Closed(result)) = next - { + let transport = match next { // A short-lived fallback has not won the race against the QUIC // attempt we already started. Its existing connect deadline bounds // this wait, including in one-shot mode. - shared.clear_session(); - match finish_upgrade(shared, &mut upgrade, &mut draining).await { - Ok((next, transport)) => Next::Upgraded(next, transport), - Err(quic) => Next::Ended(Ended::Closed(Err(crate::client::race_error( - quic, - result.err().unwrap_or_else(|| moq_net::Error::Cancel.into()), - )))), + #[cfg(all(feature = "websocket", feature = "noq"))] + Next::Ended(Ended::Closed(closed)) if tenure < initial && upgrade.is_some() => { + match finish_upgrade(shared, &mut upgrade, &mut draining, closed).await { + Ok((next, transport)) => { + session = next; + transport + } + Err(err) => break (Ended::Closed(Err(err)), tenure), + } } - } else { - next - }; - match next { Next::Ended(ended) => break (ended, tenure), Next::Upgraded(next, transport) => { - tracing::info!(peer = %Endpoint(&url), %transport, "upgraded from WebSocket"); - // UDP gets through after all, so the next dial gives QUIC its head start. - #[cfg(feature = "websocket")] - crate::websocket::forget(&url); - // The same handover as a peer's GOAWAY, initiated by us: the new session // is live, and the old one serves until its routes splice over at a group // boundary or the cap closes it. @@ -910,13 +900,18 @@ impl Connection { old.retire(); } draining = Some(Draining::new(old, goaway.handover, &shared.state)); - // Score the WebSocket tenure like any session's, so a QUIC session that - // drops right away retries in a fresh window, not one that opened - // before WebSocket served. - retry.settle(tenure, initial); - connected = tokio::time::Instant::now(); + transport } - } + }; + tracing::info!(peer = %Endpoint(&url), %transport, "upgraded from WebSocket"); + // UDP gets through after all, so the next dial gives QUIC its head start. + #[cfg(feature = "websocket")] + crate::websocket::forget(&url); + // Score the WebSocket tenure like any session's, so a QUIC session that + // drops right away retries in a fresh window, not one that opened + // before WebSocket served. + retry.settle(tenure, initial); + connected = tokio::time::Instant::now(); }; // Any upgrade still coming up is dropped with the session it was replacing. shared.upgrading(None); @@ -1537,18 +1532,33 @@ async fn run_session( .await } -/// Finish a dial after its fallback closes, still enforcing the predecessor's handover cap. +/// Finish a closed fallback's pending dial, still enforcing the predecessor's handover +/// cap, and install it as the live session. Nothing serves in between, so unlike an +/// upgrade there is no fallback to send GOAWAY or drain. A failed dial folds both +/// arms' errors as the original race does. #[cfg(all(feature = "websocket", feature = "noq"))] async fn finish_upgrade( shared: &Shared, upgrade: &mut Option, draining: &mut Option, + closed: crate::Result<()>, ) -> crate::Result<(moq_net::Session, crate::Transport)> { - kio::wait(|waiter| { + shared.clear_session(); + let finished = kio::wait(|waiter| { poll_draining(draining, waiter); poll_upgrade(shared, upgrade, waiter) }) - .await + .await; + match finished { + Ok((session, transport)) => { + shared.connected(&session, transport); + Ok((session, transport)) + } + Err(quic) => Err(crate::client::race_error( + quic, + closed.err().unwrap_or_else(|| moq_net::Error::Cancel.into()), + )), + } } /// Drive a pending upgrade, `Ready` with the QUIC session once the peer's SETUP arrives. @@ -2571,7 +2581,7 @@ mod tests { )); let deadline = draining.as_ref().unwrap().deadline.deadline(); let waiter = kio::Waiter::noop(); - let mut finishing = std::pin::pin!(finish_upgrade(&shared, &mut upgrade, &mut draining)); + let mut finishing = std::pin::pin!(finish_upgrade(&shared, &mut upgrade, &mut draining, Ok(()))); assert!(waiter.poll_future(finishing.as_mut()).is_pending()); assert!(shared.state.consume().read().predecessor.is_some()); // Await the paused clock's timer driver, not only its clock adjustment. @@ -2583,6 +2593,51 @@ mod tests { ); } + /// A closed fallback's dial takes over with nothing to drain, so a close racing + /// the takeover cannot wait on the dead fallback and return its error. + #[cfg(all(feature = "tcp", feature = "websocket", feature = "noq"))] + #[tokio::test] + async fn closed_fallback_takes_over_without_a_predecessor() { + let (mut server, url) = live().await; + let client = client().with_reconnect(false); + let mut held = Vec::new(); + let mut sessions = Vec::new(); + for _ in 0..2 { + let (connection, request) = tokio::join!(client.connect(url.clone()).established(), server.accept()); + let connection = connection.unwrap(); + sessions.push(connection.state.read().session.clone().unwrap()); + held.push((connection, request.unwrap().ok().await.unwrap())); + } + let quic = sessions.pop().unwrap(); + let fallback = sessions.pop().unwrap(); + + let shared = shared(); + shared.connected(&fallback, crate::Transport::WebSocket); + fallback.abort(moq_net::Error::Unauthorized); + let handshake: crate::client::Handshake = Box::pin(async move { Ok(quic) }); + let mut upgrade = Some(crate::client::Upgrade::new( + Box::pin(async move { Ok(handshake) }), + crate::Transport::WebTransport, + )); + let mut draining = None; + let closed = Err(moq_net::Error::Unauthorized.into()); + let (_, transport) = finish_upgrade(&shared, &mut upgrade, &mut draining, closed) + .await + .unwrap(); + assert_eq!(transport, crate::Transport::WebTransport); + + // The state a close woken by `Connected` reads. + let state = shared.state.consume(); + let state = state.read(); + assert_eq!(state.status, Some(Status::Connected)); + assert_eq!(state.transport, Some(crate::Transport::WebTransport)); + assert!( + state.predecessor.is_none(), + "the closed fallback was published as a predecessor" + ); + assert!(draining.is_none()); + } + /// The upgrade reports [`Status::Migrating`] while the MoQ handshake runs on the /// QUIC session, and a failure there puts the status back: the WebSocket session /// served throughout. @@ -2971,9 +3026,14 @@ mod tests { .expect("pending QUIC dial never took over") .expect("connection ended before pending QUIC dial"); } - let (transport, _) = fallback.accept().await; + let (transport, _quic) = fallback.accept().await; assert_eq!(transport, crate::Transport::WebTransport); assert_eq!(connection.epoch(), 2); + // The dead fallback's error does not leak into closing the QUIC session. + tokio::time::timeout(UPGRADE_WAIT, connection.close()) + .await + .expect("close never finished") + .expect("close returned the closed fallback's error"); } } From 693f1e4dd9ae4f4dd3339428098ee1fdbb3fd693 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 17:55:08 -0700 Subject: [PATCH 6/7] test(tokio): use Connection::connects after the epoch rename Co-Authored-By: Claude Opus 5.5 --- rs/moq-tokio/src/connection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index 52c001529ab..eb6b01c2514 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -3110,7 +3110,7 @@ mod tests { } let (transport, _quic) = fallback.accept().await; assert_eq!(transport, crate::Transport::WebTransport); - assert_eq!(connection.epoch(), 2); + assert_eq!(connection.connects(), 2); // The dead fallback's error does not leak into closing the QUIC session. tokio::time::timeout(UPGRADE_WAIT, connection.close()) .await From 931a2c93aed1472b849f2184bddd2254e69c7a64 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 12 Oct 2026 17:56:39 -0700 Subject: [PATCH 7/7] quest: repoint links to the finished closed-fallback quest Co-Authored-By: Claude Opus 5.5 --- quest/m1/initial-catalog-routing.md | 1 - quest/m1/ts-same-instance-resume.md | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/quest/m1/initial-catalog-routing.md b/quest/m1/initial-catalog-routing.md index a747ec0997e..e0e4bb2306c 100644 --- a/quest/m1/initial-catalog-routing.md +++ b/quest/m1/initial-catalog-routing.md @@ -54,7 +54,6 @@ decision rather than an invented retry or compatibility layer. ## Related - [Validation PR #5328](https://github.com/moq-dev/moq/pull/5328) - records the process failures and successful PR CI baseline -- [Closed fallback](/quest/m1/transport-upgrade/closed-fallback.md) - #5284 owns the pending QUIC dial after an early WebSocket close - [Fallback PR #5284](https://github.com/moq-dev/moq/pull/5284) - check its actual effect on this reproduction before duplicating work - [Transport upgrade](/quest/m1/transport-upgrade/README.md) - existing handover and epoch contracts - [TS duration fidelity](/quest/m0/ts-duration-fidelity.md) - previously captured only a short suffix, not zero-packet initial acquisition diff --git a/quest/m1/ts-same-instance-resume.md b/quest/m1/ts-same-instance-resume.md index fbab9ce0504..e596d48f915 100644 --- a/quest/m1/ts-same-instance-resume.md +++ b/quest/m1/ts-same-instance-resume.md @@ -61,5 +61,5 @@ required behavior or API change returns to the maintainer for a decision. - [Live clock validation](/quest/m1/ts-live-clock.md) - independent local live failure - [Replacement catalog linger](/quest/m1/ts-follow-catalog-bound.md) - #5305 owns the bound on a mid-stream switch's first catalog - [Same-epoch importers](/quest/m1/hop-aligned-import.md) - shared catalogs, tracks and source-derived group identity -- [Closed fallback](/quest/m1/transport-upgrade/closed-fallback.md) - #5284 owns early WebSocket closure and its transport-test fix +- [Closed fallback #5284](https://github.com/moq-dev/moq/pull/5284) - early WebSocket closure and its transport-test fix - [TS duration fidelity](/quest/m0/ts-duration-fidelity.md) - diagnose a shared end-of-stream cause only if reproduced