diff --git a/quest/m1/auth/README.md b/quest/m1/auth/README.md index 1cacf3f7ef7..7263a26844f 100644 --- a/quest/m1/auth/README.md +++ b/quest/m1/auth/README.md @@ -146,7 +146,6 @@ because `main` assigned 0x3A to NOT_FETCHABLE first. - [Binding auth polish](/quest/m1/auth/bindings-polish.md) - OBS reconnects with the presented token, Swift grant sequences - [Expiry before SETUP](/quest/m1/auth/expire-before-setup.md) - a mocked-clock test proves a grant expiring before SETUP closes as Expired -- [Tokens through a drain](/quest/m1/auth/drain-token.md) - a closing Rust connection keeps a draining publication's token grant ## Related diff --git a/quest/m1/auth/drain-token.md b/quest/m1/auth/drain-token.md deleted file mode 100644 index db50e368c07..00000000000 --- a/quest/m1/auth/drain-token.md +++ /dev/null @@ -1,16 +0,0 @@ -# [S] Rust keeps tokens through a drain - -## Goal - -A Rust `Connection` closed while a token-only publication drains keeps that -token's grant until the drain ends, as JS does since -[moq-dev/moq#6630](https://github.com/moq-dev/moq/pull/6630). - -## Plan - -- Suspected gap: `Connection::close` (`rs/moq-tokio/src/connection.rs`) drains - the session, but `Shared::end` aborts the shared `auth::Handle` when the - loop task ends. Write the test first; fix only if it fails, and delete this - quest if it passes. -- Carrying tokens across reconnects already works (one handle shared across - sessions). diff --git a/rs/moq-tokio/src/connection.rs b/rs/moq-tokio/src/connection.rs index aa02e163101..b045b378450 100644 --- a/rs/moq-tokio/src/connection.rs +++ b/rs/moq-tokio/src/connection.rs @@ -622,7 +622,7 @@ struct Shared { send_bw: BandwidthProducer, recv_bw: BandwidthProducer, closed: CloseGuard, - /// The auth handle every session is built on, ended with the loop. + /// The auth handle every session is built on, ended after the loop and any close drain. auth: moq_net::auth::Handle, } @@ -942,9 +942,17 @@ pub struct Connection { /// last one drops. struct Task { handle: AbortOnDrop, + /// A graceful close retains the loop's resources while its sessions drain. + running: std::sync::Weak, closed: CloseGuard, } +/// Resources owned by the loop, retained by a graceful close until its sessions drain. +struct Running { + shared: Shared, + tokens: tokens::Holder, +} + /// Serializes [`Connection::abort`] and [`Connection::close`] against the loop publishing a fresh session. /// /// Aborting a tokio task doesn't interrupt it before its next yield, so a redial @@ -983,11 +991,17 @@ impl Connection { auth: auth.clone(), }; + let running = std::sync::Arc::new(Running { + shared, + tokens: tokens::Holder::new(auth.clone(), client.tokens().consume()), + }); + let task_running = std::sync::Arc::downgrade(&running); let task = tokio::spawn(async move { + let shared = &running.shared; let reconnect = client.reconnect; // Isolated inside `shared`, so a panic still records a terminal error // instead of closing the channels as if the caller had stopped it. - let res = crate::panic::isolate(Self::run(&shared, client, addrs)) + let res = crate::panic::isolate(Self::run(shared, client, addrs, &running.tokens)) .await .unwrap_or(Err(Error::Panic)); if let Err(err) = res { @@ -999,11 +1013,12 @@ impl Connection { } shared.end(Some(err)); } - // Dropping the producers here closes the channels, signaling consumers. + // A graceful close may retain these producers until its sessions finish draining. }); Self { task: std::sync::Arc::new(Task { handle: AbortOnDrop::new(task), + running: task_running, closed, }), state, @@ -1042,6 +1057,7 @@ impl Connection { /// Stop the loop for every clone, closing the live session (and any predecessor /// still finishing after a GOAWAY) after draining its queued data. + /// Tokens stay presented until both sessions finish draining. /// /// See [`moq_net::Session::close`]: explicit lite-07-wip waits for the subscriber /// to receive finished tracks' tails; older Lite versions and IETF wait only for @@ -1050,6 +1066,9 @@ impl Connection { /// [`Client::close`], which closes the transport without waiting. Returns `Ok` /// when nothing was live, and a session's error if it did not drain. pub async fn close(self) -> crate::Result<()> { + // Abort stops redials, but the sessions still need their token grants until + // the drain completes. Dropping this owner also ends auth if close is cancelled. + let running = self.task.running.upgrade(); // Refuse redials and take the sessions under one lock: see [`CloseGuard`]. let (session, predecessor) = { let mut closed = self.task.closed.lock().unwrap(); @@ -1079,10 +1098,11 @@ impl Connection { } }; let (session, predecessor) = tokio::join!(session, predecessor); + drop(running); Ok(session.and(predecessor)?) } - async fn run(shared: &Shared, client: Client, addrs: Addrs) -> crate::Result<()> { + async fn run(shared: &Shared, client: Client, addrs: Addrs, held: &tokens::Holder) -> crate::Result<()> { let backoff = client.backoff.clone(); let goaway = client.goaway.resolve(); let pacing = Pacing::new(&backoff); @@ -1094,8 +1114,6 @@ impl Connection { let mut addrs = addrs; // An old session kept alive after a GOAWAY so its in-flight groups finish. let mut draining: Option = None; - // The configured tokens beside each dial's connection credential, held until this loop ends. - let held = tokens::Holder::new(shared.auth.clone(), client.tokens().consume()); // The current run of migrations, to tell a redirect loop from a healthy handover. let mut redirects = Redirects::default(); @@ -1111,7 +1129,7 @@ impl Connection { // Read once per dial, so a refusal is judged against what this dial presented. let tokens = client.tokens().snapshot(); - match Self::dial_any(shared, &client, &addrs, &mut draining, &tokens, &held).await { + match Self::dial_any(shared, &client, &addrs, &mut draining, &tokens, held).await { Ok((addr, dialed)) => { let url = addr.url().clone(); tracing::info!(peer = %Endpoint(&url), transport = %dialed.transport, "connected"); diff --git a/rs/moq-tokio/tests/integration/backend.rs b/rs/moq-tokio/tests/integration/backend.rs index c4d0d1de92d..d78a202392a 100644 --- a/rs/moq-tokio/tests/integration/backend.rs +++ b/rs/moq-tokio/tests/integration/backend.rs @@ -801,19 +801,20 @@ async fn quic_client_close_reaches_server() { } /// A client that finishes its track and closes before its runtime stops still delivers -/// the queued group and the track's finish, instead of the close discarding them. +/// the queued group and the track's finish, retaining its token-only grant through the drain. #[cfg(feature = "quic")] #[tracing_test::traced_test] #[tokio::test] async fn quic_client_close_drains_finished_track() { - // Small, since the debug build logging every packet is slow and the drain has - // one second. Queued right before the close, it is still unacknowledged then. - let payload: Vec = (0..1024).map(|i| i as u8).collect(); + // Several congestion windows, so the token must cover data still in flight + // after the reconnect task has stopped. + let payload: Vec = (0..64 * 1024).map(|i| i as u8).collect(); let quic = moq_tokio::quic::Config::default(); let mut server_config = moq_tokio::listen::Config::default(); server_config.bind = Some("127.0.0.1:0".parse().unwrap()); server_config.tls.generate = vec!["localhost".into()]; + server_config.version = vec!["moq-lite-07-wip".parse().unwrap()]; let server = server_config.init(quic.clone()).expect("failed to init server"); let mut server = server.listen().await.expect("failed to listen"); let url: url::Url = format!("moqt://localhost:{}", server.local_addr().unwrap().port()) @@ -822,6 +823,7 @@ async fn quic_client_close_drains_finished_track() { // The client gets a runtime of its own, gone as soon as the client returns: nothing // drives its endpoint afterwards, exactly as when a process exits. + let (closing, started) = tokio::sync::oneshot::channel(); let expected = payload.clone(); let client = std::thread::spawn(move || { let runtime = tokio::runtime::Builder::new_current_thread() @@ -838,6 +840,8 @@ async fn quic_client_close_drains_finished_track() { let mut config = moq_tokio::connect::Config::default(); config.tls.insecure = Some(true); + config.version = vec!["moq-lite-07-wip".parse().unwrap()]; + config.tokens = vec!["connection".parse().unwrap(), "publish".parse().unwrap()]; config.bind = Some("127.0.0.1:0".parse().unwrap()); let client = config .init(quic) @@ -856,24 +860,41 @@ async fn quic_client_close_drains_finished_track() { group.finish().expect("failed to finish group"); track.finish().expect("failed to finish track"); - connection.close().await.expect("the close drains"); + let mut close = Box::pin(connection.close()); + assert!(futures::FutureExt::now_or_never(close.as_mut()).is_none()); + closing.send(()).unwrap(); + close.await.expect("the close drains"); client.close().await; }); }); - let request = tokio::time::timeout(TIMEOUT, server.accept()) + let mut request = tokio::time::timeout(TIMEOUT, server.accept()) .await .expect("accept timed out") .expect("no incoming connection"); let origin = moq_tokio::origin::spawn(); let consumer = origin.consume(); let mut announcements = consumer.announced(); + request.auth().authorize(&moq_net::auth::Grant::default()); + let mut requests = request.requests(); let _session = request .with_subscriber(origin) .ok() .await .expect("server handshake failed"); + let grants = tokio::spawn(async move { + let mut issued = Vec::new(); + while let Some(request) = requests.next().await { + let grant = if request.token().as_ref() == b"publish" { + moq_net::auth::Grant::all() + } else { + moq_net::auth::Grant::default() + }; + issued.push(request.accept(grant)); + } + }); + tokio::time::timeout(TIMEOUT, next_update(&mut announcements)) .await .expect("announce timed out") @@ -887,6 +908,7 @@ async fn quic_client_close_drains_finished_track() { .expect("subscribe timed out") .expect("subscribe failed"); + started.await.expect("client started closing"); let mut group = tokio::time::timeout(TIMEOUT, track.recv_group()) .await .expect("recv_group timed out") @@ -912,6 +934,8 @@ async fn quic_client_close_drains_finished_track() { .await .unwrap() .expect("client thread panicked"); + grants.abort(); + let _ = grants.await; } /// A GOAWAY leaves the old session serving its subscriptions after the replacement