diff --git a/src/runtime/builder.rs b/src/runtime/builder.rs index b4b0346da..9da8eba94 100644 --- a/src/runtime/builder.rs +++ b/src/runtime/builder.rs @@ -273,9 +273,6 @@ impl Builder { // Get a handle to the clock for the runtime. let clock = self.clock.clone(); - // Get a handle to the first reactor. - let reactor = reactor_handles[0].clone(); - let pool = self.threadpool_builder .around_worker(move |w, enter| { let index = w.id().to_usize(); @@ -299,9 +296,14 @@ impl Builder { }) .build(); + // To support deprecated `reactor()` function + let reactor = Reactor::new()?; + let reactor_handle = reactor.handle(); + Ok(Runtime { inner: Some(Inner { - reactor, + reactor_handle, + reactor: Mutex::new(Some(reactor)), pool, }), }) diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 0b65606c2..8dbff44d1 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -121,9 +121,10 @@ pub use self::builder::Builder; pub use self::shutdown::Shutdown; pub use self::task_executor::TaskExecutor; -use reactor::Handle; +use reactor::{Handle, Reactor}; use std::io; +use std::sync::Mutex; use tokio_executor::enter; use tokio_threadpool as threadpool; @@ -152,8 +153,11 @@ pub struct Runtime { #[derive(Debug)] struct Inner { - /// A handle to one of the per-worker reactors. - reactor: Handle, + /// A handle to the reactor in the background thread. + reactor_handle: Handle, + + // TODO: This should go away in 0.2 + reactor: Mutex>, /// Task execution pool. pool: threadpool::ThreadPool, @@ -280,7 +284,14 @@ impl Runtime { /// ``` #[deprecated(since = "0.1.11", note = "there is now a reactor per worker thread")] pub fn reactor(&self) -> &Handle { - &self.inner().reactor + let mut reactor = self.inner().reactor.lock().unwrap(); + if let Some(reactor) = reactor.take() { + if let Ok(background) = reactor.background() { + background.forget(); + } + } + + &self.inner().reactor_handle } /// Return a handle to the runtime's executor. diff --git a/tests/runtime.rs b/tests/runtime.rs index a528ef1c5..39e8f12f5 100644 --- a/tests/runtime.rs +++ b/tests/runtime.rs @@ -466,3 +466,30 @@ mod nested_enter { } } +#[test] +fn runtime_reactor_handle() { + #![allow(deprecated)] + + use futures::Stream; + use std::net::{ + TcpListener as StdListener, + TcpStream as StdStream, + }; + + let rt = Runtime::new().unwrap(); + + let std_listener = StdListener::bind("127.0.0.1:0").unwrap(); + let tk_listener = TcpListener::from_std(std_listener, rt.handle()).unwrap(); + + let addr = tk_listener.local_addr().unwrap(); + + // Spawn a thread since we are avoiding the runtime + let th = thread::spawn(|| { + for _ in tk_listener.incoming().take(1).wait() { + } + }); + + let _ = StdStream::connect(&addr).unwrap(); + + th.join().unwrap(); +}