From 7f6f7948d8d0fb83a0ea45c248d7c901ed17acca Mon Sep 17 00:00:00 2001 From: Kevin Mehall Date: Sun, 20 Jul 2025 10:08:08 -0600 Subject: [PATCH] Avoid panic during Tokio runtime shutdown Fixes #156 --- src/maybe_future.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/maybe_future.rs b/src/maybe_future.rs index 30affbf..792452a 100644 --- a/src/maybe_future.rs +++ b/src/maybe_future.rs @@ -123,12 +123,18 @@ pub mod blocking { #[cfg(all(feature = "tokio", not(feature = "smol")))] fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - Pin::new(&mut self.0).poll(cx).map(|r| r.unwrap()) + match Pin::new(&mut self.0).poll(cx) { + Poll::Pending => Poll::Pending, + Poll::Ready(Ok(r)) => Poll::Ready(r), + Poll::Ready(Err(e)) if e.is_cancelled() => Poll::Pending, // Can happen during runtime shutdown + Poll::Ready(Err(e)) if e.is_panic() => std::panic::resume_unwind(e.into_panic()), + Poll::Ready(Err(e)) => panic!("Error from tokio blocking task: {e}"), + } } #[cfg(not(any(feature = "smol", feature = "tokio")))] fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll { - Poll::Ready(self.0.take().expect("polled after completion")) + unreachable!() } } }