go resource_error ai_generated true

runtime: goroutine 泄漏:net/http.(*persistConn).readLoop 卡住

runtime: goroutine leak: net/http.(*persistConn).readLoop stuck

ID: go/goroutine-leak-http-body

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

版本兼容性

版本状态引入弃用备注
1.18 active
1.22 active

根因分析

HTTP 响应体未关闭,导致持久连接 goroutine 一直阻塞在读取上。

English

HTTP response bodies are not closed, causing persistent connection goroutines to remain blocked reading.

generic

解决方案

  1. 95% 成功率
    resp, err := client.Do(req)
    if err != nil { return err }
    defer resp.Body.Close()
    io.Copy(io.Discard, resp.Body)
  2. 88% 成功率
    ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
    defer cancel()
    req = req.WithContext(ctx)

无效尝试

常见但无效的做法:

  1. 75% 失败

    Timeout does not close the body; the readLoop goroutine still leaks.

  2. 80% 失败

    Reusing the client does not fix unclosed bodies.