go system_error ai_generated partial

dial tcp: socket: too many open files

ID: go/too-many-open-files

Also available as: JSON · Markdown
82%Fix Rate
85%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

File descriptor limit reached. Common in HTTP clients without closing response bodies.

generic

Workarounds

  1. 95% success Close HTTP response bodies: defer resp.Body.Close()
    resp, err := http.Get(url)
    if err != nil { return err }
    defer resp.Body.Close()

    Sources: https://pkg.go.dev/net/http#Client.Do

  2. 90% success Close file handles, database connections, and other resources with defer
    // Always close with defer:
    f, err := os.Open("file.txt")
    if err != nil {
        return err
    }
    defer f.Close()
    
    // Same for DB connections:
    rows, err := db.Query(query)
    if err != nil {
        return err
    }
    defer rows.Close()

    Sources: https://go.dev/blog/defer-panic-and-recover

  3. 75% success Increase ulimit as a temporary measure: ulimit -n 65536
    ulimit -n 65536

    Sources: https://pkg.go.dev/os#File

Dead Ends

Common approaches that don't work:

  1. Increase ulimit to a very large value 60% fail

    Just delays the problem — file descriptors are still leaking

  2. Restart the process when limit is hit 80% fail

    Hack that doesn't fix the leak

Error Chain

Frequently confused with: