go resource_error ai_generated true

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

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.19 active — — —
1.21 active — — —
1.22 active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 80% fail

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

  2. 85% fail

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