From 3fcdf26908d60275da0d813de0b94002c946c43a Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Tue, 19 Mar 2024 14:19:00 +0800 Subject: [PATCH 1/2] Fix issue #835 --- commons/zenoh-runtime/src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/commons/zenoh-runtime/src/lib.rs b/commons/zenoh-runtime/src/lib.rs index fb2186ecb4..ac8e707b90 100644 --- a/commons/zenoh-runtime/src/lib.rs +++ b/commons/zenoh-runtime/src/lib.rs @@ -11,6 +11,7 @@ // Contributors: // ZettaScale Zenoh Team, // +use core::panic; use lazy_static::lazy_static; use std::{ collections::HashMap, @@ -22,7 +23,7 @@ use std::{ OnceLock, }, }; -use tokio::runtime::{Handle, Runtime}; +use tokio::runtime::{Handle, Runtime, RuntimeFlavor}; use zenoh_collections::Properties; use zenoh_result::ZResult as Result; @@ -44,7 +45,6 @@ impl ZRuntime { } fn init(&self) -> Result { - // dbg!(*ZRUNTIME_CONFIG); let config = &ZRUNTIME_CONFIG; let thread_name = format!("{self:?}"); @@ -110,6 +110,9 @@ impl ZRuntime { where F: Future, { + if Handle::current().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)]`"); + } tokio::task::block_in_place(move || self.block_on(f)) } } @@ -200,3 +203,10 @@ impl Default for ZRuntimeConfig { } } } + +#[should_panic(expected = "Zenoh runtime doesn't support")] +#[tokio::test] +async fn block_in_place_fail_test() { + use crate::ZRuntime; + ZRuntime::TX.block_in_place(async { println!("Done") }); +} From 4bdfdd88c985b66700ed15604f233d3e8a8e4d56 Mon Sep 17 00:00:00 2001 From: yuanyuyuan Date: Tue, 19 Mar 2024 16:36:17 +0800 Subject: [PATCH 2/2] Bugfix --- commons/zenoh-runtime/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/commons/zenoh-runtime/src/lib.rs b/commons/zenoh-runtime/src/lib.rs index ac8e707b90..ac040af838 100644 --- a/commons/zenoh-runtime/src/lib.rs +++ b/commons/zenoh-runtime/src/lib.rs @@ -110,8 +110,10 @@ impl ZRuntime { where F: Future, { - if Handle::current().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)]`"); + 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)]`"); + } } tokio::task::block_in_place(move || self.block_on(f)) }