Skip to content

Commit

Permalink
Mark everything in tokio::sync::mpsc as UnwindSafe
Browse files Browse the repository at this point in the history
Since these types are safe in the face of panics and cannot cause data
corruption, they're `UnwindSafe` irrespective of the channel's element
type.

CC tokio-rs#4657.
  • Loading branch information
tbu- committed Aug 18, 2024
1 parent aac5227 commit 0c4d295
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tokio/src/sync/mpsc/chan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::sync::notify::Notify;
use crate::util::cacheline::CachePadded;

use std::fmt;
use std::panic;
use std::process;
use std::sync::atomic::Ordering::{AcqRel, Acquire, Relaxed, Release};
use std::task::Poll::{Pending, Ready};
Expand Down Expand Up @@ -108,6 +109,8 @@ impl<T> fmt::Debug for RxFields<T> {

unsafe impl<T: Send, S: Send> Send for Chan<T, S> {}
unsafe impl<T: Send, S: Sync> Sync for Chan<T, S> {}
impl<T, S> panic::RefUnwindSafe for Chan<T, S> {}
impl<T, S> panic::UnwindSafe for Chan<T, S> {}

pub(crate) fn channel<T, S: Semaphore>(semaphore: S) -> (Tx<T, S>, Rx<T, S>) {
let (tx, rx) = list::channel();
Expand Down
25 changes: 25 additions & 0 deletions tokio/tests/sync_mpsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use wasm_bindgen_test::wasm_bindgen_test as maybe_tokio_test;
use tokio::test as maybe_tokio_test;

use std::fmt;
use std::panic;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio::sync::mpsc::error::{TryRecvError, TrySendError};
Expand All @@ -26,6 +27,30 @@ trait AssertSend: Send {}
impl AssertSend for mpsc::Sender<i32> {}
impl AssertSend for mpsc::Receiver<i32> {}

#[allow(unused)]
trait AssertRefUnwindSafe: panic::RefUnwindSafe {}
impl<T> AssertRefUnwindSafe for mpsc::OwnedPermit<T> {}
impl<'a, T> AssertRefUnwindSafe for mpsc::Permit<'a, T> {}
impl<'a, T> AssertRefUnwindSafe for mpsc::PermitIterator<'a, T> {}
impl<T> AssertRefUnwindSafe for mpsc::Receiver<T> {}
impl<T> AssertRefUnwindSafe for mpsc::Sender<T> {}
impl<T> AssertRefUnwindSafe for mpsc::UnboundedReceiver<T> {}
impl<T> AssertRefUnwindSafe for mpsc::UnboundedSender<T> {}
impl<T> AssertRefUnwindSafe for mpsc::WeakSender<T> {}
impl<T> AssertRefUnwindSafe for mpsc::WeakUnboundedSender<T> {}

#[allow(unused)]
trait AssertUnwindSafe: panic::UnwindSafe {}
impl<T> AssertUnwindSafe for mpsc::OwnedPermit<T> {}
impl<'a, T> AssertUnwindSafe for mpsc::Permit<'a, T> {}
impl<'a, T> AssertUnwindSafe for mpsc::PermitIterator<'a, T> {}
impl<T> AssertUnwindSafe for mpsc::Receiver<T> {}
impl<T> AssertUnwindSafe for mpsc::Sender<T> {}
impl<T> AssertUnwindSafe for mpsc::UnboundedReceiver<T> {}
impl<T> AssertUnwindSafe for mpsc::UnboundedSender<T> {}
impl<T> AssertUnwindSafe for mpsc::WeakSender<T> {}
impl<T> AssertUnwindSafe for mpsc::WeakUnboundedSender<T> {}

#[maybe_tokio_test]
async fn send_recv_with_buffer() {
let (tx, mut rx) = mpsc::channel::<i32>(16);
Expand Down

0 comments on commit 0c4d295

Please sign in to comment.