---
title: "How to deadlock Tokio application in Rust with just a single mutex"
description: "A Tokio program can deadlock with a single std::sync::Mutex when a spawn_blocking task holds the lock while calling Handle::block_on on a future, and an async task on a worker thread tries to take the same lock."
url: "https://swe.quest/articles/how-to-deadlock-tokio-application-in-rust-with-just-a-single-mutex-8d0964c04bebf8f2"
canonical: "https://turso.tech/blog/how-to-deadlock-tokio-application-in-rust-with-just-a-single-mutex"
source: "Turso"
published: "2024-09-02"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 3
tags: ["rust","concurrency","debugging"]
---

# How to deadlock Tokio application in Rust with just a single mutex

A post on [Turso](https://swe.quest/sources/turso.md), published 2024-09-02, 3 min read, Deep dive. Original: [https://turso.tech/blog/how-to-deadlock-tokio-application-in-rust-with-just-a-single-mutex](https://turso.tech/blog/how-to-deadlock-tokio-application-in-rust-with-just-a-single-mutex)

## Summary

A Tokio program can deadlock with a single std::sync::Mutex when a spawn\_blocking task holds the lock while calling Handle::block\_on on a future, and an async task on a worker thread tries to take the same lock. The likely cause is that worker threads get blocked on the mutex, so the blocked-on future cannot make progress. In effect the std mutex is held across an implicit .await point. Switching to tokio::sync::Mutex (lock().await and blocking\_lock()) fixes it, and the recommendation is to default to Tokio's mutex in async code unless profiling shows it is a bottleneck, much as SeqCst should be the default over Relaxed atomics.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Rust](https://swe.quest/tags/rust.md) (Languages)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)
- [Debugging](https://swe.quest/tags/debugging.md) (Engineering practices)

## Prerequisites

- Rust async/await ([easy posts on Rust](https://swe.quest/tags/rust.md?difficulty=easy))
- Tokio runtime basics
- mutexes

## Keywords

Tokio, std::sync::Mutex, tokio::sync::Mutex, spawn\_blocking, block\_on

## Related posts

- [Async Rust](https://swe.quest/articles/async-rust-c6376cf98e387782.md): Zed · 2024-04-09 · Medium · 11 min read · Rust, Concurrency, Runtimes & VMs, Developer tools, Performance engineering
  Zed's GPUI framework runs async Rust without tokio or smol: it uses async\_task for tasks and thin wrappers around macOS Grand Central Dispatch as the executor, dispatching Runnables via dispatch\_async\_f onto the main queue or a high-priority global queue through a trampoline function.
- [parking\_lot: ffffffffffffffff...](https://swe.quest/articles/parking-lot-ffffffffffffffff-e7cd36eac4ddf5cc.md): Fly.io · 2025-05-28 · Hard · 16 min read · Concurrency, Rust, Debugging, Incidents & postmortems, Load balancing
  Fly.io's Rust Anycast router, fly-proxy, kept deadlocking after Catalog state was moved to lazy loading.
- [Enabling the parallel frontend on nightly](https://swe.quest/articles/enabling-the-parallel-frontend-on-nightly-9c56c94985e87b86.md): Rust Blog · 2026-11-16 · Medium · 6 min read · Rust, Compilers, Performance engineering, Concurrency
  Rust nightly now runs the compiler frontend (type checking, borrow checking) in parallel by default since nightly-2026-11-14, to surface bugs ahead of stabilization.
- [Oxidizing OCaml: Data Race Freedom](https://swe.quest/articles/oxidizing-ocaml-data-race-freedom-67442324c35f0e1c.md): Jane Street Tech Blog · 2023-09-01 · Hard · 15 min read · Functional programming, Type systems, Concurrency, Language design, Memory safety
  Jane Street's OCaml extensions use a new \`sync\` mode to make data races statically unrepresentable in multicore OCaml 5.
- [How posix\_spawn & close\_range “fixed” cold starts](https://swe.quest/articles/how-posix-spawn-close-range-fixed-cold-starts-0c769ed6733a0a1b.md): Neon · 2024-02-21 · Medium · 14 min read · Linux, Performance engineering, Debugging, Rust, PostgreSQL
  Neon's Pageserver regressed cold-start and first-query-after-idle latency to seconds after it began shutting down idle per-tenant walredo sidecar processes.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
