From 5615f5760aaccb2783f82f7f1aee150e38d8ccfe Mon Sep 17 00:00:00 2001 From: Ethan Rodkin Date: Tue, 21 Mar 2023 10:40:21 -0400 Subject: [PATCH 1/2] stop panicking when connecting to offline robot --- src/rpc/base_channel.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/rpc/base_channel.rs b/src/rpc/base_channel.rs index b6b44ba..0d8bc41 100644 --- a/src/rpc/base_channel.rs +++ b/src/rpc/base_channel.rs @@ -1,6 +1,7 @@ use anyhow::Result; use std::{ fmt::Debug, + panic::{catch_unwind, set_hook, AssertUnwindSafe}, sync::{ atomic::{AtomicBool, AtomicPtr, Ordering}, Arc, @@ -93,11 +94,23 @@ impl WebRTCBaseChannel { // `close` with blocking. Should only be used in contexts where an async close is disallowed pub(crate) fn close_sync(&self) -> Result<()> { - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - rt.block_on(async move { self.close().await }) + set_hook(Box::new(|info| { + log::error!( + "Unable to close base_channel gracefully. This may be because of an attempt to connect to a robot that isn't online. Error message: {}", + info.to_string() + ) + })); + let safe_self = AssertUnwindSafe(self); + match catch_unwind(|| { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + rt.block_on(async move { safe_self.close().await }) + }) { + Ok(res) => res, + Err(_) => Err(anyhow::anyhow!("Unable to close base channel gracefully")), + } } /// Returns whether or not the channel is closed From 335e03742f8275c28f35971ef02b037e4ce66722 Mon Sep 17 00:00:00 2001 From: Ethan Rodkin Date: Tue, 21 Mar 2023 10:51:23 -0400 Subject: [PATCH 2/2] add logging for closing base channel --- src/rpc/base_channel.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rpc/base_channel.rs b/src/rpc/base_channel.rs index 0d8bc41..90fd1ee 100644 --- a/src/rpc/base_channel.rs +++ b/src/rpc/base_channel.rs @@ -81,6 +81,7 @@ impl WebRTCBaseChannel { /// Closes the channel #[allow(dead_code)] pub async fn close(&self) -> Result<()> { + log::debug!("Closing base channel"); if self.closed.load(Ordering::Acquire) { return Ok(()); }