TryLockError::WouldBlock
RustINFOCriticalConcurrency
try_lock failed — lock is already held
Quick Answer
Retry later or fall back to lock() for a blocking acquire.
What this means
try_lock() returned Err(TryLockError::WouldBlock) because another thread currently holds the lock.
Why it happens
- 1Another thread holds the mutex
- 2Contention on a hot lock
Fix
Non-blocking try with fallback
Non-blocking try with fallback
match mutex.try_lock() {
Ok(g) => process(g),
Err(std::sync::TryLockError::WouldBlock) => {
// do other work, try again later
}
Err(e) => return Err(e.into()),
}Why this works
WouldBlock is not an error state; it signals the caller to retry.
Code examples
Blocking alternativerust
let g = mutex.lock()?; // blocks until available
Sources
Official documentation ↗
Rust std::sync
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev