# 警告：数据竞争：协程X写入，协程Y读取（通道关闭）

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

## 根因

多个协程试图关闭同一个通道而没有同步，导致竞争。

## 版本兼容性

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

## 解决方案

1. **Use sync.Once to ensure close is called only once** (95% 成功率)
   ```
   var once sync.Once
once.Do(func() { close(ch) })
   ```
2. **Use a dedicated closer goroutine** (85% 成功率)
   ```
   closeCh := make(chan struct{})
go func() {
    <-closeCh
    close(ch)
}()
// signal close
close(closeCh)
   ```

## 无效尝试

- **Using atomic flag to guard close** — Close itself is not atomic; still race between check and close. (80% 失败率)
- **Using recover() to catch double close panic** — Recover doesn't prevent race; panic may still occur. (90% 失败率)
