Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Turso 3 min read Deep dive mediumAdept

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

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.

Assumes: Rust async/awaitTokio runtime basicsmutexes

Related posts

  • Async Rust

    Zed mediumAdept

    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. GPUI separates a ForegroundExecutor (main thread, which handles rendering and input and must never block) from a BackgroundExecutor (GCD global queue). Code walkthroughs from the editor, terminal search and project-wide search show the pattern of keeping quick work on the main thread and pushing lock acquisition, file scanning and searching to background tasks so frames hit the 8ms budget at 120 FPS.

  • parking_lot: ffffffffffffffff...

    Fly.io hardMaster

    Fly.io's Rust Anycast router, fly-proxy, kept deadlocking after Catalog state was moved to lazy loading. The cause was traced to parking_lot RwLock words being set to 0xFFFFFFFFFFFFFFFF. The hunt ran from a known if-let lock-lifetime footgun through closure-scoped lock refactors with timeouts and telemetry, parking_lot's deadlock detector, gdb core-dump inspection, read_recursive experiments and miri. Core dumps showed threads waiting on a lock nobody held, and logging the raw lock word revealed every bit set, including the unused UPGRADEABLE flag. The post also covers Corrosion, Fly.io's CRDT-structured SQLite replicated over SWIM gossip, and the watchdog that bounces stuck proxies to keep deadlocks survivable.

  • Enabling the parallel frontend on nightly

    Rust Blog mediumAdept

    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. In single-crate benchmarks with 8 threads, mean wall time dropped about 44% for check and 28% for opt builds, with some crates like nalgebra, cargo and diesel checking nearly 3x faster. Gains shrink when Cargo and LLVM already saturate the cores: an end-to-end cargo check of the whole crate graph improved only about 15%, and incremental rebuilds improved 3-5%. Nightly defaults to 2 frontend threads to limit memory growth; use -Zunstable-options --jobs-frontend=N to tune it or set N=1 to opt out, and expect nondeterministic diagnostic order and possible reproducibility bugs.

  • Oxidizing OCaml: Data Race Freedom

    Jane Street Tech Blog hardMaster

    Jane Street's OCaml extensions use a new `sync` mode to make data races statically unrepresentable in multicore OCaml 5. Immutable and exclusively mutable values are `sync` and shareable across domains, while ordinary mutable values are `unsync` and confined to one domain. Mode-annotated mutexes give roughly safe-Rust capabilities. Capsules go further: they encapsulate unrestricted shared mutability, such as cyclic mutable structures, behind existentially typed keys, so code can mutate shared-ownership data in parallel without Rust-style fine-grained ownership tracking or unsafe code.

  • How posix_spawn & close_range “fixed” cold starts

    Neon mediumAdept

    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. Spawning them was slow because a pre_exec hook (used to mark inherited file descriptors close-on-exec) forced Rust's std::process::Command to fall back from posix_spawn to fork+exec. The real cost turned out to be the close_fds crate's fallback: CLOSE_RANGE_CLOEXEC isn't supported on Linux 5.10, so it issued one fcntl per open FD, roughly 2s across ~200k descriptors. Moving a plain close_range call into the walredo process itself cut basebackup latency from ~2s mean and ~10s p99 to the low hundreds of milliseconds.

The quest for software engineering knowledge.

Look:
Layout: