From fa765168b9d14c08950515774e78851aeffacfe2 Mon Sep 17 00:00:00 2001 From: Gregory Terzian Date: Wed, 27 May 2020 17:10:54 +0800 Subject: [PATCH] net: shutdown async runtime on exit --- components/net/http_loader.rs | 9 ++++++--- components/net/resource_thread.rs | 7 +++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/components/net/http_loader.rs b/components/net/http_loader.rs index 47ea8f49a478..f12cfa4498c3 100644 --- a/components/net/http_loader.rs +++ b/components/net/http_loader.rs @@ -59,7 +59,7 @@ use tokio::prelude::{future, Future, Stream}; use tokio::runtime::Runtime; lazy_static! { - pub static ref HANDLE: Mutex = Mutex::new(Runtime::new().unwrap()); + pub static ref HANDLE: Mutex> = Mutex::new(Some(Runtime::new().unwrap())); } /// The various states an entry of the HttpCache can be in. @@ -95,7 +95,10 @@ impl HttpState { history_states: RwLock::new(HashMap::new()), http_cache: RwLock::new(HttpCache::new()), http_cache_state: Mutex::new(HashMap::new()), - client: create_http_client(tls_config, HANDLE.lock().unwrap().executor()), + client: create_http_client( + tls_config, + HANDLE.lock().unwrap().as_ref().unwrap().executor(), + ), } } } @@ -1613,7 +1616,7 @@ fn http_network_fetch( let timing_ptr3 = context.timing.clone(); let url1 = request.url(); let url2 = url1.clone(); - HANDLE.lock().unwrap().spawn( + HANDLE.lock().unwrap().as_mut().unwrap().spawn( res.into_body() .map_err(|_| ()) .fold(res_body, move |res_body, chunk| { diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index 388cacc77afd..28a397e78e6d 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -152,7 +152,7 @@ fn create_http_states( http_cache_state: Mutex::new(HashMap::new()), client: create_http_client( create_tls_config(&certs, ALPN_H2_H1), - HANDLE.lock().unwrap().executor(), + HANDLE.lock().unwrap().as_ref().unwrap().executor(), ), }; @@ -165,7 +165,7 @@ fn create_http_states( http_cache_state: Mutex::new(HashMap::new()), client: create_http_client( create_tls_config(&certs, ALPN_H2_H1), - HANDLE.lock().unwrap().executor(), + HANDLE.lock().unwrap().as_ref().unwrap().executor(), ), }; @@ -591,6 +591,9 @@ impl CoreResourceManager { // or a short timeout has been reached. self.thread_pool.exit(); + // Shut-down the async runtime used by fetch workers. + drop(HANDLE.lock().unwrap().take()); + debug!("Exited CoreResourceManager"); }