Skip to content

Commit

Permalink
RSDK-2404 - prevent panic in close_sync (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog authored Mar 22, 2023
1 parent e098d55 commit 6dd19cc
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/rpc/base_channel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use std::{
fmt::Debug,
panic::{catch_unwind, set_hook, AssertUnwindSafe},
sync::{
atomic::{AtomicBool, AtomicPtr, Ordering},
Arc,
Expand Down Expand Up @@ -80,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(());
}
Expand All @@ -93,11 +95,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
Expand Down

0 comments on commit 6dd19cc

Please sign in to comment.