Skip to content

Commit

Permalink
Fix the missing runtime issues in the bindings' tests (#808)
Browse files Browse the repository at this point in the history
* Put the spawning task on ZRuntime::TX

* Fix the bug of an async runtime demanded by UdpSocket::from_std
  • Loading branch information
YuanYuYuan authored Mar 11, 2024
1 parent c15d925 commit 91e0f9e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions zenoh-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ zenoh-macros = { workspace = true }
zenoh-result = { workspace = true }
zenoh-sync = { workspace = true }
zenoh-util = { workspace = true }
zenoh-runtime = { workspace = true }

[dev-dependencies]
clap = { workspace = true, features = ["derive"] }
Expand Down
3 changes: 2 additions & 1 deletion zenoh-ext/src/publication_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ impl<'a> PublicationCache<'a> {
let resources_limit = conf.resources_limit;
let history = conf.history;

// TODO(yuyuan): use CancellationToken to manage it
let (stoptx, stoprx) = bounded::<bool>(1);
tokio::task::spawn(async move {
zenoh_runtime::ZRuntime::TX.spawn(async move {
let mut cache: HashMap<OwnedKeyExpr, VecDeque<Sample>> =
HashMap::with_capacity(resources_limit.unwrap_or(32));
let limit = resources_limit.unwrap_or(usize::MAX);
Expand Down
12 changes: 10 additions & 2 deletions zenoh/src/net/runtime/orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,11 @@ impl Runtime {
// Must set to nonblocking according to the doc of tokio
// https://docs.rs/tokio/latest/tokio/net/struct.UdpSocket.html#notes
socket.set_nonblocking(true)?;
Ok(UdpSocket::from_std(socket.into())?)

// UdpSocket::from_std requires a runtime even though it's a sync function
let udp_socket = zenoh_runtime::ZRuntime::Net
.block_in_place(async { UdpSocket::from_std(socket.into()) })?;
Ok(udp_socket)
}

pub fn bind_ucast_port(addr: IpAddr) -> ZResult<UdpSocket> {
Expand Down Expand Up @@ -453,7 +457,11 @@ impl Runtime {
// Must set to nonblocking according to the doc of tokio
// https://docs.rs/tokio/latest/tokio/net/struct.UdpSocket.html#notes
socket.set_nonblocking(true)?;
Ok(UdpSocket::from_std(socket.into())?)

// UdpSocket::from_std requires a runtime even though it's a sync function
let udp_socket = zenoh_runtime::ZRuntime::Net
.block_in_place(async { UdpSocket::from_std(socket.into()) })?;
Ok(udp_socket)
}

async fn spawn_peer_connector(&self, peer: EndPoint) -> ZResult<()> {
Expand Down

0 comments on commit 91e0f9e

Please sign in to comment.