From 2dd783b1f69aa8fd60ab21ec553062d60475322a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 22:57:09 -0700 Subject: [PATCH 1/8] chore(quest): claim quest/m2/ffi-poll-migrate.md Quest-Claim: 282ba86a1fd1a146 From 8aaaf2f88831218b45b734c04165c9dfc331f2f4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 23:08:40 -0700 Subject: [PATCH 2/8] wip: poll-native moq-ffi reads --- rs/moq-ffi/src/auth.rs | 10 +- rs/moq-ffi/src/bandwidth.rs | 32 +-- rs/moq-ffi/src/consumer.rs | 250 ++++++-------------- rs/moq-ffi/src/ffi.rs | 439 ++++++++++++++++++++++++++++-------- rs/moq-ffi/src/json.rs | 65 +++--- rs/moq-ffi/src/media.rs | 72 +++--- rs/moq-ffi/src/origin.rs | 137 ++++------- rs/moq-ffi/src/producer.rs | 76 +++---- rs/moq-ffi/src/test.rs | 17 -- 9 files changed, 561 insertions(+), 537 deletions(-) diff --git a/rs/moq-ffi/src/auth.rs b/rs/moq-ffi/src/auth.rs index 9c1a046c99b..f7ed7363be4 100644 --- a/rs/moq-ffi/src/auth.rs +++ b/rs/moq-ffi/src/auth.rs @@ -1,7 +1,7 @@ //! What the peer lets a session do. use crate::error::MoqError; -use crate::ffi::Task; +use crate::ffi::Polled; /// What the peer lets this side do, in this side's own paths. /// @@ -40,14 +40,14 @@ pub struct MoqGrantConsumer { /// The watch behind [`grant`](Self::grant), which reads without the lock. peek: moq_net::auth::Watch, /// The watch behind [`changed`](Self::changed), which tracks what it last returned. - changed: Task, + changed: Polled, } impl MoqGrantConsumer { pub(crate) fn new(handle: &moq_net::auth::Handle) -> Self { Self { peek: handle.grant(), - changed: Task::new(handle.grant()), + changed: Polled::new(handle.grant()), } } } @@ -68,7 +68,9 @@ impl MoqGrantConsumer { /// session ended, or [`cancel`](Self::cancel). A reconnect is not an end. pub async fn changed(&self) -> Result, MoqError> { self.changed - .run(|mut watch| async move { Ok(watch.changed().await?.map(Into::into)) }) + .wait(|watch, waiter| { + std::task::Poll::Ready(Ok(std::task::ready!(watch.poll_changed(waiter))?.map(Into::into))) + }) .await } diff --git a/rs/moq-ffi/src/bandwidth.rs b/rs/moq-ffi/src/bandwidth.rs index beed37ae3a1..b0f80330d39 100644 --- a/rs/moq-ffi/src/bandwidth.rs +++ b/rs/moq-ffi/src/bandwidth.rs @@ -249,15 +249,13 @@ mod tests { /// An owned bandwidth estimate; holding it requests estimates until released. #[derive(uniffi::Object)] pub struct MoqBandwidthConsumer { - inner: std::sync::Mutex>, - changed: crate::ffi::Task<()>, + inner: crate::ffi::Polled, } impl MoqBandwidthConsumer { pub(crate) fn new(inner: moq_net::bandwidth::Consumer) -> Self { Self { - inner: std::sync::Mutex::new(Some(inner)), - changed: crate::ffi::Task::new(()), + inner: crate::ffi::Polled::new(inner), } } } @@ -267,36 +265,26 @@ impl MoqBandwidthConsumer { /// The current estimate in bits per second, or None while unavailable. pub fn rate(&self) -> Option { self.inner - .lock() - .unwrap() - .as_ref() - .and_then(moq_net::bandwidth::Consumer::peek) + .peek(moq_net::bandwidth::Consumer::peek) + .flatten() .map(moq_net::bandwidth::Rate::as_bps) } /// Wait for the estimate to change and return the new [`rate`](Self::rate), erroring when /// its producer closes. pub async fn rate_changed(&self) -> Result, MoqError> { - self.changed - .run(|_guard| async move { - moq_net::kio::wait(|waiter| { - let mut inner = self.inner.lock().unwrap(); - let inner = inner.as_mut().ok_or(MoqError::Cancelled)?; - inner.poll_changed(waiter).map(|result| { - result - .map(|rate| rate.map(moq_net::bandwidth::Rate::as_bps)) - .map_err(Into::into) - }) - }) - .await + self.inner + .wait(|inner, waiter| { + std::task::Poll::Ready(Ok( + std::task::ready!(inner.poll_changed(waiter))?.map(moq_net::bandwidth::Rate::as_bps) + )) }) .await } /// Release this consumer's demand, including a pending wait. pub fn cancel(&self) { - self.inner.lock().unwrap().take(); - self.changed.cancel(); + self.inner.cancel(); } } diff --git a/rs/moq-ffi/src/consumer.rs b/rs/moq-ffi/src/consumer.rs index eb0077e77bd..256ea1570c0 100644 --- a/rs/moq-ffi/src/consumer.rs +++ b/rs/moq-ffi/src/consumer.rs @@ -1,12 +1,11 @@ //! Read broadcasts, tracks, and groups. -use std::future::Future; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::task::{Poll, ready}; use std::time::Duration; use crate::error::MoqError; -use crate::ffi::{Background, Guard, Task}; +use crate::ffi::{Background, Polled}; use crate::media::*; use crate::producer::MoqTrackInfo; @@ -466,41 +465,35 @@ impl TrackInner { } } - fn poll_acquire_pending(&mut self, waiter: &kio::Waiter) -> Poll> { + /// The first frame of the next group in sequence order, skipping completed empty groups. + /// + /// One poll does the whole step, so it never trusts a slot it filled on an earlier poll: + /// a group another call took from `pending` in between is simply gone, and this acquires + /// the next one. A group still waiting on its first frame stays in `pending`, so a + /// cancelled call leaves it for the next `read_frame` or `next_group`. + fn poll_read_frame(&mut self, waiter: &kio::Waiter) -> Poll, MoqError>> { let ordered = match self.ordered() { Ok(ordered) => ordered, Err(e) => return Poll::Ready(Err(e)), }; - if ordered.pending.is_some() { - return Poll::Ready(Ok(true)); - } - match ready!(ordered.track.poll_next_group(waiter)) { - Ok(Some(group)) => { - ordered.pending = Some(group); - Poll::Ready(Ok(true)) + loop { + let group = match &mut ordered.pending { + Some(group) => group, + None => match ready!(ordered.track.poll_next_group(waiter)) { + Ok(Some(group)) => ordered.pending.insert(group), + Ok(None) => return Poll::Ready(Ok(None)), + Err(err) => return Poll::Ready(Err(err.into())), + }, + }; + let result = ready!(group.poll_read_frame(waiter)); + // Drop a completed or failed group so the next pass moves on. + ordered.pending = None; + match result { + Ok(Some(frame)) => return Poll::Ready(Ok(Some(frame))), + // An empty group, or one that expired here or at a hop that reset it. + Ok(None) | Err(moq_net::Error::Old | moq_net::Error::Stream(moq_net::StreamError::Old)) => continue, + Err(err) => return Poll::Ready(Err(err.into())), } - Ok(None) => Poll::Ready(Ok(false)), - Err(err) => Poll::Ready(Err(err.into())), - } - } - - fn poll_pending_frame(&mut self, waiter: &kio::Waiter) -> Poll, MoqError>> { - let ordered = match self.ordered() { - Ok(ordered) => ordered, - Err(e) => return Poll::Ready(Err(e)), - }; - let result = { - let group = ordered.pending.as_mut().expect("pending group"); - ready!(group.poll_read_frame(waiter)) - }; - // Drop a completed or failed group so the next call can move on. A - // cancelled wait never reaches here, so `pending` stays in that case. - ordered.pending = None; - match result { - Ok(frame) => Poll::Ready(Ok(frame)), - // The group expired, here or at a hop that reset it, so move on like an empty one. - Err(moq_net::Error::Old | moq_net::Error::Stream(moq_net::StreamError::Old)) => Poll::Ready(Ok(None)), - Err(err) => Poll::Ready(Err(err.into())), } } @@ -527,69 +520,12 @@ impl TrackInner { } } -/// Shared subscriber state for both read lanes. -/// -/// The mutex is held only across a poll, not the wait: moq-net can serve a -/// datagram while a group read is parked, and vice versa, as long as we do not -/// keep exclusive access for the duration of one `recv`. -struct TrackShared { - inner: Mutex>, -} - -impl TrackShared { - fn close(&self) { - let _ = self.inner.lock().expect("track reader").take(); - } - - fn poll(&self, f: impl FnOnce(&mut TrackInner) -> Poll>) -> Poll> { - match self.inner.lock().expect("track reader").as_mut() { - Some(inner) => f(inner), - None => Poll::Ready(Err(MoqError::Cancelled)), - } - } - - async fn recv_group(&self) -> Result, MoqError> { - kio::wait(|waiter| self.poll(|inner| inner.poll_recv_group(waiter))).await - } - - async fn next_group(&self) -> Result, MoqError> { - kio::wait(|waiter| self.poll(|inner| inner.poll_next_group(waiter))).await - } - - async fn recv_datagram(&self) -> Result, MoqError> { - kio::wait(|waiter| self.poll(|inner| inner.poll_recv_datagram(waiter))).await - } - - async fn read_frame(&self) -> Result, MoqError> { - loop { - let acquired = kio::wait(|waiter| self.poll(|inner| inner.poll_acquire_pending(waiter))).await?; - if !acquired { - return Ok(None); - } - if let Some(frame) = kio::wait(|waiter| self.poll(|inner| inner.poll_pending_frame(waiter))).await? { - return Ok(Some(raw_frame(frame)?)); - } - } - } -} - -/// Serializes one track read lane so two `next_group` calls cannot race the cursor. -struct ReadLane; - -/// Hold the lane guard until `fut` finishes. `Task::run` only serializes while the -/// guard lives; an unused `_lane` in the closure would drop it before the wait. -async fn with_lane(_lane: Guard, fut: impl Future>) -> Result { - fut.await -} - /// A subscribed track whose groups, frames, or datagrams can be read. #[derive(uniffi::Object)] pub struct MoqTrackConsumer { - shared: Arc, - /// Group reads (`recv_group`, `next_group`, `read_frame`). Independent of [`Self::datagrams`]. - groups: Task, - /// Datagram reads. Independent of [`Self::groups`]. - datagrams: Task, + /// Every read polls this; none holds it across a wait, so a datagram read progresses while + /// a group read is parked, and vice versa. + reads: Polled, control: moq_net::track::Control, info: moq_net::track::Info, /// The owner whose panic ends this track's reads, handed to every group and reader taken from it. @@ -601,13 +537,10 @@ impl MoqTrackConsumer { let control = track.control(); let info = track.info().clone(); Self { - shared: Arc::new(TrackShared { - inner: Mutex::new(Some(TrackInner { - track: Cursor::Uncommitted(track), - })), - }), - groups: Task::new(ReadLane).supervised(driver.clone()), - datagrams: Task::new(ReadLane).supervised(driver.clone()), + reads: Polled::new(TrackInner { + track: Cursor::Uncommitted(track), + }) + .supervised(driver.clone()), control, info, driver, @@ -617,43 +550,26 @@ impl MoqTrackConsumer { /// Hand the subscription to a typed reader (a JSON consumer, say), closing this handle. /// /// Errors with [`MoqError::AlreadyCommitted`] once a group read has started, since the reader - /// would miss whatever that read consumed. + /// would miss whatever that read consumed. A datagram read still parked resolves `Cancelled`. pub(crate) fn take(&self) -> Result<(moq_net::track::Subscriber, Background), MoqError> { - self.driver.check()?; - let mut guard = self.shared.inner.lock().expect("track reader"); - match guard.as_ref().map(|inner| &inner.track) { - Some(Cursor::Uncommitted(_)) => {} - Some(_) => return Err(MoqError::AlreadyCommitted), - None => return Err(MoqError::Closed), - } - let Some(TrackInner { - track: Cursor::Uncommitted(track), - }) = guard.take() - else { - unreachable!("just matched Uncommitted"); + let inner = self + .reads + .take(|inner| match inner.track { + Cursor::Uncommitted(_) => Ok(()), + _ => Err(MoqError::AlreadyCommitted), + }) + .map_err(|err| match err { + MoqError::Cancelled => MoqError::Closed, + err => err, + })?; + let Cursor::Uncommitted(track) = inner.track else { + unreachable!("just checked Uncommitted"); }; - drop(guard); - // Wake any datagram read still parked on the subscriber this handle no longer owns. - self.cancel_lanes(); Ok((track, self.driver.clone())) } - fn cancel_lanes(&self) { - self.shared.close(); - self.groups.cancel(); - self.datagrams.cancel(); - } - - /// True while a group-lane `Task::run` still holds its guard. - #[cfg(test)] - pub(crate) fn group_lane_busy(&self) -> bool { - self.groups.lock().is_none() - } -} - -impl Drop for MoqTrackConsumer { - fn drop(&mut self) { - self.shared.close(); + fn group(&self, group: Option) -> Option> { + group.map(|group| Arc::new(MoqGroupConsumer::new(group, self.driver.clone()))) } } @@ -670,18 +586,8 @@ impl MoqTrackConsumer { /// ([`Self::next_group`], [`Self::read_frame`]) fail with /// [`MoqError::AlreadyCommitted`] afterwards. pub async fn recv_group(&self) -> Result>, MoqError> { - let shared = self.shared.clone(); - let driver = self.driver.clone(); - self.groups - .run(move |lane| { - with_lane(lane, async move { - Ok(shared - .recv_group() - .await? - .map(|group| Arc::new(MoqGroupConsumer::new(group, driver)))) - }) - }) - .await + let group = self.reads.wait(|inner, waiter| inner.poll_recv_group(waiter)).await?; + Ok(self.group(group)) } /// Return the next group with a higher sequence number than any previously @@ -694,18 +600,8 @@ impl MoqTrackConsumer { /// The first call commits this track to sequence order: arrival-order reads /// ([`Self::recv_group`]) fail with [`MoqError::AlreadyCommitted`] afterwards. pub async fn next_group(&self) -> Result>, MoqError> { - let shared = self.shared.clone(); - let driver = self.driver.clone(); - self.groups - .run(move |lane| { - with_lane(lane, async move { - Ok(shared - .next_group() - .await? - .map(|group| Arc::new(MoqGroupConsumer::new(group, driver)))) - }) - }) - .await + let group = self.reads.wait(|inner, waiter| inner.poll_next_group(waiter)).await?; + Ok(self.group(group)) } /// Read the first frame of the next group, including its timestamp. @@ -718,10 +614,8 @@ impl MoqTrackConsumer { /// Shares the sequence cursor with [`Self::next_group`], committing the track /// the same way. pub async fn read_frame(&self) -> Result, MoqError> { - let shared = self.shared.clone(); - self.groups - .run(move |lane| with_lane(lane, async move { shared.read_frame().await })) - .await + let frame = self.reads.wait(|inner, waiter| inner.poll_read_frame(waiter)).await?; + frame.map(raw_frame).transpose() } /// Receive the next best-effort datagram in arrival order. @@ -732,10 +626,7 @@ impl MoqTrackConsumer { /// group order, never commits the track to one, and progresses while a group /// read is pending. pub async fn recv_datagram(&self) -> Result, MoqError> { - let shared = self.shared.clone(); - self.datagrams - .run(move |lane| with_lane(lane, async move { shared.recv_datagram().await })) - .await + self.reads.wait(|inner, waiter| inner.poll_recv_datagram(waiter)).await } /// Return the publisher-side track properties learned during subscription. @@ -759,17 +650,7 @@ impl MoqTrackConsumer { /// /// Terminal: the subscription is released here, not when the handle is. pub fn cancel(&self) { - self.cancel_lanes(); - } -} - -struct GroupInner { - group: moq_net::group::Consumer, -} - -impl GroupInner { - async fn read_frame(&mut self) -> Result, MoqError> { - self.group.read_frame().await?.map(raw_frame).transpose() + self.reads.cancel(); } } @@ -777,7 +658,7 @@ impl GroupInner { #[derive(uniffi::Object)] pub struct MoqGroupConsumer { sequence: u64, - task: Task, + reads: Polled, driver: Background, } @@ -785,7 +666,7 @@ impl MoqGroupConsumer { pub(crate) fn new(group: moq_net::group::Consumer, driver: Background) -> Self { Self { sequence: group.sequence, - task: Task::new(GroupInner { group }).supervised(driver.clone()), + reads: Polled::new(group).supervised(driver.clone()), driver, } } @@ -795,11 +676,14 @@ impl MoqGroupConsumer { /// Errors with [`MoqError::AlreadyCommitted`] once a frame has been read, since the reader /// would start mid-group, and [`MoqError::Busy`] while a read is in flight. pub(crate) fn take(&self) -> Result<(moq_net::group::Consumer, Background), MoqError> { - let inner = self.task.take(|inner| match inner.group.index() { + if self.reads.busy() { + return Err(MoqError::Busy); + } + let group = self.reads.take(|group| match group.index() { 0 => Ok(()), _ => Err(MoqError::AlreadyCommitted), })?; - Ok((inner.group, self.driver.clone())) + Ok((group, self.driver.clone())) } } @@ -814,13 +698,17 @@ impl MoqGroupConsumer { /// /// Returns `None` when the group ends. pub async fn read_frame(&self) -> Result, MoqError> { - self.task.run(|mut state| async move { state.read_frame().await }).await + let frame = self + .reads + .wait(|group, waiter| Poll::Ready(Ok(ready!(group.poll_read_frame(waiter))?))) + .await?; + frame.map(raw_frame).transpose() } /// Cancel all current and future `read_frame()` calls. /// /// Terminal: the group and whatever it still buffers are released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } diff --git a/rs/moq-ffi/src/ffi.rs b/rs/moq-ffi/src/ffi.rs index 4fdd43e7524..beeae454411 100644 --- a/rs/moq-ffi/src/ffi.rs +++ b/rs/moq-ffi/src/ffi.rs @@ -1,5 +1,7 @@ use std::future::Future; use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::task::{Poll, ready}; use crate::error::MoqError; @@ -22,22 +24,22 @@ pub(crate) struct Runtime { clock: Option, /// Taken by [`shutdown`], so the thread is stopped and joined once. thread: std::sync::Mutex>, - /// Set by [`shutdown`] to wake the [`Task::run`] calls parked in place, so they observe - /// [`Polls::stopped`] and resolve `Cancelled`. - stopped: tokio::sync::watch::Sender, - /// The [`Task::run`] polls in flight on host threads, which [`shutdown`] waits out. - polls: std::sync::Mutex, - /// Notified when the last in-flight poll finishes. + /// Closed by [`shutdown`] to wake the calls parked in place, so their next poll finds + /// [`STOPPED`] and resolves `Cancelled`. + stopped: kio::Producer<()>, + /// The polls in flight on host threads, which [`shutdown`] waits out, plus [`STOPPED`] + /// once it has begun. + polls: AtomicUsize, + /// Taken to sleep on and notify [`Self::idle`], so the last poll cannot wake shutdown + /// between its check and its wait. + idling: std::sync::Mutex<()>, + /// Notified when the last in-flight poll finishes after [`STOPPED`] is set. idle: std::sync::Condvar, } -#[derive(Default)] -struct Polls { - active: usize, - /// Set by [`shutdown`]: no poll may start once the drivers are about to stop, because a - /// tokio timer polled after its driver is gone panics rather than erroring. - stopped: bool, -} +/// Set in [`Runtime::polls`] by [`shutdown`]: no poll may start once the drivers are about to +/// stop, because a tokio timer polled after its driver is gone panics rather than erroring. +const STOPPED: usize = 1 << (usize::BITS - 1); struct Thread { /// Resolves the `block_on` the thread parks in. @@ -112,8 +114,9 @@ impl Runtime { #[cfg(feature = "testing")] clock, thread: std::sync::Mutex::new(Some(Thread { stop, join })), - stopped: tokio::sync::watch::Sender::new(false), - polls: Default::default(), + stopped: kio::Producer::new(()), + polls: AtomicUsize::new(0), + idling: Default::default(), idle: Default::default(), } } @@ -135,16 +138,16 @@ impl Runtime { return; }; // Before the thread stops, so no call awaited in place polls the runtime after it is gone. - let mut polls = runtime.polls.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - polls.stopped = true; + let idling = runtime.idling.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + runtime.polls.fetch_or(STOPPED, Ordering::SeqCst); drop( runtime .idle - .wait_while(polls, |polls| polls.active > 0) + .wait_while(idling, |_| runtime.polls.load(Ordering::SeqCst) != STOPPED) .unwrap_or_else(std::sync::PoisonError::into_inner), ); // Outside the lock: a waker may poll inline, which takes it. - runtime.stopped.send_replace(true); + let _ = runtime.stopped.close(); let _ = thread.stop.send(()); thread.join.join().expect("runtime owner panicked"); // Retain the owner lock until the join completes, including concurrent shutdown calls. @@ -154,35 +157,29 @@ impl Runtime { /// Resolves once [`shutdown`] has run. async fn stopped() { - let mut stopped = runtime().stopped.subscribe(); - // `Err` only when the sender is dropped, which a static never is. - let _ = stopped.wait_for(|&stopped| stopped).await; + kio::wait(|waiter| runtime().stopped.poll_closed(waiter)).await } -/// Marks a [`Task::run`] poll in flight, so [`shutdown`] waits for it to finish. +/// Marks a poll in flight on a host thread, so [`shutdown`] waits for it to finish. struct Polling; impl Polling { /// `None` once [`shutdown`] has begun. fn start() -> Option { - let mut polls = runtime() - .polls - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner); - if polls.stopped { - return None; - } - polls.active += 1; - Some(Self) + let previous = runtime().polls.fetch_add(1, Ordering::SeqCst); + let polling = Self; + // Dropped rather than forgotten, so a shutdown already waiting sees the count fall back. + (previous & STOPPED == 0).then_some(polling) } } impl Drop for Polling { fn drop(&mut self) { let runtime = runtime(); - let mut polls = runtime.polls.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - polls.active -= 1; - if polls.active == 0 { + if runtime.polls.fetch_sub(1, Ordering::SeqCst) == STOPPED + 1 { + // The last poll a waiting shutdown counts. Under the lock, so the wake cannot land + // between its check and its wait. + let _idling = runtime.idling.lock().unwrap_or_else(std::sync::PoisonError::into_inner); runtime.idle.notify_all(); } } @@ -275,6 +272,21 @@ impl Background { Ok(()) } + /// Ready with the owner's failure once it fails; pending while it runs or after it ended + /// cleanly, the same split [`Self::watch`] makes. + pub fn poll_failed(&self, waiter: &kio::Waiter) -> Poll { + let Some(result) = &self.result else { return Poll::Pending }; + let completed = ready!(result.poll(waiter, |state| match &**state { + Some(completed) => Poll::Ready(completed.clone()), + None => Poll::Pending, + })); + match completed { + Ok(Ok(())) => Poll::Pending, + Ok(Err(err)) => Poll::Ready(crate::session::map_closed_error(err)), + Err(_) => Poll::Ready(crate::session::map_closed_error(moq_net::Error::Cancel.into())), + } + } + pub async fn watch(&self, future: impl Future>) -> Result { self.check()?; let result = if self.result.is_some() { @@ -371,6 +383,180 @@ where } } +/// A handle's state for async calls that only poll it, which UniFFI's `RustFuture` polls directly. +/// +/// The state sits behind a std mutex held across one poll, never across a wait. Concurrent +/// calls on one handle all park, and each poll takes the next item atomically, so between +/// them they take every item once, though not in call order. Unlike [`Task`], nothing queues: +/// a call the foreign side cancelled but has not freed yet stops polling and holds nothing +/// the next call needs. +/// +/// Each poll runs inside [`shutdown`]'s in-flight accounting and the runtime context, so a poll +/// that missed the shutdown finishes before the drivers stop, and a tokio timer in the state +/// stays pollable. [`Self::cancel`] closes a channel every poll checks, then drops the state. +/// A call that awaits tokio channels, codecs, or I/O uses [`Task`] instead. +pub(crate) struct Polled { + /// `None` once cancelled or taken. + state: std::sync::Mutex>, + /// Closed by [`Self::cancel`] and [`Self::take`], waking every parked call. + cancel: kio::Producer<()>, + driver: Background, + /// Calls polled at least once and not yet dropped, for [`Self::busy`]. + calls: AtomicUsize, + /// A panic dropping the state, which every later call reports. + cleanup: std::sync::OnceLock, +} + +impl Polled { + pub fn new(inner: T) -> Self { + Self { + state: std::sync::Mutex::new(Some(inner)), + cancel: kio::Producer::new(()), + driver: Background::default(), + calls: AtomicUsize::new(0), + cleanup: Default::default(), + } + } + + /// End every call with the owner's failure, as [`Task::supervised`] does. + pub fn supervised(mut self, driver: Background) -> Self { + self.driver = driver; + self + } + + fn check(&self) -> Result<(), MoqError> { + self.driver.check()?; + match self.cleanup.get() { + Some(err) => Err(err.clone()), + None => Ok(()), + } + } + + /// Poll `poll` against the state until it is ready. + /// + /// Resolves [`MoqError::Cancelled`] once [`Self::cancel`] or [`shutdown`] has run, and the + /// owner's error once it fails. `poll` must leave the state consistent at every return, + /// since another call may poll it before this one polls again. + pub async fn wait( + &self, + mut poll: impl FnMut(&mut T, &kio::Waiter) -> Poll>, + ) -> Result { + self.calls.fetch_add(1, Ordering::Relaxed); + let _call = Call(&self.calls); + kio::wait(|waiter| self.poll(waiter, &mut poll)).await + } + + fn poll( + &self, + waiter: &kio::Waiter, + poll: &mut impl FnMut(&mut T, &kio::Waiter) -> Poll>, + ) -> Poll> { + let Some(_polling) = Polling::start() else { + return Poll::Ready(Err(MoqError::Cancelled)); + }; + let _runtime = enter(); + // Only registers: the signal closes after every in-flight poll, this one included, ends. + let _ = runtime().stopped.poll_closed(waiter); + if let Poll::Ready(err) = self.driver.poll_failed(waiter) { + return Poll::Ready(Err(err)); + } + if let Some(err) = self.cleanup.get() { + return Poll::Ready(Err(err.clone())); + } + if self.cancel.poll_closed(waiter).is_ready() { + return Poll::Ready(Err(MoqError::Cancelled)); + } + let mut state = self.state.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + match state.as_mut() { + Some(state) => poll(state, waiter), + None => Poll::Ready(Err(MoqError::Cancelled)), + } + } + + /// Whether a call is in flight: polled at least once and not yet dropped. + pub fn busy(&self) -> bool { + self.calls.load(Ordering::Relaxed) > 0 + } + + /// Read the state synchronously, or `None` once it is gone. + pub fn peek(&self, f: impl FnOnce(&T) -> R) -> Option { + self.state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .as_ref() + .map(f) + } + + /// Whether [Self::cancel] or [Self::take] has run. + pub fn is_cancelled(&self) -> bool { + self.state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .is_none() + } + + /// Move the state to a new owner, ending this handle as [Self::cancel] would. + /// + /// `check` sees the state first, and an error there leaves this handle untouched. + /// [`MoqError::Cancelled`] once the state is gone. + pub fn take(&self, check: impl FnOnce(&T) -> Result<(), MoqError>) -> Result { + self.check()?; + let mut state = self.state.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + check(state.as_ref().ok_or(MoqError::Cancelled)?)?; + let inner = state.take().ok_or(MoqError::Cancelled)?; + drop(state); + let _ = self.cancel.close(); + Ok(inner) + } + + /// Cancel all current and future calls and drop the state before returning. + /// + /// Calls resolve [MoqError::Cancelled]; later calls report a panic dropping the state. + /// Terminal: there is no way back from here, and the state is gone rather than idle. + pub fn cancel(&self) { + let _ = self.cancel.close(); + let state = self + .state + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .take(); + self.release(state); + } + + /// Drop `state` inside the runtime, so the reactor unregisters whatever it held, and keep + /// a panic for later calls rather than unwinding into the caller's. + fn release(&self, state: Option) { + let Some(state) = state else { return }; + let polling = Polling::start(); + let _runtime = polling.as_ref().map(|_| enter()); + if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(state))).is_err() { + let _ = self + .cleanup + .set(crate::session::map_closed_error(moq_tokio::Error::Panic)); + } + } +} + +impl Drop for Polled { + fn drop(&mut self) { + let state = self + .state + .get_mut() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .take(); + self.release(state); + } +} + +/// Counts one [`Polled::wait`] call until it is dropped. +struct Call<'a>(&'a AtomicUsize); + +impl Drop for Call<'_> { + fn drop(&mut self) { + self.0.fetch_sub(1, Ordering::Relaxed); + } +} + /// Serializes access to `T` across the FFI boundary, and releases it on cancel. /// /// The state is dropped rather than parked once [Self::cancel] runs, so a handle the foreign @@ -516,31 +702,6 @@ impl Task { } } - /// Move the state to a new owner, ending this handle as [Self::cancel] would. - /// - /// `check` sees the state first, and an error there leaves this handle untouched. - /// [`MoqError::Busy`] while a [Self::run] owns the state, [`MoqError::Cancelled`] after - /// [Self::cancel]. - pub fn take(&self, check: impl FnOnce(&T) -> Result<(), MoqError>) -> Result { - self.driver.check()?; - if let Some(cleanup) = self.cleanup.lock().unwrap().as_ref() { - cleanup.check()?; - } - if *self.cancel.borrow() { - return Err(MoqError::Cancelled); - } - let mut state = self.state.clone().try_lock_owned().map_err(|_| MoqError::Busy)?; - // Re-read under the lock, as `lock` does: a cancel we don't see here has not taken the state. - if *self.cancel.borrow() { - return Err(MoqError::Cancelled); - } - check(state.as_ref().ok_or(MoqError::Cancelled)?)?; - let inner = state.take().ok_or(MoqError::Cancelled)?; - // The state is already gone, so this only fails later runs; nothing is left to drop. - self.cancel.send_replace(true); - Ok(inner) - } - /// Cancel all current and future [Self::run] calls and drop the state. /// /// Calls return [MoqError::Cancelled]; later calls report a completed cleanup panic. @@ -643,13 +804,11 @@ mod tests { // Wait until the first caller has flagged the stop, so it holds the owner lock and is // joining the workers while the callback is still held. - let mut stopped = runtime.stopped.subscribe(); let first = std::thread::spawn(shutdown(runtime.clone(), finished.clone())); tokio::runtime::Builder::new_current_thread() .build() .unwrap() - .block_on(stopped.wait_for(|&stopped| stopped)) - .unwrap(); + .block_on(kio::wait(|waiter| runtime.stopped.poll_closed(waiter))); let (started, starting) = std::sync::mpsc::channel(); let second = std::thread::spawn({ @@ -820,49 +979,145 @@ mod tests { assert!(matches!(err, MoqError::Cancelled)); } - #[tokio::test] - async fn take_is_busy_while_a_run_holds_the_lock() { - let task = Task::new(7); - let (started, running) = tokio::sync::oneshot::channel(); - let run = task.run(move |state| async move { - let _state = state; - started.send(()).expect("test should still be waiting"); - std::future::pending::>().await + /// A cursor over items `0..` behind a [`Polled`], read the way a moq-net consumer is. + struct Cursor { + /// How many items exist, closed once no more will. + produced: kio::Consumer, + /// The next item to take. + next: u32, + } + + fn queue() -> (kio::Producer, Polled) { + let producer = kio::Producer::new(0); + let polled = Polled::new(Cursor { + produced: producer.consume(), + next: 0, }); - tokio::pin!(run); + (producer, polled) + } - soon(async { - tokio::select! { - _ = &mut run => panic!("a pending run should not resolve"), - started = running => started.expect("the run should start"), - } - }) - .await; - assert!(matches!(task.take(|_| Ok(())), Err(MoqError::Busy))); + /// Take the next item, or `None` once every item is taken and no more will come. + async fn pop(polled: &Polled) -> Result, MoqError> { + polled + .wait(|cursor, waiter| { + let next = cursor.next; + let available = cursor.produced.poll(waiter, |produced| match **produced > next { + true => Poll::Ready(()), + false => Poll::Pending, + }); + Poll::Ready(Ok(match ready!(available) { + Ok(()) => { + cursor.next += 1; + Some(next) + } + Err(_closed) => None, + })) + }) + .await + } + + /// Poll `future` once with a no-op waker. + fn poll_once(future: std::pin::Pin<&mut F>) -> Poll { + future.poll(&mut std::task::Context::from_waker(std::task::Waker::noop())) } #[tokio::test] - async fn take_moves_the_state_unless_refused() { - let task = Task::new(7); + async fn polled_take_moves_the_state_unless_refused() { + let polled = Polled::new(7); assert!(matches!( - task.take(|_| Err(MoqError::AlreadyCommitted)), + polled.take(|_| Err(MoqError::AlreadyCommitted)), Err(MoqError::AlreadyCommitted) )); assert_eq!( - task.take(|state| { - assert_eq!(*state, 7); - Ok(()) - }) - .unwrap(), + polled + .take(|state| { + assert_eq!(*state, 7); + Ok(()) + }) + .unwrap(), 7 ); - let err = task - .run(|_| async { Ok(()) }) + let err = polled + .wait(|_, _| Poll::Ready(Ok(()))) .await - .expect_err("taken state cannot run"); + .expect_err("taken state cannot be polled"); assert!(matches!(err, MoqError::Cancelled)); - assert!(matches!(task.take(|_| Ok(())), Err(MoqError::Cancelled))); + assert!(matches!(polled.take(|_| Ok(())), Err(MoqError::Cancelled))); + } + + #[tokio::test] + async fn polled_is_busy_until_a_parked_call_is_dropped() { + let (_producer, polled) = queue(); + assert!(!polled.busy()); + let mut parked = Box::pin(pop(&polled)); + assert!(poll_once(parked.as_mut()).is_pending()); + assert!(polled.busy(), "a parked call is in flight"); + drop(parked); + assert!(!polled.busy()); + } + + #[tokio::test] + async fn polled_cancel_drops_the_state_and_ends_a_parked_call() { + let (tx, mut dropped) = tokio::sync::oneshot::channel(); + let polled = Polled::new(Tracked(Some(tx))); + let mut parked = Box::pin(polled.wait(|_, _| Poll::>::Pending)); + assert!(poll_once(parked.as_mut()).is_pending()); + + polled.cancel(); + // Dropped before cancel returns, though the parked call still holds a borrow. + assert!(matches!(dropped.try_recv(), Ok(()))); + assert!(polled.is_cancelled()); + assert!(matches!(soon(parked).await, Err(MoqError::Cancelled))); + assert!(matches!( + polled.wait(|_, _| Poll::Ready(Ok(()))).await, + Err(MoqError::Cancelled) + )); + } + + #[tokio::test] + async fn polled_cleanup_panic_reaches_later_calls() { + struct Panics; + impl Drop for Panics { + fn drop(&mut self) { + panic!("injected cleanup panic"); + } + } + let polled = Polled::new(Panics); + polled.cancel(); + let result = polled.wait(|_, _| Poll::Ready(Ok(()))).await; + assert!(matches!(result, Err(MoqError::Task { .. })), "{result:?}"); + } + + /// Two calls parked on one handle both wake, and between them take every item once. + #[tokio::test] + async fn polled_concurrent_calls_take_every_item_once() { + let (producer, polled) = queue(); + let polled = Arc::new(polled); + let reader = |polled: Arc>| { + tokio::spawn(async move { + let mut items = Vec::new(); + while let Some(item) = pop(&polled).await.unwrap() { + items.push(item); + } + items + }) + }; + let first = reader(polled.clone()); + let second = reader(polled.clone()); + // Let both park before anything arrives. + tokio::task::yield_now().await; + for _ in 0..100 { + *producer.write().ok().unwrap() += 1; + tokio::task::yield_now().await; + } + assert!(producer.close().is_ok()); + + let first = soon(first).await.unwrap(); + let second = soon(second).await.unwrap(); + let mut all: Vec = first.iter().chain(&second).copied().collect(); + all.sort_unstable(); + assert_eq!(all, (0..100).collect::>()); } #[test] diff --git a/rs/moq-ffi/src/json.rs b/rs/moq-ffi/src/json.rs index 7efb874fc37..c6f35bf22e6 100644 --- a/rs/moq-ffi/src/json.rs +++ b/rs/moq-ffi/src/json.rs @@ -8,13 +8,14 @@ //! parses and serializes on its own side. use std::sync::Arc; +use std::task::{Poll, ready}; use serde_json::Value; use crate::consumer::{MoqTrackConsumer, timestamp_us}; use crate::demand::MoqTrackDemand; use crate::error::MoqError; -use crate::ffi::Task; +use crate::ffi::Polled; use crate::producer::{Child, MoqBroadcastProducer, MoqTrackProducer}; /// Options for a JSON snapshot producer (lossy latest-value mode). @@ -222,14 +223,10 @@ impl MoqJsonValue { } } -struct SnapshotConsumer { - inner: moq_json::snapshot::Consumer, -} - /// Consumes a JSON snapshot track, yielding each reconstructed state or skipping to the newest. #[derive(uniffi::Object)] pub struct MoqJsonSnapshotConsumer { - task: Task, + reads: Polled>, } #[uniffi::export] @@ -244,7 +241,7 @@ impl MoqJsonSnapshotConsumer { let (track, driver) = track.take()?; let consumer = moq_json::snapshot::Consumer::::new(track, Default::default()); Ok(Arc::new(Self { - task: Task::new(SnapshotConsumer { inner: consumer }).supervised(driver), + reads: Polled::new(consumer).supervised(driver), })) } @@ -253,25 +250,29 @@ impl MoqJsonSnapshotConsumer { /// Groups older than the subscription's `max_delay` are skipped, so a playhead /// reader sets that budget to its distance behind live. pub async fn next(&self) -> Result, MoqError> { - self.task - .run(|mut state| async move { MoqJsonValue::new(state.inner.next().await?) }) - .await + let value = self + .reads + .wait(|consumer, waiter| Poll::Ready(Ok(ready!(consumer.poll_next(waiter))?))) + .await?; + MoqJsonValue::new(value) } /// Skip to the newest state, or wait for one. Returns `None` once the track ends. /// /// Skipped states are gone; a later `next()` continues after this one. pub async fn latest(&self) -> Result, MoqError> { - self.task - .run(|mut state| async move { MoqJsonValue::new(state.inner.latest().await?) }) - .await + let value = self + .reads + .wait(|consumer, waiter| Poll::Ready(Ok(ready!(consumer.poll_latest(waiter))?))) + .await?; + MoqJsonValue::new(value) } /// Cancel all current and future `next()` and `latest()` calls. /// /// Terminal: the subscription is released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } @@ -354,14 +355,10 @@ impl MoqJsonStreamProducer { } } -struct StreamConsumer { - inner: moq_json::stream::Consumer, -} - /// Consumes an ordered log of JSON records, yielding every record in order. #[derive(uniffi::Object)] pub struct MoqJsonStreamConsumer { - task: Task, + reads: Polled>, } #[uniffi::export] @@ -376,22 +373,24 @@ impl MoqJsonStreamConsumer { let (track, driver) = track.take()?; let consumer = moq_json::stream::Consumer::::new(track, Default::default()); Ok(Arc::new(Self { - task: Task::new(StreamConsumer { inner: consumer }).supervised(driver), + reads: Polled::new(consumer).supervised(driver), })) } /// Get the next record. Returns `None` once the track ends. pub async fn next(&self) -> Result, MoqError> { - self.task - .run(|mut state| async move { MoqJsonValue::new(state.inner.next().await?) }) - .await + let value = self + .reads + .wait(|consumer, waiter| Poll::Ready(Ok(ready!(consumer.poll_next(waiter))?))) + .await?; + MoqJsonValue::new(value) } /// Cancel all current and future `next()` calls. /// /// Terminal: the subscription is released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } @@ -607,14 +606,10 @@ impl MoqJsonWindowTimed { } } -struct WindowConsumer { - inner: moq_json::window::Consumer, -} - /// Consumes a sliding window of JSON records, yielding each push, pop, and skip in order. #[derive(uniffi::Object)] pub struct MoqJsonWindowConsumer { - task: Task, + reads: Polled>, } #[uniffi::export] @@ -629,7 +624,7 @@ impl MoqJsonWindowConsumer { let (track, driver) = track.take()?; let consumer = moq_json::window::Consumer::::new(track, Default::default()); Ok(Arc::new(Self { - task: Task::new(WindowConsumer { inner: consumer }).supervised(driver), + reads: Polled::new(consumer).supervised(driver), })) } @@ -637,15 +632,17 @@ impl MoqJsonWindowConsumer { /// /// Group rolls never surface: a restated record this reader already has yields nothing. pub async fn next(&self) -> Result, MoqError> { - self.task - .run(|mut state| async move { MoqJsonWindowTimed::new(state.inner.next().await?) }) - .await + let event = self + .reads + .wait(|consumer, waiter| Poll::Ready(Ok(ready!(consumer.poll_next(waiter))?))) + .await?; + MoqJsonWindowTimed::new(event) } /// Cancel all current and future `next()` calls. /// /// Terminal: the subscription is released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } diff --git a/rs/moq-ffi/src/media.rs b/rs/moq-ffi/src/media.rs index 930f09d0b38..5423659217e 100644 --- a/rs/moq-ffi/src/media.rs +++ b/rs/moq-ffi/src/media.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::sync::{Arc, Weak}; +use std::task::{Poll, ready}; use bytes::Buf; use moq_mux::catalog::hang::Extra; @@ -11,7 +12,7 @@ use crate::consumer::{ }; use crate::demand::MoqTrackDemand; use crate::error::MoqError; -use crate::ffi::Task; +use crate::ffi::Polled; use crate::producer::{Child, Children, MoqBroadcastProducer, MoqTrackRequest}; /// A width and height in pixels. @@ -1352,23 +1353,9 @@ fn media_container( /// Reads the latest catalog snapshots from a broadcast. #[derive(uniffi::Object)] pub struct MoqMediaCatalogConsumer { - task: Task, -} - -struct Catalog { // Consume with the untyped `Extra` extension so application sections survive into // `MoqCatalog.sections` instead of being dropped. - inner: moq_mux::catalog::hang::Consumer, -} - -impl Catalog { - async fn next(&mut self) -> Result, MoqError> { - match self.inner.next().await { - Ok(Some(catalog)) => Ok(Some(convert_catalog(&catalog))), - Ok(None) => Ok(None), - Err(e) => Err(e.into()), - } - } + reads: Polled>, } /// One encoded media read: a container-decoded frame, a gap, or an encoder end. @@ -1418,17 +1405,7 @@ impl TryFrom for MoqMediaRead { /// Reads encoded frames and the breaks between them from a live media track. #[derive(uniffi::Object)] pub struct MoqMediaTrackConsumer { - task: Task, -} - -struct Media { - inner: moq_mux::container::Consumer, -} - -impl Media { - async fn next(&mut self) -> Result, MoqError> { - self.inner.read().await?.map(MoqMediaRead::try_from).transpose() - } + reads: Polled>, } #[cfg(test)] @@ -1533,21 +1510,12 @@ mod read_tests { } } -struct MediaGroupInner { - inner: moq_mux::container::GroupConsumer, -} - -impl MediaGroupInner { - async fn next(&mut self) -> Result, MoqError> { - self.inner.read().await?.map(media_frame).transpose() - } -} /// A finite, container-decoded media group, fetched or decoded from a raw group. #[derive(uniffi::Object)] pub struct MoqMediaGroupConsumer { sequence: u64, - task: Task, + reads: Polled>, } impl MoqMediaGroupConsumer { @@ -1559,7 +1527,7 @@ impl MoqMediaGroupConsumer { let inner = moq_mux::container::GroupConsumer::new(group, container); Self { sequence: inner.sequence(), - task: Task::new(MediaGroupInner { inner }).supervised(driver), + reads: Polled::new(inner).supervised(driver), } } } @@ -1573,14 +1541,18 @@ impl MoqMediaGroupConsumer { /// Read the next decoded media frame, or `None` when the group ends. pub async fn next(&self) -> Result, MoqError> { - self.task.run(|mut state| async move { state.next().await }).await + let frame = self + .reads + .wait(|group, waiter| Poll::Ready(Ok(ready!(group.poll_read(waiter))?))) + .await?; + frame.map(media_frame).transpose() } /// Cancel all current and future `next()` calls. /// /// Terminal: the subscription is released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } @@ -1590,14 +1562,18 @@ impl MoqMediaGroupConsumer { impl MoqMediaCatalogConsumer { /// Get the next catalog update. Returns `None` when the track ends or is closed. pub async fn next(&self) -> Result, MoqError> { - self.task.run(|mut state| async move { state.next().await }).await + let catalog = self + .reads + .wait(|catalog, waiter| Poll::Ready(Ok(ready!(catalog.poll_next(waiter))?))) + .await?; + Ok(catalog.as_ref().map(convert_catalog)) } /// Cancel all current and future `next()` calls. /// /// Terminal: the subscription is released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } @@ -1607,14 +1583,18 @@ impl MoqMediaCatalogConsumer { impl MoqMediaTrackConsumer { /// Get the next encoded frame, gap, or end; `None` once the track ends or is closed. pub async fn next(&self) -> Result, MoqError> { - self.task.run(|mut state| async move { state.next().await }).await + let read = self + .reads + .wait(|media, waiter| Poll::Ready(Ok(ready!(media.poll_read(waiter))?))) + .await?; + read.map(MoqMediaRead::try_from).transpose() } /// Cancel all current and future `next()` calls. /// /// Terminal: the subscription is released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } @@ -1635,7 +1615,7 @@ impl MoqMediaCatalogConsumer { .await?; let consumer = moq_mux::catalog::hang::Consumer::from(track); Ok(Arc::new(MoqMediaCatalogConsumer { - task: Task::new(Catalog { inner: consumer }).supervised(broadcast.driver.clone()), + reads: Polled::new(consumer).supervised(broadcast.driver.clone()), })) } } @@ -1675,7 +1655,7 @@ impl MoqMediaTrackConsumer { .await?; let consumer = moq_mux::container::Consumer::new(track, media); Ok(Arc::new(MoqMediaTrackConsumer { - task: Task::new(Media { inner: consumer }).supervised(broadcast.driver.clone()), + reads: Polled::new(consumer).supervised(broadcast.driver.clone()), })) } } diff --git a/rs/moq-ffi/src/origin.rs b/rs/moq-ffi/src/origin.rs index 590e3172b8f..17520d5e720 100644 --- a/rs/moq-ffi/src/origin.rs +++ b/rs/moq-ffi/src/origin.rs @@ -1,10 +1,11 @@ //! Publish, discover, and route broadcasts. use std::sync::Arc; +use std::task::{Poll, ready}; use crate::consumer::MoqBroadcastConsumer; use crate::error::MoqError; -use crate::ffi::{Background, Task}; +use crate::ffi::{Background, Polled}; use crate::producer::MoqBroadcastProducer; /// Config used when creating an origin. @@ -128,7 +129,7 @@ pub struct MoqOriginConsumer { /// A stream of broadcast announcement changes. #[derive(uniffi::Object)] pub struct MoqAnnounceConsumer { - task: Task, + reads: Polled, } #[derive(uniffi::Object)] @@ -138,8 +139,8 @@ pub struct MoqAnnounceConsumer { /// Keeps its origin running, like a published broadcast, after every /// `MoqOriginProducer` is gone. pub struct MoqOriginRequests { - slot: Slot, - task: std::sync::Mutex>>>, + reads: Polled, + driver: Background, } #[derive(uniffi::Object)] @@ -166,66 +167,6 @@ pub struct MoqBroadcastPrepared { driver: Background, } -struct Announced { - inner: moq_net::announce::Consumer, - driver: Background, -} - -/// The served route behind one `MoqOriginProducer::requests` handle, shared -/// between the handle and the task draining it. Emptied by the handle's cancel, -/// which retracts the route: the task sees the empty slot and ends. -type Slot = Arc>>; - -struct OriginRequests { - slot: Slot, - driver: Background, -} - -impl OriginRequests { - async fn requested_broadcast(&mut self) -> Result, MoqError> { - let request = self - .driver - .watch(async { - kio::wait(|waiter| match self.slot.lock().unwrap().as_ref() { - Some(dynamic) => dynamic.poll_requested_broadcast(waiter), - None => std::task::Poll::Ready(Err(moq_net::Error::Closed)), - }) - .await - .map_err(|err| match err { - moq_net::Error::Closed => MoqError::Closed, - other => other.into(), - }) - }) - .await?; - Ok(Arc::new(MoqBroadcastRequest::new(request, self.driver.clone()))) - } -} - -impl Announced { - async fn next(&mut self) -> Result, MoqError> { - self.driver - .watch(async { Ok(self.inner.next().await.map(Into::into)) }) - .await - } -} - -/// Follows one exact path, resolving each publisher instance that serves it. -struct Following { - inner: moq_net::origin::Following, - origin: moq_net::origin::Consumer, - driver: Background, -} - -impl Following { - async fn available(&mut self) -> Result, MoqError> { - let broadcast = self.driver.watch(async { Ok(self.inner.available().await?) }).await?; - Ok(Arc::new(MoqBroadcastConsumer::routed( - broadcast, - self.origin.clone(), - self.driver.clone(), - ))) - } -} /// A route over a prefix, carried by a [`MoqAnnounceEvent`]. /// @@ -315,7 +256,9 @@ impl From for MoqAnnounceEvent { /// Follows a specific broadcast across the publisher instances that serve it. #[derive(uniffi::Object)] pub struct MoqFollowing { - task: Task, + reads: Polled, + origin: moq_net::origin::Consumer, + driver: Background, } impl MoqOriginProducer { @@ -408,13 +351,9 @@ impl MoqOriginProducer { let result = self.inner.requests(prefix); self.inner.driver.check()?; let dynamic = result?; - let slot = Arc::new(std::sync::Mutex::new(Some(dynamic))); Ok(Arc::new(MoqOriginRequests { - slot: slot.clone(), - task: std::sync::Mutex::new(Some(Arc::new(Task::new(OriginRequests { - slot, - driver: self.inner.driver.clone(), - })))), + reads: Polled::new(dynamic).supervised(self.inner.driver.clone()), + driver: self.inner.driver.clone(), })) } @@ -462,10 +401,7 @@ impl MoqOriginConsumer { .scope("", &moq_net::Patterns::from(filter))? .with_hidden(config.hidden); Ok(Arc::new(MoqAnnounceConsumer { - task: Task::new(Announced { - inner: origin.announced(), - driver: self.driver.clone(), - }), + reads: Polled::new(origin.announced()).supervised(self.driver.clone()), })) } @@ -485,11 +421,9 @@ impl MoqOriginConsumer { let inner = self.inner.follow(path.as_str(), epoch)?; Ok(Arc::new(MoqFollowing { - task: Task::new(Following { - inner, - origin: self.inner.clone(), - driver: self.driver.clone(), - }), + reads: Polled::new(inner).supervised(self.driver.clone()), + origin: self.inner.clone(), + driver: self.driver.clone(), })) } @@ -526,21 +460,28 @@ impl MoqOriginRequests { /// Returns a [`MoqBroadcastRequest`]: accept it with a broadcast producer or reject /// it with an application error code. The requesting consumer stays pending until then. pub async fn requested_broadcast(&self) -> Result, MoqError> { - let task = self.task.lock().unwrap().clone().ok_or(MoqError::Closed)?; - task.run(|mut state| async move { state.requested_broadcast().await }) - .await + // A call after cancel finds the route retracted, rather than racing the cancel. + if self.reads.is_cancelled() { + return Err(MoqError::Closed); + } + let request = self + .reads + .wait(|dynamic, waiter| { + Poll::Ready(match ready!(dynamic.poll_requested_broadcast(waiter)) { + Ok(request) => Ok(request), + Err(moq_net::Error::Closed) => Err(MoqError::Closed), + Err(err) => Err(err.into()), + }) + }) + .await?; + Ok(Arc::new(MoqBroadcastRequest::new(request, self.driver.clone()))) } /// Stop serving and retract the route. Terminal: this handler is released /// here, not when the handle is, so pending requests are rejected before /// this returns. pub fn cancel(&self) { - let _guard = crate::ffi::enter(); - let dynamic = self.slot.lock().unwrap().take(); - drop(dynamic); - if let Some(task) = self.task.lock().unwrap().take() { - task.cancel(); - } + self.reads.cancel(); } } @@ -654,14 +595,16 @@ impl MoqPublication { impl MoqAnnounceConsumer { /// Get the next announce event. Returns `None` when the origin is closed. pub async fn next(&self) -> Result, MoqError> { - self.task.run(|mut state| async move { state.next().await }).await + self.reads + .wait(|announced, waiter| Poll::Ready(Ok(ready!(announced.poll_next(waiter)).map(Into::into)))) + .await } /// Cancel all current and future `next()` calls. /// /// Terminal: the announcement stream is released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } @@ -678,14 +621,22 @@ impl MoqFollowing { /// previous instance then and read the new one: the old one's tracks end when its route /// dies, with an error unless this side closed that session. pub async fn available(&self) -> Result, MoqError> { - self.task.run(|mut state| async move { state.available().await }).await + let broadcast = self + .reads + .wait(|following, waiter| Poll::Ready(Ok(ready!(following.poll_available(waiter))?))) + .await?; + Ok(Arc::new(MoqBroadcastConsumer::routed( + broadcast, + self.origin.clone(), + self.driver.clone(), + ))) } /// Cancel all current and future `available()` calls. /// /// Terminal: the announcement watch is released here, not when the handle is. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } diff --git a/rs/moq-ffi/src/producer.rs b/rs/moq-ffi/src/producer.rs index db3e02b6c2d..319bb438a98 100644 --- a/rs/moq-ffi/src/producer.rs +++ b/rs/moq-ffi/src/producer.rs @@ -1,13 +1,14 @@ //! Publish broadcasts, tracks, and groups. use std::sync::Arc; +use std::task::{Poll, ready}; use moq_mux::catalog::hang::Extra; use crate::consumer::{MoqBroadcastConsumer, MoqGroupConsumer, MoqOrder, MoqSubscription, MoqTrackConsumer}; use crate::demand::MoqTrackDemand; use crate::error::MoqError; -use crate::ffi::{Background, Task}; +use crate::ffi::{Background, Polled}; use crate::media::MoqFrame; /// A location within a track, mirroring [`moq_net::track::Position`]. @@ -226,49 +227,24 @@ pub struct MoqBroadcastProducer { /// A handler for requests for tracks in a broadcast. #[derive(uniffi::Object)] pub struct MoqBroadcastRequests { - task: Task, + reads: Polled, + children: Children, } /// Serves on-demand fetches of uncached groups for one track. #[derive(uniffi::Object)] pub struct MoqTrackRequests { - task: Task, + reads: Polled, } impl MoqTrackRequests { fn new(inner: moq_net::track::Requests) -> Self { Self { - task: Task::new(TrackDynamicProducer { inner }), + reads: Polled::new(inner), } } } -struct RequestsProducer { - inner: moq_net::broadcast::Requests, - children: Children, -} - -struct TrackDynamicProducer { - inner: moq_net::track::Requests, -} - -impl RequestsProducer { - async fn requested_track(&mut self) -> Result, MoqError> { - // Hand back the un-accepted request, mirroring `moq_net::broadcast::Requests`: the caller - // accepts it (raw, at a chosen timescale) or publishes media onto it (importer accepts). - // The subscriber's subscribe stays pending until then. - let request = self.inner.requested_track().await?; - Ok(Arc::new(MoqTrackRequest::new(request, self.children.clone()))) - } -} - -impl TrackDynamicProducer { - async fn requested_fill(&mut self) -> Result, MoqError> { - let fill = self.inner.requested_fill().await?; - Ok(Arc::new(MoqFill::new(fill))) - } -} - impl MoqBroadcastProducer { /// Wrap a `moq_net::broadcast::Producer` (standalone or origin-created), attaching /// the catalog track every FFI broadcast carries. @@ -334,11 +310,8 @@ impl MoqBroadcastProducer { let _guard = crate::ffi::enter(); self.with_state(|state| { Ok(Arc::new(MoqBroadcastRequests { - task: Task::new(RequestsProducer { - inner: state.broadcast.requests(), - children: state.children.clone(), - }) - .supervised(state.driver.clone()), + reads: Polled::new(state.broadcast.requests()).supervised(state.driver.clone()), + children: state.children.clone(), })) }) } @@ -410,9 +383,14 @@ impl MoqBroadcastRequests { /// /// Returns an error once the broadcast is closed or aborted. pub async fn requested_track(&self) -> Result, MoqError> { - self.task - .run(|mut state| async move { state.requested_track().await }) - .await + // Hand back the un-accepted request, mirroring `moq_net::broadcast::Requests`: the caller + // accepts it (raw, at a chosen timescale) or publishes media onto it (importer accepts). + // The subscriber's subscribe stays pending until then. + let request = self + .reads + .wait(|requests, waiter| Poll::Ready(Ok(ready!(requests.poll_requested_track(waiter))?))) + .await?; + Ok(Arc::new(MoqTrackRequest::new(request, self.children.clone()))) } /// Cancel all current and future `requested_track()` calls. @@ -420,7 +398,7 @@ impl MoqBroadcastRequests { /// Terminal: the dynamic broadcast is released here, not when the handle is, so any pending /// request is rejected. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } @@ -433,9 +411,11 @@ impl MoqTrackRequests { /// /// Cached groups are served without reaching this method. pub async fn requested_fill(&self) -> Result, MoqError> { - self.task - .run(|mut state| async move { state.requested_fill().await }) - .await + let fill = self + .reads + .wait(|requests, waiter| Poll::Ready(Ok(ready!(requests.poll_requested_fill(waiter))?))) + .await?; + Ok(Arc::new(MoqFill::new(fill))) } /// Cancel all current and future `requested_fill()` calls. @@ -443,7 +423,7 @@ impl MoqTrackRequests { /// Terminal: the dynamic track is released here, not when the handle is, so any pending /// fetch is rejected. pub fn cancel(&self) { - self.task.cancel(); + self.reads.cancel(); } } @@ -457,13 +437,13 @@ impl MoqTrackRequests { #[derive(uniffi::Object)] pub struct MoqFill { inner: moq_net::track::Fill, - // The watcher `wanted_changed` advances, kept apart so concurrent waits queue in turn. - changes: crate::ffi::Task, + /// The watcher `wanted_changed` advances, kept apart from `inner` so its cursor is its own. + changes: Polled, } impl MoqFill { fn new(inner: moq_net::track::Fill) -> Self { - let changes = crate::ffi::Task::new(inner.clone()); + let changes = Polled::new(inner.clone()); Self { inner, changes } } } @@ -509,10 +489,10 @@ impl MoqFill { /// Wait for `wanted` to change and return the new value. The first call returns at once. pub async fn wanted_changed(&self) -> Result, MoqError> { - // Driven in place, not detached: a cancelled call left unfreed must not advance the + // Polled in place, not detached: a cancelled call left unfreed must not advance the // watcher and swallow the change the next call waits for. self.changes - .run(|mut fill| async move { Ok(fill.wanted_changed().await) }) + .wait(|fill, waiter| Poll::Ready(Ok(ready!(fill.poll_wanted_changed(waiter))))) .await } diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index fd119bcfa2a..37fef124dd4 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -5167,23 +5167,6 @@ async fn raw_track_pending_datagram_does_not_block_read_frame() { pending_datagram_does_not_block_group(|consumer| async move { consumer.read_frame().await }).await; } -#[tokio::test] -async fn raw_track_pending_group_read_holds_the_lane() { - let (_broadcast, _track, consumer) = raw_track(); - - let group_read = { - let consumer = consumer.clone(); - spawn_parked(async move { consumer.next_group().await }).await - }; - assert!(!group_read.is_finished(), "group read should still be pending"); - assert!( - consumer.group_lane_busy(), - "a parked group read should hold the lane guard" - ); - - group_read.abort(); -} - #[tokio::test] async fn raw_track_update_during_pending_group_still_reads_datagram() { let (_broadcast, track, consumer) = raw_track(); From 88b32fb02a58d466a776ccb9ea3390fafc48add7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 23:20:53 -0700 Subject: [PATCH 3/8] wip: tests and changelog for poll-native reads --- rs/moq-ffi/CHANGELOG.md | 6 + rs/moq-ffi/src/test.rs | 285 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 291 insertions(+) diff --git a/rs/moq-ffi/CHANGELOG.md b/rs/moq-ffi/CHANGELOG.md index 309ba71dcad..371588cddad 100644 --- a/rs/moq-ffi/CHANGELOG.md +++ b/rs/moq-ffi/CHANGELOG.md @@ -26,6 +26,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 subscribe with `MoqAudioConsumer::subscribe` / `MoqVideoConsumer::subscribe`. Codec-only `MoqAudioEncoder` / `MoqVideoEncoder` are available alongside them. - Audio frame duration defaults to the codec's own frame; video GOP mode is an enum. +- Track, group, and datagram reads, announcement and follow streams, request and fill + waits, the JSON and media consumers, and grant and bandwidth waits poll their handle's + state directly. Concurrent calls on one handle no longer queue in call order: each + takes the next item as it arrives, and sequence or arrival order is unchanged. A call + the binding cancelled but has not freed yet no longer holds up the next one, and + `cancel()` on these handles releases their state before it returns. ## [0.4.12](https://github.com/moq-dev/moq/compare/moq-ffi-v0.4.11...moq-ffi-v0.4.12) - 2026-10-06 diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 37fef124dd4..1172755042f 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -8471,3 +8471,288 @@ async fn next_encoded_frame(consumer: &MoqMediaTrackConsumer) -> Result> = Mutex::new(Vec::new()); + /// Notified on each continuation. + static CALLED: Condvar = Condvar::new(); + + extern "C" fn continuation(data: u64, poll: RustFuturePoll) { + CONTINUATIONS.lock().unwrap().push((data, poll)); + CALLED.notify_all(); + } + + /// Wait for a continuation for `data`, then take every one called for it. + fn wait_called(data: u64) -> Vec { + let continuations = CONTINUATIONS.lock().unwrap(); + let (continuations, timeout) = CALLED + .wait_timeout_while(continuations, super::TIMEOUT, |continuations| { + !continuations.iter().any(|(d, _)| *d == data) + }) + .unwrap(); + assert!(!timeout.timed_out(), "the read never woke"); + drop(continuations); + called(data) + } + + /// Take the continuations called for `data`. + fn called(data: u64) -> Vec { + let mut continuations = CONTINUATIONS.lock().unwrap(); + let (called, rest) = continuations.drain(..).partition(|(d, _)| *d == data); + *continuations = rest; + called.into_iter().map(|(_, poll)| poll).collect::>() + } + + /// A read as a foreign future, resolving to the frame's payload. + pub(super) struct Read { + handle: Handle, + data: u64, + } + + impl Read { + pub(super) fn start(read: F, data: u64) -> Self + where + F: Future, MoqError>> + Send + 'static, + { + let handle = uniffi::rust_future_new( + async move { + let frame = read.await.expect("read failed").expect("read ended"); + Ok::(frame.payload[0]) + }, + crate::UniFfiTag, + ); + let read = Self { handle, data }; + read.poll(); + read + } + + fn poll(&self) { + // SAFETY: the handle is live until `Drop` frees it. + unsafe { uniffi::rust_future_poll::(self.handle.clone(), continuation, self.data) } + } + + /// Whether the read is still parked: no continuation since its last poll. + pub(super) fn parked(&self) -> bool { + called(self.data).is_empty() + } + + /// Stop polling without freeing, which is all `rust_future_cancel` does: Kotlin and Swift + /// leave this window open until they free the future. + pub(super) fn cancel(&self) { + // SAFETY: the handle is live until `Drop` frees it. + unsafe { uniffi::rust_future_cancel::(self.handle.clone()) }; + assert_eq!(called(self.data), [RustFuturePoll::Ready], "cancel resolves the continuation"); + } + + /// Poll on every wake until ready, then complete the read. + pub(super) fn finish(&self) -> u8 { + while !wait_called(self.data).contains(&RustFuturePoll::Ready) { + self.poll(); + } + let mut status = uniffi::RustCallStatus::default(); + // SAFETY: the handle is live, and its continuation reported it ready. + let value = unsafe { uniffi::rust_future_complete::(self.handle.clone(), &mut status) }; + assert_eq!(status.code, uniffi::RustCallStatusCode::Success); + value + } + } + + impl Drop for Read { + fn drop(&mut self) { + // SAFETY: freed once, here. + unsafe { uniffi::rust_future_free::(self.handle.clone()) }; + } + } +} + +/// A read the foreign side cancelled but has not freed yet holds nothing the next read on the +/// same handle needs, so the next read resolves while the first is still allocated. +fn cancelled_unfreed_read_does_not_block_the_next(read: F, write: impl FnOnce(), data: u64) +where + F: Fn() -> Fut, + Fut: Future, MoqError>> + Send + 'static, +{ + let first = rust_future::Read::start(read(), data); + assert!(first.parked(), "the first read parks"); + first.cancel(); + + let second = rust_future::Read::start(read(), data + 1); + write(); + assert_eq!(second.finish(), 7, "the second read gets the frame written after the cancel"); + drop(second); + drop(first); +} + +#[test] +fn raw_read_frame_cancelled_unfreed_does_not_block_the_next() { + let (_broadcast, track, consumer) = raw_track(); + cancelled_unfreed_read_does_not_block_the_next( + || { + let consumer = consumer.clone(); + async move { consumer.read_frame().await } + }, + || track.write_frame(group_frame(&[7])).unwrap(), + 100, + ); +} + +#[test] +fn raw_group_read_frame_cancelled_unfreed_does_not_block_the_next() { + let (_broadcast, track, _consumer) = raw_track(); + let group = track.append_group().unwrap(); + let consumer = group.consume().unwrap(); + cancelled_unfreed_read_does_not_block_the_next( + || { + let consumer = consumer.clone(); + async move { consumer.read_frame().await } + }, + || group.write_frame(group_frame(&[7])).unwrap(), + 200, + ); +} + +/// Reads parked together on one track each wake, and between them take every group once, +/// whether they go through `read_frame` or `next_group`. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn raw_track_concurrent_reads_take_every_group_once() { + const GROUPS: u64 = 200; + let broadcast = MoqBroadcastProducer::new().unwrap(); + let track = broadcast.publish_track("events".into(), Some(timed_info())).unwrap(); + // No age limit, so a group nobody has read yet is never skipped for a newer one. + let consumer = track + .consume(Some(MoqSubscription { + max_delay: None, + ..moq_subscription_defaults() + })) + .unwrap(); + + // Each reader returns the group sequences it took: a frame's payload names its group. + let frames = |consumer: Arc| { + tokio::spawn(async move { + let mut taken = Vec::new(); + while let Some(frame) = consumer.read_frame().await.unwrap() { + taken.push(u64::from_be_bytes(frame.payload.try_into().unwrap())); + } + taken + }) + }; + let groups = |consumer: Arc| { + tokio::spawn(async move { + let mut taken = Vec::new(); + while let Some(group) = consumer.next_group().await.unwrap() { + taken.push(group.sequence()); + } + taken + }) + }; + let readers = [ + frames(consumer.clone()), + frames(consumer.clone()), + groups(consumer.clone()), + groups(consumer.clone()), + ]; + ffi_caught_up().await; + + for sequence in 0..GROUPS { + let group = track.create_group(sequence).unwrap(); + group + .write_frame(MoqFrame { + payload: sequence.to_be_bytes().to_vec(), + timestamp_us: Some(sequence), + }) + .unwrap(); + group.finish().unwrap(); + if sequence % 8 == 0 { + tokio::task::yield_now().await; + } + } + track.finish().unwrap(); + + let mut taken = Vec::new(); + let mut each = Vec::new(); + for reader in readers { + let reader = moq_clock::timeout(TIMEOUT, reader) + .await + .expect("a parked read never woke") + .expect("reader panicked"); + each.push(reader.len()); + taken.extend(reader); + } + taken.sort_unstable(); + assert_eq!(taken, (0..GROUPS).collect::>(), "per reader: {each:?}"); +} + +/// A poll in flight when shutdown starts finishes before shutdown returns, a call parked +/// across it resolves `Cancelled`, and so does every later call. +#[test] +fn shutdown_waits_for_a_polled_call() { + if !shutdown_child("shutdown_waits_for_a_polled_call") { + return; + } + + let polled = Arc::new(crate::ffi::Polled::new(())); + let block_on = |future: std::pin::Pin> + Send>>| { + std::thread::spawn(move || { + tokio::runtime::Builder::new_current_thread() + .build() + .unwrap() + .block_on(future) + }) + }; + + // Parked with no item coming: only the shutdown can end it. + let parked = block_on(Box::pin({ + let polled = polled.clone(); + async move { polled.wait(|_, _| std::task::Poll::Pending).await } + })); + + let (entered, entered_rx) = std::sync::mpsc::channel(); + let (release, release_rx) = std::sync::mpsc::channel::<()>(); + let holding = block_on(Box::pin({ + let polled = polled.clone(); + async move { + polled + .wait(move |_, _| { + entered.send(()).unwrap(); + // Holds this poll open while shutdown starts on another thread. + release_rx.recv().unwrap(); + std::task::Poll::Ready(Ok(7)) + }) + .await + } + })); + entered_rx.recv().unwrap(); + + let shutdown = std::thread::spawn(|| crate::moq_ffi_shutdown()); + + // A fresh call resolves `Cancelled` on its first poll once shutdown has begun. On another + // handle, since the held poll holds this one's state. + let probe = crate::ffi::Polled::new(()); + loop { + let mut call = std::pin::pin!(probe.wait(|_, _| std::task::Poll::Ready(Ok(0u8)))); + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + match call.as_mut().poll(&mut cx) { + std::task::Poll::Ready(Err(MoqError::Cancelled)) => break, + std::task::Poll::Ready(result) => assert!(matches!(result, Ok(0)), "{result:?}"), + std::task::Poll::Pending => unreachable!("the call is ready or cancelled"), + } + std::thread::yield_now(); + } + assert!(!shutdown.is_finished(), "shutdown returned with a poll in flight"); + + release.send(()).unwrap(); + // It began before the shutdown, so it delivers; the shutdown waited for it. + assert!(matches!(holding.join().unwrap(), Ok(7))); + shutdown.join().unwrap(); + let parked = parked.join().unwrap(); + assert!(matches!(parked, Err(MoqError::Cancelled)), "{parked:?}"); +} From 23af1c67f7a2fe3e56d9fc7030a1852abd31ec19 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 23:27:54 -0700 Subject: [PATCH 4/8] wip: format, finish quest --- quest/m2/README.md | 1 - quest/m2/ffi-poll-migrate.md | 80 ------------------------------------ rs/moq-ffi/src/ffi.rs | 4 +- rs/moq-ffi/src/media.rs | 1 - rs/moq-ffi/src/origin.rs | 1 - rs/moq-ffi/src/test.rs | 16 ++++++-- 6 files changed, 15 insertions(+), 88 deletions(-) delete mode 100644 quest/m2/ffi-poll-migrate.md diff --git a/quest/m2/README.md b/quest/m2/README.md index 337557447b7..8154182dfd7 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -40,7 +40,6 @@ lite-07; bitrate enforcement is not part of this release. - [A runtime-free QUIC driver](/quest/m2/quic-sansio-driver.md) - a caller's own loop moves UDP datagrams and polls moq sessions over moq-quic, for rust2's bindings - [MoQ on rind](/quest/m2/rind/README.md) - every MoQ crate takes its crypto from rind, a pure-Rust graviola fork, so no build compiles C crypto - [Benchmark sources](/quest/m2/bench-sources.md) - every measuring nightly benchmark feeds the regression table, and none is a smoke run -- [Poll-native async](/quest/m2/ffi-poll-migrate.md) - moq-ffi's pure-kio async exports poll their state directly instead of going through `ffi::Task` - [QoS](/quest/m2/qos/README.md) - the relay's egress `lag` histogram and `dropped` counters on the stats totals and prefix tracks - [Lazy retained HLS instances](/quest/m2/hls-lazy-instances.md) - a replaced HLS instance starts its watchers only when a request reaches it - [HLS and DASH browser playback](/quest/m2/hls-player-test.md) - hls.js and dash.js play, reload, switch, and survive reconfiguration nightly diff --git a/quest/m2/ffi-poll-migrate.md b/quest/m2/ffi-poll-migrate.md deleted file mode 100644 index c211625940c..00000000000 --- a/quest/m2/ffi-poll-migrate.md +++ /dev/null @@ -1,80 +0,0 @@ -# [L] Poll-native async in moq-ffi - -## Goal - -Every moq-ffi async export over kio-backed state (track, group, and datagram -reads, announcements, the JSON and media consumers) is a `kio::wait` -over the underlying `poll_*`, which UniFFI's `RustFuture` polls directly. -`ffi::Task`'s tokio `Mutex`, `watch` cancel flag, and `select!` are gone -from those paths. Export signatures, and so every -binding, are unchanged. - -## Plan - -Decided with the maintainer on 2026-10-16, after -[the spike](https://github.com/moq-dev/moq/pull/5851): Shape A. The -prototype is on the `spike/ffi-poll-prototype` branch; delete that branch -once this lands. - -- **No UniFFI change on this path**: dropping a future at - `rust_future_cancel` (mozilla/uniffi-rs#2771) is optional in - [the fork](/quest/m2/uniffi-fork.md). Shape A has no read lane for an - unfreed future to hold, so this quest must not wait for it. -- **What the spike found**: in Go, Shape A cut a ready `read_frame` from 2.3 - to 1.2 µs, and where every read parks it raised throughput 13 to 32 percent - and cut CPU per frame 20 to 30 percent across 1 to 64 subscribers. Python - was within noise, since asyncio's own wakeup dominates. With - no read lane, a cancelled read that UniFFI has not freed yet no longer - blocks the next read, so this needs no UniFFI change. -- **Keep shutdown's contract**: `moq_ffi_shutdown` documents that every - parked and later call resolves `Cancelled`. The spike's prototype broke - that: its kio reads delivered frames after shutdown. Each poll runs inside - shutdown's in-flight accounting (today's `Polling` guard, or something as - strong), so shutdown cannot return while a poll that missed it still - delivers an item. -- **Handle state**: a std `Mutex` held only across one poll, never across a - wait. Cancel closes a kio channel that every poll checks, then drops the - state. Driver supervision (`Background`) is kio already, so poll it in the - same closure. -- **Concurrency**: two concurrent reads on one handle no longer queue FIFO; - both park, and each poll takes the next item atomically. Iteration order - (sequence or arrival) is unchanged; only which concurrent call gets which - item is. Check wrapper docs that promise serialization. -- **Multi-step reads**: an operation that spans polls through shared handle - state must tolerate another call changing it between polls. The track's - `read_frame` acquires a group into the shared `pending` slot, then reads - from it on a later poll. A concurrent `next_group`, or a second - `read_frame`, can take that group in between, and the current `expect` would - panic. Either make the whole step one poll, re-acquire when the slot is - empty, or keep per-operation ownership for these reads. -- **Still lost on cancel**: when a RustFuture finishes and the foreign side - then drops it as cancelled (Python asyncio cancel, Go ctx), the result is - gone. The spike measured this in both `Task` and Shape A. Keep the Go - wrapper's terminal cancel. [Foreign poll](https://github.com/moq-dev/rust2/blob/main/quest/m1/ffi-poll-protocol.md) - fixes it later for the hot reads. -- **What stays**: migrate an export only when it is pure kio polling over - handle state. Anything that awaits tokio channels, codecs, or I/O keeps - `Task`, which supplies its serialized state, terminal cancel, and - shutdown's in-flight counter. Examples: client `connect`, server `listen` - and `accept`, request `accept` and `reject`, session `closed` and `status`, - and the video and audio consumers' `next`, which drive codecs (cancelling a - queued video decode poisons its sink). Demand waits and session `shutdown` - stay `detached`. Inventory every `Task::run` and `detached` site in the PR, each marked - migrated or kept, with the reason. -- **Tests**: a read cancelled but not yet freed does not block the next read - on the same handle (drive `rust_future_*` directly, as the spike did). A - read parked across shutdown resolves `Cancelled` without a crash. Two reads - parked on one handle each wake, and between them they take every item - exactly once. That includes a mixed `read_frame` and `next_group` on one - track. -- **Benchmark both fan-out axes**: the spike swept subscribers at one - publisher. Confirm the gain on the binding benchmark across publishers and - subscribers before and after; the Go runner should move. - -Public API: no signature changes. Concurrent reads on one handle no longer -get items in call order; sequence and arrival order are unchanged. Wire: none. - -## Related - -- [Foreign poll for hot reads](https://github.com/moq-dev/rust2/blob/main/quest/m1/ffi-poll-protocol.md) - the generated protocol that follows this, once the fork exists -- [Libraries spawn nothing](/quest/m1/poll-first/no-spawn.md) - moq-ffi spawns only its root at the binding boundary diff --git a/rs/moq-ffi/src/ffi.rs b/rs/moq-ffi/src/ffi.rs index beeae454411..22206c8372e 100644 --- a/rs/moq-ffi/src/ffi.rs +++ b/rs/moq-ffi/src/ffi.rs @@ -275,7 +275,9 @@ impl Background { /// Ready with the owner's failure once it fails; pending while it runs or after it ended /// cleanly, the same split [`Self::watch`] makes. pub fn poll_failed(&self, waiter: &kio::Waiter) -> Poll { - let Some(result) = &self.result else { return Poll::Pending }; + let Some(result) = &self.result else { + return Poll::Pending; + }; let completed = ready!(result.poll(waiter, |state| match &**state { Some(completed) => Poll::Ready(completed.clone()), None => Poll::Pending, diff --git a/rs/moq-ffi/src/media.rs b/rs/moq-ffi/src/media.rs index 5423659217e..f9d4d65db65 100644 --- a/rs/moq-ffi/src/media.rs +++ b/rs/moq-ffi/src/media.rs @@ -1510,7 +1510,6 @@ mod read_tests { } } - /// A finite, container-decoded media group, fetched or decoded from a raw group. #[derive(uniffi::Object)] pub struct MoqMediaGroupConsumer { diff --git a/rs/moq-ffi/src/origin.rs b/rs/moq-ffi/src/origin.rs index 17520d5e720..b1cda86e77f 100644 --- a/rs/moq-ffi/src/origin.rs +++ b/rs/moq-ffi/src/origin.rs @@ -167,7 +167,6 @@ pub struct MoqBroadcastPrepared { driver: Background, } - /// A route over a prefix, carried by a [`MoqAnnounceEvent`]. /// /// Carries no broadcast: resolve a specific path with diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 1172755042f..48be58e8cfb 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -7187,8 +7187,8 @@ async fn cancel_before_connect_fails_fast() { /// Dropping the future returned by a `Task::run` call used to leave the spawned /// closure detached and still holding the state lock, so it consumed the next /// transition into its own cursor and the retry blocked behind it, missing the -/// edge. Every repeatable read on the bindings (`status`, `next`, `read_frame`, -/// `recv_datagram`) sits on that path; `status` is just the easiest to drive. +/// edge. Session `status` still sits on that path; the poll-native reads have their +/// own cancelled-call tests. #[tokio::test] async fn cancelled_state_wait_does_not_swallow_the_next_transition() { let server = MoqServer::new(MoqServerConfig { @@ -8550,7 +8550,11 @@ mod rust_future { pub(super) fn cancel(&self) { // SAFETY: the handle is live until `Drop` frees it. unsafe { uniffi::rust_future_cancel::(self.handle.clone()) }; - assert_eq!(called(self.data), [RustFuturePoll::Ready], "cancel resolves the continuation"); + assert_eq!( + called(self.data), + [RustFuturePoll::Ready], + "cancel resolves the continuation" + ); } /// Poll on every wake until ready, then complete the read. @@ -8587,7 +8591,11 @@ where let second = rust_future::Read::start(read(), data + 1); write(); - assert_eq!(second.finish(), 7, "the second read gets the frame written after the cancel"); + assert_eq!( + second.finish(), + 7, + "the second read gets the frame written after the cancel" + ); drop(second); drop(first); } From 256438d965c377a7284c874459103e7d5e6323a7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 00:12:03 -0700 Subject: [PATCH 5/8] fix(ffi): wake a parked read_frame when next_group takes its group --- rs/moq-ffi/src/consumer.rs | 10 +++++++-- rs/moq-ffi/src/test.rs | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/rs/moq-ffi/src/consumer.rs b/rs/moq-ffi/src/consumer.rs index 256ea1570c0..7c2938d6dd8 100644 --- a/rs/moq-ffi/src/consumer.rs +++ b/rs/moq-ffi/src/consumer.rs @@ -399,6 +399,8 @@ struct OrderedCursor { track: moq_net::track::Ordered, /// Group whose first frame `read_frame` has not returned yet. pending: Option, + /// `read_frame` calls parked on `pending`, woken when `next_group` takes it from them. + readers: kio::WaiterList, } struct TrackInner { @@ -436,6 +438,7 @@ impl TrackInner { self.track = Cursor::Ordered(Box::new(OrderedCursor { track: track.ordered(), pending: None, + readers: kio::WaiterList::new(), })); } match &mut self.track { @@ -457,6 +460,8 @@ impl TrackInner { match self.ordered() { Ok(ordered) => { if let Some(group) = ordered.pending.take() { + // A frame reader parked on it would otherwise wait for a frame it no longer reads. + ordered.readers.wake(); return Poll::Ready(Ok(Some(group))); } ordered.track.poll_next_group(waiter).map(|r| r.map_err(Into::into)) @@ -468,8 +473,8 @@ impl TrackInner { /// The first frame of the next group in sequence order, skipping completed empty groups. /// /// One poll does the whole step, so it never trusts a slot it filled on an earlier poll: - /// a group another call took from `pending` in between is simply gone, and this acquires - /// the next one. A group still waiting on its first frame stays in `pending`, so a + /// a group `next_group` took from `pending` in between is simply gone, and the wake it + /// sends on `readers` makes this acquire the next one. A group still waiting on its first frame stays in `pending`, so a /// cancelled call leaves it for the next `read_frame` or `next_group`. fn poll_read_frame(&mut self, waiter: &kio::Waiter) -> Poll, MoqError>> { let ordered = match self.ordered() { @@ -485,6 +490,7 @@ impl TrackInner { Err(err) => return Poll::Ready(Err(err.into())), }, }; + waiter.register(&mut ordered.readers); let result = ready!(group.poll_read_frame(waiter)); // Drop a completed or failed group so the next pass moves on. ordered.pending = None; diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 48be58e8cfb..3b2128dd659 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -8628,6 +8628,50 @@ fn raw_group_read_frame_cancelled_unfreed_does_not_block_the_next() { ); } +/// A `read_frame` parked on a frameless group moves on when `next_group` takes that group +/// from it, rather than waiting for a frame it no longer reads. +#[tokio::test] +async fn raw_read_frame_moves_on_when_next_group_takes_its_group() { + let broadcast = MoqBroadcastProducer::new().unwrap(); + let track = broadcast.publish_track("events".into(), Some(timed_info())).unwrap(); + let consumer = track + .consume(Some(MoqSubscription { + max_delay: None, + ..moq_subscription_defaults() + })) + .unwrap(); + + let open = track.create_group(0).unwrap(); + let read = { + let consumer = consumer.clone(); + spawn_parked(async move { consumer.read_frame().await }).await + }; + let later = track.create_group(1).unwrap(); + later.write_frame(group_frame(b"later")).unwrap(); + later.finish().unwrap(); + // Let the read run any wake the new group sent, so it is parked on the open group again. + for _ in 0..4 { + tokio::task::yield_now().await; + } + assert!(!read.is_finished(), "the open group has no frame yet"); + + let taken = moq_clock::timeout(TIMEOUT, consumer.next_group()) + .await + .expect("next_group timed out") + .unwrap() + .expect("expected the open group"); + assert_eq!(taken.sequence(), 0); + + let frame = moq_clock::timeout(TIMEOUT, read) + .await + .expect("the parked read_frame never moved on") + .expect("read task panicked") + .unwrap() + .expect("expected the later group's frame"); + assert_eq!(frame.payload, b"later"); + drop(open); +} + /// Reads parked together on one track each wake, and between them take every group once, /// whether they go through `read_frame` or `next_group`. #[tokio::test(flavor = "multi_thread", worker_threads = 4)] From d60a595f69e0284462a4122d677c11db53738bbe Mon Sep 17 00:00:00 2001 From: quest fixer Date: Sun, 1 Nov 2026 08:57:18 -0800 Subject: [PATCH 6/8] fix(ffi): follow base clock module rename in tests --- rs/moq-ffi/src/test.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index bb1b7f8c72b..11a4cd944c0 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -8671,14 +8671,14 @@ async fn raw_read_frame_moves_on_when_next_group_takes_its_group() { } assert!(!read.is_finished(), "the open group has no frame yet"); - let taken = moq_clock::timeout(TIMEOUT, consumer.next_group()) + let taken = kio::clock::timeout(TIMEOUT, consumer.next_group()) .await .expect("next_group timed out") .unwrap() .expect("expected the open group"); assert_eq!(taken.sequence(), 0); - let frame = moq_clock::timeout(TIMEOUT, read) + let frame = kio::clock::timeout(TIMEOUT, read) .await .expect("the parked read_frame never moved on") .expect("read task panicked") @@ -8748,7 +8748,7 @@ async fn raw_track_concurrent_reads_take_every_group_once() { let mut taken = Vec::new(); let mut each = Vec::new(); for reader in readers { - let reader = moq_clock::timeout(TIMEOUT, reader) + let reader = kio::clock::timeout(TIMEOUT, reader) .await .expect("a parked read never woke") .expect("reader panicked"); From afce050de0793a2a662850ba89fc2fd95ece8e1a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 14:23:13 -0800 Subject: [PATCH 7/8] fix(ffi): wake every call parked on a polled handle moq-net's track cursor keeps one waker per read, so with several calls parked on one MoqTrackConsumer only the latest caller ever woke and the rest hung (raw_track_concurrent_reads_take_every_group_once failed most runs, which is what ejected this PR from the land queue). Polled now keeps its state behind a kio::Bridge and always polls it with the bridge's one waiter, which wakes every parked call. The bridge also holds a wake sent under its lock until the lock is released, so a binding that polls a woken call inline cannot deadlock on the state. That covers next_group waking a read_frame parked on the group it took, so the track's separate readers list is gone. Co-Authored-By: Claude Opus 5.5 --- rs/moq-ffi/src/consumer.rs | 13 ++-- rs/moq-ffi/src/ffi.rs | 120 ++++++++++++++++++++++++++----------- rs/moq-ffi/src/test.rs | 60 +++++++++++++++++++ 3 files changed, 149 insertions(+), 44 deletions(-) diff --git a/rs/moq-ffi/src/consumer.rs b/rs/moq-ffi/src/consumer.rs index 7c2938d6dd8..9e3c4499002 100644 --- a/rs/moq-ffi/src/consumer.rs +++ b/rs/moq-ffi/src/consumer.rs @@ -399,8 +399,6 @@ struct OrderedCursor { track: moq_net::track::Ordered, /// Group whose first frame `read_frame` has not returned yet. pending: Option, - /// `read_frame` calls parked on `pending`, woken when `next_group` takes it from them. - readers: kio::WaiterList, } struct TrackInner { @@ -438,7 +436,6 @@ impl TrackInner { self.track = Cursor::Ordered(Box::new(OrderedCursor { track: track.ordered(), pending: None, - readers: kio::WaiterList::new(), })); } match &mut self.track { @@ -460,8 +457,9 @@ impl TrackInner { match self.ordered() { Ok(ordered) => { if let Some(group) = ordered.pending.take() { - // A frame reader parked on it would otherwise wait for a frame it no longer reads. - ordered.readers.wake(); + // A frame reader parked on it would otherwise wait for a frame it no longer + // reads. The waker wakes every call parked on this handle, once it unlocks. + waiter.waker().wake_by_ref(); return Poll::Ready(Ok(Some(group))); } ordered.track.poll_next_group(waiter).map(|r| r.map_err(Into::into)) @@ -474,8 +472,8 @@ impl TrackInner { /// /// One poll does the whole step, so it never trusts a slot it filled on an earlier poll: /// a group `next_group` took from `pending` in between is simply gone, and the wake it - /// sends on `readers` makes this acquire the next one. A group still waiting on its first frame stays in `pending`, so a - /// cancelled call leaves it for the next `read_frame` or `next_group`. + /// sends makes this acquire the next one. A group still waiting on its first frame stays + /// in `pending`, so a cancelled call leaves it for the next `read_frame` or `next_group`. fn poll_read_frame(&mut self, waiter: &kio::Waiter) -> Poll, MoqError>> { let ordered = match self.ordered() { Ok(ordered) => ordered, @@ -490,7 +488,6 @@ impl TrackInner { Err(err) => return Poll::Ready(Err(err.into())), }, }; - waiter.register(&mut ordered.readers); let result = ready!(group.poll_read_frame(waiter)); // Drop a completed or failed group so the next pass moves on. ordered.pending = None; diff --git a/rs/moq-ffi/src/ffi.rs b/rs/moq-ffi/src/ffi.rs index 4562d43c4b0..d406bd52958 100644 --- a/rs/moq-ffi/src/ffi.rs +++ b/rs/moq-ffi/src/ffi.rs @@ -387,19 +387,24 @@ where /// A handle's state for async calls that only poll it, which UniFFI's `RustFuture` polls directly. /// -/// The state sits behind a std mutex held across one poll, never across a wait. Concurrent -/// calls on one handle all park, and each poll takes the next item atomically, so between -/// them they take every item once, though not in call order. Unlike [`Task`], nothing queues: -/// a call the foreign side cancelled but has not freed yet stops polling and holds nothing -/// the next call needs. +/// The state sits behind a [`kio::Bridge`], locked across one poll, never across a wait. +/// Concurrent calls on one handle all park, and each poll takes the next item atomically, so +/// between them they take every item once, though not in call order. Unlike [`Task`], nothing +/// queues: a call the foreign side cancelled but has not freed yet stops polling and holds +/// nothing the next call needs. +/// +/// The bridge is what lets calls park together: the state always polls with the bridge's one +/// waiter, which wakes every parked call, since a handle may keep one waker per wait (moq-net's +/// track cursor does) and would otherwise wake only its latest caller. It also holds a wake the +/// state sends while locked until the lock is released, since UniFFI runs the foreign +/// continuation on the waking thread and a binding may poll the woken call right there. /// /// Each poll runs inside [`shutdown`]'s in-flight accounting and the runtime context, so a poll /// that missed the shutdown finishes before the drivers stop, and a tokio timer in the state /// stays pollable. [`Self::cancel`] closes a channel every poll checks, then drops the state. /// A call that awaits tokio channels, codecs, or I/O uses [`Task`] instead. pub(crate) struct Polled { - /// `None` once cancelled or taken. - state: std::sync::Mutex>, + state: kio::Bridge>, /// Closed by [`Self::cancel`] and [`Self::take`], waking every parked call. cancel: kio::Producer<()>, driver: Background, @@ -409,10 +414,23 @@ pub(crate) struct Polled { cleanup: std::sync::OnceLock, } +/// What a [`Polled`] bridges. +struct Slot { + /// `None` once cancelled or taken. + state: Option, + /// The waiter every poll hands the state, made from the bridge's waker on the first poll. + /// Kept across calls, so the state's registrations stay live and its one-waker waits always + /// see the same waker. + waiter: Option, +} + impl Polled { pub fn new(inner: T) -> Self { Self { - state: std::sync::Mutex::new(Some(inner)), + state: kio::Bridge::new(Slot { + state: Some(inner), + waiter: None, + }), cancel: kio::Producer::new(()), driver: Background::default(), calls: AtomicUsize::new(0), @@ -468,11 +486,13 @@ impl Polled { if self.cancel.poll_closed(waiter).is_ready() { return Poll::Ready(Err(MoqError::Cancelled)); } - let mut state = self.state.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - match state.as_mut() { - Some(state) => poll(state, waiter), - None => Poll::Ready(Err(MoqError::Cancelled)), - } + self.state.poll(waiter, |slot, cx| { + let waiter = slot.waiter.get_or_insert_with(|| kio::Waiter::new(cx.waker().clone())); + match slot.state.as_mut() { + Some(state) => poll(state, waiter), + None => Poll::Ready(Err(MoqError::Cancelled)), + } + }) } /// Whether a call is in flight: polled at least once and not yet dropped. @@ -482,19 +502,12 @@ impl Polled { /// Read the state synchronously, or `None` once it is gone. pub fn peek(&self, f: impl FnOnce(&T) -> R) -> Option { - self.state - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .as_ref() - .map(f) + self.state.lock().state.as_ref().map(f) } /// Whether [Self::cancel] or [Self::take] has run. pub fn is_cancelled(&self) -> bool { - self.state - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .is_none() + self.state.lock().state.is_none() } /// Move the state to a new owner, ending this handle as [Self::cancel] would. @@ -503,10 +516,10 @@ impl Polled { /// [`MoqError::Cancelled`] once the state is gone. pub fn take(&self, check: impl FnOnce(&T) -> Result<(), MoqError>) -> Result { self.check()?; - let mut state = self.state.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - check(state.as_ref().ok_or(MoqError::Cancelled)?)?; - let inner = state.take().ok_or(MoqError::Cancelled)?; - drop(state); + let mut slot = self.state.lock(); + check(slot.state.as_ref().ok_or(MoqError::Cancelled)?)?; + let inner = slot.state.take().ok_or(MoqError::Cancelled)?; + drop(slot); let _ = self.cancel.close(); Ok(inner) } @@ -517,11 +530,7 @@ impl Polled { /// Terminal: there is no way back from here, and the state is gone rather than idle. pub fn cancel(&self) { let _ = self.cancel.close(); - let state = self - .state - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .take(); + let state = self.state.lock().state.take(); self.release(state); } @@ -541,11 +550,7 @@ impl Polled { impl Drop for Polled { fn drop(&mut self) { - let state = self - .state - .get_mut() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .take(); + let state = self.state.get_mut().state.take(); self.release(state); } } @@ -1122,6 +1127,49 @@ mod tests { assert_eq!(all, (0..100).collect::>()); } + /// A state that keeps one waker, as moq-net's track cursor does, still wakes every call + /// parked on it, not only the one that polled last. + #[tokio::test] + async fn polled_one_waker_state_wakes_every_parked_call() { + #[derive(Default)] + struct OneWaker { + ready: bool, + waker: Option, + } + let shared = Arc::new(std::sync::Mutex::new(OneWaker::default())); + let polled = Arc::new(Polled::new(shared.clone())); + let call = |polled: Arc>>>| { + tokio::spawn(async move { + polled + .wait(|state, waiter| { + let mut state = state.lock().unwrap(); + if state.ready { + return Poll::Ready(Ok(())); + } + // Replaces the last caller's waker. + state.waker = Some(waiter.waker().clone()); + Poll::Pending + }) + .await + }) + }; + let first = call(polled.clone()); + let second = call(polled.clone()); + for _ in 0..4 { + tokio::task::yield_now().await; + } + assert!(!first.is_finished() && !second.is_finished()); + + let waker = { + let mut state = shared.lock().unwrap(); + state.ready = true; + state.waker.take().expect("a call parked") + }; + waker.wake(); + soon(first).await.unwrap().unwrap(); + soon(second).await.unwrap().unwrap(); + } + #[test] fn configure_is_cancelled_from_the_moment_cancel_returns() { let task = Task::new(()); diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 11a4cd944c0..0e4751c8fec 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -8688,6 +8688,66 @@ async fn raw_read_frame_moves_on_when_next_group_takes_its_group() { drop(open); } +/// UniFFI's waker runs the foreign continuation on the waking thread, and a binding may poll +/// the woken call right there. `next_group` taking a parked `read_frame`'s group wakes that +/// read, so it must have released the track's state by then, or the inline poll deadlocks. +#[tokio::test] +async fn raw_next_group_wakes_read_frame_after_unlocking() { + type Read = std::pin::Pin, MoqError>> + Send>>; + + /// Polls its read inline on every wake, as an unconfined coroutine would. + struct Inline { + read: std::sync::Mutex, + polls: std::sync::atomic::AtomicUsize, + } + impl Inline { + fn poll(self: &Arc) -> std::task::Poll, MoqError>> { + // The read wakes itself while it ends; that wake has nothing left to poll. + let Ok(mut read) = self.read.try_lock() else { + return std::task::Poll::Pending; + }; + self.polls.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + let waker = std::task::Waker::from(self.clone()); + read.as_mut().poll(&mut std::task::Context::from_waker(&waker)) + } + } + impl std::task::Wake for Inline { + fn wake(self: Arc) { + let _ = self.poll(); + } + } + + let broadcast = MoqBroadcastProducer::new().unwrap(); + let track = broadcast.publish_track("events".into(), Some(timed_info())).unwrap(); + let consumer = track + .consume(Some(MoqSubscription { + max_delay: None, + ..moq_subscription_defaults() + })) + .unwrap(); + let open = track.create_group(0).unwrap(); + + let reader = consumer.clone(); + let inline = Arc::new(Inline { + read: std::sync::Mutex::new(Box::pin(async move { reader.read_frame().await })), + polls: Default::default(), + }); + assert!(inline.poll().is_pending(), "the open group has no frame yet"); + + let taken = kio::clock::timeout(TIMEOUT, consumer.next_group()) + .await + .expect("next_group timed out") + .unwrap() + .expect("expected the open group"); + assert_eq!(taken.sequence(), 0); + assert_eq!( + inline.polls.load(std::sync::atomic::Ordering::SeqCst), + 2, + "next_group should wake the parked read once" + ); + drop(open); +} + /// Reads parked together on one track each wake, and between them take every group once, /// whether they go through `read_frame` or `next_group`. #[tokio::test(flavor = "multi_thread", worker_threads = 4)] From 3f886d3b27d5b46a8a710d08e469f86fc61516b7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 14:27:42 -0800 Subject: [PATCH 8/8] fix(ffi): park each polled call on its own waiter A waiter shared by every call on a handle was never retired, so a call re-polled by another's wakes past its eight records stacked duplicate registrations on a list it stayed parked on. Each call now holds its own kio::Park against the bridge's context: its waiter still wakes through the bridge's one waker, which one-waker handles need, and retires as usual. Co-Authored-By: Claude Opus 5.5 --- rs/moq-ffi/src/ffi.rs | 54 +++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/rs/moq-ffi/src/ffi.rs b/rs/moq-ffi/src/ffi.rs index d406bd52958..2f1f46538ac 100644 --- a/rs/moq-ffi/src/ffi.rs +++ b/rs/moq-ffi/src/ffi.rs @@ -393,9 +393,9 @@ where /// queues: a call the foreign side cancelled but has not freed yet stops polling and holds /// nothing the next call needs. /// -/// The bridge is what lets calls park together: the state always polls with the bridge's one -/// waiter, which wakes every parked call, since a handle may keep one waker per wait (moq-net's -/// track cursor does) and would otherwise wake only its latest caller. It also holds a wake the +/// The bridge is what lets calls park together: each call polls the state with a waiter on the +/// bridge's one waker, which wakes every parked call, since a handle may keep one waker per wait +/// (moq-net's track cursor does) and would otherwise wake only its latest caller. It also holds a wake the /// state sends while locked until the lock is released, since UniFFI runs the foreign /// continuation on the waking thread and a binding may poll the woken call right there. /// @@ -404,7 +404,8 @@ where /// stays pollable. [`Self::cancel`] closes a channel every poll checks, then drops the state. /// A call that awaits tokio channels, codecs, or I/O uses [`Task`] instead. pub(crate) struct Polled { - state: kio::Bridge>, + /// `None` once cancelled or taken. + state: kio::Bridge>, /// Closed by [`Self::cancel`] and [`Self::take`], waking every parked call. cancel: kio::Producer<()>, driver: Background, @@ -414,23 +415,10 @@ pub(crate) struct Polled { cleanup: std::sync::OnceLock, } -/// What a [`Polled`] bridges. -struct Slot { - /// `None` once cancelled or taken. - state: Option, - /// The waiter every poll hands the state, made from the bridge's waker on the first poll. - /// Kept across calls, so the state's registrations stay live and its one-waker waits always - /// see the same waker. - waiter: Option, -} - impl Polled { pub fn new(inner: T) -> Self { Self { - state: kio::Bridge::new(Slot { - state: Some(inner), - waiter: None, - }), + state: kio::Bridge::new(Some(inner)), cancel: kio::Producer::new(()), driver: Background::default(), calls: AtomicUsize::new(0), @@ -463,12 +451,15 @@ impl Polled { ) -> Result { self.calls.fetch_add(1, Ordering::Relaxed); let _call = Call(&self.calls); - kio::wait(|waiter| self.poll(waiter, &mut poll)).await + // The waiter this call polls the state with, on the bridge's waker. + let mut park = kio::Park::default(); + kio::wait(|waiter| self.poll(waiter, &mut park, &mut poll)).await } fn poll( &self, waiter: &kio::Waiter, + park: &mut kio::Park, poll: &mut impl FnMut(&mut T, &kio::Waiter) -> Poll>, ) -> Poll> { let Some(_polling) = Polling::start() else { @@ -486,12 +477,9 @@ impl Polled { if self.cancel.poll_closed(waiter).is_ready() { return Poll::Ready(Err(MoqError::Cancelled)); } - self.state.poll(waiter, |slot, cx| { - let waiter = slot.waiter.get_or_insert_with(|| kio::Waiter::new(cx.waker().clone())); - match slot.state.as_mut() { - Some(state) => poll(state, waiter), - None => Poll::Ready(Err(MoqError::Cancelled)), - } + self.state.poll(waiter, |state, cx| match state.as_mut() { + Some(state) => poll(state, park.hold(cx)), + None => Poll::Ready(Err(MoqError::Cancelled)), }) } @@ -502,12 +490,12 @@ impl Polled { /// Read the state synchronously, or `None` once it is gone. pub fn peek(&self, f: impl FnOnce(&T) -> R) -> Option { - self.state.lock().state.as_ref().map(f) + self.state.lock().as_ref().map(f) } /// Whether [Self::cancel] or [Self::take] has run. pub fn is_cancelled(&self) -> bool { - self.state.lock().state.is_none() + self.state.lock().is_none() } /// Move the state to a new owner, ending this handle as [Self::cancel] would. @@ -516,10 +504,10 @@ impl Polled { /// [`MoqError::Cancelled`] once the state is gone. pub fn take(&self, check: impl FnOnce(&T) -> Result<(), MoqError>) -> Result { self.check()?; - let mut slot = self.state.lock(); - check(slot.state.as_ref().ok_or(MoqError::Cancelled)?)?; - let inner = slot.state.take().ok_or(MoqError::Cancelled)?; - drop(slot); + let mut state = self.state.lock(); + check(state.as_ref().ok_or(MoqError::Cancelled)?)?; + let inner = state.take().ok_or(MoqError::Cancelled)?; + drop(state); let _ = self.cancel.close(); Ok(inner) } @@ -530,7 +518,7 @@ impl Polled { /// Terminal: there is no way back from here, and the state is gone rather than idle. pub fn cancel(&self) { let _ = self.cancel.close(); - let state = self.state.lock().state.take(); + let state = self.state.lock().take(); self.release(state); } @@ -550,7 +538,7 @@ impl Polled { impl Drop for Polled { fn drop(&mut self) { - let state = self.state.get_mut().state.take(); + let state = self.state.get_mut().take(); self.release(state); } }