# fatal error: 所有 goroutine 都休眠 - 死锁! (sync.Once.Do 递归)

- **ID:** `go/sync-once-reentrant`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

sync.Once.Do 的函数再次（直接或间接）对同一个 Once 调用 Do，导致自死锁，因为 Once 在执行 f 时持有内部互斥锁。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   var once sync.Once
once.Do(func() {
    // do not reference `once` here
    initConfig()
})
   ```
2. **** (88% 成功率)
   ```
   var onceA, onceB sync.Once
onceA.Do(initA)
onceB.Do(initB)
   ```

## 无效尝试

- **** — The deadlock is internal to Once; no timeout can break the held mutex. (90% 失败率)
- **** — Reintroduces the data race Once was meant to prevent. (70% 失败率)
