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

@biaoma-ty biaoma-ty commented Nov 30, 2026 •

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), and relax oneshot::Sender<T> to implement Sync unconditionally.

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: Option<Box<WakerSet>> under each channel's existing state lock rather than introducing a second lock or shared close-event type, keeping idle channels allocation-free with minimal inline overhead 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. Relaxing oneshot::Sender<T> to unconditional Sync (sound because &Sender<T> never accesses or drops T) lets Disconnected borrow &Sender<T> directly without a duplicate NonNull or unsafe impl Send.
  • completion::Shared<T> packs DISCONNECT_WAITERS_BIT into observers: AtomicUsize (with half-range overflow headroom in Completion::clone), stores disconnect: Option<Box<WakerSet>> under waiters, and holds 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, and Disconnected::poll skips fetch_or once the bit is already set.

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

Fixes #467

@biaoma-ty
biaoma-ty force-pushed the feat/sender-disconnected branch from c82cd46 to 9edb13e Compare December 3, 2026 06:37

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