diff --git a/commons/zenoh-runtime/Cargo.toml b/commons/zenoh-runtime/Cargo.toml index e3f0c7a3c0..cfb63b7e60 100644 --- a/commons/zenoh-runtime/Cargo.toml +++ b/commons/zenoh-runtime/Cargo.toml @@ -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 } diff --git a/commons/zenoh-runtime/src/lib.rs b/commons/zenoh-runtime/src/lib.rs index 1a9d765420..cb58cac570 100644 --- a/commons/zenoh-runtime/src/lib.rs +++ b/commons/zenoh-runtime/src/lib.rs @@ -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:?}"); + } + } } } }