# fatal error: sync: 解锁未加锁的互斥锁

- **ID:** `go/fatal-error-sync-unlock-of-unlocked-mutex`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在互斥锁未被持有的情况下调用 mu.Unlock()，通常因为提前返回后 defer unlock 仍执行，或重复解锁。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Use defer mu.Unlock() immediately after mu.Lock() in the same function so they always pair.
   ```
2. **** (90% 成功率)
   ```
   Audit all code paths: ensure no early return between Lock and Unlock without defer, and never unlock in a different function than the one that locked.
   ```

## 无效尝试

- **** — This is a fatal runtime error, not a panic; recover cannot catch it and the process exits. (98% 失败率)
- **** — The flag itself needs synchronization and is often set incorrectly, reintroducing the bug under concurrency. (80% 失败率)
