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

Can't block on a runtime from within a block_in_place call #2639

Closed
bdonlan opened this issue Jul 1, 2020 · 2 comments · Fixed by #2645
Closed

Can't block on a runtime from within a block_in_place call #2639

bdonlan opened this issue Jul 1, 2020 · 2 comments · Fixed by #2645
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. I-crash Problems and improvements related to program crashes/panics. M-runtime Module: tokio/runtime

Comments

@bdonlan
Copy link
Contributor

bdonlan commented Jul 1, 2020

Version

Using tokio 0.2.21 on the playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=3f63cb60db402855462f41a6980e719b

When reproducing locally, here are the tokio crate versions:

│   │   │   ├── tokio v0.2.21
│   │   │   │   └── tokio-macros v0.2.5
│   │   │   └── tokio-util v0.3.1
│   │   │       └── tokio v0.2.21 (*)
│   │   ├── tokio v0.2.21 (*)
│   ├── tokio v0.2.21 (*)
└── tokio v0.2.21 (*)

Platform
64-bit linux, 5.4.30

Description

Often synchronous library APIs can be implemented in terms of a private tokio runtime. While it would be preferable to structure calls into these libraries as async calls, this isn't always possible, and so it should be possible to enter a blocking context using block_in_place and then block_on a different runtime. However, currently this fails:

use tokio; // 0.2.21

#[test]
fn tokio_nesting() {
    let mut rt = tokio::runtime::Builder::new()
        .enable_all()
        .threaded_scheduler()
        .build()
        .unwrap();

    rt.block_on(async {
        tokio::task::block_in_place(|| {
            let mut rt = tokio::runtime::Builder::new()
                .enable_all()
                .basic_scheduler()
                .build()
                .unwrap();
            rt.block_on(tokio::time::delay_for(std::time::Duration::from_millis(50)));
        })
    });
}

fails with:

running 1 test
test tokio_nesting ... FAILED

failures:

---- tokio_nesting stdout ----
thread 'tokio_nesting' panicked at '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.', /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-0.2.21/src/runtime/enter.rs:38:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tokio_nesting

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
@bdonlan bdonlan added A-tokio Area: The main tokio crate C-bug Category: This is a bug. labels Jul 1, 2020
@Darksonn
Copy link
Contributor

Darksonn commented Jul 2, 2020

To be specific, this fails because you are not doing it on a worker thread managed by the threaded runtime. If you add a spawn around the block_in_place, it works.

@carllerche
Copy link
Member

This is still a bug though. We probably missed something when adding support for block_in_place off threaded RT threads.

@hawkw hawkw added I-crash Problems and improvements related to program crashes/panics. M-runtime Module: tokio/runtime labels Jul 2, 2020
bdonlan pushed a commit to bdonlan/tokio that referenced this issue Jul 7, 2020
A fast path in block_on_place was failing to call exit() in the case where we
were in a block_on call.

Fixes: tokio-rs#2639
carllerche pushed a commit that referenced this issue Jul 15, 2020
A fast path in block_on_place was failing to call exit() in the case where we
were in a block_on call.

Fixes: #2639

Co-authored-by: Bryan Donlan <bdonlan@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. I-crash Problems and improvements related to program crashes/panics. M-runtime Module: tokio/runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants