rust.arc_mutex_contention
Performance
Medium
Detects Arc<Mutex<T>> or Arc<RwLock<T>> patterns that may cause lock contention in async code.
Why It Matters
Section titled “Why It Matters”Lock contention:
- Blocks async runtime — Mutex guards block the executor thread
- Serializes operations — Concurrent tasks wait for lock
- Defeats concurrency — Parallelism lost to synchronization
Example
Section titled “Example”// ❌ 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}What Unfault Detects
Section titled “What Unfault Detects”- Lock guards held across
.awaitpoints - Long-held mutex guards
MutexwhereRwLockwould be better