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

- **ID:** `go/too-many-files-open-in-docker`
- **Domain:** go
- **Category:** system_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Go 1.20 | active | — | — |
| Go 1.21 | active | — | — |
| Go 1.22 | active | — | — |

## Workarounds

1. **Increase system-wide file descriptor limit: edit /etc/sysctl.conf with 'fs.file-max = 100000' and apply with sysctl -p.** (75% success)
   ```
   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.** (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.
   ```
3. **Use a connection pool with limits (e.g., http.Transport.MaxIdleConnsPerHost) to prevent excessive file descriptor usage.** (80% success)
   ```
   Use a connection pool with limits (e.g., http.Transport.MaxIdleConnsPerHost) to prevent excessive file descriptor usage.
   ```

## Dead Ends

- **** — Temporary fix; the error will reoccur once file descriptors accumulate again. (90% fail)
- **** — May not affect system-wide limits; also does not address the root cause of file descriptor leaks. (60% fail)
- **** — If close() fails silently, file descriptors may still leak. (50% fail)
