1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-21 07:36:43 +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};
thread_local! {
static RT: RefCell<Runtime> = {
RefCell::new(Runtime::new().unwrap())
static RT: RefCell<Inner> = {
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
/// completes.
///
@ -47,7 +61,7 @@ pub fn block_on<F>(f: F) -> Result<F::Item, F::Error>
where
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
@ -65,7 +79,7 @@ where
F: FnOnce() -> R,
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)]
@ -77,8 +91,12 @@ pub fn run_on<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
RT.with(move |rt| rt.borrow_mut().block_on(lazy(|| Ok::<_, ()>(f()))))
.unwrap()
RT.with(move |rt| {
rt.borrow_mut()
.get_mut()
.block_on(lazy(|| Ok::<_, ()>(f())))
})
.unwrap()
}
/// Create service that always responds with `HttpResponse::Ok()`
@ -657,7 +675,7 @@ mod tests {
);
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());
}
}