diff options
author | Carl Lerche <me@carllerche.com> | 2018-03-13 13:14:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-13 13:14:51 -0700 |
commit | 8eb3e58b7dc01a2e818798219958dbf1dcf53740 (patch) | |
tree | 84b2f78049db92f63dc61e6d49f79a14b798d34a /src | |
parent | c0a2cc1f9e06d6a058f6bdb4da756676d9b77449 (diff) |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/runtime.rs | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/runtime.rs b/src/runtime.rs index 1b4a83dd..3fb3c9aa 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -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 = (); |