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/auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ because `main` assigned 0x3A to NOT_FETCHABLE first.
- [Token in band](/quest/m1/auth/token-in-band.md) - the credential can leave
the URL: a session starts on what the URL carried and its AUTH streams add
the rest, with the URL kept for peers without the AUTH stream
- [Join revocation](/quest/m1/auth/join-revocation.md) - a revoked grant ends our
subscription even while its joining FETCH waits for request-ID credit

## Related
Expand Down
29 changes: 0 additions & 29 deletions quest/m1/auth/join-revocation.md

This file was deleted.

44 changes: 42 additions & 2 deletions rs/moq-net/src/ietf/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub(crate) fn initial_max_request_id(window: u64, peer_client: bool) -> u64 {

struct ControlState {
request_id_next: RequestId,
/// Legacy request headers that must finish before a later ID can be allocated.
/// A count, since two tasks can allocate before either takes its hold.
request_id_sending: usize,
/// None means no flow control (draft17 removed MaxRequestId).
request_id_max: Option<RequestId>,
/// The peer's requests, flow controlled on drafts 14 to 16 only.
Expand Down Expand Up @@ -106,11 +109,21 @@ pub(super) struct Control {
state: kio::Shared<ControlState>,
}

/// The session must keep a committed legacy header alive until this hold ends.
pub(super) struct RequestSend(Control);

impl Drop for RequestSend {
fn drop(&mut self) {
self.0.state.lock().request_id_sending -= 1;
}
}

impl Control {
pub fn new(request_id_max: Option<RequestId>, client: bool) -> Self {
Self {
state: kio::Shared::new(ControlState {
request_id_next: if client { RequestId(0) } else { RequestId(1) },
request_id_sending: 0,
request_id_max,
incoming: None,
// The peer takes the other parity: a client's IDs are even, a server's odd.
Expand Down Expand Up @@ -222,7 +235,13 @@ impl Control {
Poll::Ready(RequestId(incoming.max))
}

/// Allocate the next request_id, blocking until MAX_REQUEST_ID allows it.
/// Hold later allocations until an already allocated legacy header is sent.
pub fn sending(&self) -> RequestSend {
self.state.lock().request_id_sending += 1;
RequestSend(self.clone())
Comment on lines +239 to +241

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Acquire the send hold atomically with the request ID

When two joining FETCH tasks run concurrently on a multithreaded executor, both can return from next_request_id before either reaches sending(): the first task releases the state lock after allocating its ID, allowing the second to allocate the next ID and write its header first. The counter only blocks a third allocation, so a draft-14/15 peer can still receive request IDs out of sequence and close the session with INVALID_REQUEST_ID. Return the ID and its send hold from one locked operation so allocation order also orders header publication.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed that allocation and header order are not atomic, but this race is not introduced here and is not specific to joining FETCH. Every legacy request (SUBSCRIBE, FETCH, PUBLISH_NAMESPACE, ...) calls next_request_id and writes its header afterwards, so two tasks on different threads can already publish IDs out of order on main. On the adapter path the window is also tiny: VirtualSendStream writes never pend, so the header is queued synchronously right after allocation.

The hold in this PR only keeps a header that was committed before its subscription cancelled from being overtaken. The counter (56d420b) fixes the narrower bug where two overlapping holds tripped the debug_assert and the first release unblocked allocation early.

Making allocation and header publication atomic for all request types is a broader change to the request-ID API, so I'm leaving it for a follow-up instead of adding it here.

(Written by Opus 5.5)

}

/// Allocate the next request_id once credit and the committed header allow it.
///
/// `runtime` arms the give-up timer, so the wait cannot outlive the caller's
/// clock.
Expand All @@ -235,7 +254,11 @@ impl Control {
None => true,
Some(max) => state.request_id_next < max,
};
if allowed { Poll::Ready(()) } else { Poll::Pending }
if allowed && state.request_id_sending == 0 {
Poll::Ready(())
} else {
Poll::Pending
}
});

if let Poll::Ready(mut state) = allowed {
Expand Down Expand Up @@ -491,4 +514,21 @@ mod tests {
assert!(control.accept(RequestId(1)).unwrap().is_none());
assert_eq!(granted(&control), None);
}

/// Allocation waits until every overlapping legacy header hold has ended.
#[test]
fn overlapping_send_holds_block_allocation_until_all_end() {
let control = Control::new(None, false);
let clock = crate::time::Clock::sim();
let a = control.sending();
let b = control.sending();
drop(a);
let mut next = std::pin::pin!(control.next_request_id(&clock));
assert!(kio::Waiter::noop().poll_future(next.as_mut()).is_pending());
drop(b);
assert!(matches!(
kio::Waiter::noop().poll_future(next.as_mut()),
Poll::Ready(Ok(RequestId(1)))
));
}
}
Loading
Loading