Skip to content

feat(broadcast): add MPMC receiver state queries, bounded reservation, and trait assertions - #452

Merged
tisonkun merged 5 commits into
mainfrom
feat/broadcast-parity-and-reserve
Nov 22, 2026
Merged

tisonkun merged 5 commits into
mainfrom
feat/broadcast-parity-and-reserve

Conversation

@tisonkun

@tisonkun tisonkun commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of #445 (Wave 2).

  • Add lock-free is_disconnected and per-cursor is_empty to broadcast::mpmc::{BoundedReceiver, UnboundedReceiver} so callers can inspect sender liveness and unread backlog without receiving.
  • Add cancellation-safe reserve and try_reserve returning a borrowed broadcast::mpmc::Permit<'_, T> on broadcast::mpmc::BoundedSender, allowing producers to wait for capacity in select! branches before constructing a message without pre-assigning sequence numbers.
  • Extend traits_test to assert Send, Sync, Unpin, UnwindSafe, and RefUnwindSafe across broadcast::mpmc endpoints, Permit, and futures, and add try_reserve_and_send_and_try_recv and cancel_blocked_reserve benchmarks to primitives::broadcast::mpmc::bounded.

Design Notes

  • Quota-only reservation without sequence gaps: Holding a Permit increments SendQueue::reserved (retained + reserved <= capacity) rather than claiming a version slot in Backlog. Message sequence numbers are assigned only when Permit::send publishes, so dropping an unused Permit or cancelling a pending reserve never leaves a gap in committed broadcast order.
  • Zero-receiver invariants on RetryWaiter: Because BoundedSender::subscribe and Permit::send are both synchronous and infallible, concurrent Permit reservations stay bounded by capacity even while no receivers are active (preventing a late subscribe() followed by Permit::send calls from exceeding the fixed backlog capacity), whereas direct send and try_send continue to discard immediately without waiting when no receivers exist. SendQueue reuses WaitList<RetryWaiter> directly without touching internal/.
  • Overflow unwind ordering: Both Permit::send and BoundedSender::send retire their reservation or waiter only after Backlog::publish succeeds, ensuring a version-overflow panic unlocks the channel, releases or forwards the notification, and wakes the next waiting producer before running the payload destructor.

Benchmarks

Apple M4 Max (cargo bench -p benchmarks --bench primitives -- broadcast::mpmc::bounded, median of 5 runs):

Benchmark Rounds main (d8157bc) This PR
try_send_and_try_recv 9M 224 ms 223 ms
try_reserve_and_send_and_try_recv 9M — 284 ms
try_send_and_drain_fanout/1 20M 498 ms 502 ms
try_send_and_drain_fanout/4 5M 232 ms 221 ms
try_send_and_drain_fanout/16 1.25M 162 ms 163 ms
full_channel_sender_handoff_cycle/1 2M 189 ms 191 ms
full_channel_sender_handoff_cycle/4 500k 120 ms 123 ms
deliver_to_waiting_receiver 6M 247 ms 247 ms
send_without_receivers 48M 256 ms 259 ms
try_send_when_full 60M 305 ms 305 ms
cancel_blocked_send 6M 189 ms 192 ms
cancel_blocked_reserve 6M — 84 ms
drop_lagging_receiver_wakes_senders/32 62.5k 188 ms 184 ms

Add `is_disconnected` and `is_empty` to `broadcast::mpmc::BoundedReceiver`
and `UnboundedReceiver` for endpoint-state parity with `mpsc`, `mpmc`, and
`spmc`:
- `is_disconnected` loads the atomic sender count with `Acquire` ordering
  without locking the channel, reporting `true` as soon as the final sender
  is dropped even while unread buffered messages remain available.
- `is_empty` checks whether the receiver's own cursor has no unread
  messages, ignoring messages retained only for slower subscriptions.

Signed-off-by: tison <[email protected]>
Add cancellation-safe two-phase reservation (`reserve`, `try_reserve`, and
`broadcast::mpmc::Permit<'_, T>`) to `broadcast::mpmc::BoundedSender`:
- Track held reservations in `SendQueue::reserved` alongside retained
  messages (`retained + reserved <= capacity`) without pre-assigning
  broadcast sequence numbers, so dropping a permit or cancelling a pending
  reservation never creates gaps in committed message order.
- Keep direct `send` and `try_send` non-blocking when no receivers are
  active while bounding concurrent `Permit` reservations to `capacity`, and
  distinguish slot-backed vs zero-receiver notifications in `SendWaiter` so
  dropping the last receiver wakes blocked `send` futures without spuriously
  waking `reserve` waiters beyond remaining capacity.
- Release the reserved slot and wake a waiting producer before running the
  rejected payload's destructor if `Permit::send` panics on version
  overflow.

Signed-off-by: tison <[email protected]>
…angelog

Assert `Send`, `Sync`, `Unpin`, `UnwindSafe`, and `RefUnwindSafe` for
`broadcast::mpmc` endpoints, `Permit`, and futures in `traits_test`.

Add `try_reserve_and_send_and_try_recv` and `cancel_blocked_reserve` to
`primitives::broadcast::mpmc::bounded`, and document the new receiver state
queries and bounded reservation APIs in `CHANGELOG.md`.

Signed-off-by: tison <[email protected]>
… unwind

Tighten bounded broadcast wakeup accounting and cleanup unused internal
helpers:
- Track detached `SendWaiter::NotifiedSlot` nodes in `SendQueue::notified_slots`
  so dropping the last receiver or cancelling/completing a zero-receiver send
  only notifies `reserve` waiters for unclaimed free slots rather than
  re-waking for slots that already have a notification in flight.
- Retire a pending `send` waiter only after `Backlog::publish` succeeds so a
  version-overflow unwind forwards its slot notification to the next waiter
  before dropping the unsent payload outside the channel lock.
- Clarify `bounded` constructor docs on direct-send vs reservation waiting
  conditions, and remove unused batch-notification helpers from `RetryWaiter`.

Signed-off-by: tison <[email protected]>
Replace the custom `SendWaiter` / `notified_slots` state machine and
`WaitList::unlink_if` helper with the existing `WaitList<RetryWaiter>` queue:
- Bounded broadcast producers already re-check capacity and re-queue on wakeup
  whenever a racing `try_send` or `try_reserve` claims a freed slot, so
  `notify_all` on the final receiver drop needs no separate per-waiter slot
  ledger or `internal/` changes.
- Deduplicate the version-overflow unwind tests onto `assert_overflow_drops_unlocked`
  and consolidate the zero-receiver reservation integration checks.

Signed-off-by: tison <[email protected]>
@tisonkun
tisonkun enabled auto-merge (squash) November 22, 2026 13:34
@tisonkun
tisonkun merged commit 7054cef into main Nov 22, 2026
9 checks passed
@tisonkun
tisonkun deleted the feat/broadcast-parity-and-reserve branch November 22, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant