Skip to content

reqwest::blocking should advertise that it doesn't like tokio #1233

@ppershing

Description

@ppershing

This bug was extremely hard to track down. I was wondering why my tokio runtime was failing even when spawned on separate thread. Note that I do know that running blocking tasks on tokio isn't a good idea. But I was trying to slowly migrate from blocking to non-blocking and this was interim state.

What would be great is

  1. the documentation should explicitly state that instantiating client::blocking inside tokio ends up with fireworks
  2. reqwest::blocking::Client should actually use tokio's enter(false) to check for runtime-inside-runtime and at least should fatal early on (e.g. something like "You should not run reqwest::blocking inside existing tokio runtime")

The smallest repro is

use reqwest;
use tokio;

#[tokio::main(flavor = "current_thread")]
async fn worker() {
    println!("worker started!");
    let client = reqwest::blocking::Client::new();
    println!("worker ended");
}

pub fn main() {
    worker();
}

Running this fails with

worker started!
thread 'main' panicked at 'Cannot drop a runtime in a context where blocking is not allowed. This happens when a runtime is dropped from within an asynchronous context.', /home/ppershing/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.4.0/src/runtime/blocking/shutdown.rs:51:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

which isn't helpful at all if it happens inside larger codebase

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyEffort: Easy! Start here :DE-pr-welcomeThe feature is welcome to be added, instruction should be found in the issue.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions