Skip to content

Add async disconnection wait to channel senders #467

Backlog

Description

@biaoma-ty

Motivation

#299 (#391, #450) added synchronous is_disconnected(&self) -> bool queries to channel endpoints, leaving an async disconnect-wait API out of scope.

When a producer task forwards a stream into a bounded channel—for example, an HTTP/SSE gateway pumping an upstream response body into mpsc::bounded for a downstream client—it spends most of its time awaiting the upstream source rather than reserve or send. If the downstream client disconnects and drops the receiver while the upstream is idle, is_disconnected() cannot wake the parked task to cancel the upstream request and release held permits.

Without a disconnect-wait future on the sender, callers have to pair each mpsc channel with a side oneshot::channel::<()> whose sender is held by the consumer stream solely to wake a select! branch on drop. That adds an extra heap allocation per stream and requires manual tuple drop ordering on the consumer side.

Add pub fn disconnected(&self) on senders where receiver disconnection is irreversible, following the disconnected vocabulary from #141 and #299.

Scope

Endpoint Change
mpsc::{BoundedSender, UnboundedSender} Wait until the receiver is dropped.
mpmc::{BoundedSender, UnboundedSender} Wait until all receivers are dropped.
spmc::{BoundedSender, UnboundedSender} Wait until all receivers are dropped.
oneshot::Sender Wait until the receiver or Recv future is dropped.
completion::Completer Wait until all Completion observers are dropped (#449 added is_disconnected).
watch::Sender and broadcast::mpmc senders Exclude: subscribe can create new receivers after the count reaches zero, so subscriber absence is not irreversible disconnection (#299).

Semantics

  • Completes immediately if is_disconnected() is already true, and remains ready on subsequent calls.
  • Cancel-safe: dropping a pending wait future unregisters its waker without changing channel state, capacity, or other senders, so sender.disconnected() can be polled repeatedly in a select! loop.
  • Available on impl<T> without extra bounds on T, returning an Unpin future (consistent with refactor(mpsc): replace mutex-backed bounded queue with preallocated slot ring #465).
  • Channels that never poll disconnected() should not pay extra heap allocations.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions