
Understand it
An async function runs synchronously whenever it is polled until it yields or returns. A blocking call occupies that worker; suspension at await is different and need not occupy a thread. An immediately ready await may not yield. A lock held across suspension can still block competing tasks and create deadlocks.
An async function does not turn every operation inside it into nonblocking work. An executor polls tasks on worker threads. If a task calls a synchronous sleep or performs a long CPU loop, that worker cannot poll other ready tasks until the call returns or the code yields. Even a short operation can matter at high concurrency if it appears in every request. The right diagnosis is to measure the delay of unrelated tasks while the suspicious operation runs.
A synchronous mutex is not automatically wrong in async code. It can protect a tiny critical section that never crosses an .await. Holding a guard across an await is dangerous because the task may suspend while retaining the lock; other tasks then wait, and some executors may reject a non-Send future. Use an async-aware mutex only when waiting for the lock should itself yield, and still keep the guarded work short. Do not assume that switching mutex types fixes a long blocking calculation.
Move true blocking operations to a dedicated blocking pool or thread, with owned inputs and explicit result/error handling. CPU-heavy work may need a bounded compute pool so it does not overwhelm the process. Observe what happens when the caller cancels: offloaded synchronous work may continue after its async handle is dropped. In an interview answer, state the workload, the harm to executor fairness, the chosen boundary for offloading, and a measurement that would confirm improvement.
For a blocking API you cannot replace, name the maximum number of concurrent calls you will offload. An unbounded blocking pool can preserve executor responsiveness while overwhelming storage or CPU elsewhere. Backpressure belongs at that boundary too. After the change, compare timer jitter and request tail latency under load.
Interview follow-up: Can a started spawn_blocking closure be forcefully cancelled by dropping its JoinHandle? Design shutdown for work that may continue.
Read the code
let text = tokio::task::spawn_blocking(|| {
std::fs::read_to_string("big.csv")
})
.await??;Tokio fragment: requires a prepared rt-capable dependency graph and a context returning compatible nested errors; it is not a std-only example.
Practice starter
async fn handler() -> std::io::Result<Vec<u8>> {
std::fs::read("big.bin")
}Try it
Find the blocking call in an async handler and move it off the runtime.
What success looks like: No task holds an executor thread while it waits on something synchronous.
Check your understanding
What does the synchronous read do while the executor polls this task?
Reveal the answer
- The async keyword automatically schedules std::fs work on a blocking pool.
- It occupies the current executor worker until the synchronous call returns.
- Calling a synchronous function from async code necessarily causes a panic.
This read does not yield while it runs. On a multi-thread runtime other workers may still progress; measure latency and bound any offloaded work.
Go deeper
Common misconception: Calling a synchronous function from async code necessarily causes a panic.
It compiles and runs without panicking. std::fs::read simply holds the worker until it returns, so the harm is lost progress for other tasks, not a crash.
- Identify the contract: One blocking call can stall an entire async runtime.
- Trace the concrete example: The blocking call is moved off the executor’s worker threads.
- Test the changed constraint: Can a started spawn_blocking closure be forcefully cancelled by dropping its JoinHandle? Design shutdown for work that may continue.
Transfer challenge: Can a started spawn_blocking closure be forcefully cancelled by dropping its JoinHandle? Design shutdown for work that may continue.
Connections
- Async vs Threads: Waiting versus computing
- Cancellation Safety: Dropping a future mid-await
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Blocking an Executor (courseId "interview", lessonId "q-blocking-async") Link: https://crabrix.com/learn/interview/q-blocking-async/ Goal: Never block an executor thread: yield, or move the work off it. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-blocking-async) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.