Why does an outbound proxy hit EADDRNOTAVAIL after 47 seconds?
Question
A Linux reverse proxy opens 600 short-lived TCP connections per second to one upstream backend (10.0.1.20:8080), closing each socket after one HTTP response. Traffic runs cleanly for 47 seconds, then outbound connect() fails with EADDRNOTAVAIL.
- Using Linux defaults (
ip_local_port_range = 32768..60999,TIME_WAIT = 60 s), prove why ports run out after ~47 seconds. - Why does TCP keep closed sockets in
TIME_WAITinstead of reusing ports immediately? - Write a helper that checks port exhaustion and sizes a keep-alive pool to cut
TIME_WAITchurn to zero.
# Linux defaults: # net.ipv4.ip_local_port_range = 32768 60999 # TCP_TIMEWAIT_LEN = 60 seconds # Incident traffic: 600 new connections/sec to 10.0.1.20:8080
Show hints (3)
- "COUNT LOCAL PORTS"
Ports
32768through60999inclusive give60999 - 32768 + 1 = 28,232local ports for one upstream(dst_ip, dst_port). - "MULTIPLY BY TIME WAIT"
Each closed socket stays in
TIME_WAITfor 60 seconds on Linux. Multiply600 conn/s * 60 sand compare with28,232. - "REUSE CONNECTIONS FIRST"
HTTP keep-alive with connection pooling stops per-request closes. Divide
600 req/sby per-connection throughput to size the pool.
Show checked explanation
Linux defaults give 28,232 ephemeral ports (32768..60999 inclusive) per upstream (dst_ip, dst_port), and Linux holds active-closed sockets in TIME_WAIT for 60 s. At 600 conn/s, steady state needs 36,000 ports, exhausting all 28,232 ports in 47.05 s. Enable HTTP keep-alive connection pooling (40 sockets at 15 req/s each gives 0 TIME_WAIT churn).
import math def port_budget(rate_rps, low=32768, high=60999, tw_sec=60): ports = high - low + 1 needed = rate_rps * tw_sec exhaust_sec = ports / rate_rps if needed > ports else None return ports, needed, exhaust_sec def pool_size(rate_rps, req_per_conn_sec=15): return math.ceil(rate_rps / req_per_conn_sec) ports, needed, secs = port_budget(600) print(f"ports={ports} needed={needed} exhaust={secs:.2f}s") print(f"keepalive_conns={pool_size(600, 15)} tw_churn=0")
ports=28232 needed=36000 exhaust=47.05s keepalive_conns=40 tw_churn=0
Ran the ephemeral-port exhaustion and keep-alive pool calculations on CPython 3.12.10 (Windows x64); Linux sysctl and TCP TIME_WAIT rules checked against docs.kernel.org, man7.org, and rfc-editor.org. Checked by an editor other than the author.
Walkthrough
TCP identifies a connection by its 4-tuple: (src_ip, src_port, dst_ip, dst_port). Because the proxy uses one source IP and calls one upstream 10.0.1.20:8080, only src_port can vary.
Linux's default net.ipv4.ip_local_port_range is 32768 to 60999 inclusive, giving 28,232 ports. When the proxy actively closes each connection first (sending the first FIN), the kernel places that 4-tuple into TIME_WAIT for 60 seconds (TCP_TIMEWAIT_LEN in Linux, acting as 2 * MSL).
- Sustainable short-lived rate:
28,232 / 60 = 470.53conn/s. - Needed ports at
600conn/s:600 * 60 = 36,000slots (7,768over budget). - Time to
EADDRNOTAVAIL:28,232 / 600 = 47.05seconds.
Why TIME_WAIT exists (RFC 9293). First, it absorbs delayed packets from the old connection so they cannot corrupt a new socket on the same 4-tuple. Second, if the proxy's final ACK gets lost, the upstream retransmits FIN and TIME_WAIT lets the proxy re-ACK cleanly instead of sending RST.
| mitigation | effect at 600 req/s | risk or limit |
|---|---|---|
HTTP keep-alive pool (40 conns) | 0 TIME_WAIT churn/s | Best fix; also cuts TCP and TLS handshakes |
Widen port range (1024..65535) | 64,512 ports (caps at 1,075 conn/s) | Buys headroom, still hits a ceiling under growth |
net.ipv4.tcp_tw_reuse = 1 | Reuses outbound TIME_WAIT after 1 s | Needs RFC 7323 timestamps; secondary safety net |
net.ipv4.tcp_tw_recycle | Removed in Linux 4.12 | Broke NAT clients with non-monotonic timestamps |
Enabling keep-alive pooling solves the root cause: 40 persistent sockets handling 15 req/s each carry 600 req/s with zero port exhaustion.
Common mistakes
- Recommending
net.ipv4.tcp_tw_recycle = 1. Linux removed it in kernel 4.12 because per-IP timestamp checks dropped legitimate connections from clients behind NAT. - Thinking
TIME_WAITports are a global limit across all destinations. The limit is per(src_ip, dst_ip, dst_port)tuple; talking to two upstreams doubles available 4-tuples. - Confusing
SO_REUSEADDRwith outbound port reuse.SO_REUSEADDRlets a listening server rebind a port inTIME_WAIT, not an outbound client reuse an identical 4-tuple. - Widening
ip_local_port_rangewithout fixing keep-alive. Jumping to64,512ports only shifts the crash from470conn/s to1,075conn/s and still wastes CPU on handshakes.
Follow-up questions
- "How do you confirm
TIME_WAITexhaustion on a live Linux host?" Runss -sto check thetimewaitsocket total, andss -tan state time-wait dst 10.0.1.20:8080 | wc -lto count sockets pinned to that upstream againstsysctl net.ipv4.ip_local_port_range. - "Why does
tcp_tw_reusestay safe wheretcp_tw_recyclebroke NAT?"tcp_tw_reuseapplies only to outbound client connections and checks RFC 7323 TCP timestamps per connection after at least one second.tcp_tw_recycleaffected inbound sockets and tracked one timestamp per remote IP, dropping packets from peers sharing a NAT IP. - "What if your proxy truly must open over 1,500 short-lived connections per second to one upstream?" Bind outbound sockets across multiple local source IP addresses (using
IP_BIND_ADDRESS_NO_PORTso the kernel still picks the port), or spread traffic across multiple upstream IP:port endpoints. Each unique(src_ip, dst_ip, dst_port)gets its own full ephemeral port range.
What a strong answer covers
Computes 28,232 ports and 47.05 s to exhaustion at 600 conn/s, and explains TIME_WAIT's two RFC 9293 roles. Fixes the root cause with keep-alive pooling and warns that tcp_tw_recycle was removed in Linux 4.12.
Open in interactive practice deck →