Skip to content

Commit

Permalink
chore(sdk): Async error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Oct 10, 2024
1 parent d9035bd commit 6994a5c
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions packages/rs-sdk/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,34 @@
//! inside a tokio runtime. This module spawns async futures in active tokio runtime, and retrieves the result
//! using a channel.
use drive_proof_verifier::error::ContextProviderError;
use std::future::Future;
use std::{future::Future, sync::mpsc::SendError};
use tokio::runtime::TryCurrentError;

#[derive(Debug, thiserror::Error)]
pub enum AsyncError {
/// Not running inside tokio runtime
#[error("not running inside tokio runtime: {0}")]
NotInTokioRuntime(#[from] TryCurrentError),

/// Cannot receive response from async function
#[error("cannot receive response from async function: {0}")]
RecvError(#[from] std::sync::mpsc::RecvError),

/// Cannot send response from async function
#[error("cannot send response from async function: {0}")]
SendError(String),

#[error("asynchronous call from synchronous context failed: {0}")]
#[allow(unused)]
Generic(String),
}

impl<T> From<SendError<T>> for AsyncError {
fn from(error: SendError<T>) -> Self {
Self::SendError(error.to_string())
}
}

impl From<AsyncError> for ContextProviderError {
fn from(error: AsyncError) -> Self {
ContextProviderError::AsyncError(error.to_string())
Expand All @@ -32,30 +51,18 @@ impl From<AsyncError> for crate::Error {
///
/// Due to limitations of tokio runtime, we cannot use `tokio::runtime::Runtime::block_on` if we are already inside a tokio runtime.
/// This function is a workaround for that limitation.
pub fn block_on<F>(fut: F) -> Result<F::Output, ContextProviderError>
pub fn block_on<F>(fut: F) -> Result<F::Output, AsyncError>
where
F: Future + Send + 'static,
F::Output: Send,
{
tracing::trace!("block_on: running async function from sync code");
let rt = tokio::runtime::Handle::try_current().map_err(|e| {
ContextProviderError::AsyncError(format!(
"sync-async error: cannot get current tokio runtime handle: {:?}",
e
))
})?;
let rt = tokio::runtime::Handle::try_current()?;
let (tx, rx) = std::sync::mpsc::channel();
tracing::trace!("block_on: Spawning worker");
let hdl = rt.spawn(worker(fut, tx));
tracing::trace!("block_on: Worker spawned");
let recv = tokio::task::block_in_place(|| rx.recv());

let resp = recv.map_err(|e| {
ContextProviderError::AsyncError(format!(
"sync-async error: cannot receive response from async function: {:?}",
e
))
})?;
let resp = tokio::task::block_in_place(|| rx.recv())?;

Check failure on line 65 in packages/rs-sdk/src/sync.rs

View workflow job for this annotation

GitHub Actions / Rust packages (dash-sdk) / Linting

cannot find function `block_in_place` in module `tokio::task`

error[E0425]: cannot find function `block_in_place` in module `tokio::task` --> packages/rs-sdk/src/sync.rs:65:29 | 65 | let resp = tokio::task::block_in_place(|| rx.recv())?; | ^^^^^^^^^^^^^^ not found in `tokio::task` | note: found an item that was configured out --> /home/ubuntu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.40.0/src/task/mod.rs:334:27 | 334 | pub use blocking::block_in_place; | ^^^^^^^^^^^^^^ = note: the item is gated behind the `rt-multi-thread` feature

tracing::trace!("Response received");
if !hdl.is_finished() {
Expand All @@ -71,13 +78,11 @@ async fn worker<F: Future>(
fut: F,
// response: oneshot::Sender<F::Output>,
response: std::sync::mpsc::Sender<F::Output>,
) -> Result<(), drive_proof_verifier::error::ContextProviderError> {
) -> Result<(), AsyncError> {
tracing::trace!("Worker start");
let result = fut.await;
tracing::trace!("Worker async function completed, sending response");
response.send(result).map_err(|e| {
ContextProviderError::Generic(format!("sync-async error: response cannot be sent: {}", e))
})?;
response.send(result)?;
tracing::trace!("Worker response sent");

Ok(())
Expand Down

0 comments on commit 6994a5c

Please sign in to comment.