Skip to content

Commit

Permalink
sync: improve cancel-safety documentation for mpsc::Sender::send (#5947)
Browse files Browse the repository at this point in the history
This specific issue (data loss because a send got cancelled) has bitten
our team a couple of times over the last few months. We've switched to
recommending this kind of reserve pattern instead.
  • Loading branch information
sunshowers committed Aug 25, 2023
1 parent d1dae25 commit 0fe24fc
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tokio/src/sync/mpsc/bounded.rs
Expand Up @@ -439,7 +439,11 @@ impl<T> Sender<T> {
///
/// If `send` is used as the event in a [`tokio::select!`](crate::select)
/// statement and some other branch completes first, then it is guaranteed
/// that the message was not sent.
/// that the message was not sent. **However, in that case, the message
/// is dropped and will be lost.**
///
/// To avoid losing messages, use [`reserve`](Self::reserve) to reserve
/// capacity, then use the returned [`Permit`] to send the message.
///
/// This channel uses a queue to ensure that calls to `send` and `reserve`
/// complete in the order they were requested. Cancelling a call to
Expand Down

0 comments on commit 0fe24fc

Please sign in to comment.