Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions quest/m1/lite07-session-tasks.md

This file was deleted.

26 changes: 26 additions & 0 deletions rs/moq-net/benches/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(rt: &tokio::runtime::Runtime, built: Option<T>) {
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");
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
}

/// 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()
Expand Down Expand Up @@ -1232,6 +1253,7 @@ fn delivery<B: Backend>(c: &mut Criterion, name: &str, shapes: impl IntoIterator
let room = room.get_or_insert_with(|| rt.block_on(Room::<B>::new(version, *shape)));
b.iter_custom(|iters| rt.block_on(room.measure(iters)))
});
retire(&rt, room);
}
}
group.finish();
Expand Down Expand Up @@ -1477,6 +1499,7 @@ fn join<B: Backend>(c: &mut Criterion, name: &str, shapes: impl IntoIterator<Ite
})
})
});
retire(&rt, cluster);
}
}
group.finish();
Expand Down Expand Up @@ -1540,6 +1563,7 @@ fn announce<B: Backend>(c: &mut Criterion) {
})
})
});
retire(&rt, room);
}
}
group.finish();
Expand Down Expand Up @@ -1668,6 +1692,7 @@ fn dash<B: Backend>(c: &mut Criterion, name: &str, shapes: impl IntoIterator<Ite
let room = room.get_or_insert_with(|| rt.block_on(DashRoom::<B>::new(version, *shape)));
b.iter_custom(|iters| rt.block_on(room.measure(iters)))
});
retire(&rt, room);
}
}
group.finish();
Expand Down Expand Up @@ -1846,6 +1871,7 @@ fn churn<B: Backend>(c: &mut Criterion, name: &str, shapes: impl IntoIterator<It
let room = room.get_or_insert_with(|| rt.block_on(ChurnRoom::<B>::new(version, *shape)));
b.iter_custom(|iters| rt.block_on(room.measure(iters)))
});
retire(&rt, room);
}
}
group.finish();
Expand Down
60 changes: 53 additions & 7 deletions rs/moq-net/src/model/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -3807,16 +3805,26 @@ struct OwnClaims {
}

/// The driver's task feeding [`OwnClaims`], which [`Consumer::route_node`] also catches up.
struct FeedOwnClaims(Arc<std::sync::Mutex<OwnClaims>>);
/// 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<std::sync::Mutex<OwnClaims>>,
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)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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]
Expand Down
13 changes: 11 additions & 2 deletions rs/moq-relay/benches/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ struct Node(Arc<State>);
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<anyhow::Result<()>>,
decider: tokio::task::JoinHandle<()>,
}

impl Drop for State {
fn drop(&mut self) {
self.driver.abort();
self.decider.abort();
}
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down