Lesson 8 of 9 · 26m
Debugging Stalls, Deadlocks, and Memory Growth
Use a disciplined evidence path for hung tasks, wedged workers, unbounded queues, retained futures, and cancellation-triggered leaks.
When an async Rust service stops making progress, the worst response is random tuning: double the workers, increase channel capacity, add a timeout, restart twice. Sometimes one of those coincidentally helps, which teaches the team the wrong lesson. Debugging requires identifying what resource is exhausted and what task is holding or waiting for it.
Classify the stall first
Use symptoms as hypotheses, not conclusions:
workers hot synchronous CPU, spin loop, lock contention
workers idle no wakeups, deadlocked lock, closed channel, downstream wait
memory rising unbounded queue, retained futures, cache without eviction
single shard slow key skew, lock convoy, downstream hot partitionThe shape matters because the fix is different. More workers can worsen lock contention. Bigger queues can worsen memory growth. More timeouts can increase retry pressure.
Hung wakeups are often missing wakeups
A future completes only when it is polled to completion. If a custom implementation fails to store or call a Waker correctly, the task never wakes. In application code the equivalent mistake is usually indirect: waiting on a channel whose sender was dropped incorrectly, waiting on a Notify that fired before the waiter registered, or using a oneshot whose sender was lost.
tokio::sync::Notify is explicitly not a persistent event counter:
notify.notify_one();
// If nobody is waiting yet, this notification is not necessarily retained
// for a future waiter the way newcomers often expect.For state changes, prefer watch channels or explicit state checks plus notification.
Deadlocks in async systems are often logical
Even if Tokio's scheduler remains healthy, your application can deadlock through dependency cycles:
task A holds mutex X -> waits for response from actor B
actor B needs mutex X -> waits for task AThe runtime cannot rescue you because no runtime invariant is violated. The fix is architectural: reduce lock scope, remove synchronous response cycles, use timeouts as a safety net, and make ownership direction acyclic.
Memory growth usually means ownership is broader than intended
A future can retain surprisingly large state across an await. This innocent-looking handler holds the entire request body while waiting on a slow downstream call:
let body = read_full_body().await;
upstream_call().await;
validate(&body);If the body is large, every in-flight request pins that memory. Parse early, discard what you do not need, stream large payloads, or move heavy data into bounded spill/storage. Heap profilers help, but first inspect which values are alive across long waits.
Capture a task inventory before mitigating
If tokio-console or internal task telemetry is available, capture it. Otherwise add a lightweight task registry with IDs, states, and start times. During incidents, answer these questions: how many tasks exist, which phase are they in, how old are they, which downstream resource do they share, and what changed at the inflection point? Restart only after recording enough state to compare before and after.
Field notes
Notifyis a wakeup primitive, not a reliable event log.- A timeout can hide a deadlock while multiplying load through retries.
Was this lesson helpful?