src/tools/runtime.rs: call OPENSSL_thread_stop to avoid race with openssl cleanup handlers

This commit is contained in:
Dietmar Maurer 2020-02-26 10:38:55 +01:00
parent ca2dbb8af1
commit dd04383bb8

View File

@ -2,7 +2,7 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::future::Future; use std::future::Future;
use std::sync::Arc; use std::sync::{Arc, Weak, Mutex};
use std::task::{Context, Poll, RawWaker, Waker}; use std::task::{Context, Poll, RawWaker, Waker};
use std::thread::{self, Thread}; use std::thread::{self, Thread};
@ -43,21 +43,41 @@ impl Drop for BlockingGuard {
} }
lazy_static! { lazy_static! {
static ref RUNTIME: Runtime = { // avoid openssl bug: https://github.com/openssl/openssl/issues/6214
runtime::Builder::new() // by dropping the runtime as early as possible
.threaded_scheduler() static ref RUNTIME: Mutex<Weak<Runtime>> = Mutex::new(Weak::new());
.enable_all() }
.build()
.expect("failed to spawn tokio runtime") extern {
}; fn OPENSSL_thread_stop();
} }
/// Get or create the current main tokio runtime. /// Get or create the current main tokio runtime.
/// ///
/// This makes sure that tokio's worker threads are marked for us so that we know whether we /// This makes sure that tokio's worker threads are marked for us so that we know whether we
/// can/need to use `block_in_place` in our `block_on` helper. /// can/need to use `block_in_place` in our `block_on` helper.
pub fn get_runtime() -> &'static Runtime { pub fn get_runtime() -> Arc<Runtime> {
&RUNTIME
let mut guard = RUNTIME.lock().unwrap();
if let Some(rt) = guard.upgrade() { return rt; }
let rt = Arc::new(
runtime::Builder::new()
.on_thread_stop(|| {
// avoid openssl bug: https://github.com/openssl/openssl/issues/6214
// call OPENSSL_thread_stop to avoid race with openssl cleanup handlers
unsafe { OPENSSL_thread_stop(); }
})
.threaded_scheduler()
.enable_all()
.build()
.expect("failed to spawn tokio runtime")
);
*guard = Arc::downgrade(&rt.clone());
rt
} }
/// Block on a synchronous piece of code. /// Block on a synchronous piece of code.