From 69f0859445e43d10b02b2ca0c13f3fac860c77e3 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 20 Oct 2026 15:46:33 -0700 Subject: [PATCH 1/6] quest(m0): claim audit-simplify From 50df5b82d3750bb0b6f4661afbc87ba319493adb Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 20 Oct 2026 16:17:16 -0700 Subject: [PATCH 2/6] refactor(moq-c): drop never-None slab options and reuse Task The session, server, dynamic, broadcast-request, and raw-track slabs wrapped every entry in an Option that was never None. The session, server, and dynamic entries also hand-rolled Task's close sender and callback. Co-Authored-By: Claude Opus 5.5 --- rs/moq-c/src/consume.rs | 13 +++------ rs/moq-c/src/origin.rs | 50 ++++++++++++---------------------- rs/moq-c/src/server.rs | 33 +++++++++-------------- rs/moq-c/src/session.rs | 60 ++++++++++++++--------------------------- 4 files changed, 54 insertions(+), 102 deletions(-) diff --git a/rs/moq-c/src/consume.rs b/rs/moq-c/src/consume.rs index 4b248c878c7..8b9b0d2e85d 100644 --- a/rs/moq-c/src/consume.rs +++ b/rs/moq-c/src/consume.rs @@ -187,7 +187,7 @@ pub struct Consume { frame: NonZeroSlab, /// Raw track consumer tasks (no media/container framing). - raw_task: NonZeroSlab>, + raw_task: NonZeroSlab, /// Buffered raw frames ready for consumption. raw_frame: NonZeroSlab, @@ -593,7 +593,7 @@ impl Consume { let (task, close) = Task::new(on_frame); let (update, updates) = mpsc::unbounded_channel(); - let id = self.raw_task.insert(Some(RawTaskEntry { task, update }))?; + let id = self.raw_task.insert(RawTaskEntry { task, update })?; // `subscribe` blocks on SUBSCRIBE_OK, so run it inside the task. tokio::spawn(async move { @@ -606,7 +606,7 @@ impl Consume { // Deliver one final terminal callback (code <= 0), then drop the entry. // Pull it out from under the lock so the callback never runs while held. - let entry = State::lock().consume.raw_task.remove(id).flatten(); + let entry = State::lock().consume.raw_task.remove(id); if let Some(entry) = entry { entry.task.callback.call(res); } @@ -717,11 +717,7 @@ impl Consume { track: Id, subscription: Option, ) -> Result<(), Error> { - let entry = self - .raw_task - .get_mut(track) - .and_then(|entry| entry.as_mut()) - .ok_or(Error::TrackNotFound)?; + let entry = self.raw_task.get_mut(track).ok_or(Error::TrackNotFound)?; entry.update.send(subscription).map_err(|_| Error::TrackNotFound)?; Ok(()) } @@ -729,7 +725,6 @@ impl Consume { pub fn raw_track_close(&mut self, track: Id) -> Result<(), Error> { self.raw_task .get_mut(track) - .and_then(|entry| entry.as_mut()) .ok_or(Error::TrackNotFound)? .task .close(Error::TrackNotFound) diff --git a/rs/moq-c/src/origin.rs b/rs/moq-c/src/origin.rs index 389bf6126ef..b51c78287cd 100644 --- a/rs/moq-c/src/origin.rs +++ b/rs/moq-c/src/origin.rs @@ -26,10 +26,10 @@ pub struct Origin { consume_task: NonZeroSlab, /// Served routes from [Self::dynamic], retracted when the handle is closed. - dynamic: NonZeroSlab>, + dynamic: NonZeroSlab, /// Broadcast requests delivered to a dynamic handler, freed after accept/reject. - broadcast_request: NonZeroSlab>, + broadcast_request: NonZeroSlab, } #[derive(Clone)] @@ -114,9 +114,8 @@ impl AnnouncedRecord { } struct DynamicEntry { + task: Task, inner: Option, - close: Option>, - callback: OnStatus, } impl Origin { @@ -391,18 +390,17 @@ impl Origin { let result = origin.producer.dynamic(prefix, route); origin.driver.check()?; let inner = result?; - let channel = oneshot::channel(); - let id = self.dynamic.insert(Some(DynamicEntry { + let (task, close) = Task::new(on_request); + let id = self.dynamic.insert(DynamicEntry { + task, inner: Some(inner), - close: Some(channel.0), - callback: on_request, - }))?; + })?; tokio::spawn(async move { - let res = crate::ffi::run(driver.watch(Self::run_dynamic(id, channel.1))).await; - let entry = State::lock().origin.dynamic.remove(id).flatten(); + let res = crate::ffi::run(driver.watch(Self::run_dynamic(id, close))).await; + let entry = State::lock().origin.dynamic.remove(id); if let Some(entry) = entry { - entry.callback.call(res); + entry.task.callback.call(res); } }); @@ -416,7 +414,7 @@ impl Origin { _ = &mut close => return Ok(()), res = kio::wait(|waiter| { let state = State::lock(); - match state.origin.dynamic.get(id).and_then(|entry| entry.as_ref()).and_then(|entry| entry.inner.as_ref()) { + match state.origin.dynamic.get(id).and_then(|entry| entry.inner.as_ref()) { Some(dynamic) => dynamic.poll_requested_broadcast(waiter), None => std::task::Poll::Ready(Err(moq_net::Error::Closed)), } @@ -427,13 +425,8 @@ impl Origin { }, }; - let request_id = State::lock().origin.broadcast_request.insert(Some(request))?; - let callback = State::lock() - .origin - .dynamic - .get(id) - .and_then(|entry| entry.as_ref()) - .map(|entry| entry.callback); + let request_id = State::lock().origin.broadcast_request.insert(request)?; + let callback = State::lock().origin.dynamic.get(id).map(|entry| entry.task.callback); let Some(callback) = callback else { return Ok(()); }; @@ -445,30 +438,21 @@ impl Origin { let dynamic = self .dynamic .get(dynamic) - .and_then(|entry| entry.as_ref()) .and_then(|entry| entry.inner.as_ref()) .ok_or(Error::NotFound)?; Ok(dynamic.update(route)?) } pub fn dynamic_close(&mut self, dynamic: Id) -> Result<(), Error> { - let entry = self - .dynamic - .get_mut(dynamic) - .and_then(|entry| entry.as_mut()) - .ok_or(Error::NotFound)?; + let entry = self.dynamic.get_mut(dynamic).ok_or(Error::NotFound)?; let inner = entry.inner.take().ok_or(Error::NotFound)?; - entry.close.take(); + entry.task.close(Error::NotFound)?; drop(inner); Ok(()) } pub fn broadcast_request_path(&self, request: Id, dst: &mut crate::moq_string) -> Result<(), Error> { - let request = self - .broadcast_request - .get(request) - .and_then(|slot| slot.as_ref()) - .ok_or(Error::NotFound)?; + let request = self.broadcast_request.get(request).ok_or(Error::NotFound)?; let path = request.path(); *dst = crate::moq_string { data: path.as_str().as_ptr().cast::(), @@ -478,7 +462,7 @@ impl Origin { } pub fn broadcast_request_take(&mut self, request: Id) -> Result { - self.broadcast_request.remove(request).flatten().ok_or(Error::NotFound) + self.broadcast_request.remove(request).ok_or(Error::NotFound) } pub fn close(&mut self, origin: Id) -> Result<(), Error> { diff --git a/rs/moq-c/src/server.rs b/rs/moq-c/src/server.rs index 3f621e41099..669bd68c0bd 100644 --- a/rs/moq-c/src/server.rs +++ b/rs/moq-c/src/server.rs @@ -5,15 +5,15 @@ use std::ffi::c_char; use tokio::sync::oneshot; use crate::ffi::OnStatus; +use crate::task::Task; use crate::{Error, Id, NonZeroSlab, State, moq_string}; -/// A listener task: `close` signals shutdown, `callback` delivers requests and the terminal status. +/// A listener task, whose callback delivers requests and the terminal status. /// /// The task delivers one final terminal callback and then removes itself, so /// `user_data` and the borrowed `addr`/`fingerprints` stay valid until then. struct ServerEntry { - close: Option>, - callback: OnStatus, + task: Task, addr: String, fingerprints: Vec, } @@ -21,7 +21,7 @@ struct ServerEntry { #[derive(Default)] pub struct Server { /// Listener tasks. Close signals shutdown; the task delivers a final callback, then removes itself. - task: NonZeroSlab>, + task: NonZeroSlab, /// Incoming sessions delivered to a listener callback, freed after accept/reject. request: NonZeroSlab, @@ -32,22 +32,21 @@ impl Server { pub fn listen(&mut self, server: moq_tokio::Server, callback: OnStatus) -> Result { let addr = server.local_addr()?.to_string(); let fingerprints = server.certificates().fingerprints(); - let (close, closed) = oneshot::channel(); - let id = self.task.insert(Some(ServerEntry { - close: Some(close), - callback, + let (task, closed) = Task::new(callback); + let id = self.task.insert(ServerEntry { + task, addr, fingerprints, - }))?; + })?; tokio::spawn(async move { let res = crate::ffi::run(Self::run(server, callback, closed)).await; // Deliver one final terminal callback (code <= 0), then drop the entry. // Pull it out from under the lock so the callback never runs while held. - let entry = State::lock().server.task.remove(id).flatten(); + let entry = State::lock().server.task.remove(id); if let Some(entry) = entry { - entry.callback.call(res); + entry.task.callback.call(res); } }); @@ -85,19 +84,13 @@ impl Server { // Signal shutdown; the task delivers a final callback and removes itself. self.task .get_mut(server) - .and_then(|entry| entry.as_mut()) .ok_or(Error::NotFound)? - .close - .take() - .ok_or(Error::NotFound)?; - Ok(()) + .task + .close(Error::NotFound) } fn entry(&self, server: Id) -> Result<&ServerEntry, Error> { - self.task - .get(server) - .and_then(|entry| entry.as_ref()) - .ok_or(Error::NotFound) + self.task.get(server).ok_or(Error::NotFound) } pub fn addr(&self, server: Id, dst: &mut moq_string) -> Result<(), Error> { diff --git a/rs/moq-c/src/session.rs b/rs/moq-c/src/session.rs index 129bf158e13..d74bc653edc 100644 --- a/rs/moq-c/src/session.rs +++ b/rs/moq-c/src/session.rs @@ -1,19 +1,14 @@ use std::sync::Arc; use anyhow::Context; -use tokio::sync::oneshot; use url::Url; +use crate::task::Task; use crate::{Error, Id, NonZeroSlab, State, ffi}; -/// A spawned task entry: `close` signals shutdown, `callback` delivers status. -/// -/// `close` is an `Option` so `close()` can drop just the sender without -/// removing the entry. The task delivers one final terminal callback and then -/// removes itself, so `user_data` stays valid until that callback fires. +/// A session task and the connection backing its handle. struct TaskEntry { - close: Option>, - callback: ffi::OnStatus, + task: Task, link: Link, } @@ -86,7 +81,7 @@ pub(crate) struct PreparedConnect { #[derive(Default)] pub struct Session { /// Session tasks. Close signals shutdown; the task delivers a final callback, then removes itself. - task: NonZeroSlab>, + task: NonZeroSlab, } impl Session { @@ -108,13 +103,8 @@ impl Session { bandwidth: moq_net::bandwidth::Allocator::new(reconnect.send_bandwidth()), }; - let closed = oneshot::channel(); - let entry = TaskEntry { - close: Some(closed.0), - callback, - link, - }; - let id = self.task.insert(Some(entry))?; + let (task, closed) = Task::new(callback); + let id = self.task.insert(TaskEntry { task, link })?; tokio::spawn(async move { // Keep the origin producers alive for the lifetime of the reconnect loop: @@ -128,7 +118,7 @@ impl Session { let res = ffi::run(publish_driver.watch(consume_driver.watch(async move { tokio::select! { // close() requested: drain what the session queued, then close it. - _ = closed.1 => reconnect.clone().close().await.map_err(map_connect_error), + _ = closed => reconnect.clone().close().await.map_err(map_connect_error), res = Self::report(callback, reconnect.clone()) => res, } }))) @@ -146,9 +136,9 @@ impl Session { // Deliver one final terminal callback (0 = closed, < 0 = error), then // drop the entry. Pull it out from under the lock so the callback never // runs while held. - let entry = State::lock().session.task.remove(id).flatten(); + let entry = State::lock().session.task.remove(id); if let Some(entry) = entry { - entry.callback.call(res); + entry.task.callback.call(res); } }); @@ -171,13 +161,11 @@ impl Session { request = request.with_subscriber(consume.producer.clone()); } - let closed = oneshot::channel(); - let entry = TaskEntry { - close: Some(closed.0), - callback, + let (task, closed) = Task::new(callback); + let id = self.task.insert(TaskEntry { + task, link: Link::Accepted(None), - }; - let id = self.task.insert(Some(entry))?; + })?; tokio::spawn(async move { // Keep the origin producers alive for the lifetime of the session. @@ -189,13 +177,13 @@ impl Session { // A panic ends only this session, reported through the terminal callback. let mut res = ffi::run(publish_driver.watch(consume_driver.watch(async move { tokio::select! { - _ = closed.1 => Ok(()), + _ = closed => Ok(()), res = Self::serve(id, callback, request) => res, } }))) .await; - let entry = State::lock().session.task.remove(id).flatten(); + let entry = State::lock().session.task.remove(id); if let Some(entry) = entry { // Close the transport now rather than whenever the last clone drops. if let Link::Accepted(Some(accepted)) = &entry.link @@ -208,7 +196,7 @@ impl Session { res = Err(error); } let res = publish_driver.check().and_then(|()| consume_driver.check()).and(res); - entry.callback.call(res); + entry.task.callback.call(res); } }); @@ -230,7 +218,7 @@ impl Session { .map(moq_net::bandwidth::Allocator::new) .unwrap_or_else(moq_net::bandwidth::Allocator::unlimited); - if let Some(entry) = State::lock().session.task.get_mut(id).and_then(|entry| entry.as_mut()) { + if let Some(entry) = State::lock().session.task.get_mut(id) { entry.link = Link::Accepted(Some(Accepted { session: session.clone(), driver: driver.clone(), @@ -245,12 +233,7 @@ impl Session { } fn link(&self, id: Id) -> Result<&Link, Error> { - Ok(&self - .task - .get(id) - .and_then(|entry| entry.as_ref()) - .ok_or(Error::SessionNotFound)? - .link) + Ok(&self.task.get(id).ok_or(Error::SessionNotFound)?.link) } /// The session's bandwidth allocator. Clones share one reservation registry. @@ -331,12 +314,9 @@ impl Session { // Signal shutdown; the task delivers a final callback and removes itself. self.task .get_mut(id) - .and_then(|entry| entry.as_mut()) .ok_or(Error::SessionNotFound)? - .close - .take() - .ok_or(Error::SessionNotFound)?; - Ok(()) + .task + .close(Error::SessionNotFound) } } From 229a607b690a38643bef00c773a6eacdc4cfa6a5 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 20 Oct 2026 16:17:17 -0700 Subject: [PATCH 3/6] refactor(moq-net): share the queue-once keyed waker lite's RouteWake and the origin's TrackReady were the same waker: queue a key once per burst of wakes, unmarked when the owner pops it. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 36 +++++++--------------------- rs/moq-net/src/model/origin.rs | 39 ++++++------------------------- rs/moq-net/src/util.rs | 2 ++ rs/moq-net/src/util/wake.rs | 39 +++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 60 deletions(-) create mode 100644 rs/moq-net/src/util/wake.rs diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 825dcbe86ec..c24dd65b680 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -5281,11 +5281,7 @@ impl Announced { /// Attach a route, queued for its first serve pass. fn attach(&mut self, path: PathOwned, route: crate::origin::Route, dynamic: crate::origin::Dynamic) { - let wake = Arc::new(RouteWake { - path: path.clone(), - queued: atomic::AtomicBool::new(false), - ready: self.ready.clone(), - }); + let wake = crate::util::QueueWake::new(path.clone(), self.ready.clone()); let route = AnnouncedRoute::new(route, dynamic, wake); route.waker.wake_by_ref(); self.routes.insert(path, Held::Attached(Box::new(route))); @@ -5391,7 +5387,7 @@ impl Announced { continue; }; // Cleared before polling, so a request landing mid-pass queues the route again. - entry.wake.queued.store(false, atomic::Ordering::Release); + entry.wake.popped(); let cx = std::task::Context::from_waker(&entry.waker); let route_waiter = entry.park.hold(&cx); while let Poll::Ready(Ok(request)) = entry.dynamic.poll_requested_broadcast(route_waiter) { @@ -5482,14 +5478,18 @@ struct AnnouncedRoute { /// Whether the GOAWAY drain already re-priced this route. drained: bool, /// Queues this route on its prefix's ready set when its request queue wakes. - wake: Arc, + wake: Arc>, waker: std::task::Waker, /// Keeps this route's registration on its request queue alive between passes. park: kio::Park, } impl AnnouncedRoute { - fn new(route: crate::origin::Route, dynamic: crate::origin::Dynamic, wake: Arc) -> Self { + fn new( + route: crate::origin::Route, + dynamic: crate::origin::Dynamic, + wake: Arc>, + ) -> Self { Self { route, dynamic, @@ -5522,26 +5522,6 @@ impl AnnouncedRoute { } } -/// One attached route's waker: queues the route for the next serve pass. -struct RouteWake { - path: PathOwned, - /// Set while queued, so a burst of wakes queues the route once. - queued: atomic::AtomicBool, - ready: kio::Queue, -} - -impl std::task::Wake for RouteWake { - fn wake(self: Arc) { - self.wake_by_ref(); - } - - fn wake_by_ref(self: &Arc) { - if !self.queued.swap(true, atomic::Ordering::AcqRel) { - let _ = self.ready.try_push(self.path.clone()); - } - } -} - /// How a [`TrackServe`] run ends. enum ServeEnd { /// The upstream FIN'd: the track is over for good. diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 607e23345ab..ba3baff28d5 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -6,7 +6,7 @@ use std::{ collections::{BTreeSet, HashMap, HashSet, VecDeque}, fmt, sync::Arc, - sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}, + sync::atomic::{AtomicU64, AtomicUsize, Ordering}, task::{Context, Poll, Wake, Waker, ready}, time::Duration, }; @@ -2715,29 +2715,11 @@ struct Asked { sub: Option>, } -/// Queues just the logical track whose watched resource changed. -struct TrackReady { - name: Arc, - queue: kio::Queue>, - queued: AtomicBool, -} - -impl Wake for TrackReady { - fn wake(self: Arc) { - self.wake_by_ref(); - } - - fn wake_by_ref(self: &Arc) { - if !self.queued.swap(true, Ordering::AcqRel) { - let _ = self.queue.try_push(self.name.clone()); - } - } -} - /// The driver's side of one logical track: the handles behind the names the /// machine uses. struct TrackIo { - ready: Arc, + /// Queues just this track when a resource it watches changes. + ready: Arc>>, park: kio::Park, /// The logical track, until the first copy says what it is. request: Option, @@ -3351,7 +3333,7 @@ async fn serve_front(task: FrontTask) -> Vec { } while let Poll::Ready(Ok(name)) = ready.poll_pop(waiter) { let Some(io) = tracks.get_mut(&name) else { continue }; - io.ready.queued.store(false, Ordering::Release); + io.ready.popped(); let waker = Waker::from(io.ready.clone()); let waiter = io.park.hold(&Context::from_waker(&waker)); let step = (|| { @@ -3432,11 +3414,7 @@ async fn serve_front(task: FrontTask) -> Vec { let event = match step { Step::Assigned(request) => { let name: Arc = request.name().into(); - let track_ready = Arc::new(TrackReady { - name: name.clone(), - queue: ready.clone(), - queued: AtomicBool::new(false), - }); + let track_ready = crate::util::QueueWake::new(name.clone(), ready.clone()); track_ready.wake_by_ref(); tracks.insert( name.clone(), @@ -6290,6 +6268,7 @@ mod tests { #[cfg(feature = "async")] use crate::model::test_tracing::count_drop_warnings; use futures::FutureExt; + use std::sync::atomic::AtomicBool; fn origin(id: u64) -> Hop { Hop::new(id).unwrap() @@ -6336,11 +6315,7 @@ mod tests { let weak = request.weak(); let accepted = request.routes(routes.consume()).accept(None); let mut io = TrackIo { - ready: Arc::new(TrackReady { - name: "catalog.json".into(), - queue: kio::Queue::new(), - queued: AtomicBool::new(false), - }), + ready: crate::util::QueueWake::new("catalog.json".into(), kio::Queue::new()), park: Default::default(), request: None, accepted: Some(accepted), diff --git a/rs/moq-net/src/util.rs b/rs/moq-net/src/util.rs index 828c66125c1..20aea505200 100644 --- a/rs/moq-net/src/util.rs +++ b/rs/moq-net/src/util.rs @@ -8,7 +8,9 @@ use std::{collections::VecDeque, future::Future, pin::Pin, task::Poll}; mod cell; +mod wake; pub(crate) use cell::SendCell; +pub(crate) use wake::QueueWake; /// Prove a future's native Send bound here rather than inside every caller's future. pub(crate) fn sendable(future: F) -> impl Future + kio::MaybeSend { diff --git a/rs/moq-net/src/util/wake.rs b/rs/moq-net/src/util/wake.rs new file mode 100644 index 00000000000..1aa0fc33f5b --- /dev/null +++ b/rs/moq-net/src/util/wake.rs @@ -0,0 +1,39 @@ +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, +}; + +/// A waker that queues its key, once per burst of wakes, for the owner's next pass. +pub(crate) struct QueueWake { + key: K, + /// Set while queued, so a burst of wakes queues the key once. + queued: AtomicBool, + queue: kio::Queue, +} + +impl QueueWake { + pub fn new(key: K, queue: kio::Queue) -> Arc { + Arc::new(Self { + key, + queued: AtomicBool::new(false), + queue, + }) + } + + /// Unmark the key once popped, before polling, so a wake landing mid-pass queues it again. + pub fn popped(&self) { + self.queued.store(false, Ordering::Release); + } +} + +impl std::task::Wake for QueueWake { + fn wake(self: Arc) { + self.wake_by_ref(); + } + + fn wake_by_ref(self: &Arc) { + if !self.queued.swap(true, Ordering::AcqRel) { + let _ = self.queue.try_push(self.key.clone()); + } + } +} From c3c8b11981af851a274d56e4eee99c167408c316 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 20 Oct 2026 16:20:41 -0700 Subject: [PATCH 4/6] refactor(js/net): share the armed change wait Three publisher loops hand-built the same promise that listens for the next change to several signals before reconciling, each marked with a TODO. Co-Authored-By: Claude Opus 5.5 --- js/net/src/ietf/publisher.ts | 46 ++++++++++-------------------------- js/net/src/lite/publisher.ts | 17 ++++--------- js/net/src/util/change.ts | 21 ++++++++++++++++ 3 files changed, 37 insertions(+), 47 deletions(-) create mode 100644 js/net/src/util/change.ts diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 2d48c91f4da..85a9f5d6ae8 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -31,6 +31,7 @@ import { type Stream, Writer } from "../stream.ts"; import { Timescale } from "../time.ts"; import type { Groups, Location as TrackLocation, Subscriber as TrackSubscriber } from "../track.ts"; import { untilAborted } from "../util/abort.ts"; +import { nextChange } from "../util/change.ts"; import { Deadline, REQUEST_TIMEOUT_MS, requestOptions } from "../util/deadline.ts"; import { LIMITS, type Limits, Slots } from "../util/limits.ts"; import { TimeoutError, withTimeout } from "../util/timeout.ts"; @@ -1332,21 +1333,10 @@ export class Publisher { // Subscribe BEFORE reconciling, for the same reason as // {@link runPublishNamespaces}: a publish landing while an advertisement // waits for its reply only notifies listeners already registered. - // TODO Make a better helper within Signals. - let dispose!: Dispose; - const changed = new Promise<"changed">((resolve) => { - const table = this.#advertised.changed(() => resolve("changed")); - // A grant change re-diffs the same way, withdrawing what it no longer covers. - // The loop top re-reads the table, so an ended origin still stops it there. - const grant = this.#grant.changed(() => resolve("changed")); - // A token that ended leaves the request carrying it to be made again without. - const ended = this.#auth?.ended.changed(() => resolve("changed")); - dispose = () => { - table(); - grant(); - ended?.(); - }; - }); + // A grant change re-diffs the same way, withdrawing what it no longer covers. + // The loop top re-reads the table, so an ended origin still stops it there. + // A token that ended leaves the request carrying it to be made again without. + const { changed, dispose } = nextChange(this.#advertised, this.#grant, this.#auth?.ended); const advertised = this.#advertised.peek(); const updated = advertised ? presented(prefix, advertised, carries) : new Map(); @@ -1493,23 +1483,11 @@ export class Publisher { // for the peer's reply, and a publish that lands in that window notifies // only the listeners already registered; one created afterwards would sleep // through it and leave the namespace unadvertised until something unrelated - // changed. - // TODO Make a better helper within Signals. - const changed = new Promise<"changed">((resolve) => { - const table = this.#advertised.changed(() => resolve("changed")); - // A grant change re-diffs the same way, withdrawing what it no longer covers. - // The loop top re-reads the table, so an ended origin still stops it there. - const grant = this.#grant.changed(() => resolve("changed")); - // A token that ended leaves the request carrying it to be made again without. - const ended = this.#auth?.ended.changed(() => resolve("changed")); - const lapsed = this.#lapsed.changed(() => resolve("changed")); - dispose = () => { - table(); - grant(); - ended?.(); - lapsed(); - }; - }); + // changed. A grant change re-diffs the same way, withdrawing what it no longer + // covers; the loop top re-reads the table, so an ended origin still stops it there. + // A token that ended leaves the request carrying it to be made again without. + const change = nextChange(this.#advertised, this.#grant, this.#auth?.ended, this.#lapsed); + dispose = change.dispose; // The peer no longer holds a namespace whose request ended while idle. The wait // before offering it again doubles each time, so a peer that ends every one it @@ -1552,8 +1530,8 @@ export class Publisher { // Wait for the next change, which has already fired if one landed above. const next = await (retry - ? race([changed, this.#withdrawal.closing, closed, retryAfter(retry).then(() => advertised)]) - : race([changed, this.#withdrawal.closing, closed])); + ? race([change.changed, this.#withdrawal.closing, closed, retryAfter(retry).then(() => advertised)]) + : race([change.changed, this.#withdrawal.closing, closed])); dispose?.(); if (!next || next === true) break; } diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index c9e64cc4336..20a839ec5cc 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -18,6 +18,7 @@ import { type Reader, type Stream, Writer } from "../stream.ts"; import { type Timescale, Timestamp } from "../time.ts"; import type * as track from "../track.ts"; import { untilAborted } from "../util/abort.ts"; +import { nextChange } from "../util/change.ts"; import { Interests, LIMITS, type Limits, Slots } from "../util/limits.ts"; import { nextTask } from "../util/yield.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; @@ -571,18 +572,8 @@ export class Publisher { // afterwards would sleep through it, leaving the change unannounced until something // unrelated moved. A grant change re-diffs the same way, withdrawing what it no longer // covers and announcing what it now does. - // TODO Make a better helper within Signals. - let dispose: Dispose = () => {}; - const arm = () => - new Promise<"changed">((resolve) => { - const table = this.#advertised.changed(() => resolve("changed")); - const grant = this.#grant?.changed(() => resolve("changed")); - dispose = () => { - table(); - grant?.(); - }; - }); - let changed = arm(); + const arm = () => nextChange(this.#advertised, this.#grant); + let { changed, dispose } = arm(); try { // Nothing is announced before the grant it would be checked against. A local close @@ -724,7 +715,7 @@ export class Publisher { if (woke !== "changed") break; // Re-arm before reading, so an advertise that lands while we write is not lost. - changed = arm(); + ({ changed, dispose } = arm()); const latest = this.#advertised.peek(); if (!latest) break; diff --git a/js/net/src/util/change.ts b/js/net/src/util/change.ts new file mode 100644 index 00000000000..26df9d3edfc --- /dev/null +++ b/js/net/src/util/change.ts @@ -0,0 +1,21 @@ +import type { Dispose, Getter } from "@moq/signals"; + +/** + * Resolve with "changed" on the next change to any of `sources`, listening from now. + * + * Unlike the lazy `Signal.race`, this registers before the caller's next await, so a change + * landing while it works still wakes the wait. Call `dispose` once done waiting. + */ +export function nextChange(...sources: (Getter | undefined)[]): { + changed: Promise<"changed">; + dispose: Dispose; +} { + const { promise: changed, resolve } = Promise.withResolvers<"changed">(); + const disposers = sources.map((source) => source?.changed(() => resolve("changed"))); + return { + changed, + dispose: () => { + for (const dispose of disposers) dispose?.(); + }, + }; +} From c5ffb463b0e5da3ea936d6f538b636a18367e7e3 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 20 Oct 2026 16:21:51 -0700 Subject: [PATCH 5/6] quest: finish the m0 simplification audit and add its m1 successor Co-Authored-By: Claude Opus 5.5 --- quest/m0/README.md | 1 - quest/m0/audit-simplify.md | 80 ---------------------------------- quest/m1/README.md | 1 + quest/m1/audit-simplify.md | 89 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 81 deletions(-) delete mode 100644 quest/m0/audit-simplify.md create mode 100644 quest/m1/audit-simplify.md diff --git a/quest/m0/README.md b/quest/m0/README.md index a2e2ed681b9..0d12bcc495d 100644 --- a/quest/m0/README.md +++ b/quest/m0/README.md @@ -95,5 +95,4 @@ Each lands its fixes in its own PR and plans only what needs a decision. - [Performance audit](/quest/m0/audit-perf.md) - benchmarks that regressed since the last run are explained, fixed, or planned, and unbenched hot paths get coverage - [Compile time audit](/quest/m0/audit-compile-time.md) - build-time hotspots are measured against a baseline and the worst are fixed or planned - [Artifact size audit](/quest/m0/audit-size.md) - binary, FFI, WASM, and bundle growth since the last run is explained, fixed, or planned -- [Simplification audit](/quest/m0/audit-simplify.md) - overbuilt code and APIs are collapsed, merged, or narrowed into a net simpler whole - [Quest tree audit](/quest/m0/audit-quests.md) - the tree agrees with itself and with main diff --git a/quest/m0/audit-simplify.md b/quest/m0/audit-simplify.md deleted file mode 100644 index 8dda4d297fc..00000000000 --- a/quest/m0/audit-simplify.md +++ /dev/null @@ -1,80 +0,0 @@ -# [M] Simplification audit - -## Goal - -The code and APIs that cost the most complexity for what they deliver are -simplified: duplicate mechanisms merged, layers, options, and generics with -one user collapsed, and features that don't earn their keep narrowed or -removed. The result is net simpler, even where that means less scope. - -## Plan - -Recurs once per major release (decided 2026-10-19). The PR that finishes -this run also adds the next one with the same file name at the end of the next -milestone's `Required` (m0 to m1, m1 to m2). The successor keeps the Goal -and this paragraph; the rest of its Plan is rewritten with what this run -learned: baselines, results, and what to check first. A successor placed in m2 -also lists the [release cut](/quest/m2/release/cut.md) under `Required`, so it -waits for the release in flight to ship. Only `/quest-delete` ends the -recurrence. Fixes land in the run's PR, one commit each, rather than as follow-ups. -Only a change that needs a maintainer decision (public API, scope) or is -independently landable and too large to review alongside the rest becomes a -follow-up through `/quest-plan`. - -Narrowing a feature or reshaping an API is the maintainer's call, so those -are proposals with a recommendation, filed as follow-ups. Unlike the [dead -code audit](/quest/m1/audit-dead-code.md), this targets code that is used but -overbuilt. - -Starting points (2026-10-19): look for one concept implemented twice (across -crates, or Rust and JS drifting apart), traits and generics with a single -implementation, config knobs every caller sets the same way, wrappers that -only forward, and the largest modules and functions. Prefer reusing what the -stack already has over a parallel mechanism. - -Progress (2026-10-19): the JSON stream consumer reuses the byte stream -consumer's single-group lifecycle instead of maintaining its own receive, -roll detection, and teardown state machine. JSON validation, error identity, -and concurrent-read refusal stay at the JSON boundary. Its record count, -payload size, and compression sweep runs in the JavaScript nightly lane. - -The initial scan covered private Rust traits, media/container dispatch, -transport adapters, JSON codecs, and duplicated JavaScript utilities. -Backend and playback traits have multiple implementations or device-free -test doubles; catalog traits erase application extension types. Keep those -seams. Transport adapter deduplication and compression removal already have -dedicated work; avoid a second design in this audit. - -Second checkpoint (2026-10-19): the large publisher/subscriber modules and -binding layers are reviewed. Landed, all private: one IETF request-error -writer in Rust and JS (draft-14 bytes pinned per request kind), Rust's lite -track-stream and GOAWAY gates mirrored in JS, one moq-c callback task slab and -spawn helper, one moq-c media consume path, one Kotlin pull adapter, one -Python async stream base, and small Go duplicates. Dart and C++ wrappers are -already thin. - -Still to do before completing the run, each waiting on the named open work: - -- `ietf/session.rs` `start()` builds both halves twice across the draft arms, - and the IETF publisher/subscriber constructors drifted from lite's config - structs. Wait for the GOAWAY, close-drain, and receive-credit PRs that edit - `start()`. -- Lite and IETF `GroupServe` share their open, flush, and FIN-settle driver. - Wait for compressed tracks. -- `ietf/adapter.rs` reimplements `kio::Queue`; keep a drop guard for the - writer. Wait for the GOAWAY PR that edits the same impl. -- Keyed "mark ready and wake" wakers repeat in `lite/subscriber.rs`, - `ietf/publisher.rs`, and `model/origin.rs`. -- JS: hand-built "wake on any of these signals" waits can use - `Signal.race`, armed before reconciling; subscriber authorization could - reuse `auth_gate.ts` `Gate`. -- moq-c: the session, server, dynamic, request, and raw slabs still wrap - entries in an `Option` that is never `None`. - -Maintainer proposals, not changes: a moq-video raw-frame publisher both -bindings wrap (moq-c's copy also runs its bandwidth follower on the C -runtime, which may stall it), moving the bindings' duplicated `Background` -into moq-tokio, typealiasing the Swift wrapper's forwarding classes, and -making Python's `connect` the `Client` class. Kotlin's `AnnounceConsumer` -flow cancels on every completion while the others cancel only on -cancellation; unifying it is a behavior call. diff --git a/quest/m1/README.md b/quest/m1/README.md index c22ba8665ba..54c5db57da8 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -131,6 +131,7 @@ lands before the cut. - [Interop report](/quest/m1/interop-report.md) - nightly Interop results publish as a color-coded matrix on the Pages site instead of only in logs - [CI workflow audit](/quest/m1/audit-ci.md) - every workflow and job gives a signal someone sees, at a cost that signal justifies, or is fixed, moved, or deleted - [Documentation audit](/quest/m1/audit-docs.md) - stale pages, examples, links, and undocumented exports are fixed or planned +- [Simplification audit](/quest/m1/audit-simplify.md) - repeat the simplification audit, starting with the m0 leftovers, before the next major release ## Related diff --git a/quest/m1/audit-simplify.md b/quest/m1/audit-simplify.md new file mode 100644 index 00000000000..8f1a49b95f9 --- /dev/null +++ b/quest/m1/audit-simplify.md @@ -0,0 +1,89 @@ +# [M] Simplification audit + +## Goal + +The code and APIs that cost the most complexity for what they deliver are +simplified: duplicate mechanisms merged, layers, options, and generics with +one user collapsed, and features that don't earn their keep narrowed or +removed. The result is net simpler, even where that means less scope. + +## Plan + +Recurs once per major release (decided 2026-10-19). The PR that finishes +this run also adds the next one with the same file name at the end of the next +milestone's `Required` (m0 to m1, m1 to m2). The successor keeps the Goal +and this paragraph; the rest of its Plan is rewritten with what this run +learned: baselines, results, and what to check first. A successor placed in m2 +also lists the [release cut](/quest/m2/release/cut.md) under `Required`, so it +waits for the release in flight to ship. Only `/quest-delete` ends the +recurrence. Fixes land in the run's PR, one commit each, rather than as follow-ups. +Only a change that needs a maintainer decision (public API, scope) or is +independently landable and too large to review alongside the rest becomes a +follow-up through `/quest-plan`. + +Narrowing a feature or reshaping an API is the maintainer's call, so those +are proposals with a recommendation, filed as follow-ups. Unlike the [dead +code audit](/quest/m1/audit-dead-code.md), this targets code that is used but +overbuilt. + +### Check first + +Left from the m0 run because open work edits the same code: + +- `ietf/session.rs` `start()` builds both halves twice across the draft arms, + and the IETF publisher/subscriber constructors drifted from lite's config + structs. Waits on [close-tail drain](/quest/m1/close-tail-drain.md). +- Lite and IETF `GroupServe` share their open, flush, and FIN-settle driver. + Waits on [compressed tracks](/quest/m1/compress/core.md). +- `ietf/adapter.rs` reimplements `kio::Queue`; keep a drop guard for the + writer. Waits on close-tail drain. + +Maintainer proposals from m0, not yet planned, each with its recommendation: + +- Yes: a moq-video raw-frame publisher that moq-ffi and moq-c both wrap, + removing about 250 duplicated encode and bandwidth-follow lines. moq-c's + copy calls `pollster::block_on(set_bitrate)` on the C runtime while holding + the encoder lock, which may stall other moq-c tasks (unverified). +- Yes, after the publisher: move the bindings' duplicated `Background` into + moq-tokio, with a blanket `Driver for &mut D` in moq-net. +- Yes: make Python's `connect` the `Client` class, dropping a 27-parameter + forwarding signature. +- No: typealias the Swift wrapper's forwarding classes. The wrapping is + deliberate (argument labels, no `Moq` prefixes), and it would break Swift. +- Keep: Kotlin's `AnnounceConsumer.updates()` cancels on every completion + while the other flows cancel only on cancellation; its doc promises that. + +The Go wrapper's `Server.Accept` and handle-returning methods skip +`bridge.CallHandle`, which may leak an accepted handle on cancellation. This +is unverified and worth its own investigation. + +### Results (m0, 2026-10-20) + +Three PRs, all private and behavior-preserving: +[#5977](https://github.com/moq-dev/moq/pull/5977), +[#6049](https://github.com/moq-dev/moq/pull/6049), and this run's final PR. +They merged the JSON stream consumer into the byte stream consumer, one IETF +request-error writer in Rust and JS, Rust's lite version gates mirrored in +JS, one moq-c task slab with no never-`None` entries, one moq-c media consume +path, one Kotlin pull adapter, one Python async stream base, small Go +duplicates, one queue-once keyed waker in moq-net, and one armed change wait +in js/net's publishers. + +### Kept on purpose + +- Backend and playback traits have several implementations or device-free + test doubles; catalog traits erase application extension types. +- The ietf publisher's `RequestWaker` and lite's `FetchWake` stay apart from + the queue-once waker: one collects into a list and wakes an owner, the + other fills a set. +- js/net's subscriber grant checks stay hand-written: `auth_gate.ts` `Gate` + only checks publish grants, and a direction flag costs what it saves. +- The JS datagram send and receive loops: the lite and IETF error handling + and `served` bookkeeping differ, so a shared loop needs hooks. +- Dart and C++ wrappers are already thin. + +Transport adapter deduplication and compression removal have dedicated work; +avoid a second design here. Start with the items above, then the largest +modules again, and the bindings' forwarding layers. The largest moq-net +modules at the end of m0: `ietf/publisher.rs` 16,047 lines, +`model/origin.rs` 11,863, and `lite/subscriber.rs` 7,181. From 2408c0da9279276d18a7e4e14b9ae552f1a4bfd4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 21 Oct 2026 11:03:40 -0700 Subject: [PATCH 6/6] fix: format the merge resolution and drop a stale quest link Compressed tracks landed in moq-dev/moq#5928, so the GroupServe item no longer waits on it. Co-Authored-By: Claude Opus 5.5 --- quest/m1/audit-simplify.md | 1 - rs/moq-c/src/origin.rs | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/quest/m1/audit-simplify.md b/quest/m1/audit-simplify.md index 8f1a49b95f9..b3717b75048 100644 --- a/quest/m1/audit-simplify.md +++ b/quest/m1/audit-simplify.md @@ -34,7 +34,6 @@ Left from the m0 run because open work edits the same code: and the IETF publisher/subscriber constructors drifted from lite's config structs. Waits on [close-tail drain](/quest/m1/close-tail-drain.md). - Lite and IETF `GroupServe` share their open, flush, and FIN-settle driver. - Waits on [compressed tracks](/quest/m1/compress/core.md). - `ietf/adapter.rs` reimplements `kio::Queue`; keep a drop guard for the writer. Waits on close-tail drain. diff --git a/rs/moq-c/src/origin.rs b/rs/moq-c/src/origin.rs index be349592520..7e7eaf7296f 100644 --- a/rs/moq-c/src/origin.rs +++ b/rs/moq-c/src/origin.rs @@ -381,7 +381,10 @@ impl Origin { }, }; - let request_id = State::lock().origin.broadcast_request.insert((request, driver.clone()))?; + let request_id = State::lock() + .origin + .broadcast_request + .insert((request, driver.clone()))?; let callback = State::lock().origin.dynamic.get(id).map(|entry| entry.task.callback); let Some(callback) = callback else { return Ok(());