go resource_error ai_generated true

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

error: accept tcp :8080: accept4: too many open files

ID: go/too-many-open-files-goroutine-leak

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-07-24首次发现

版本兼容性

版本状态引入弃用备注
1.19 active — — —
1.21 active — — —
1.22 active — — —

根因分析

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

English

Goroutines leak because HTTP response bodies are not closed or client connections are not drained, exhausting file descriptors.

generic

解决方案

  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)

无效尝试

常见但无效的做法:

  1. 80% 失败

    Delays the crash; leaked descriptors keep accumulating and the process eventually dies again.

  2. 85% 失败

    Treats the symptom; the leak persists and restart frequency increases.