Skip to content

Commit 0fe24fc

Browse files
authored
sync: improve cancel-safety documentation for mpsc::Sender::send (#5947)
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.
1 parent d1dae25 commit 0fe24fc

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

tokio/src/sync/mpsc/bounded.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,11 @@ impl<T> Sender<T> {
439439
///
440440
/// If `send` is used as the event in a [`tokio::select!`](crate::select)
441441
/// statement and some other branch completes first, then it is guaranteed
442-
/// that the message was not sent.
442+
/// that the message was not sent. **However, in that case, the message
443+
/// is dropped and will be lost.**
444+
///
445+
/// To avoid losing messages, use [`reserve`](Self::reserve) to reserve
446+
/// capacity, then use the returned [`Permit`] to send the message.
443447
///
444448
/// This channel uses a queue to ensure that calls to `send` and `reserve`
445449
/// complete in the order they were requested. Cancelling a call to

0 commit comments

Comments
 (0)