go system_error ai_generated partial

accept tcp [::1]:8080: accept4: too many open files in system

ID: go/too-many-files-open-in-docker

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
1Evidence
2024-01-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Go 1.20 active
Go 1.21 active
Go 1.22 active

Root Cause

The system-wide file descriptor limit or per-process limit has been exceeded, often due to leaked file handles or high concurrency in a Go server.

generic

中文

系统范围的文件描述符限制或进程限制已被超出,通常由于 Go 服务器中的文件句柄泄漏或高并发导致。

Official Documentation

https://go.dev/doc/effective_go#leaks

Workarounds

  1. 75% success Increase system-wide file descriptor limit: edit /etc/sysctl.conf with 'fs.file-max = 100000' and apply with sysctl -p.
    Increase system-wide file descriptor limit: edit /etc/sysctl.conf with 'fs.file-max = 100000' and apply with sysctl -p.
  2. 85% success Check for leaked file handles using lsof -p <pid> and ensure all resources (http.Response.Body, os.File) are properly closed in all code paths, using defer for cleanup.
    Check for leaked file handles using lsof -p <pid> and ensure all resources (http.Response.Body, os.File) are properly closed in all code paths, using defer for cleanup.
  3. 80% success Use a connection pool with limits (e.g., http.Transport.MaxIdleConnsPerHost) to prevent excessive file descriptor usage.
    Use a connection pool with limits (e.g., http.Transport.MaxIdleConnsPerHost) to prevent excessive file descriptor usage.

中文步骤

  1. Increase system-wide file descriptor limit: edit /etc/sysctl.conf with 'fs.file-max = 100000' and apply with sysctl -p.
  2. Check for leaked file handles using lsof -p <pid> and ensure all resources (http.Response.Body, os.File) are properly closed in all code paths, using defer for cleanup.
  3. Use a connection pool with limits (e.g., http.Transport.MaxIdleConnsPerHost) to prevent excessive file descriptor usage.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Temporary fix; the error will reoccur once file descriptors accumulate again.

  2. 60% fail

    May not affect system-wide limits; also does not address the root cause of file descriptor leaks.

  3. 50% fail

    If close() fails silently, file descriptors may still leak.