Skip to content

rust.arc_mutex_contention

Performance Medium

Detects Arc<Mutex<T>> or Arc<RwLock<T>> patterns that may cause lock contention in async code.

Lock contention:

  • Blocks async runtime — Mutex guards block the executor thread
  • Serializes operations — Concurrent tasks wait for lock
  • Defeats concurrency — Parallelism lost to synchronization
// ❌ Before (holding lock across await)
let data = Arc::new(Mutex::new(vec![]));
async fn process(data: Arc<Mutex<Vec<i32>>>) {
let mut guard = data.lock().unwrap();
some_async_op().await; // Lock held across await!
guard.push(1);
}
// ✅ After (minimize lock scope)
async fn process(data: Arc<Mutex<Vec<i32>>>) {
some_async_op().await;
{
let mut guard = data.lock().unwrap();
guard.push(1);
} // Lock released immediately
}
  • Lock guards held across .await points
  • Long-held mutex guards
  • Mutex where RwLock would be better