skip to content

A Rust CLI drives a Tokio-based library call with `futures::executor::block_on` and panics with "there is no reactor running"; why, and how do you fix it?

level: seniorimportance: should knowfreq 38%

answer

  1. I/O types belong to one reactor
  2. context found through a thread-local
  3. drive it with its own runtime
  4. never block_on inside async code

basics

~20 s

Tokio's I/O and timer types need a Tokio runtime's driver in the current context; futures' block_on is a bare executor that sets none up, so creating the timer panics. Drive the call with that runtime's own block_on.

solid answer

~50 s

The `Future` trait is shared, but I/O and timer types are not: a Tokio `sleep` or socket registers itself with the driver of the Tokio runtime it finds in the current thread's context. `futures::executor::block_on` is a minimal executor that only polls and parks, so no such context exists, and Tokio panics with "there is no reactor running, must be called from the context of a Tokio 1.x runtime". The fix is to drive the call with the runtime its types belong to: build that runtime once in `main` and use its `block_on`, or enter its context. To use Tokio-based libraries from smol, smol's own docs point to the `async-compat` adapter. The mirror-image bug is calling any `block_on` from inside async code: futures' version panics with "cannot execute `LocalPool` executor from within another executor"; the fix there is `.await`.

code

rust · 14 lines
rust
use std::time::Duration;

async fn wait_then_report() -> &'static str {
    // A timer type from a runtime that needs its own driver running.
    let timer = tokio::time::sleep(Duration::from_millis(10));
    timer.await;
    "done"
}

fn main() {
    // futures' block_on polls the future, but it runs no Tokio driver.
    let r = futures::executor::block_on(wait_then_report());
    println!("{r}");
}

go deeper

for a junior

Recall that all runtimes share the Future trait but not their sockets and timers, and that block_on must not be called from inside async code.

for a middle

Explain how runtime resources find their reactor (Tokio through a thread-local context, smol through a fallback thread) and why futures' block_on supplies neither.

for a senior

Diagnose the missing-reactor and nested-executor panics from their messages, fix them by driving the call with the right runtime, and remove nested block_on calls in review.

for a principal

Set the boundary rules: one runtime per binary, runtime-agnostic library APIs, and adapters only as a documented, temporary bridge.

## The shared contract and the parts that are not shared Every Rust async runtime polls the same `core::future::Future` trait, so a pure future, one built only from other futures, channels and combinators, runs under any executor. **I/O and timer types are different.** A socket or a timer must be registered with a **reactor** (the component that waits on the operating system and calls the right `Waker`), and each runtime has its own reactor with its own registration API. Runtimes locate their reactor in one of two ways: - **an ambient context**: Tokio's resources look for a runtime handle stored in a thread-local, which is set while that runtime's `block_on` or one of its worker threads is running; - **a global fallback**: smol's `async-io` crate starts a background thread named "async-io" on first use and uses it as the reactor whenever no `async_io::block_on` is processing events, so async-io's types work under any executor. ## Reading the panic ```plaintext there is no reactor running, must be called from the context of a Tokio 1.x runtime ``` This is a run-time panic, not a compile error: the program type-checks because `tokio::time::sleep` returns an ordinary future. In the code example, the panic points at the line that *creates* the sleep, before anything is awaited, because the timer looks up its driver the moment it is constructed. `futures::executor::block_on` set up no Tokio context, so the lookup fails. The same pattern appears with any library that uses Tokio's sockets or timers internally; the library's async function is the future, and its insides need Tokio. ## Fixes, in order of preference 1. **Drive the call with the runtime it was written for.** Create that runtime once near `main` and call its `block_on` for each synchronous entry point; how to build and configure a Tokio runtime is covered with Tokio itself. Build it once rather than per call: each runtime owns threads and a reactor. 2. **Enter the runtime's context** when the code that creates the I/O type runs on a thread the runtime does not own; again, the exact API belongs to the runtime. 3. **Adapt across runtimes** when the binary is committed to a different one: smol's documentation recommends the `async-compat` adapter for running Tokio-based futures and I/O types under smol. 4. **Prefer runtime-agnostic libraries** when you have the choice, so a CLI does not inherit a runtime decision from a dependency. What does not work: wrapping the call in a second executor's `block_on`, polling it in a loop with `Waker::noop()` (it never wakes anything and still starts no driver), or calling the async function on a freshly spawned `std::thread`, which only builds the future and drops it. ## The mirror-image bug: `block_on` inside async code The other classic way to mix runtimes wrongly is to call a `block_on` from code that an executor is already polling, typically to "make an async call from a sync helper" deep inside an async handler. In the futures crate the executor marks the thread as entered, and a nested `block_on` panics: ```plaintext cannot execute `LocalPool` executor from within another executor: EnterError ``` Tokio has the equivalent check and panics with "Cannot start a runtime from within a runtime", explaining that `block_on` attempted to block a thread that is driving asynchronous tasks. Even where an executor does not detect nesting, blocking inside a task stalls that executor thread and can deadlock if the inner future waits on work scheduled on the same thread. The fix is the same in every runtime: inside async code, `.await` the future; keep `block_on` for the synchronous edges. ## Practical rules a reviewer checks - **one runtime per binary**, created once, with libraries chosen to match it; - **`block_on` only at synchronous edges**: `main`, tests, a callback from a synchronous framework; - **no runtime-specific types in a library's public API** unless the library is explicitly built for that runtime and says so; - **a panic mentioning a missing reactor or context means a wrong driver, not a bug in the future**: look for who is polling it; - **tests follow the same rule**: an async test that touches Tokio's sockets or timers needs a Tokio runtime, through its test attribute or its `block_on`, not a generic executor.

  • Why does the panic happen when the Tokio sleep is created, rather than when it is first awaited?
    Tokio's timer looks up the current runtime's driver as it is constructed, so it can register with it. Without a Tokio context that lookup fails immediately, which is why the panic location is the `tokio::time::sleep(...)` call itself. That is useful when debugging: the panic's line points at the resource that needed a runtime.
  • Is it acceptable to run two runtimes in one Rust process, for example smol for a CLI and Tokio for one library?
    It works if each runtime drives only futures whose I/O belongs to it, but each brings its own threads and reactor, and any future that crosses from one to the other needs an adapter or fails. Most teams treat a second runtime as a temporary bridge and standardise on one per binary.

  • Saying a future written for Tokio uses a different Future trait.
  • Calling block_on inside an async handler to get a value synchronously.
  • Building a fresh runtime inside a helper on every call.
  • Assuming any executor can drive any library's sockets and timers.
  • Treating the missing-reactor panic as a bug inside the library.