diff --git a/ci/valgrind-check/src/pub_sub/bin/z_pub_sub.rs b/ci/valgrind-check/src/pub_sub/bin/z_pub_sub.rs index 60bda80bda..9561f7016a 100644 --- a/ci/valgrind-check/src/pub_sub/bin/z_pub_sub.rs +++ b/ci/valgrind-check/src/pub_sub/bin/z_pub_sub.rs @@ -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(); diff --git a/ci/valgrind-check/src/queryable_get/bin/z_queryable_get.rs b/ci/valgrind-check/src/queryable_get/bin/z_queryable_get.rs index 1c82339392..78fb705fe8 100644 --- a/ci/valgrind-check/src/queryable_get/bin/z_queryable_get.rs +++ b/ci/valgrind-check/src/queryable_get/bin/z_queryable_get.rs @@ -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(); diff --git a/commons/zenoh-runtime/src/lib.rs b/commons/zenoh-runtime/src/lib.rs index dcd46744e6..2023c63bb3 100644 --- a/commons/zenoh-runtime/src/lib.rs +++ b/commons/zenoh-runtime/src/lib.rs @@ -145,11 +145,17 @@ 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).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).read(), + ); + } } } @@ -157,12 +163,6 @@ pub struct ZRuntimePool(HashMap>); 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()) }