# fatal error: sync: 解锁未加锁的 RWMutex

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

## 根因

在没有匹配 RLock() 的情况下调用 RUnlock()，或读锁已被释放，破坏了读者计数。

## 版本兼容性

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

## 解决方案

1. **** (94% 成功率)
   ```
   Pair defer mu.RUnlock() with mu.RLock() on the next line, and defer mu.Unlock() with mu.Lock(), never mixing them.
   ```
2. **** (90% 成功率)
   ```
   Use a helper: func withReadLock(mu *sync.RWMutex, fn func()) { mu.RLock(); defer mu.RUnlock(); fn() }.
   ```

## 无效尝试

- **** — This sacrifices read concurrency and does not fix the missing RLock; if RUnlock is still called, it now mismatches Lock/Unlock. (70% 失败率)
- **** — Adds an extra lock that is never released, causing a permanent deadlock on the next writer. (90% 失败率)
