Go to content
Web development

Managing Rust Async Runtime Compatibility

When you build a Rust service that does network I/O, you choose an async runtime early in the project. Rust’s async/await syntax lets you write concurrent code that reads like ordinary sequential code, but the compiler only rewrites your async functions into state machines, which are data structures that track where each function paused. The compiler does not provide the machinery that runs those state machines. That machinery is a runtime: a library that polls futures, schedules tasks across threads, and drives timers and network I/O. A future is a value representing work that has not finished yet; the runtime repeatedly checks whether that work can make progress and resumes it when it can. The common choices are Tokio, async-std, and smol, and the one you pick determines which async crates you can add without friction.

Some libraries call one runtime’s APIs directly, so they only work on that runtime. Others return plain futures and let you bring your own runtime, often selected through feature flags on the dependency. When you mix runtimes in one process, or call a runtime’s APIs from the wrong context, the program can panic, deadlock, or return channel errors that are hard to trace back to the dependency choice.

You should therefore evaluate runtime compatibility as part of dependency selection, before you commit to a crate. You can avoid most of these failures by checking whether a crate calls a specific runtime’s APIs, reading its feature flags, and running a small integration test on your actual runtime before you build the rest of the service around it.

Why some libraries are bound to a specific runtime

A runtime provides more than a way to poll futures. Tokio provides runtime-specific APIs such as tokio::spawn to start a new task, tokio::sync::mpsc for multi-producer, single-consumer channels, tokio::time::timeout for deadlines, and the tokio::select! macro for waiting on several async operations at once. A library that calls any of these directly is tied to Tokio. The part of the runtime that polls futures is the executor; the part that watches sockets and timers and wakes tasks when they can make progress is the reactor. A library that touches either one carries that runtime’s requirements into your dependency tree.

Some popular crates make this coupling explicit. axum, the web framework, is built directly on Tokio, so you cannot run it on async-std or smol. tonic and hyper are more independent: hyper exposes its own executor, timer, and I/O abstractions under hyper::rt, which lets it run on other runtimes. Cloudflare built Pingora on Tokio, citing Tokio’s multithreading, shared resources such as connection pools, and work stealing as the reasons, and wrote its own HTTP implementation instead of adopting hyper.

The ecosystem has consolidated around Tokio. More than 60% of the most popular async Rust projects on GitHub use Tokio internally, and Tokio gets roughly 51x more downloads than smol recently. That dominance is self-reinforcing: library authors target the runtime most of their users already run, which leaves the smaller runtimes with fewer new libraries. The runtime you pick also carries platform constraints. glommio, a thread-per-core runtime built on io_uring, only runs on Linux and requires kernel 5.8 or newer.

When you add a crate, you also add its runtime coupling. If the crate calls tokio::spawn internally, your whole service needs a Tokio runtime in every thread that calls into the crate. If you chose async-std for another part of the service, you now have two runtimes to keep alive, each with its own thread pools and I/O registration.

How feature flags select a runtime

Cargo features are compile-time switches that turn optional parts of a crate on or off, and they are the standard mechanism for choosing a runtime at dependency resolution time. The MongoDB Rust driver is a clear example: it supports both tokio and async-std, with tokio as the default, and you select the runtime by adding the tokio-runtime or async-std-runtime feature flag to the mongodb dependency in Cargo.toml. The driver also provides a synchronous API through the sync or tokio-sync feature flags, and you can use both the async and sync APIs in the same application.

Feature flags work because Cargo merges them across the whole dependency graph. If one crate enables mongodb‘s tokio-runtime feature and another enables async-std-runtime, Cargo compiles both runtime integrations into the same driver. That is why the Rust ecosystem requires features to be additive: enabling a feature should not disable functionality, and it should usually be safe to enable any combination of features.

Some code generators break that rule. The maybe_async crate generates sync and async versions of the same code. It has a known limitation: it does not support the case where one dependency uses it with the sync feature enabled and another uses it with the async feature. Because Cargo unifies features, the two dependencies conflict and the build fails with a compile error. The SyncA generator avoids this by producing async and sync APIs in separate modules, so both can coexist in one project.

Tokio itself follows the additive model. It enables no features by default, and the full feature set turns on APIs and optional dependencies that a minimal configuration does not need. When you add a dependency that enables tokio/full, you pull all of those APIs into your build. So check a crate’s feature requirements before you adopt it.

What breaks when runtimes mix

Even if you choose features correctly, you can still break a service by mixing runtimes at runtime. Each runtime keeps its own executor, reactor, and task bookkeeping. A future created on one runtime and polled by another can panic or deadlock, because runtime-specific state such as timer registrations and I/O readiness does not transfer. You usually see a panic about a missing runtime context, or a task that never wakes.

Most of these failures start when your code calls a runtime’s APIs from the wrong context. Each runtime’s spawn function requires its own runtime to be running in the current thread. If you call the wrong one, the program panics. tokio::spawn inside an async-std executor fails immediately because no Tokio runtime is running in that thread.

You can also break a runtime by blocking the executor. There is no widely portable way to do non-blocking filesystem operations. Tokio and other runtimes use a thread pool for blocking work, so a runtime-agnostic library has to ask the caller to provide its spawn_blocking function or equivalent. If you call a blocking operation such as std::fs::read_to_string directly inside an async function, you block the executor thread and stall every other task scheduled on it. Google’s Comprehensive Rust course lists this among the common async pitfalls.

You also run into trouble when you try to bridge between runtimes inside one process. One user on the Rust forums bridged Tokio into a synchronous MIDI callback with Runtime::new and rt.block_on. They saw a SendError with the message "channel closed" when events arrived in quick succession. Removing the send eliminated the errors, which suggested the block_on approach caused the issue. The futures crate includes a compat module for bridging code written against older futures APIs into newer futures versions, but no equivalent standard layer translates between runtimes.

What a runtime-agnostic crate looks like

When you evaluate a crate, its design tells you whether it will work with a different runtime. A library that wants to work on any runtime exposes plain std::future::Future values and portable traits, and lets the caller bring the runtime. The library uses cfg(feature = "...") to isolate runtime-specific pieces behind feature flags. It uses a specific runtime’s I/O only when it has to, for example when it specifically wants smol’s net or executor. smol itself is built this way: no macros, a minimal core, and component crates such as async-io, async-executor, and blocking that you can pull in piecemeal.

The most portable design is called sans I/O. The library implements a state machine that consumes bytes and produces outcomes, and separate blocking and async wrappers feed that state machine and return its results. The k8s-openapi crate works this way: its API produces a request value you can send with whatever sync or async HTTP client you want, plus a function to parse the response.

Some libraries take a simpler route. They implement two separate traits with the same methods, one per runtime, or they implement the same trait with only one implementation visible at a time and a compile error if you enable both runtimes or none. That works, but it makes the crate’s public API more complicated.

The reason these workarounds exist is that Rust has not standardized async traits in std. There is no standard AsyncOpen trait, no pluggable runtime interface, and no keyword generics, so a library cannot write one implementation that works everywhere. The apache_avro maintainers describe supporting both blocking and async APIs as not a trivial task. They explored SyncA and the sans I/O approach, and noted that SyncA‘s heavy procedural macros make the IntelliJ RustRover IDE stop working.

Compatibility checks before adoption

You can evaluate a crate’s runtime compatibility before you add it, using the crate’s documentation, its public API, and a couple of build-time checks. This is one dimension of evaluating open source dependency risk before you commit.

Run these checks:

  • Read the crate’s docs for runtime feature flags. A crate that offers a runtime choice usually documents the flag names, as the MongoDB driver does with tokio-runtime and async-std-runtime.
  • Look at the public API for runtime types. If you see tokio::spawn, tokio::sync::mpsc, or tokio::time::timeout in signatures, the crate is Tokio-bound. If it returns plain impl Future, it is probably portable.
  • Check the runtime’s maintenance status.
  • Check transitive dependencies for feature unification conflicts, such as two crates using maybe_async with different features.
  • Compare the feature sets you actually need and clean-build times on your target. The cost of a heavy runtime feature set mostly shows up on clean or uncached CI builds.

The maintenance check matters more than it looks. RustSec issued RUSTSEC-2025-0052, an informational advisory that flags async-std as unmaintained, and the crate’s README says to use smol instead. An unmaintained advisory does not mean the crate fails to compile today; it means no one is publishing fixes for breakage that appears later. Running cargo audit in CI surfaces these findings, though unmaintained findings are warnings unless you configure CI to deny them. This is part of assessing the maintenance risk of any dependency.

Then test the actual combination early. Write a small integration test that runs the crate’s future on your runtime, exercises a timer and a network call, and uses the same feature set you plan to ship. That test will surface the missing-runtime panics and channel errors that the docs rarely mention.

More in Web development Back to top ↑