TCP server spins at 100% CPU when accept() fails with EMFILE

Aperta
#1,148 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@itsGiaan ci sta già lavorando.

Dal 17/11/2026.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

bug

Description

When a TCP server runs out of file descriptors, accept() fails with EMFILE. The pending connection stays in the listen queue, so the listening socket remains readable. Every event wait then returns immediately, accept() fails again, and the server busy-loops at 100% CPU.

Because the server loop only runs scheduled timers when the event wait returns no events, timers (ping-restart, hand-window, etc.) stall for as long as the condition lasts. The same applies to the management listener.

The default --max-clients is 1024, the same as the usual default soft RLIMIT_NOFILE on Linux. Since each TCP client uses one descriptor, plus the tun device, listeners, log and management, a TCP server with default settings hits EMFILE before reaching --max-clients.

On Linux the issue is not specific to the userspace data channel: it reproduces with DCO as well, since accepting connections and the event loop stay in userspace.

Affected platforms

Linux: reproduced, with both epoll and poll. The default limits are enough.
FreeBSD 14.5: reproduced (poll backend). The kernel also leaves the pending connection in the queue after a failed accept(), on a stock system the default descriptor limit is much higher (kern.maxfilesperproc), so it needs a lowered limit or a --max-clients close to it.
macOS: not affected, because XNU drops the pending connection when accept() fails.
Windows: not affected.

How to reproduce

Start a server with tcp-server listener, on Linux the default limits are enough (about 1000 connections are needed); a low limit such as ulimit -n 64 makes it quicker to reproduce.
Open TCP connections to the server port, without completing the handshake, until accept() fails with Too many open files.
Watch the CPU usage of the server process: it stays at 100% and the log fills with TCP: accept(...) failed: Too many open files (errno=24).

Patches are ready and will be sent to Gerrit.

Lingua principale
C
Stelle
14.8k
Fork
3.4k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OpenVPN/openvpn

Tutte le issue di OpenVPN/openvpn

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.