# accept tcp [::1]:8080: accept4: 系统中打开的文件过多

- **ID:** `go/too-many-files-open-in-docker`
- **领域:** go
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Go 1.20 | active | — | — |
| Go 1.21 | active | — | — |
| Go 1.22 | active | — | — |

## 解决方案

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

## 无效尝试

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