You are paged: an API server listening on :8080 behind an L7 proxy starts failing after a few hours of uptime with accept4: too many open files in its log. On the server, ss -tan state close-wait '( sport = :8080 )' | wc -l returns about 9,000 and climbs steadily, while ss -tan state established | wc -l is a normal ~300. What is happening, and where does the fix live — kernel or application?
- These are TIME_WAIT sockets from normal connection churn and are harmless. TIME_WAIT is held by the side that closed first, expires after 60 s and holds no file descriptor; CLOSE-WAIT is the other side, holds an fd, and never expires on its own.
- Lower
net.ipv4.tcp_fin_timeoutso the kernel reaps CLOSE-WAIT sockets sooner.tcp_fin_timeoutbounds FIN_WAIT_2 on the side that closed first; CLOSE-WAIT has no timer at all, because the kernel is waiting for the application to call close(). - The proxy is leaking connections toward us; raise the server's
ulimit -nso the sockets fit. A higher limit only delays the same failure; the sockets are in CLOSE-WAIT because this process owns them and has not closed them, whatever the proxy did. - CLOSE-WAIT is the state of a socket whose peer has sent FIN while the local application has not yet called close(): the proxy closed idle or timed-out connections and the server never closed its end, so each socket still holds a file descriptor. This is an application leak — a handler that returns without closing, or a pool that never reaps peer-closed connections — and the fix is in the code or its idle handling, not in a sysctl. The kernel cannot close a socket on the application's behalf; CLOSE-WAIT accumulates only when our own process ignores EOF, and each one is a live file descriptor.
CLOSE-WAIT means our own process never called close() after the proxy hung up; fix the leak in the application, not a sysctl.
Closing a TCP connection is two half-closes. When the peer sends FIN, our kernel acknowledges it, moves the socket to CLOSE-WAIT, and delivers end-of-file to the application (read() returns 0). The state ends only when the application calls close(), which sends our FIN and moves through LAST-ACK to CLOSED. Nothing in the kernel does that for us, so a growing CLOSE-WAIT count is by definition a file-descriptor leak in our own process — typically a handler that stops reading a connection the proxy has already timed out and closed. Each leaked fd counts against RLIMIT_NOFILE; when the limit is reached, accept() fails with EMFILE and the listener stops serving anyone. Confirm ownership with ss -tanp state close-wait (the PID) and ls /proc/<pid>/fd | wc -l; mitigate with a restart if you must, but the fix is in the code path that ignores EOF.
Reaching for a sysctl — tcp_fin_timeout, tcp_tw_reuse — or raising ulimit -n. No kernel timer clears CLOSE-WAIT; the only thing that does is close() in the application, and a higher fd limit just moves the crash later in the day.