Skip to content

Commit 2ce3fa7

Browse files
committed
Shutdown the runtime on drop
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 tokio-rs#209
1 parent 2a15851 commit 2ce3fa7

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/runtime.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,7 @@ impl Runtime {
339339
/// [mod]: index.html
340340
pub fn shutdown_now(mut self) -> Shutdown {
341341
let inner = self.inner.take().unwrap();
342-
343-
let inner = Box::new({
344-
let pool = inner.pool;
345-
let reactor = inner.reactor;
346-
347-
pool.shutdown_now().and_then(|_| {
348-
reactor.shutdown_now()
349-
})
350-
});
351-
352-
Shutdown { inner }
342+
Shutdown::shutdown_now(inner)
353343
}
354344

355345
fn inner(&self) -> &Inner {
@@ -361,6 +351,15 @@ impl Runtime {
361351
}
362352
}
363353

354+
impl Drop for Runtime {
355+
fn drop(&mut self) {
356+
if let Some(inner) = self.inner.take() {
357+
let shutdown = Shutdown::shutdown_now(inner);
358+
let _ = shutdown.wait();
359+
}
360+
}
361+
}
362+
364363
// ===== impl TaskExecutor =====
365364

366365
impl TaskExecutor {
@@ -425,6 +424,24 @@ impl ::executor::Executor for TaskExecutor {
425424

426425
// ===== impl Shutdown =====
427426

427+
impl Shutdown {
428+
fn shutdown_now(inner: Inner) -> Self {
429+
let inner = Box::new({
430+
let pool = inner.pool;
431+
let reactor = inner.reactor;
432+
433+
pool.shutdown_now().and_then(|_| {
434+
reactor.shutdown_now()
435+
.then(|_| {
436+
Ok(())
437+
})
438+
})
439+
});
440+
441+
Shutdown { inner }
442+
}
443+
}
444+
428445
impl Future for Shutdown {
429446
type Item = ();
430447
type Error = ();

0 commit comments

Comments
 (0)