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 mut rt = runtime::Builder::new()
49         .basic_scheduler()
50         .enable_all()
51         .build()
52         .unwrap();
53 
54     let (addr_tx, addr_rx) = mpsc::channel();
55 
56     // Define a task that just drains the listener
57     let task = Arc::new(Task::new(async move {
58         // Create a listener
59         let mut listener = assert_ok!(TcpListener::bind("127.0.0.1:0").await);
60 
61         // Send the address
62         let addr = listener.local_addr().unwrap();
63         addr_tx.send(addr).unwrap();
64 
65         loop {
66             let _ = listener.accept().await;
67         }
68     }));
69 
70     {
71         rt.enter(|| {
72             let waker = waker_ref(&task);
73             let mut cx = Context::from_waker(&waker);
74             assert_pending!(task.future.lock().unwrap().as_mut().poll(&mut cx));
75         });
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