From 74209378333f15b1c684ffdfb0172900881cc7a7 Mon Sep 17 00:00:00 2001 From: ChenYing Kuo Date: Tue, 29 Oct 2024 11:51:34 +0800 Subject: [PATCH] Able to check Tokio Runtime is alive inside is_closed. Signed-off-by: ChenYing Kuo --- commons/zenoh-runtime/src/lib.rs | 17 ++++++++++++++--- zenoh/src/api/session.rs | 6 +++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/commons/zenoh-runtime/src/lib.rs b/commons/zenoh-runtime/src/lib.rs index e1a8652507..cf4e63fad2 100644 --- a/commons/zenoh-runtime/src/lib.rs +++ b/commons/zenoh-runtime/src/lib.rs @@ -129,13 +129,24 @@ impl ZRuntime { where F: Future, { - if let Ok(handle) = Handle::try_current() { - if handle.runtime_flavor() == RuntimeFlavor::CurrentThread { - panic!("Zenoh runtime doesn't support Tokio's current thread scheduler. Please use multi thread scheduler instead, e.g. a multi thread scheduler with one worker thread: `#[tokio::main(flavor = \"multi_thread\", worker_threads = 1)]`"); + match Handle::try_current() { + Ok(handle) => { + if handle.runtime_flavor() == RuntimeFlavor::CurrentThread { + panic!("Zenoh runtime doesn't support Tokio's current thread scheduler. Please use multi thread scheduler instead, e.g. a multi thread scheduler with one worker thread: `#[tokio::main(flavor = \"multi_thread\", worker_threads = 1)]`"); + } + } + Err(e) => { + if e.is_thread_local_destroyed() { + panic!("TLS inside Tokio is missing. You might call Zenoh API inside atexit, which should be avoided."); + } } } tokio::task::block_in_place(move || self.block_on(f)) } + + pub fn is_alive() -> bool { + Handle::try_current().is_ok() + } } impl Deref for ZRuntime { diff --git a/zenoh/src/api/session.rs b/zenoh/src/api/session.rs index c5ba24b98f..c4b295e521 100644 --- a/zenoh/src/api/session.rs +++ b/zenoh/src/api/session.rs @@ -632,7 +632,11 @@ impl Session { /// assert!(session.is_closed()); /// # } pub fn is_closed(&self) -> bool { - zread!(self.0.state).primitives.is_none() + if zenoh_runtime::ZRuntime::is_alive() { + zread!(self.0.state).primitives.is_none() + } else { + true + } } pub fn undeclare<'a, T>(&'a self, decl: T) -> impl Resolve> + 'a