DEV Community

Discussion on: Explained: How does async work in Rust?

Collapse
 
creepy_owlet profile image
Dmitry Tantsur

Hi Bastian,

I've learned the hard way that this is not quite right:

tokio::run(response_is_ok);

Depending on what happens inside client, this can hang forever. This actually does happen with reqwest::async since Hyper tend to spawn long-living futures, presumably for keep-alive connections in its pool.

What I had to use to make it reliable is

let mut rt = Runtime::new().expect("Cannot create a runtime");
rt.block_on(response_is_ok).expect("Failed");

As a nice side effect you get the outcome of the future as a Result, you don't have to force it to be Future<Item = (), Error = ()>.

Collapse
 
gruberb profile image
Bastian Gruber

Thats super helpful, thank you so much Dmitry!