go resource_error ai_generated true

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

ID: go/goroutine-leak-http-body

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-07-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.22 active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 75% fail

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

  2. 80% fail

    Reusing the client does not fix unclosed bodies.