Skip to content

Commit

Permalink
fix(zenoh-runtime): zenoh-c DLL crash in libc::atexit handler (#972)
Browse files Browse the repository at this point in the history
* fix(zenoh-runtime): zenoh-c DLL crash in libc::atexit handler

* fix(zenoh-runtime): properly handle and report the status of `ZRuntime` drop

* fix: add `set_hook` to suppress the panic error.

* Trigger CI

* Trigger CI
  • Loading branch information
YuanYuYuan authored Apr 25, 2024
1 parent ac6bbf4 commit 274166d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions commons/zenoh-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ zenoh-result = { workspace = true, features = ["std"] }
zenoh-collections = { workspace = true, features = ["std"] }
zenoh-macros = { workspace = true }
tokio = { workspace = true, features = ["fs", "io-util", "macros", "net", "rt-multi-thread", "sync", "time"] }
tracing = { workspace = true }
31 changes: 28 additions & 3 deletions commons/zenoh-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,42 @@ impl ZRuntimePool {
// If there are any blocking tasks spawned by ZRuntimes, the function will block until they return.
impl Drop for ZRuntimePool {
fn drop(&mut self) {
std::panic::set_hook(Box::new(|_| {
// To suppress the panic error caught in the following `catch_unwind`.
}));

let handles: Vec<_> = self
.0
.drain()
.filter_map(|(_name, mut rt)| {
rt.take()
.map(|r| std::thread::spawn(move || r.shutdown_timeout(Duration::from_secs(1))))
rt.take().map(|r| {
// NOTE: The error of the atexit handler in DLL (static lib is fine)
// failing to spawn a new thread in `cleanup` has been identified.
std::panic::catch_unwind(|| {
std::thread::spawn(move || r.shutdown_timeout(Duration::from_secs(1)))
})
})
})
.collect();

for hd in handles {
let _ = hd.join();
match hd {
Ok(handle) => {
if let Err(err) = handle.join() {
tracing::error!(
"The handle failed to join during `ZRuntimePool` drop due to {err:?}"
);
}
}
Err(err) => {
// WARN: Windows with DLL is expected to panic for the time being.
// Otherwise, report the error.
#[cfg(not(target_os = "windows"))]
tracing::error!("`ZRuntimePool` failed to drop due to {err:?}");
#[cfg(target_os = "windows")]
tracing::trace!("`ZRuntimePool` failed to drop due to {err:?}");
}
}
}
}
}
Expand Down

0 comments on commit 274166d

Please sign in to comment.