Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-2404 - prevent panic in close_sync #35

Merged
merged 3 commits into from
Mar 22, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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