Rust • 2026-11-19 • 3 min read
Where tokio Tasks Go Wrong: Blocking Work on the Async Runtime
One synchronous call in an async handler does not slow down one request. It stalls every other request scheduled on that worker thread. How to find it and where to put the work instead.

The symptom is always the same: latency that is fine under light load and catastrophic under moderate load, with CPU nowhere near saturated. Almost every time, something synchronous is sitting on a tokio worker thread.
Why one call hurts everything
tokio runs a small pool of worker threads — by default, one per core. Tasks are cooperatively scheduled: a task holds its worker until it hits an .await that actually yields. A blocking call never yields. For the duration of a 200 ms password hash, that worker runs no other task, and the dozens of ready tasks queued on it simply wait.
With eight workers and eight concurrent hashes, the entire runtime is stalled. Nothing in the metrics says "blocked"; it looks like the service got slow.
The usual culprits
- Password hashing —
argon2,bcrypt,scrypt— which is *designed* to burn CPU std::fsanywhere, including a config reload or reading a certificate- Synchronous database or HTTP clients pulled in by a transitive dependency
serde_json::from_sliceon a payload measured in megabytes- Compression, image work, and anything cryptographic over a large buffer
std::sync::Mutexheld across an.await, which is the same problem wearing a different hat
Finding it
Add tokio-console in staging, or turn on the unstable runtime metrics and watch for tasks whose poll time is long. In CI we fail the build on a lint for std::thread::sleep and std::fs inside async functions — crude, but it catches the honest mistakes, and the dishonest ones get found by the console.
Where the work goes
Three options, in the order we reach for them.
- `spawn_blocking` for a bounded, occasional cost: file reads, a one-off hash. It moves the closure to a separate, much larger pool meant for exactly this.
- A dedicated rayon pool for sustained CPU work, sized deliberately, so a burst of hashing cannot starve the blocking pool that your file I/O also uses.
- A different process when the work is heavy and bursty enough that it should be scaled and deployed on its own.
let hash = tokio::task::spawn_blocking(move || {
argon2::hash_encoded(password.as_bytes(), &salt, &ARGON2_CONFIG)
})
.await
.map_err(|_| AuthError::Internal)??;Note the two ?. spawn_blocking returns a JoinError if the closure panicked, and then the closure's own Result inside it. Collapsing both into one error type at the boundary keeps the handler readable.
The mutex case
std::sync::Mutex is correct and fast, and it is the right choice for a lock held for a few microseconds around a counter. It becomes a bug the moment the guard is alive across an .await: the task yields while holding a lock that other tasks on other workers now block on. Use tokio::sync::Mutex when the critical section contains an await, and keep the std one when it does not — the async mutex is slower, so it is not a blanket replacement.
Once this is clean, the backpressure settings on the service start meaning what they say.