Skip to content

Commit 6f8f375

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 6f8f375

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/runtime.rs

+30-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,26 @@ 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+
println!("POOL shut down");
435+
reactor.shutdown_now()
436+
.then(|_| {
437+
println!("REACTOR SHUTDOWN");
438+
Ok(())
439+
})
440+
})
441+
});
442+
443+
Shutdown { inner }
444+
}
445+
}
446+
428447
impl Future for Shutdown {
429448
type Item = ();
430449
type Error = ();

0 commit comments

Comments
 (0)