1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-20 00:21:07 +00:00

fix reactor drop panic

This commit is contained in:
Nikolay Kim 2019-04-29 10:14:08 -07:00
parent d2c1791067
commit f4e1205cbb

View file

@ -28,11 +28,25 @@ use crate::service::{ServiceRequest, ServiceResponse};
use crate::{Error, HttpRequest, HttpResponse}; use crate::{Error, HttpRequest, HttpResponse};
thread_local! { thread_local! {
static RT: RefCell<Runtime> = { static RT: RefCell<Inner> = {
RefCell::new(Runtime::new().unwrap()) RefCell::new(Inner(Some(Runtime::new().unwrap())))
}; };
} }
struct Inner(Option<Runtime>);
impl Inner {
fn get_mut(&mut self) -> &mut Runtime {
self.0.as_mut().unwrap()
}
}
impl Drop for Inner {
fn drop(&mut self) {
std::mem::forget(self.0.take().unwrap())
}
}
/// Runs the provided future, blocking the current thread until the future /// Runs the provided future, blocking the current thread until the future
/// completes. /// completes.
/// ///
@ -47,7 +61,7 @@ pub fn block_on<F>(f: F) -> Result<F::Item, F::Error>
where where
F: Future, F: Future,
{ {
RT.with(move |rt| rt.borrow_mut().block_on(f)) RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f))
} }
/// Runs the provided function, blocking the current thread until the resul /// Runs the provided function, blocking the current thread until the resul
@ -65,7 +79,7 @@ where
F: FnOnce() -> R, F: FnOnce() -> R,
R: IntoFuture, R: IntoFuture,
{ {
RT.with(move |rt| rt.borrow_mut().block_on(f().into_future())) RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(|| f())))
} }
#[doc(hidden)] #[doc(hidden)]
@ -77,8 +91,12 @@ pub fn run_on<F, R>(f: F) -> R
where where
F: FnOnce() -> R, F: FnOnce() -> R,
{ {
RT.with(move |rt| rt.borrow_mut().block_on(lazy(|| Ok::<_, ()>(f())))) RT.with(move |rt| {
.unwrap() rt.borrow_mut()
.get_mut()
.block_on(lazy(|| Ok::<_, ()>(f())))
})
.unwrap()
} }
/// Create service that always responds with `HttpResponse::Ok()` /// Create service that always responds with `HttpResponse::Ok()`
@ -657,7 +675,7 @@ mod tests {
); );
let req = TestRequest::post().uri("/index.html").to_request(); let req = TestRequest::post().uri("/index.html").to_request();
let res = block_on(app.call(req)).unwrap(); let res = block_fn(|| app.call(req)).unwrap();
assert!(res.status().is_success()); assert!(res.status().is_success());
} }
} }