Skip to content

How to use Handle block_on on a current thread runtime #4563

Answered by Darksonn
Guara92 asked this question in Q&A
Discussion options

You must be logged in to vote

This fails because the timer driver runs on the main thread, but by calling the blocking call_sync method on the main thread, you never yield back to the runtime, and so the timer driver is never able to notify any of its timers that they have completed. Even if you make call_sync async, then the same issue will appear because then you are calling the blocking std::thread::JoinHandle::join method.

To fix this, change call_sync to be async, then use an asynchronous way to wait for the thread to complete:

use tokio::runtime::Handle;
use tokio::sync::oneshot;
use tokio::time::sleep;

#[tokio::main(flavor = "current_thread")]
async fn main() {
    call_sync().await;
}

pub async fn call_sync() {

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@brandonros
Comment options

@Darksonn
Comment options

@brandonros
Comment options

@Darksonn
Comment options

@DarumaDockerDev
Comment options

Answer selected by Guara92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants