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

fix(zenoh-runtime): zenoh-c DLL crash in libc::atexit handler #972

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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