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