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

Able to check Tokio Runtime is alive inside is_closed. #1569

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 14 additions & 3 deletions commons/zenoh-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,24 @@ impl ZRuntime {
where
F: Future<Output = R>,
{
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 {
Expand Down
6 changes: 5 additions & 1 deletion zenoh/src/api/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZResult<()>> + 'a
Expand Down
Loading