diff --git a/quest/m1/lite07-session-tasks.md b/quest/m1/lite07-session-tasks.md deleted file mode 100644 index 8d96e3f19d6..00000000000 --- a/quest/m1/lite07-session-tasks.md +++ /dev/null @@ -1,28 +0,0 @@ -# [M] A lite-07 session leaves no task behind - -## Goal - -Once a lite-07 session's handles and connection close, every task it spawned -ends, as lite-06's do. The session bench (`rs/moq-net/benches/session`) -asserts zero leftover tasks after each room drops, for both versions, so a -relay speaking lite-07 no longer accumulates one task per session it served. - -## Plan - -Planned 2026-11-10 (maintainer, /quest-complete follow-up of -[moq-dev/moq#8397](https://github.com/moq-dev/moq/pull/8397)). Milestone: -✅ m1 (rejected: m2 routes epic, m0, dropping it), since relays run lite-07 -in production now. - -#8397's `session_delivery_broadcasts` bench found 18 tasks still alive in the -16-viewer delivery cell after a lite-07 room dropped, against 0 on lite-06: -about one per session. The per-cell runtimes from -[moq-dev/moq#8352](https://github.com/moq-dev/moq/pull/8352) hid this. The -likely cause is a lite-07 session driver (or a route-layer task it spawns) -that outlives its handles; confirm before fixing. - -Reproduce on the bench first, then in a unit test on mock time. Fix the -owner of the leaked task at its root rather than aborting stragglers, and -keep the zero-task assertion in the bench for both versions. - -Public API: none expected. Wire: none. diff --git a/rs/moq-net/benches/session/mod.rs b/rs/moq-net/benches/session/mod.rs index f484d8f4c5b..193750f4b44 100644 --- a/rs/moq-net/benches/session/mod.rs +++ b/rs/moq-net/benches/session/mod.rs @@ -430,6 +430,27 @@ fn runtime() -> tokio::runtime::Runtime { .unwrap() } +/// Drop what a cell built on `rt`, if Criterion's filter ran it, and assert every task its +/// sessions and origins spawned ends with it: a task that outlives its handles leaks for +/// as long as a relay runs. +fn retire(rt: &tokio::runtime::Runtime, built: Option) { + let Some(built) = built else { + return; + }; + drop(built); + // Each close reaches the next driver on its following poll; this many passes is far + // more than any teardown chain takes. + rt.block_on(async { + for _ in 0..1024 { + if rt.metrics().num_alive_tasks() == 0 { + break; + } + tokio::task::yield_now().await; + } + }); + assert_eq!(rt.metrics().num_alive_tasks(), 0, "tasks outlived the cell's handles"); +} + /// A multi-threaded runtime with `workers` threads, for the scenarios timed in process CPU. fn threaded(workers: usize) -> tokio::runtime::Runtime { tokio::runtime::Builder::new_multi_thread() @@ -1232,6 +1253,7 @@ fn delivery(c: &mut Criterion, name: &str, shapes: impl IntoIterator let room = room.get_or_insert_with(|| rt.block_on(Room::::new(version, *shape))); b.iter_custom(|iters| rt.block_on(room.measure(iters))) }); + retire(&rt, room); } } group.finish(); @@ -1477,6 +1499,7 @@ fn join(c: &mut Criterion, name: &str, shapes: impl IntoIterator(c: &mut Criterion) { }) }) }); + retire(&rt, room); } } group.finish(); @@ -1668,6 +1692,7 @@ fn dash(c: &mut Criterion, name: &str, shapes: impl IntoIterator::new(version, *shape))); b.iter_custom(|iters| rt.block_on(room.measure(iters))) }); + retire(&rt, room); } } group.finish(); @@ -1846,6 +1871,7 @@ fn churn(c: &mut Criterion, name: &str, shapes: impl IntoIterator::new(version, *shape))); b.iter_custom(|iters| rt.block_on(room.measure(iters))) }); + retire(&rt, room); } } group.finish(); diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 40bc97f0f83..3417a7a3d75 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -3791,8 +3791,6 @@ impl Drop for AnnounceProducer { } } -/// Drives origin lifecycle work and cache expiration with caller-supplied time. -/// /// Feeds a route layer node the routes this node serves itself, best per prefix, /// for as long as the origin lives. struct OwnClaims { @@ -3807,16 +3805,26 @@ struct OwnClaims { } /// The driver's task feeding [`OwnClaims`], which [`Consumer::route_node`] also catches up. -struct FeedOwnClaims(Arc>); +/// It ends once nothing owns the origin, after feeding the withdrawals still queued: its +/// cursors end only in the driver's teardown, which waits for this task. +struct FeedOwnClaims { + claims: Arc>, + origin: TasksWeak, +} impl kio::Task for FeedOwnClaims { type Output = (); fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { - // Wakes wait for the feeder's lock, which a tail expression's guard drops before - // the hold; see `Consumer::route_node`. + // Wakes wait for the feeder's lock, which drops before the hold; see + // `Consumer::route_node`. let _hold = kio::hold(); - lock_own(&self.0).poll(waiter) + let mut claims = lock_own(&self.claims); + if self.origin.poll_orphaned(waiter).is_ready() { + claims.catch_up(); + return Poll::Ready(()); + } + claims.poll(waiter) } } @@ -3924,6 +3932,8 @@ impl OwnClaims { } } +/// Drives origin lifecycle work and cache expiration with caller-supplied time. +/// /// Returned by [`Producer::new`]. Poll on external activity or at the deadline /// it returns, supplying nondecreasing instants. RouteInfo changes, track serving, linger, /// failover, and teardown run here; the route table and announce cursors update @@ -7407,7 +7417,10 @@ impl Consumer { let created = (node, Arc::downgrade(&claims)); shared.route_node = Some(created.clone()); drop(shared); - self.tasks.push(FeedOwnClaims(claims)); + self.tasks.push(FeedOwnClaims { + claims, + origin: self.tasks.clone(), + }); created } @@ -15786,6 +15799,39 @@ mod tests { assert!(matches!(driver.poll(Instant::now(), &waiter), Err(Error::Closed))); } + /// The task feeding a route node never keeps the driver running: it finishes once + /// the last owner drops, whatever read handles remain, and still feeds the node the + /// withdrawals that last drop queued. + #[kio::test] + async fn a_route_node_never_keeps_the_driver_running() { + let (producer, mut driver) = Producer::new(Config::from(origin(1))); + let waiter = kio::Waiter::noop(); + let link = crate::route::Link { + proxy: true, + ..Default::default() + }; + let consumer = producer.consume(); + let alice = producer.announce("room/alice", RouteInfo::new()).unwrap(); + let stream = consumer.route_node().answer(link, Path::empty()).unwrap(); + assert!( + driver.poll(Instant::now(), &waiter).is_ok(), + "the producer owns the origin" + ); + while stream.try_output().is_some() {} + drop(alice); + drop(producer); + assert!(matches!(driver.poll(Instant::now(), &waiter), Err(Error::Closed))); + let output = stream.try_output(); + assert!( + matches!( + &output, + Some(crate::route::node::Output::Batch { changes, .. }) + if changes.iter().any(|(path, claim)| path.as_str() == "room/alice" && claim.is_none()) + ), + "the stream is sent the withdrawal: {output:?}" + ); + } + /// A handler holding only its `Route` keeps serving once every producer /// handle drops, and the driver finishes once the handler is gone too. #[kio::test] diff --git a/rs/moq-relay/benches/session.rs b/rs/moq-relay/benches/session.rs index 9cfe86ac852..e07d0df77ab 100644 --- a/rs/moq-relay/benches/session.rs +++ b/rs/moq-relay/benches/session.rs @@ -55,11 +55,14 @@ struct Node(Arc); struct State { cluster: cluster::Cluster, auth: auth::Auth, + /// The cluster's driver, which would otherwise run on until its stats publisher next ticks. + driver: tokio::task::JoinHandle>, decider: tokio::task::JoinHandle<()>, } impl Drop for State { fn drop(&mut self) { + self.driver.abort(); self.decider.abort(); } } @@ -82,7 +85,7 @@ impl Backend for Relay { stats.enabled = true; stats.node = Some(id.to_string()); let (cluster, driver) = cluster::Cluster::new(options.with_cache(cache).with_stats(stats)).unwrap(); - tokio::spawn(driver.run()); + let driver = tokio::spawn(driver.run()); let (auth, mut admissions) = auth::Auth::embedded(format!("bench-{id}")); let decider = tokio::spawn(async move { @@ -93,7 +96,13 @@ impl Backend for Relay { admission.grant(lease::Consumer::fixed(grant)); } }); - (Node(Arc::new(State { cluster, auth, decider })), pool) + let state = State { + cluster, + auth, + driver, + decider, + }; + (Node(Arc::new(state)), pool) } fn origin(relay: &Node) -> &origin::Producer {