Shutdown the runtime on drop (#214)

Currently, the runtime does not shutdown if the runtime handle is
dropped. This can happen during a panic or when the value is simply
dropped.

This patch forces the runtime to shutdown if it is not explicitly
shutdown.

Fixes #209
This commit is contained in:
Carl Lerche
2018-03-13 13:14:51 -07:00
committed by GitHub
parent c0a2cc1f9e
commit 8eb3e58b7d
+28 -11
View File
@@ -339,17 +339,7 @@ impl Runtime {
/// [mod]: index.html
pub fn shutdown_now(mut self) -> Shutdown {
let inner = self.inner.take().unwrap();
let inner = Box::new({
let pool = inner.pool;
let reactor = inner.reactor;
pool.shutdown_now().and_then(|_| {
reactor.shutdown_now()
})
});
Shutdown { inner }
Shutdown::shutdown_now(inner)
}
fn inner(&self) -> &Inner {
@@ -361,6 +351,15 @@ impl Runtime {
}
}
impl Drop for Runtime {
fn drop(&mut self) {
if let Some(inner) = self.inner.take() {
let shutdown = Shutdown::shutdown_now(inner);
let _ = shutdown.wait();
}
}
}
// ===== impl TaskExecutor =====
impl TaskExecutor {
@@ -425,6 +424,24 @@ impl ::executor::Executor for TaskExecutor {
// ===== impl Shutdown =====
impl Shutdown {
fn shutdown_now(inner: Inner) -> Self {
let inner = Box::new({
let pool = inner.pool;
let reactor = inner.reactor;
pool.shutdown_now().and_then(|_| {
reactor.shutdown_now()
.then(|_| {
Ok(())
})
})
});
Shutdown { inner }
}
}
impl Future for Shutdown {
type Item = ();
type Error = ();