Async Reliability · 18 min · free lesson

Blocking in Async Code

Keep executor threads available to poll other futures.

Core idea

A blocking call occupies an executor worker without yielding, so unrelated tasks can stall.

Diagram for Blocking in Async Code: a Rust code example flows to the lesson rule and the key observation.
.await on a timer gives the thread back to the executor; std::thread::sleep would not.

Understand it

A blocking call occupies an executor worker without yielding, so unrelated tasks can stall. Move CPU-heavy or blocking work to a dedicated pool and await its result.

An executor worker can poll many async tasks because each task yields while waiting for nonblocking I/O. A synchronous sleep, filesystem call, expensive compression loop, or blocking foreign-library call can occupy that worker instead. Other ready tasks then wait even though they could run. Putting async on a function signature does not change a blocking call inside it into cooperative work.

Use the runtime's blocking pool or a dedicated worker thread for truly blocking operations, and pass owned input into it. The offload has overhead, so a tiny calculation may be cheaper to perform directly; measure the latency of other tasks under realistic load. CPU-heavy jobs can saturate even a blocking pool, so consider a bounded queue or a separate compute service when throughput matters. Think about shutdown too: a blocking call may not stop simply because its async caller was cancelled.

Create one task that repeatedly awaits a short timer and logs the delay. Alongside it, run a second task with a long synchronous sleep. Observe the timer stalls when both compete for an executor worker, then move the blocking operation to the runtime's blocking facility and compare. Change the sleep to a CPU loop and watch the same scheduling issue. Keep track of where the result and error travel back to the async caller. The goal is predictable responsiveness, not merely making code compile inside an async block.

Read the code

async fn handler() {
    // Do not sleep the executor thread:
    // std::thread::sleep(...);
    async_timer().await;
}

.await on a timer gives the thread back to the executor; std::thread::sleep would not.

Practice starter

use std::time::Duration;

async fn handler() {
    // Holds this executor thread for a full second.
    std::thread::sleep(Duration::from_secs(1));
}

Practise this code in Playground ↗

Try it

Rewrite handler so the wait yields, using your runtime's async timer. Then run it beside a second task and compare how late that task's timer fires.

What success looks like: The second task keeps its schedule, and you can say why sleep inside an async fn delayed it.

Check your understanding

Why is a blocking sleep harmful here?

Reveal the answer
  1. It stalls an executor worker
  2. It moves the future
  3. It poisons a lock

There is no .await in that wait, so the task cannot yield. The worker is held for the full second, and other ready tasks on it wait too.

Go deeper

Common misconception: “Putting a blocking call inside an async fn makes it non-blocking.”

A blocking call occupies an executor worker without yielding, so unrelated tasks can stall. Marking the function async does not change that.

  1. Enter the handler: handler() returns a future: its body starts only when an executor polls it, and it runs on that executor's worker.
  2. Reach the await: While the timer runs, async_timer().await makes handler's poll return Poll::Pending, so the worker goes back to polling other tasks.
  3. Compare the comment: The commented-out std::thread::sleep would hold the worker for the whole delay, with no .await where the task could yield.

Transfer challenge: Put a CPU-heavy loop in handler where the comment is. Predict whether a second task's timer stalls as it did with sleep, then move the loop to the runtime's blocking pool and compare.

Connections

  • Cancellation Safety: Expect a future to be dropped at any await point
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 in Async Code (courseId "concurrency", lessonId "blocking-async")
Link: https://crabrix.com/learn/concurrency/blocking-async/
Goal: A blocking call occupies an executor worker without yielding, so unrelated tasks can stall.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=concurrency/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.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.