Skip to content

BlockingLayer example in async context panics #5857

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

Closed
jorgehermo9 opened this issue Mar 23, 2025 · 3 comments
Closed

BlockingLayer example in async context panics #5857

jorgehermo9 opened this issue Mar 23, 2025 · 3 comments

Comments

@jorgehermo9
Copy link
Contributor

jorgehermo9 commented Mar 23, 2025

While working on #5678 I had the necessity of calling an async function within a sync function.

I saw the BlockingLayer did something very similar of what I wanted in those two lines:

handle: Handle::try_current()

and

self.handle.block_on(async {

However, when I tried the following BlockingLayer example at

/// async fn main() -> Result<()> {

with the following main code based on that example:

#[tokio::main]
async fn main() -> Result<()> {
    // Build an `BlockingOperator` with blocking layer to start operating the storage.
    let op: BlockingOperator = Operator::new(services::Memory::default())?
        .layer(BlockingLayer::create()?)
        .finish()
        .blocking();
    op.write("test", "content")?;
    Ok(())
}

It panicked with the following error:

thread 'main' panicked at /home/jorge/github/opendal/core/src/layers/blocking.rs:232:21:
Cannot start a runtime from within a runtime. This happens because a function (like `block_on`) attempted to block the current thread while the thread is being used to drive asynchronous tasks.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

It seems that using Handle::current().block_on(..) does not work propertly within async contexts... Or am I doing something wrong?

I am trying to do something very similar at #5678 and I wonder what should be the correct approach in these cases

@Xuanwo
Copy link
Member

Xuanwo commented Mar 24, 2025

I had the necessity of calling an async function within a sync function.

I'm guessing you need this because of the cache layer. However, this is not a good pattern and shouldn't happen. Operators with BlockingLayer should only be used in a blocking context, such as cross-language bindings.

We can only implement CacheLayer for async operations.

@jorgehermo9
Copy link
Contributor Author

I'm guessing you need this because of the cache layer. However, this is not a good pattern and shouldn't happen. Operators with BlockingLayer should only be used in a blocking context, such as cross-language bindings.

Perfect, thanks for your explanation!

And about this BlockingLayer example, is the panic expected? or is the documentation example wrong?

@Xuanwo
Copy link
Member

Xuanwo commented Apr 14, 2025

Not a bug to fix, closing.

@Xuanwo Xuanwo closed this as completed Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants