# UDP: 端口 12345 上的套接字接收缓冲区溢出，丢弃了 5000 个数据包

- **ID:** `networking/udp-socket-buffer-overflow`
- **领域:** networking
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

UDP 套接字缓冲区溢出发生在应用程序未能足够快地从接收缓冲区读取数据时，导致内核丢弃传入数据包以防止内存耗尽。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Linux kernel 5.15+ | active | — | — |
| Linux kernel 6.5+ | active | — | — |
| FreeBSD 13.1+ | active | — | — |

## 解决方案

1. ```
   Increase the UDP receive buffer size system-wide: sysctl -w net.core.rmem_max=26214400 && sysctl -w net.core.rmem_default=26214400, then restart the application.
   ```
2. ```
   Optimize the application to read UDP packets in larger batches or use non-blocking I/O: In Python, use socket.setblocking(0) and a loop to drain the buffer.
   ```
3. ```
   Use the SO_RCVBUF socket option to set a larger buffer per socket: In C, setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
   ```

## 无效尝试

- **** — 任意增加套接字接收缓冲区大小（例如 net.core.rmem_max = 10000000）可能延迟溢出，但无法解决应用程序处理速度慢的问题。 (75% 失败率)
- **** — 重启应用程序或服务器会暂时清空缓冲区，但如果应用程序的读取速率与传入流量速率不匹配，溢出会再次发生。 (90% 失败率)
- **** — 禁用 UDP 校验和卸载可能减少 CPU 负载，但无法解决由应用程序级背压引起的缓冲区溢出。 (80% 失败率)
