Skip to content

Commit

Permalink
fix: remove the unsound atexit cleanup (#1015)
Browse files Browse the repository at this point in the history
* fix: remove the unsound atexit cleanup

* fix: refine the unsafe function
  • Loading branch information
YuanYuYuan authored May 6, 2024
1 parent 1e1027c commit 7cf2eee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
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

0 comments on commit 7cf2eee

Please sign in to comment.