# 致命错误：所有协程都处于休眠状态 - 死锁！（互斥锁）

- **ID:** `go/goroutine-mutex-deadlock`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

两个或多个协程持有锁并等待彼此释放，导致循环等待条件。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.19 | active | — | — |
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **Always lock mutexes in the same order** (95% 成功率)
   ```
   // Lock mu1 then mu2 consistently
mu1.Lock()
mu2.Lock()
// unlock in reverse order
   ```
2. **Use trylock with a fallback** (80% 成功率)
   ```
   if mu.TryLock() {
    defer mu.Unlock()
    // critical section
} else {
    // handle contention
}
   ```

## 无效尝试

- **Use a single global mutex** — A single mutex can cause contention but not deadlock if used correctly; however, if nested locks are needed, it can still deadlock. (50% 失败率)
- **Ignore the deadlock and restart the program** — Temporary fix; the deadlock will recur. (90% 失败率)
