# 错误：accept tcp :8080: accept4: 打开的文件过多

- **ID:** `go/too-many-open-files-goroutine-leak`
- **领域:** go
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

由于未关闭 HTTP 响应体或未排空客户端连接，goroutine 泄漏，最终耗尽文件描述符。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   resp, err := http.Get(url)
if err != nil { return err }
defer resp.Body.Close()
io.Copy(io.Discard, resp.Body)
   ```
2. **** (90% 成功率)
   ```
   ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := http.DefaultClient.Do(req)
   ```

## 无效尝试

- **** — Delays the crash; leaked descriptors keep accumulating and the process eventually dies again. (80% 失败率)
- **** — Treats the symptom; the leak persists and restart frequency increases. (85% 失败率)
