basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    TIME-WAIT in ss is a normal TCP timer, not a socket leak

    Hundreds or thousands of TIME-WAIT rows on a reverse proxy or busy server are normal cleanup for short-lived TCP connections: the side that closes first holds the entry for 60 seconds. An actual application leak piles up in a different state — CLOSE-WAIT.

    Why TIME-WAIT appears and how it differs from CLOSE-WAIT

    When a client or proxy calls close() first and sends FIN, the kernel keeps the 5-tuple entry in TIME-WAIT for 60 seconds (2×MSL). That prevents delayed packets from the old session from landing in a new connection on the same ports and ensures the peer can receive a retransmitted final ACK (TCP: flags and states).

    In Linux, this interval is compiled into the kernel (60 seconds) — net.ipv4.tcp_fin_timeout controls FIN-WAIT-2, unrelated to TIME-WAIT.

    Property TIME-WAIT CLOSE-WAIT
    Who closed first Local side sent FIN first Remote peer sent FIN, local program never called close()
    Holds a file descriptor (ulimit -n) No (only a compact kernel struct) Yes — growth crashes the process with Too many open files
    Clears on its own Yes, after exactly 60 seconds No, stays stuck until process restart
    What it signals High rate of short-lived connections Socket leak in application code

    When TIME-WAIT hurts and how to fix it

    On an inbound web server port (443), thousands of TIME-WAIT entries cause no trouble: clients arrive from different IPs and ports, and each TIME-WAIT struct takes a couple hundred bytes of RAM.

    Trouble starts only on the outbound side — for example when a reverse proxy opens hundreds of short requests per second to the same backend 10.0.0.10:8080. The kernel has one ephemeral port pool, net.ipv4.ip_local_port_range = 32768 60999 (28,232 ports): above roughly 470 new connections per second to a single IP:port pair, ports run out and calls fail with Cannot assign requested address.

    The proper fix is to cut down reconnect churn rather than copying random sysctl snippets:

    • turn on keep-alive and connection pooling between proxy and backend (keepalive inside nginx upstream, a shared HTTP client and DB connection pool inside your app);
    • if TIME-WAIT sockets target a local loopback address, Ubuntu 24.04 already ships with net.ipv4.tcp_tw_reuse = 2 (allowing reuse on loopback); tcp_tw_recycle from older guides was removed back in Linux 4.12 because it broke clients behind NAT.

    How to check

    Count socket states and watch the live countdown timer:

    ss -s
    # TCP:   40 (estab 5, closed 24, orphaned 0, timewait 7)
    
    ss -tno state time-wait | head -5
    # Recv-Q Send-Q Local Address:Port Peer Address:Port Process
    # 0      0        172.18.0.1:54192   172.18.0.2:8080 timer:(timewait,57sec,0)
    
    sudo ss -tnp state close-wait

    If the TIME-WAIT timers count down toward zero and close-wait is empty, nothing is leaking.

    Also in this chapter