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
1 change: 0 additions & 1 deletion quest/m1/broadcast-lifecycle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ timer on main at 1582fdc07d:
## Required

- [Closing a binding broadcast ends its media](/quest/m1/broadcast-lifecycle/ffi-close.md) - moq-ffi and moq-c media handles end with a cause on close or drop, never a cause-less drop
- [lite-03/04 close FIN](/quest/m1/broadcast-lifecycle/lite-close-fin.md) - a graceful relay close delivers pending SUBSCRIBE FINs on lite-03 and lite-04

## Related

Expand Down
22 changes: 0 additions & 22 deletions quest/m1/broadcast-lifecycle/lite-close-fin.md

This file was deleted.

29 changes: 22 additions & 7 deletions rs/moq-net/src/lite/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,20 +631,24 @@ impl SubscriberDriver {
self.cleanup.close();
}

/// Read only the closed transport's buffered uni streams. Control machines
/// stay owned, so their cancellation guards cannot remove subscription state.
/// Read what the closed transport already delivered: the buffered uni streams, then
/// the subscribe responses. Serve machines wait for the group streams, since ending a
/// subscription removes the state their tails are delivered into.
pub fn poll_drain(&mut self, waiter: &kio::Waiter) -> Poll<()> {
let closed = self.uni.poll(waiter).is_ready();
let _ = self.uni.children.poll(waiter);
// Once every remaining stream waits on a SETUP that never arrived, none of
// them can deliver it: the peer can open no more.
let stuck = self.subscriber.peer_setup.ietf().is_none()
&& self.uni.children.len() == self.subscriber.setup_waits.load(atomic::Ordering::Relaxed);
if closed && (self.uni.children.is_empty() || stuck) {
Poll::Ready(())
} else {
Poll::Pending
if !closed || !(self.uni.children.is_empty() || stuck) {
return Poll::Pending;
}
// The peer's close can win the race against a SUBSCRIBE FIN it sent first, and on
// Lite03/04 that FIN is the only end: read it, so the track finishes rather than
// aborts. Each source reads its streams once, then retires on the closed session.
let _ = self.sources.poll(waiter);
Poll::Ready(())
}

pub fn poll(&mut self, waiter: &kio::Waiter) -> Poll<Result<(), Error>> {
Expand Down Expand Up @@ -6618,7 +6622,18 @@ impl kio::Task for TrackServeRun {
}
ready!(self.gate.poll_ready(waiter));
// A finished track only waits for its FIN to be acknowledged: nothing is left to revoke.
if !matches!(self.state, TrackRunState::Finish(_)) && self.gate.poll_denied(waiter).is_ready() {
// Nor on a closed session, whose grants end with it: the session's end decides how
// its tracks end.
if !matches!(self.state, TrackRunState::Finish(_))
&& self.gate.poll_denied(waiter).is_ready()
&& self
.serve
.subscriber
.session
.clone()
.poll_closed(&mut waiter.context())
.is_pending()
{
self.revoke();
return Poll::Ready(());
}
Expand Down
150 changes: 80 additions & 70 deletions rs/moq-net/tests/integration/relay_upstream_close.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(feature = "async")]

//! A finished track keeps reaching a relay's existing subscribers after the publisher's
//! Finished tracks keep reaching a relay's existing subscribers after the publisher's
//! session to the relay ends, cleanly or not.
//!
//! A broadcast has no finished state: a publisher ends its content with a track FIN and
Expand All @@ -9,7 +9,7 @@
//!
//! Deterministic: simulated time over the mock transport. The relay's group streams to
//! the subscriber are held, with a one-message stream buffer, so the relay still holds
//! most of the track when the upstream session ends.
//! most of each track when the upstream session ends.

use crate::support;

Expand All @@ -29,6 +29,8 @@ const TIMEOUT: Duration = Duration::from_secs(10);
const MAX_DELAY: Duration = Duration::from_secs(1);
const GROUPS: u64 = 4;
const FRAMES: u64 = 8;
/// Tracks finished together, so several SUBSCRIBE FINs are pending when the session ends.
const TRACKS: usize = 3;

const VERSIONS: &[&str] = &[
"moq-lite-03",
Expand Down Expand Up @@ -56,13 +58,44 @@ enum End {
Abort,
}

/// Publish a finished track through a relay, end the publisher's session while the
/// subscriber has read none of it, and return the frames it then reads and how it ended.
async fn round(name: &str, end: End) -> (Vec<(u64, u64)>, Result<(), Error>) {
/// What one subscription read: its `(group, frame)` pairs, then how it ended.
type Outcome = (Vec<(u64, u64)>, Result<(), Error>);

/// Read a subscription to its end.
async fn read(mut sub: moq_net::track::Subscriber) -> Outcome {
let mut got = Vec::new();
loop {
let mut group = match sub.recv_group().await {
Ok(Some(group)) => group,
Ok(None) => return (got, Ok(())),
Err(err) => return (got, Err(err)),
};
loop {
match group.read_frame().await {
Ok(Some(frame)) => {
let index: u64 = std::str::from_utf8(&frame.payload).unwrap().parse().unwrap();
got.push((group.sequence, index));
}
Ok(None) => break,
Err(err) => return (got, Err(err)),
}
}
}
}

/// Publish finished tracks through a relay, end the publisher's session while the
/// subscriber has read none of them, and return what each subscription then reads.
async fn round(name: &str, end: End) -> Vec<Outcome> {
let version: Version = name.parse().unwrap();
let publisher = produce_origin(1);
let broadcast = publisher.create_broadcast("bcast").unwrap();
let track = broadcast.create_track("video", timed()).unwrap();
let tracks: Vec<_> = (0..TRACKS)
.map(|index| {
broadcast
.create_track(format!("track{index}").as_str(), timed())
.unwrap()
})
.collect();
broadcast.announce(Default::default()).unwrap();

let relay = produce_origin(2);
Expand All @@ -88,55 +121,33 @@ async fn round(name: &str, end: End) -> (Vec<(u64, u64)>, Result<(), Error>) {
.expect("resolve timeout")
.expect("broadcast resolves");

let (ready, active) = futures::channel::oneshot::channel();
let reader = moq_clock::sim::spawn(async move {
let mut readers = Vec::new();
for (index, track) in tracks.iter().enumerate() {
let subscription = Subscription::default().with_max_delay(MAX_DELAY).with_range(0..);
let mut sub = remote
.track("video")
.unwrap()
.subscribe(subscription)
let sub = remote.track(&format!("track{index}")).unwrap().subscribe(subscription);
let sub = moq_clock::timeout(TIMEOUT, sub)
.await
.expect("the subscription never became active")
.expect("subscribe");
let _ = ready.send(());
let mut got = Vec::new();
loop {
let mut group = match sub.recv_group().await {
Ok(Some(group)) => group,
Ok(None) => return (got, Ok(())),
Err(err) => return (got, Err(err)),
};
loop {
match group.read_frame().await {
Ok(Some(frame)) => {
let index: u64 = std::str::from_utf8(&frame.payload).unwrap().parse().unwrap();
got.push((group.sequence, index));
}
Ok(None) => break,
Err(err) => return (got, Err(err)),
}
}
}
});

moq_clock::timeout(TIMEOUT, support::harness::subscribed(&track))
.await
.expect("no subscriber appeared");
moq_clock::timeout(TIMEOUT, active)
.await
.expect("the subscription never became active")
.expect("reader alive");
readers.push(moq_clock::sim::spawn(read(sub)));
moq_clock::timeout(TIMEOUT, support::harness::subscribed(track))
.await
.expect("no subscriber appeared");
}

// The subscriber now reads nothing until the upstream session is gone.
downstream.server_transport.hold_unis();
for sequence in 0..GROUPS {
let mut group = track.append_group().unwrap();
for index in 0..FRAMES {
let at = Timestamp::from_millis(sequence * FRAMES + index).unwrap();
group.write_frame(at, index.to_string().into_bytes()).unwrap();
for track in &tracks {
for sequence in 0..GROUPS {
let mut group = track.append_group().unwrap();
for index in 0..FRAMES {
let at = Timestamp::from_millis(sequence * FRAMES + index).unwrap();
group.write_frame(at, index.to_string().into_bytes()).unwrap();
}
group.finish().unwrap();
}
group.finish().unwrap();
track.finish().unwrap();
}
track.finish().unwrap();

match end {
End::Close => moq_clock::timeout(TIMEOUT, upstream.server.clone().close())
Expand All @@ -150,41 +161,40 @@ async fn round(name: &str, end: End) -> (Vec<(u64, u64)>, Result<(), Error>) {
upstream.server.abort(Error::Cancel);
}
}
drop((track, broadcast, publisher, upstream));
drop((tracks, broadcast, publisher, upstream));
// The relay sees the session and its broadcast go before the subscriber reads on.
moq_clock::sleep(Duration::from_millis(100)).await;
downstream.server_transport.release_unis();

let outcome = moq_clock::timeout(TIMEOUT, reader)
.await
.unwrap_or_else(|_| panic!("{name} {end:?}: the track never ended"))
.expect("reader panicked");
let mut outcomes = Vec::new();
for reader in readers {
let outcome = moq_clock::timeout(TIMEOUT, reader)
.await
.unwrap_or_else(|_| panic!("{name} {end:?}: a track never ended"))
.expect("reader panicked");
outcomes.push(outcome);
}
drop((downstream, relay, subscriber));
outcome
outcomes
}

/// Every frame, then a clean end. Lite03 and Lite04 declare no end, so the relay is still
/// waiting out its tail grace when the session goes.
///
/// Their close is left out: it drains on the transport's acknowledgement of the
/// subscription's FIN, which the relay may not have read when the session closes, and
/// without a declared end nothing else says the track is over.
/// Every frame of every track, then a clean end. Lite03 and Lite04 declare no end, so the
/// relay is still waiting out its tail grace when the session goes, and on a close each
/// track's SUBSCRIBE FIN is its only end: the transport acknowledges it, which lets the
/// close drain, before the relay has read it.
#[moq_clock::test]
async fn relay_delivers_a_finished_track_after_upstream_closes() {
async fn relay_delivers_finished_tracks_after_upstream_closes() {
let want: Vec<(u64, u64)> = (0..GROUPS)
.flat_map(|group| (0..FRAMES).map(move |frame| (group, frame)))
.collect();
for version in VERSIONS {
let ends: &[End] = match *version {
"moq-lite-03" | "moq-lite-04" => &[End::Abort],
_ => &[End::Close, End::Abort],
};
for &end in ends {
let (mut got, result) = round(version, end).await;
// Groups arrive in any order; each one's frames arrive in order.
got.sort_by_key(|&(group, _)| group);
assert_eq!(got, want, "{version} {end:?}: result={result:?}");
assert!(result.is_ok(), "{version} {end:?}: ended with {result:?}");
for end in [End::Close, End::Abort] {
for (track, (mut got, result)) in round(version, end).await.into_iter().enumerate() {
// Groups arrive in any order; each one's frames arrive in order.
got.sort_by_key(|&(group, _)| group);
assert_eq!(got, want, "{version} {end:?} track{track}: result={result:?}");
assert!(result.is_ok(), "{version} {end:?} track{track}: ended with {result:?}");
}
}
}
}
Loading