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: remove the unsound atexit cleanup #1015

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 ci/valgrind-check/src/pub_sub/bin/z_pub_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use zenoh::prelude::r#async::*;

#[tokio::main]
async fn main() {
let _z = zenoh_runtime::ZRuntimePoolGuard;
zenoh_util::init_log_test();

let pub_key_expr = KeyExpr::try_from("test/valgrind/data").unwrap();
Expand Down
1 change: 1 addition & 0 deletions ci/valgrind-check/src/queryable_get/bin/z_queryable_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use zenoh::prelude::r#async::*;

#[tokio::main]
async fn main() {
let _z = zenoh_runtime::ZRuntimePoolGuard;
zenoh_util::init_log_test();

let queryable_key_expr = KeyExpr::try_from("test/valgrind/data").unwrap();
Expand Down
22 changes: 11 additions & 11 deletions commons/zenoh-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,24 @@ lazy_static! {
.collect();
}

// To drop the data mannually since Rust does not drop static variables.
pub extern "C" fn cleanup() {
unsafe {
std::mem::drop((ZRUNTIME_POOL.deref() as *const ZRuntimePool).read());
std::mem::drop((ZRUNTIME_INDEX.deref() as *const HashMap<ZRuntime, AtomicUsize>).read());
// A runtime guard used to explicitly drop the static variables that Rust doesn't drop by default
pub struct ZRuntimePoolGuard;

impl Drop for ZRuntimePoolGuard {
fn drop(&mut self) {
unsafe {
std::mem::drop((ZRUNTIME_POOL.deref() as *const ZRuntimePool).read());
std::mem::drop(
(ZRUNTIME_INDEX.deref() as *const HashMap<ZRuntime, AtomicUsize>).read(),
);
}
}
}

pub struct ZRuntimePool(HashMap<ZRuntime, OnceLock<Runtime>>);

impl ZRuntimePool {
fn new() -> Self {
// It has been recognized that using atexit within Windows DLL is problematic
#[cfg(not(target_os = "windows"))]
// Register a callback to clean the static variables.
unsafe {
libc::atexit(cleanup);
}
Self(ZRuntime::iter().map(|zrt| (zrt, OnceLock::new())).collect())
}

Expand Down