Skip to content

Instantly share code, notes, and snippets.

@eminence
Last active March 26, 2021 05:05
Show Gist options
  • Save eminence/f8fc852bfa2c719ec0eab9b04591cc07 to your computer and use it in GitHub Desktop.
Save eminence/f8fc852bfa2c719ec0eab9b04591cc07 to your computer and use it in GitHub Desktop.
Runtime errors from async rust

Using tokio 1.0 with a library that requires something older than 1.0

Dependencies:

[dependencies]
tokio-tungstenite = "0.12.0"
tokio = { version = "1", features = ["rt", "full"] }

Rust code:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let _ = tokio_tungstenite::connect_async("ws://localhost:8080").await;

    Ok(())
}

Output:

   Compiling foo v0.1.0 (C:\Users\achin\tmp\19\foo)
    Finished dev [unoptimized + debuginfo] target(s) in 1.74s
     Running `target\debug\foo.exe`
thread 'main' panicked at 'requires the `rt` Tokio feature flag', C:\Users\achin\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.3.7\src\blocking.rs:18:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\foo.exe` (exit code: 101)

Using actix with tokio, since actix doesn't support tokio 1.0 at all

Rust code:

#[actix_web::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let body = reqwest::get("https://www.rust-lang.org")
    .await?
    .text()
    .await?;

    Ok(())
}

Runtime error:

thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime'

Mixing async-std and tokio

Rust code:

use async_std::task;
use hyper::Client;

async fn say_hello() {
    println!("Hello, world!");

    let client = Client::new();
    let uri = "http://httpbin.org/ip".parse().unwrap();
    let resp = client.get(uri).await.unwrap();
    println!("Response: {}", resp.status());

}

fn main() {
    task::block_on(say_hello())
}

Runtime error:

    Finished dev [unoptimized + debuginfo] target(s) in 0.11s
     Running `target\debug\foo.exe`
Hello, world!
thread 'main' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', C:\Users\achin\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.4.0\src\runtime\blocking\pool.rs:85:33
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\foo.exe` (exit code: 101)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment