Skip to content

feat: add async disconnected wait to channel senders and Completer - #491

Open
biaoma-ty wants to merge 2 commits into
apache:mainfrom
biaoma-ty:feat/sender-disconnected
Open

biaoma-ty wants to merge 2 commits into
apache:mainfrom
biaoma-ty:feat/sender-disconnected

Conversation

@biaoma-ty

Copy link
Copy Markdown
Contributor

Summary

Add disconnected(&self) -> impl Future<Output = ()> to senders and completers whose receiver disconnection is irreversible across two focused commits:

  1. Add disconnected to mpsc, mpmc, and spmc senders: Expose disconnected on mpsc::{BoundedSender, UnboundedSender}, mpmc::{BoundedSender, UnboundedSender}, and spmc::{BoundedSender, UnboundedSender}. Each future completes immediately once all receivers are dropped, stays ready on re-poll, is Unpin (and Send for T: Send), and unregisters only its own waker when cancelled.
  2. Add disconnected to oneshot::Sender and completion::Completer: Expose disconnected on oneshot::Sender (waking when Receiver or a polled Recv future is dropped) and completion::Completer (waking when all Completion observers are dropped).

watch::Sender and broadcast::mpmc senders remain excluded because subscribe can recreate receivers after the count reaches zero.

Design Notes

  • Queue channels (mpsc, mpmc, and spmc) store disconnect_waiters: WakerSet under each channel's existing state lock rather than introducing a second lock or shared close-event type, keeping unused channels allocation-free and waking disconnect waiters before dropping buffered payloads.
  • oneshot::Channel<T> lazily initializes disconnect: OnceLock<Option<Arc<DisconnectWaiters>>>, avoiding heap allocation when disconnected() is unused and avoiding extra raw-pointer unsafe. Receiver teardown seals OnceLock and clones an owned Arc before publishing DISCONNECTED and releasing Channel<T>; a concurrent poll observing the seal window yields via wake_by_ref() and Poll::Pending without spinning.
  • completion::Shared<T> packs DISCONNECT_WAITERS_BIT into observers: AtomicUsize and stores Option<Arc<Shared<T>>> in Completer<T> (preserving its pointer-sized niche layout and is_disconnected() linearizability). Normal Completion::drop remains lock-free when disconnected() has not been polled, locking waiters only on the cold transition that drops the final observer with DISCONNECT_WAITERS_BIT set.

Validation: cargo x lint, cargo x check, cargo x test, Miri (-Zmiri-strict-provenance -Zmiri-symbolic-alignment-check), and MSRV (1.86.0).

Fixes #467

This branch has not been deployed

No deployments
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.

Add async disconnection wait to channel senders

1 participant