diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index 3047bb5d1db..da0f3739e64 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"; @@ -1351,21 +1352,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(); @@ -1510,23 +1500,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 @@ -1569,8 +1547,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 63015bf351a..249e0382063 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -20,6 +20,7 @@ import { type Timescale, Timestamp } from "../time.ts"; import type * as track from "../track.ts"; import { liteTimescale } 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"; @@ -609,18 +610,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 @@ -762,7 +753,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?.(); + }, + }; +} diff --git a/quest/m0/README.md b/quest/m0/README.md index 7daf1289a1b..6c9774abb5b 100644 --- a/quest/m0/README.md +++ b/quest/m0/README.md @@ -47,6 +47,5 @@ Each lands its fixes in its own PR and plans only what needs a decision. - [Release fixed delay](/quest/m0/release-fixed-delay.md) - a fixed latency on `release`, which moq.dev runs, ignores the catalog's jitter and delay as `main` already does - [Release resubscribe hang](/quest/m0/release-resubscribe-hang.md) - a moq-lite-06 subscription that closes and reopens on `release` is proven to reach the relay, or the hang is fixed and backported - [CI cost and latency](/quest/m0/ci/README.md) - cloud CI runs only in the merge queue as one `just ci` pass, expensive checks run nightly and gate `release`, caches move to R2, and runners are chosen last -- [Simplification audit](/quest/m0/audit-simplify.md) - overbuilt code and APIs are collapsed, merged, or narrowed into a net simpler whole - [Lint override and deprecation audit](/quest/m0/audit-lints.md) - every lint suppression is still needed and says why, nothing calls a deprecated API, and stale Rust overrides fail the build - [Interop audit](/quest/m0/audit-interop.md) - our interop runner entries match what we ship, and every red interop cell with a moq.dev side is fixed, reported, or noted as down diff --git a/quest/m0/audit-simplify.md b/quest/m0/audit-simplify.md deleted file mode 100644 index 8fcc04ab321..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/m2/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 c46e8eec290..8778c286fe1 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -116,6 +116,7 @@ lite-07 stream type here. - [Interop coverage](/quest/m1/interop-coverage/README.md) - the riskiest moq-transport features moq-net exposes get cross-implementation tests over the even drafts 14 to 22 - [Runner nightly](/quest/m1/interop-runner-nightly.md) - a nightly job runs the upstream interop runner against our relay and clients, so newly registered implementations with a published image are tested without a change here - [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 +- [Simplification audit](/quest/m1/audit-simplify.md) - repeat the simplification audit, starting with the m0 leftovers, before the next major release - [Artifact size audit](/quest/m1/audit-size.md) - binary, FFI, WASM, and bundle growth since the m0 run is explained, fixed, or planned - [Compile time audit](/quest/m1/audit-compile-time.md) - build-time hotspots are measured against the m0 baselines and the worst are fixed or planned - [Performance audit](/quest/m1/audit-perf.md) - benchmarks that regressed since the m0 run are explained, fixed, or planned, and unbenched hot paths get coverage diff --git a/quest/m1/audit-simplify.md b/quest/m1/audit-simplify.md new file mode 100644 index 00000000000..1df5b2b6422 --- /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/m2/audit-dead-code.md), this targets code that is used but +overbuilt. + +### Check first + +Left from the m0 run because open work edited the same code (close-tail drain +landed in [#5762](https://github.com/moq-dev/moq/pull/5762)): + +- `ietf/session.rs` `start()` builds both halves twice across the draft arms, + and the IETF publisher/subscriber constructors drifted from lite's config + structs. +- Lite and IETF `GroupServe` share their open, flush, and FIN-settle driver. +- `ietf/adapter.rs` reimplements `kio::Queue`; keep a drop guard for the + writer. + +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. diff --git a/rs/moq-c/src/consume.rs b/rs/moq-c/src/consume.rs index 8e46e7f44c5..24d4f6c5aea 100644 --- a/rs/moq-c/src/consume.rs +++ b/rs/moq-c/src/consume.rs @@ -197,7 +197,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, @@ -596,7 +596,7 @@ impl Consume { let (task, close) = Task::new(on_frame); let control = RawControl::Pending(None); - let id = self.raw_task.insert(Some(RawTaskEntry { task, control }))?; + let id = self.raw_task.insert(RawTaskEntry { task, control })?; // `subscribe` blocks on SUBSCRIBE_OK, so run it inside the task. tokio::spawn(async move { @@ -605,12 +605,7 @@ impl Consume { let control = track.control(); Self::update_raw(&control, Self::raw_subscription(subscription))?; // Go live under the lock, so no later update lands before the pending one. - if let Some(entry) = State::lock() - .consume - .raw_task - .get_mut(id) - .and_then(|entry| entry.as_mut()) - { + if let Some(entry) = State::lock().consume.raw_task.get_mut(id) { let pending = std::mem::replace(&mut entry.control, RawControl::Live(control.clone())); if let RawControl::Pending(Some(subscription)) = pending { Self::update_raw(&control, subscription)?; @@ -622,7 +617,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); } @@ -712,11 +707,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)?; let subscription = Self::raw_subscription(subscription); match &mut entry.control { RawControl::Pending(pending) => *pending = Some(subscription), @@ -728,7 +719,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 069ada23da8..7e7eaf7296f 100644 --- a/rs/moq-c/src/origin.rs +++ b/rs/moq-c/src/origin.rs @@ -26,13 +26,13 @@ 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<(moq_net::origin::Request, crate::ffi::Background)>, /// Claim answers being filled before their exact route is announced. - prepared: NonZeroSlab>, + prepared: NonZeroSlab<(moq_net::origin::Prepared, crate::ffi::Background)>, } #[derive(Clone)] @@ -117,9 +117,8 @@ impl AnnouncedRecord { } struct DynamicEntry { + task: Task, inner: Option, - close: Option>, - callback: OnStatus, } impl Origin { @@ -342,19 +341,18 @@ impl Origin { let result = origin.producer.dynamic(prefix); 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, - }))?; + })?; let request_driver = driver.clone(); tokio::spawn(async move { - let res = crate::ffi::run(driver.watch(Self::run_dynamic(id, channel.1, request_driver))).await; - let entry = State::lock().origin.dynamic.remove(id).flatten(); + let res = crate::ffi::run(driver.watch(Self::run_dynamic(id, close, request_driver))).await; + let entry = State::lock().origin.dynamic.remove(id); if let Some(entry) = entry { - entry.callback.call(res); + entry.task.callback.call(res); } }); @@ -372,7 +370,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)), } @@ -386,13 +384,8 @@ impl Origin { let request_id = State::lock() .origin .broadcast_request - .insert(Some((request, driver.clone())))?; - let callback = State::lock() - .origin - .dynamic - .get(id) - .and_then(|entry| entry.as_ref()) - .map(|entry| entry.callback); + .insert((request, driver.clone()))?; + let callback = State::lock().origin.dynamic.get(id).map(|entry| entry.task.callback); let Some(callback) = callback else { return Ok(()); }; @@ -401,23 +394,15 @@ impl Origin { } 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.0.path(); *dst = crate::moq_string { data: path.as_str().as_ptr().cast::(), @@ -430,7 +415,7 @@ impl Origin { &mut self, request: Id, ) -> Result<(moq_net::origin::Request, crate::ffi::Background), Error> { - self.broadcast_request.remove(request).flatten().ok_or(Error::NotFound) + self.broadcast_request.remove(request).ok_or(Error::NotFound) } pub fn prepared_insert( @@ -438,14 +423,14 @@ impl Origin { prepared: moq_net::origin::Prepared, driver: crate::ffi::Background, ) -> Result { - self.prepared.insert(Some((prepared, driver))) + self.prepared.insert((prepared, driver)) } pub fn prepared_take( &mut self, prepared: Id, ) -> Result<(moq_net::origin::Prepared, crate::ffi::Background), Error> { - self.prepared.remove(prepared).flatten().ok_or(Error::NotFound) + self.prepared.remove(prepared).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 b12bfeb152f..df02aa46a63 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, } @@ -81,7 +76,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 { @@ -102,13 +97,8 @@ impl Session { monitor: reconnect.monitor(), }; - 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: @@ -122,7 +112,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, } }))) @@ -140,9 +130,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); } }); @@ -165,13 +155,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. @@ -183,13 +171,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 @@ -202,7 +190,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); } }); @@ -220,7 +208,7 @@ impl Session { } }); - 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(), @@ -234,12 +222,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. Every handle shares one reservation registry, @@ -321,12 +304,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) } } diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 2f5bc62050c..885103943eb 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -5901,11 +5901,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))); @@ -6011,7 +6007,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) { @@ -6102,14 +6098,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, @@ -6142,26 +6142,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 4229dc2d341..cb593b1bb9d 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, }; @@ -2820,29 +2820,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, @@ -3516,7 +3498,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 = (|| { @@ -3597,11 +3579,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(), @@ -6990,6 +6968,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() @@ -7036,11 +7015,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()); + } + } +}