Skip to content

Commit 5f418f0

Browse files
Merge pull request #819 from async-rs/fix-sockets
2 parents 5d55fa7 + 06a2fb8 commit 5f418f0

File tree

14 files changed

+63
-18
lines changed

14 files changed

+63
-18
lines changed

Diff for: src/future/future/delay.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Duration;
55
use pin_project_lite::pin_project;
66

77
use crate::task::{Context, Poll};
8-
use crate::utils::Timer;
8+
use crate::utils::{timer_after, Timer};
99

1010
pin_project! {
1111
#[doc(hidden)]
@@ -20,7 +20,7 @@ pin_project! {
2020

2121
impl<F> DelayFuture<F> {
2222
pub fn new(future: F, dur: Duration) -> DelayFuture<F> {
23-
let delay = Timer::after(dur);
23+
let delay = timer_after(dur);
2424

2525
DelayFuture { future, delay }
2626
}

Diff for: src/future/timeout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::time::Duration;
77
use pin_project_lite::pin_project;
88

99
use crate::task::{Context, Poll};
10-
use crate::utils::Timer;
10+
use crate::utils::{timer_after, Timer};
1111

1212
/// Awaits a future or times out after a duration of time.
1313
///
@@ -51,7 +51,7 @@ impl<F> TimeoutFuture<F> {
5151
pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
5252
TimeoutFuture {
5353
future,
54-
delay: Timer::after(dur),
54+
delay: timer_after(dur),
5555
}
5656
}
5757
}

Diff for: src/io/timeout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::Duration;
66
use pin_project_lite::pin_project;
77

88
use crate::io;
9-
use crate::utils::Timer;
9+
use crate::utils::{timer_after, Timer};
1010

1111
/// Awaits an I/O future or times out after a duration of time.
1212
///
@@ -37,7 +37,7 @@ where
3737
F: Future<Output = io::Result<T>>,
3838
{
3939
Timeout {
40-
timeout: Timer::after(dur),
40+
timeout: timer_after(dur),
4141
future: f,
4242
}
4343
.await

Diff for: src/net/tcp/listener.rs

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ impl TcpListener {
7575
///
7676
/// [`local_addr`]: #method.local_addr
7777
pub async fn bind<A: ToSocketAddrs>(addrs: A) -> io::Result<TcpListener> {
78+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
79+
7880
let mut last_err = None;
7981
let addrs = addrs.to_socket_addrs().await?;
8082

@@ -200,6 +202,8 @@ impl<'a> Stream for Incoming<'a> {
200202
impl From<std::net::TcpListener> for TcpListener {
201203
/// Converts a `std::net::TcpListener` into its asynchronous equivalent.
202204
fn from(listener: std::net::TcpListener) -> TcpListener {
205+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
206+
203207
TcpListener {
204208
watcher: Async::new(listener).expect("TcpListener is known to be good"),
205209
}

Diff for: src/net/tcp/stream.rs

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ impl TcpStream {
7171
/// # Ok(()) }) }
7272
/// ```
7373
pub async fn connect<A: ToSocketAddrs>(addrs: A) -> io::Result<TcpStream> {
74+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
75+
7476
let mut last_err = None;
7577
let addrs = addrs.to_socket_addrs().await?;
7678

@@ -356,6 +358,8 @@ impl Write for &TcpStream {
356358
impl From<std::net::TcpStream> for TcpStream {
357359
/// Converts a `std::net::TcpStream` into its asynchronous equivalent.
358360
fn from(stream: std::net::TcpStream) -> TcpStream {
361+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
362+
359363
TcpStream {
360364
watcher: Arc::new(Async::new(stream).expect("TcpStream is known to be good")),
361365
}

Diff for: src/net/udp/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ impl UdpSocket {
6868
/// # Ok(()) }) }
6969
/// ```
7070
pub async fn bind<A: ToSocketAddrs>(addrs: A) -> io::Result<UdpSocket> {
71+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
72+
7173
let mut last_err = None;
7274
let addrs = addrs.to_socket_addrs().await?;
7375

@@ -479,6 +481,8 @@ impl UdpSocket {
479481
impl From<std::net::UdpSocket> for UdpSocket {
480482
/// Converts a `std::net::UdpSocket` into its asynchronous equivalent.
481483
fn from(socket: std::net::UdpSocket) -> UdpSocket {
484+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
485+
482486
UdpSocket {
483487
watcher: Async::new(socket).expect("UdpSocket is known to be good"),
484488
}

Diff for: src/os/unix/net/datagram.rs

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub struct UnixDatagram {
4545

4646
impl UnixDatagram {
4747
fn new(socket: StdUnixDatagram) -> UnixDatagram {
48+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
49+
4850
UnixDatagram {
4951
watcher: Async::new(socket).expect("UnixDatagram is known to be good"),
5052
}
@@ -64,6 +66,8 @@ impl UnixDatagram {
6466
/// # Ok(()) }) }
6567
/// ```
6668
pub async fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
69+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
70+
6771
let path = path.as_ref().to_owned();
6872
let socket = Async::<StdUnixDatagram>::bind(path)?;
6973
Ok(UnixDatagram { watcher: socket })
@@ -305,6 +309,8 @@ impl fmt::Debug for UnixDatagram {
305309
impl From<StdUnixDatagram> for UnixDatagram {
306310
/// Converts a `std::os::unix::net::UnixDatagram` into its asynchronous equivalent.
307311
fn from(datagram: StdUnixDatagram) -> UnixDatagram {
312+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
313+
308314
UnixDatagram {
309315
watcher: Async::new(datagram).expect("UnixDatagram is known to be good"),
310316
}
@@ -319,6 +325,8 @@ impl AsRawFd for UnixDatagram {
319325

320326
impl FromRawFd for UnixDatagram {
321327
unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
328+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
329+
322330
let raw = StdUnixDatagram::from_raw_fd(fd);
323331
let datagram = Async::<StdUnixDatagram>::new(raw).expect("invalid file descriptor");
324332
UnixDatagram { watcher: datagram }

Diff for: src/os/unix/net/listener.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ impl UnixListener {
6868
/// # Ok(()) }) }
6969
/// ```
7070
pub async fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
71+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
72+
7173
let path = path.as_ref().to_owned();
7274
let listener = Async::<StdUnixListener>::bind(path)?;
7375

@@ -93,7 +95,12 @@ impl UnixListener {
9395
pub async fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
9496
let (stream, addr) = self.watcher.accept().await?;
9597

96-
Ok((UnixStream { watcher: Arc::new(stream) }, addr))
98+
Ok((
99+
UnixStream {
100+
watcher: Arc::new(stream),
101+
},
102+
addr,
103+
))
97104
}
98105

99106
/// Returns a stream of incoming connections.
@@ -187,6 +194,8 @@ impl Stream for Incoming<'_> {
187194
impl From<StdUnixListener> for UnixListener {
188195
/// Converts a `std::os::unix::net::UnixListener` into its asynchronous equivalent.
189196
fn from(listener: StdUnixListener) -> UnixListener {
197+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
198+
190199
UnixListener {
191200
watcher: Async::new(listener).expect("UnixListener is known to be good"),
192201
}

Diff for: src/os/unix/net/stream.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ impl UnixStream {
5757
/// # Ok(()) }) }
5858
/// ```
5959
pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
60+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
61+
6062
let path = path.as_ref().to_owned();
6163
let stream = Arc::new(Async::<StdUnixStream>::connect(path).await?);
6264

@@ -79,6 +81,8 @@ impl UnixStream {
7981
/// # Ok(()) }) }
8082
/// ```
8183
pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
84+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
85+
8286
let (a, b) = Async::<StdUnixStream>::pair()?;
8387
let a = UnixStream {
8488
watcher: Arc::new(a),
@@ -224,8 +228,12 @@ impl fmt::Debug for UnixStream {
224228
impl From<StdUnixStream> for UnixStream {
225229
/// Converts a `std::os::unix::net::UnixStream` into its asynchronous equivalent.
226230
fn from(stream: StdUnixStream) -> UnixStream {
231+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
232+
227233
let stream = Async::new(stream).expect("UnixStream is known to be good");
228-
UnixStream { watcher: Arc::new(stream) }
234+
UnixStream {
235+
watcher: Arc::new(stream),
236+
}
229237
}
230238
}
231239

Diff for: src/stream/interval.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::task::{Context, Poll};
44
use std::time::Duration;
55

66
use crate::stream::Stream;
7-
use crate::utils::Timer;
7+
use crate::utils::{timer_after, Timer};
88

99
/// Creates a new stream that yields at a set interval.
1010
///
@@ -45,7 +45,7 @@ use crate::utils::Timer;
4545
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
4646
pub fn interval(dur: Duration) -> Interval {
4747
Interval {
48-
delay: Timer::after(dur),
48+
delay: timer_after(dur),
4949
interval: dur,
5050
}
5151
}
@@ -72,7 +72,7 @@ impl Stream for Interval {
7272
return Poll::Pending;
7373
}
7474
let interval = self.interval;
75-
let _ = std::mem::replace(&mut self.delay, Timer::after(interval));
75+
let _ = std::mem::replace(&mut self.delay, timer_after(interval));
7676
Poll::Ready(Some(()))
7777
}
7878
}

Diff for: src/stream/stream/delay.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use pin_project_lite::pin_project;
66

77
use crate::stream::Stream;
88
use crate::task::{Context, Poll};
9-
use crate::utils::Timer;
9+
use crate::utils::{timer_after, Timer};
1010

1111
pin_project! {
1212
#[doc(hidden)]
@@ -24,7 +24,7 @@ impl<S> Delay<S> {
2424
pub(super) fn new(stream: S, dur: Duration) -> Self {
2525
Delay {
2626
stream,
27-
delay: Timer::after(dur),
27+
delay: timer_after(dur),
2828
delay_done: false,
2929
}
3030
}

Diff for: src/stream/stream/throttle.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use pin_project_lite::pin_project;
66

77
use crate::stream::Stream;
88
use crate::task::{Context, Poll};
9-
use crate::utils::Timer;
9+
use crate::utils::{timer_after, Timer};
1010

1111
pin_project! {
1212
/// A stream that only yields one element once every `duration`.
@@ -35,7 +35,7 @@ impl<S: Stream> Throttle<S> {
3535
stream,
3636
duration,
3737
blocked: false,
38-
delay: Timer::after(Duration::default()),
38+
delay: timer_after(Duration::default()),
3939
}
4040
}
4141
}
@@ -59,7 +59,7 @@ impl<S: Stream> Stream for Throttle<S> {
5959
Poll::Ready(None) => Poll::Ready(None),
6060
Poll::Ready(Some(v)) => {
6161
*this.blocked = true;
62-
let _ = std::mem::replace(&mut *this.delay, Timer::after(*this.duration));
62+
let _ = std::mem::replace(&mut *this.delay, timer_after(*this.duration));
6363
Poll::Ready(Some(v))
6464
}
6565
}

Diff for: src/stream/stream/timeout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use pin_project_lite::pin_project;
88

99
use crate::stream::Stream;
1010
use crate::task::{Context, Poll};
11-
use crate::utils::Timer;
11+
use crate::utils::{timer_after, Timer};
1212

1313
pin_project! {
1414
/// A stream with timeout time set
@@ -23,7 +23,7 @@ pin_project! {
2323

2424
impl<S: Stream> Timeout<S> {
2525
pub(crate) fn new(stream: S, dur: Duration) -> Self {
26-
let delay = Timer::after(dur);
26+
let delay = timer_after(dur);
2727

2828
Self { stream, delay }
2929
}

Diff for: src/utils.rs

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ mod timer {
6464
pub type Timer = smol::Timer;
6565
}
6666

67+
#[cfg(any(feature = "unstable", feature = "default"))]
68+
pub(crate) fn timer_after(dur: std::time::Duration) -> timer::Timer {
69+
#[cfg(not(target_os = "unknown"))]
70+
once_cell::sync::Lazy::force(&crate::rt::RUNTIME);
71+
72+
Timer::after(dur)
73+
}
74+
6775
#[cfg(any(
6876
all(target_arch = "wasm32", feature = "default"),
6977
all(feature = "unstable", not(feature = "default"))

0 commit comments

Comments
 (0)