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

block_in_place panic on runtime block_on #1838

Closed
driftluo opened this issue Nov 27, 2019 · 10 comments
Closed

block_in_place panic on runtime block_on #1838

driftluo opened this issue Nov 27, 2019 · 10 comments

Comments

@driftluo
Copy link
Contributor

driftluo commented Nov 27, 2019

version: tokio 0.2.1
feature: ["time", "io-util", "tcp", "dns", "rt-threaded", "blocking"]

fn main() {
    let mut rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async move {
        tokio::task::block_in_place(|| println!("test"))
    })
}

output:

thread 'main' panicked at 'can only call blocking when on Tokio runtime', src/libcore/option.rs:1190:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

The cause of this problem is that the ON_BLOCK of the current thread is not set.

Other workers On_BLOCK is set on here:

workers.spawn(&blocking_spawner);

But Runtime current thread not set on block_on function, is it a feature or a bug?

@carllerche
Copy link
Member

The panic message needs to be improved... and block_in_place still needs to be generalized.

As a work around, you can do:

    let mut rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async move {
        tokio::spawn(async {
            tokio::task::block_in_place(|| println!("test"))
        }).await
    })

@jebrosen
Copy link
Contributor

Is this a bug or intended behavior (and will it be different between the multithreaded and current_thread runtimes?)

Aside from any 'static or Send requirements, are there any important differences between spawn_blocking or spawn(block_in_place)?

@carllerche
Copy link
Member

the main difference is "block_in_place" moves any runtime infrastructure co-located on the thread to a different thread.

It should be possible to implement this for everything except LocalSet.

@leoyvens
Copy link

A nested spawn_blocking will also panic, which is annoying, but the workaround fixes it.

@carllerche
Copy link
Member

@leotvyens that is unexpected. Would you mind filing a follow up issue?

@dbcfd
Copy link
Contributor

dbcfd commented Dec 15, 2019

There's sometimes I can get the workaround to work, and other times I can't. The most common time this happens is in #[tokio::test] where I am testing an async function that is using block_in_place and then attempt tokio::spawn(async move { inside my test function. If my dev dependencies does not include blocking and rt-threaded it will fail, but even with adding those, I may still get this error.

@dbcfd
Copy link
Contributor

dbcfd commented Dec 15, 2019

I also get the failure when moving the tokio::spawn(async move { inside my function I am testing.

@dbcfd
Copy link
Contributor

dbcfd commented Dec 16, 2019

Had to change my #[tokio::test] to #[tokio::test(threaded_scheduler)] when the other approaches didn't work.

@jgrund
Copy link

jgrund commented May 20, 2020

Can this be considered fixed with #2410 ?

@carllerche
Copy link
Member

I believe so 👍

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

6 participants