From 1281c893a053d2507967259995a89cdd8b8542de Mon Sep 17 00:00:00 2001 From: Jamie Hodkinson Date: Fri, 7 Jan 2022 09:39:26 +0100 Subject: [PATCH 1/5] add UnwindSafe impls to UdpSocket Fixes: #4336 --- tokio/src/net/udp.rs | 5 +++++ tokio/tests/udp.rs | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tokio/src/net/udp.rs b/tokio/src/net/udp.rs index 68cc982390b..61c2d81d135 100644 --- a/tokio/src/net/udp.rs +++ b/tokio/src/net/udp.rs @@ -5,6 +5,7 @@ use std::convert::TryFrom; use std::fmt; use std::io; use std::net::{self, Ipv4Addr, Ipv6Addr, SocketAddr}; +use std::panic::{RefUnwindSafe, UnwindSafe}; use std::task::{Context, Poll}; cfg_io_util! { @@ -1704,6 +1705,10 @@ impl TryFrom for UdpSocket { } } +impl UnwindSafe for UdpSocket {} + +impl RefUnwindSafe for UdpSocket {} + impl fmt::Debug for UdpSocket { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.io.fmt(f) diff --git a/tokio/tests/udp.rs b/tokio/tests/udp.rs index 11a97276c1f..5ab679cbf43 100644 --- a/tokio/tests/udp.rs +++ b/tokio/tests/udp.rs @@ -2,7 +2,7 @@ #![cfg(feature = "full")] use futures::future::poll_fn; -use std::io; +use std::{io, panic}; use std::net::SocketAddr; use std::sync::Arc; use tokio::{io::ReadBuf, net::UdpSocket}; @@ -494,3 +494,14 @@ async fn peer_addr() { sock.connect(peer_addr).await.unwrap(); assert_eq!(sock.peer_addr().unwrap().ip(), peer_addr.ip()); } + +#[tokio::test] +async fn udp_socket_is_unwind_safe() { + let addr = "127.0.0.1:0".parse::().unwrap(); + let peer_addr = "127.0.0.1:11100".parse::().unwrap(); + let sock = UdpSocket::bind(addr).await.unwrap(); + + assert!(panic::catch_unwind(|| async { + sock.connect(peer_addr).await.unwrap(); + }).is_ok()); +} From 44f80f37a4bbe882d3f77730625cb7910c3c24ff Mon Sep 17 00:00:00 2001 From: Jamie Hodkinson Date: Fri, 7 Jan 2022 09:50:46 +0100 Subject: [PATCH 2/5] fix rustfmt issue --- tokio/tests/udp.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tokio/tests/udp.rs b/tokio/tests/udp.rs index 5ab679cbf43..f0721ccad2c 100644 --- a/tokio/tests/udp.rs +++ b/tokio/tests/udp.rs @@ -2,9 +2,9 @@ #![cfg(feature = "full")] use futures::future::poll_fn; -use std::{io, panic}; use std::net::SocketAddr; use std::sync::Arc; +use std::{io, panic}; use tokio::{io::ReadBuf, net::UdpSocket}; use tokio_test::assert_ok; @@ -503,5 +503,6 @@ async fn udp_socket_is_unwind_safe() { assert!(panic::catch_unwind(|| async { sock.connect(peer_addr).await.unwrap(); - }).is_ok()); + }) + .is_ok()); } From 17814f0950440a9511b101e930a73a0727f4b569 Mon Sep 17 00:00:00 2001 From: Jamie Hodkinson Date: Fri, 7 Jan 2022 17:10:33 +0100 Subject: [PATCH 3/5] io: add UnwindSafe impls to PollEvented --- tokio/src/io/poll_evented.rs | 17 +++++++++++++++++ tokio/src/net/udp.rs | 5 ----- tokio/tests/udp.rs | 14 +------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tokio/src/io/poll_evented.rs b/tokio/src/io/poll_evented.rs index 44e68a2a9a0..e11b76a9924 100644 --- a/tokio/src/io/poll_evented.rs +++ b/tokio/src/io/poll_evented.rs @@ -4,6 +4,7 @@ use mio::event::Source; use std::fmt; use std::io; use std::ops::Deref; +use std::panic::{RefUnwindSafe, UnwindSafe}; cfg_io_driver! { /// Associates an I/O resource that implements the [`std::io::Read`] and/or @@ -185,6 +186,10 @@ feature! { } } +impl UnwindSafe for PollEvented {} + +impl RefUnwindSafe for PollEvented {} + impl Deref for PollEvented { type Target = E; @@ -207,3 +212,15 @@ impl Drop for PollEvented { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn assert_unwind_safe() { + is_unwind_safe::>(); + } + + fn is_unwind_safe() {} +} diff --git a/tokio/src/net/udp.rs b/tokio/src/net/udp.rs index 61c2d81d135..68cc982390b 100644 --- a/tokio/src/net/udp.rs +++ b/tokio/src/net/udp.rs @@ -5,7 +5,6 @@ use std::convert::TryFrom; use std::fmt; use std::io; use std::net::{self, Ipv4Addr, Ipv6Addr, SocketAddr}; -use std::panic::{RefUnwindSafe, UnwindSafe}; use std::task::{Context, Poll}; cfg_io_util! { @@ -1705,10 +1704,6 @@ impl TryFrom for UdpSocket { } } -impl UnwindSafe for UdpSocket {} - -impl RefUnwindSafe for UdpSocket {} - impl fmt::Debug for UdpSocket { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.io.fmt(f) diff --git a/tokio/tests/udp.rs b/tokio/tests/udp.rs index f0721ccad2c..11a97276c1f 100644 --- a/tokio/tests/udp.rs +++ b/tokio/tests/udp.rs @@ -2,9 +2,9 @@ #![cfg(feature = "full")] use futures::future::poll_fn; +use std::io; use std::net::SocketAddr; use std::sync::Arc; -use std::{io, panic}; use tokio::{io::ReadBuf, net::UdpSocket}; use tokio_test::assert_ok; @@ -494,15 +494,3 @@ async fn peer_addr() { sock.connect(peer_addr).await.unwrap(); assert_eq!(sock.peer_addr().unwrap().ip(), peer_addr.ip()); } - -#[tokio::test] -async fn udp_socket_is_unwind_safe() { - let addr = "127.0.0.1:0".parse::().unwrap(); - let peer_addr = "127.0.0.1:11100".parse::().unwrap(); - let sock = UdpSocket::bind(addr).await.unwrap(); - - assert!(panic::catch_unwind(|| async { - sock.connect(peer_addr).await.unwrap(); - }) - .is_ok()); -} From d0f6b7430fc7961b598af14eb80da7fd9f5d95c4 Mon Sep 17 00:00:00 2001 From: Jamie Hodkinson Date: Sat, 8 Jan 2022 11:01:43 +0100 Subject: [PATCH 4/5] add tests to check tokio::net types are UnwindSafe --- tokio/src/io/poll_evented.rs | 12 ------------ tokio/tests/net_types_unwind.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 tokio/tests/net_types_unwind.rs diff --git a/tokio/src/io/poll_evented.rs b/tokio/src/io/poll_evented.rs index e11b76a9924..ce4c1426acc 100644 --- a/tokio/src/io/poll_evented.rs +++ b/tokio/src/io/poll_evented.rs @@ -212,15 +212,3 @@ impl Drop for PollEvented { } } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn assert_unwind_safe() { - is_unwind_safe::>(); - } - - fn is_unwind_safe() {} -} diff --git a/tokio/tests/net_types_unwind.rs b/tokio/tests/net_types_unwind.rs new file mode 100644 index 00000000000..f4f5bb58ad0 --- /dev/null +++ b/tokio/tests/net_types_unwind.rs @@ -0,0 +1,29 @@ +#![warn(rust_2018_idioms)] +#![cfg(feature = "full")] + +use std::panic::{RefUnwindSafe, UnwindSafe}; + +#[test] +fn net_types_are_unwind_safe() { + is_unwind_safe::(); + is_unwind_safe::(); + is_unwind_safe::(); + is_unwind_safe::(); +} + +#[test] +#[cfg(unix)] +fn unix_net_types_are_unwind_safe() { + is_unwind_safe::(); + is_unwind_safe::(); + is_unwind_safe::(); +} + +#[test] +#[cfg(windows)] +fn windows_net_types_are_unwind_safe() { + is_unwind_safe::(); + is_unwind_safe::(); +} + +fn is_unwind_safe() {} From 42f32c765394b267eb24b5960c8b1cb9c0075d1b Mon Sep 17 00:00:00 2001 From: Jamie Hodkinson Date: Sat, 8 Jan 2022 11:57:12 +0100 Subject: [PATCH 5/5] resolve windows build issue --- tokio/tests/net_types_unwind.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tokio/tests/net_types_unwind.rs b/tokio/tests/net_types_unwind.rs index f4f5bb58ad0..4eb4a87fd7a 100644 --- a/tokio/tests/net_types_unwind.rs +++ b/tokio/tests/net_types_unwind.rs @@ -22,8 +22,11 @@ fn unix_net_types_are_unwind_safe() { #[test] #[cfg(windows)] fn windows_net_types_are_unwind_safe() { - is_unwind_safe::(); - is_unwind_safe::(); + use tokio::net::windows::named_pipe::NamedPipeClient; + use tokio::net::windows::named_pipe::NamedPipeServer; + + is_unwind_safe::(); + is_unwind_safe::(); } fn is_unwind_safe() {}