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
3 changes: 2 additions & 1 deletion doc/bin/relay/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ A draining upstream may name a replacement URI. `same-host` follows it only
onto the host already dialed, so a peer can move us between ports and schemes;
`follow` also lets it choose the host, which trusts it not to point us into
the local network; `ignore` keeps the current address. `handover` caps how long
the old connection keeps serving after a GOAWAY.
the old connection keeps serving after a GOAWAY; it closes sooner, once the
replacement is up and it carries no subscription or fetch.

## \[cache]

Expand Down
13 changes: 10 additions & 3 deletions rs/moq-net/src/ietf/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,11 +806,15 @@ enum FetchGate {
Joining { id: RequestId, auth: crate::auth::Link },
}

struct Serve(Arc<AtomicUsize>);
/// A dispatched SUBSCRIBE or FETCH serve, counted until dropped.
struct Serve {
owed: Arc<AtomicUsize>,
_working: crate::session::Working,
}

impl Drop for Serve {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::Relaxed);
self.owed.fetch_sub(1, Ordering::Relaxed);
}
}

Expand Down Expand Up @@ -1027,7 +1031,10 @@ impl Publisher {
// Count at dispatch so close cannot pass a request waiting for its first poll.
let serve = matches!(id, ietf::Subscribe::ID | ietf::Fetch::ID).then(|| {
self.owed.fetch_add(1, Ordering::Relaxed);
Serve(self.owed.clone())
Serve {
owed: self.owed.clone(),
_working: self.session.work().start(),
}
});
let task = match id {
ietf::Subscribe::ID => {
Expand Down
29 changes: 25 additions & 4 deletions rs/moq-net/src/ietf/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,9 @@ impl Subscriber {
route: Option<kio::Consumer<()>>,
request: track::Request,
) {
// Holds the session busy while the request waits on authorization or TRACK_STATUS.
// Past that, only a subscription or a fill counts.
let mut setup = Some(self.session.work().start());
// Data streams wait on the alias bound by SUBSCRIBE_OK, so leave the model request
// pending until its immutable track metadata is known.
// Subscribe only to what our grant covers (MoQ Auth), and cancel once it no longer
Expand Down Expand Up @@ -2934,6 +2937,7 @@ impl Subscriber {
else {
return;
};
setup = None;
let Some(next) = self
.linger(&broadcast_path, &track_name, idle, &mut group_fetches, &mut gate)
.await
Expand All @@ -2944,6 +2948,8 @@ impl Subscriber {
}
_ => Target::Request(request),
};
// `subscribe_once` counts the subscription itself.
drop(setup);
loop {
let Some(idle) = self
.subscribe_once(&broadcast_path, &track_name, target, &mut group_fetches, &mut gate)
Expand Down Expand Up @@ -2972,6 +2978,7 @@ impl Subscriber {
group_fetches: &mut TaskSet,
gate: &mut crate::auth::Gate,
) -> Option<Idle> {
let _working = self.session.work().start();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let validity = self.validity.start();
let mut resolved = ResolvedRanges::default();
let subscription = target
Expand Down Expand Up @@ -3857,6 +3864,7 @@ impl Subscriber {
}
End::Fill(requested) => {
let fetch = self.clone().serve_fill(
self.session.work().start(),
broadcast_path.clone(),
track_name.to_owned(),
requested,
Expand Down Expand Up @@ -4087,9 +4095,13 @@ impl Subscriber {

match step {
Step::Fill(fill) => {
let fetch =
self.clone()
.serve_fill(broadcast_path.clone(), track_name.to_owned(), fill, idle.timescale);
let fetch = self.clone().serve_fill(
self.session.work().start(),
broadcast_path.clone(),
track_name.to_owned(),
fill,
idle.timescale,
);
group_fetches.push(fetch);
}
Step::Subscribe => break true,
Expand Down Expand Up @@ -5436,7 +5448,16 @@ impl Subscriber {
/// group always goes alone: a run including the group its source is still writing would
/// hold the earlier groups until it finishes, or be cut short at the Largest Object by a
/// publisher following draft-22 section 9.11.
async fn serve_fill(self, broadcast: Path, name: String, fill: track::Fill, timescale: Option<Timescale>) {
///
/// `_working` counts the fill from when it is queued, before its first poll.
async fn serve_fill(
self,
_working: crate::session::Working,
broadcast: Path,
name: String,
fill: track::Fill,
timescale: Option<Timescale>,
) {
let fill = std::sync::Arc::new(fill);
let mut last = None;
while let Some(run) = fill.next_run() {
Expand Down
9 changes: 9 additions & 0 deletions rs/moq-net/src/lite/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,9 @@ trait Request: Sized {
/// Whether the request counts toward the session's subscriptions peak while it is open.
/// Its stream's credit, not a count, bounds how many the peer holds.
const SUBSCRIPTION: bool = false;
/// Whether the request carries track data (a subscription or a fetch), which keeps
/// the session from reading as idle while it is open.
const DATA: bool = false;
/// Whether the request stays open past its reply, keeping what serving it took, until
/// the requester closes its side.
const HOLDS: bool;
Expand Down Expand Up @@ -1589,6 +1592,8 @@ struct RequestServe<R: Request> {
owing: bool,
// A subscription's place in the session's subscriptions peak.
_subscription: Option<crate::session::Slot>,
// Holds the session busy while a subscription or fetch is open.
_working: Option<crate::session::Working>,
// The latest update to arrive before the reply started. Each one replaces the last.
update: Option<R::Update>,
/// Ends the request, even mid-reply, once the session stops allowing its broadcast.
Expand Down Expand Up @@ -1628,12 +1633,14 @@ enum RequestState<M, R> {
impl<R: Request> RequestServe<R> {
fn new(shared: Arc<Shared>, stream: Stream<Transport, Version>) -> Self {
shared.owed.fetch_add(1, Ordering::Relaxed);
let _working = R::DATA.then(|| shared.session.work().start());
Self {
shared,
stream: Some(stream),
state: RequestState::Decode,
owing: true,
_subscription: None,
_working,
update: None,
gate: None,
broadcast: Default::default(),
Expand Down Expand Up @@ -2017,6 +2024,7 @@ impl Request for SubscribeServe {
// Ending a subscription early is still a valid end.
const GRACEFUL: bool = true;
const SUBSCRIPTION: bool = true;
const DATA: bool = true;
const HOLDS: bool = false;
type Message = lite::Subscribe<'static>;
type Update = lite::SubscribeUpdate;
Expand Down Expand Up @@ -2261,6 +2269,7 @@ impl Request for FetchServe {
const KIND: &'static str = "fetch";
// The response carries no header, so a run cut short would read as whole.
const GRACEFUL: bool = false;
const DATA: bool = true;
const HOLDS: bool = false;
type Message = lite::Fetch<'static>;
type Update = NoUpdate;
Expand Down
46 changes: 36 additions & 10 deletions rs/moq-net/src/lite/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,7 @@ mod tests {
unanswered: None,
bounded: false,
updated: false,
_working: crate::session::Work::default().start(),
})
}

Expand Down Expand Up @@ -2325,6 +2326,7 @@ mod tests {
unanswered: None,
bounded: false,
updated: false,
_working: crate::session::Work::default().start(),
});
if update {
h.serve
Expand Down Expand Up @@ -2781,6 +2783,7 @@ mod tests {
unanswered: None,
bounded: false,
updated: false,
_working: crate::session::Work::default().start(),
};
assert_eq!(
sub.owed(None),
Expand Down Expand Up @@ -6586,6 +6589,8 @@ struct SubStream {
/// Whether an update changed the ranges since the SUBSCRIBE, which a publisher may
/// FIN before reading.
updated: bool,
/// Holds the session busy for as long as the subscription is open.
_working: crate::session::Working,
}

impl SubStream {
Expand Down Expand Up @@ -7154,6 +7159,7 @@ impl TrackServe {
Establish {
validity: self.validity.start(),
serve: self.clone(),
_working: session.work().start(),
closed: session.clone(),
session,
id,
Expand Down Expand Up @@ -7268,6 +7274,8 @@ struct Establish {
/// The ranges the SUBSCRIBE carries for `subscription`.
ranges: Vec<lite::Range>,
tail: kio::Producer<Tail>,
/// Holds the session busy while the SUBSCRIBE opens.
_working: crate::session::Working,
Comment thread
kixelated marked this conversation as resolved.
Resolved
state: EstablishState,
}

Expand Down Expand Up @@ -7345,6 +7353,7 @@ impl Establish {
wanted: None,
bounded: bounded(&self.ranges),
updated: false,
_working: self.session.work().start(),
}
}
}
Expand All @@ -7369,10 +7378,16 @@ enum TrackRunState {
Info {
request: Option<track::Request>,
info: TrackInfoFetch,
/// Holds the session busy while the request waits on its TRACK_INFO.
_working: crate::session::Working,
},
Serve(ServeLoop),
/// Preserve the lite07 completion FIN until the publisher acknowledges it.
Finish(crate::coding::Writer<crate::transport::boxed::SendStream, Version>),
Finish {
writer: crate::coding::Writer<crate::transport::boxed::SendStream, Version>,
/// The subscription keeps the session busy until then.
_working: crate::session::Working,
},
Done,
}

Expand All @@ -7382,6 +7397,7 @@ impl TrackServeRun {
TrackRunState::Info {
request: Some(request),
info: TrackInfoFetch::new(&serve),
_working: serve.subscriber.session.work().start(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The release of this guard is not atomic with the next guard's acquisition: replacing self.state at lite/subscriber.rs:7496 drops Info._working, and the successor guard is only taken in prepare_establish (:7162) later in the same poll. Since moq-tokio's drain loop polls Session::poll_idle from a separate task, a multi-threaded runtime can read Work == 0 in that instant and close a predecessor that still has a subscription mid-setup — the same failure class this change targets, now only a few instructions wide. Low severity and a strict improvement over the prior round-trip window; raising it so it is a known limitation rather than an oversight.

Technical details
# `Work` can read zero across the `Info -> Serve` guard handoff

## Affected sites
- `rs/moq-net/src/lite/subscriber.rs:7400` — `Info._working` created in `TrackServeRun::new`.
- `rs/moq-net/src/lite/subscriber.rs:7483` — the `Info` arm now ignores `_working` via `..`.
- `rs/moq-net/src/lite/subscriber.rs:7496` — `self.state = TrackRunState::Serve(...)` drops `Info._working`.
- `rs/moq-net/src/lite/subscriber.rs:7162` — `prepare_establish` takes the replacement `Working` later in the same poll.
- `rs/moq-net/src/session.rs:917` / `:936` — `Work::start` and `Working::drop` lock separately, so the pair is not atomic.
- `rs/moq-tokio/src/connection.rs:1771` — `Draining::poll_idle` reads the count and starts `session.close()` while a replacement serves.

## Why it matters
`Session::poll_idle` is documented ready only when the session "carries no subscription or fetch". Between the `Info` guard's drop and the replacement guard's acquisition the count is momentarily zero, so a concurrent `poll_idle` can close a session whose subscription is still establishing. The `Establish -> SubStream` handoff acquires before dropping, making this the one drop-then-acquire transition.

## Required outcome
`Work` never reads zero while a live subscription is on the session, across threads.

## Suggested approach (optional)
Have `ServeLoop` own a `Working` moved out of `Info` (started in `TrackServeRun::new` for the pre-lite-05 arm too) and release it once `Sub::Active` is established, so no window reads zero.

## Open questions for the human (optional)
- Is this residual window acceptable given the timing is already loose (see the announce-sync follow-up), or worth closing now?

}
} else {
// Older wires declare no publisher retention limit, and no timeline: their
Expand Down Expand Up @@ -7416,7 +7432,7 @@ impl TrackServeRun {
}
// Each fetch in flight resets itself as it drops, seeing the same denial.
}
TrackRunState::Finish(_) | TrackRunState::Done => {}
TrackRunState::Finish { .. } | TrackRunState::Done => {}
}
}
}
Expand Down Expand Up @@ -7455,7 +7471,7 @@ impl kio::Task for TrackServeRun {
// A finished track only waits for its FIN to be acknowledged: nothing is left to revoke.
// 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(_))
if !matches!(self.state, TrackRunState::Finish { .. })
&& self.gate.poll_denied(waiter).is_ready()
&& self.serve.subscriber.session.clone().poll_closed(waiter).is_pending()
{
Expand All @@ -7464,7 +7480,7 @@ impl kio::Task for TrackServeRun {
}
loop {
match &mut self.state {
TrackRunState::Info { request, info } => {
TrackRunState::Info { request, info, .. } => {
// Abandonment was checked at the top of this poll, arming its wait.
let res = ready!(info.poll_fetch(&self.serve, waiter));
let request = request.take().expect("request pending");
Expand Down Expand Up @@ -7531,14 +7547,17 @@ impl kio::Task for TrackServeRun {
if active.stream.writer.finish().is_ok()
&& finished && self.serve.subscriber.version.waits_for_subscriber_fin()
{
self.state = TrackRunState::Finish(active.stream.writer);
self.state = TrackRunState::Finish {
writer: active.stream.writer,
_working: active._working,
};
continue;
}
}

return Poll::Ready(());
}
TrackRunState::Finish(writer) => {
TrackRunState::Finish { writer, .. } => {
let _ = ready!(writer.poll_close(waiter));
self.state = TrackRunState::Done;
}
Expand Down Expand Up @@ -7695,8 +7714,12 @@ struct ServeLoop {
/// demand still asks for just them: subscribing again would only resend them.
covered: Option<Vec<lite::Range>>,
/// Our side of the covered subscription, finished and held until the publisher reads
/// the FIN, since dropping it would reset it.
closing: Option<crate::coding::Writer<crate::transport::boxed::SendStream, Version>>,
/// the FIN, since dropping it would reset it. The subscription keeps the session busy
/// until then.
closing: Option<(
crate::coding::Writer<crate::transport::boxed::SendStream, Version>,
crate::session::Working,
)>,
}

#[expect(
Expand Down Expand Up @@ -8036,7 +8059,7 @@ impl ServeLoop {
serve.subscriber.remove_subscribe(active.id);
let mut writer = active.stream.writer;
if writer.finish().is_ok() {
self.closing = Some(writer);
self.closing = Some((writer, active._working));
}
// Unchanged ranges are what the publisher covered, even if a datagram
// went missing. Changed ones settled by the grace instead may have
Expand Down Expand Up @@ -8064,7 +8087,7 @@ impl ServeLoop {
return Poll::Pending;
}
ServeMode::Select => {
if let Some(writer) = &mut self.closing
if let Some((writer, _)) = &mut self.closing
&& writer.poll_close(waiter).is_ready()
{
self.closing = None;
Expand Down Expand Up @@ -8436,6 +8459,8 @@ struct FetchServeRun {
info: track::Info,
group: u64,
state: FetchRunState,
/// Holds the session busy until the fetch ends.
_working: crate::session::Working,
}

enum FetchRunState {
Expand Down Expand Up @@ -8484,6 +8509,7 @@ impl FetchServeRun {
validity: serve.validity.start(),
gate: serve.gate(),
serve,
_working: session.work().start(),
session,
info,
group,
Expand Down
Loading