1 #![warn(rust_2018_idioms)]
2 // Wasi does not support panic recovery or threading
3 #![cfg(all(feature = "full", not(target_os = "wasi")))]
4
5 use tokio::net::TcpListener;
6 use tokio::runtime;
7 use tokio_test::{assert_ok, assert_pending};
8
9 use futures::task::{waker_ref, ArcWake};
10 use std::future::Future;
11 use std::net::TcpStream;
12 use std::pin::Pin;
13 use std::sync::{mpsc, Arc, Mutex};
14 use std::task::Context;
15
16 struct Task<T> {
17 future: Mutex<Pin<Box<T>>>,
18 }
19
20 impl<T: Send> ArcWake for Task<T> {
wake_by_ref(_: &Arc<Self>)21 fn wake_by_ref(_: &Arc<Self>) {
22 // Do nothing...
23 }
24 }
25
26 impl<T> Task<T> {
new(future: T) -> Task<T>27 fn new(future: T) -> Task<T> {
28 Task {
29 future: Mutex::new(Box::pin(future)),
30 }
31 }
32 }
33
34 #[test]
35 #[cfg_attr(miri, ignore)] // No `socket` in miri.
test_drop_on_notify()36 fn test_drop_on_notify() {
37 // When the reactor receives a kernel notification, it notifies the
38 // task that holds the associated socket. If this notification results in
39 // the task being dropped, the socket will also be dropped.
40 //
41 // Previously, there was a deadlock scenario where the reactor, while
42 // notifying, held a lock and the task being dropped attempted to acquire
43 // that same lock in order to clean up state.
44 //
45 // To simulate this case, we create a fake executor that does nothing when
46 // the task is notified. This simulates an executor in the process of
47 // shutting down. Then, when the task handle is dropped, the task itself is
48 // dropped.
49
50 let rt = runtime::Builder::new_current_thread()
51 .enable_all()
52 .build()
53 .unwrap();
54
55 let (addr_tx, addr_rx) = mpsc::channel();
56
57 // Define a task that just drains the listener
58 let task = Arc::new(Task::new(async move {
59 // Create a listener
60 let listener = assert_ok!(TcpListener::bind("127.0.0.1:0").await);
61
62 // Send the address
63 let addr = listener.local_addr().unwrap();
64 addr_tx.send(addr).unwrap();
65
66 loop {
67 let _ = listener.accept().await;
68 }
69 }));
70
71 {
72 let _enter = rt.enter();
73 let waker = waker_ref(&task);
74 let mut cx = Context::from_waker(&waker);
75 assert_pending!(task.future.lock().unwrap().as_mut().poll(&mut cx));
76 }
77
78 // Get the address
79 let addr = addr_rx.recv().unwrap();
80
81 drop(task);
82
83 // Establish a connection to the acceptor
84 let _s = TcpStream::connect(addr).unwrap();
85
86 // Force the reactor to turn
87 rt.block_on(async {});
88 }
89
90 #[test]
91 #[should_panic(
92 expected = "A Tokio 1.x context was found, but IO is disabled. Call `enable_io` on the runtime builder to enable IO."
93 )]
94 #[cfg_attr(miri, ignore)] // No `socket` in miri.
panics_when_io_disabled()95 fn panics_when_io_disabled() {
96 let rt = runtime::Builder::new_current_thread().build().unwrap();
97
98 rt.block_on(async {
99 let _ =
100 tokio::net::TcpListener::from_std(std::net::TcpListener::bind("127.0.0.1:0").unwrap());
101 });
102 }
103