Skip to content

Commit 093d640

Browse files
fix(net): ensure the reactor and runtime are running
If this is not done, then reactor is not running, resulting in the sockets not actually connecting. Closes #818
1 parent a602a91 commit 093d640

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed

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
}

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
}

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
}

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 }

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
}

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

0 commit comments

Comments
 (0)