download

Why does an outbound proxy hit EADDRNOTAVAIL after 47 seconds?

Systems~10 minReviewed 3 Dec 2026Free · No sign-in

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.

  1. Using Linux defaults (ip_local_port_range = 32768..60999, TIME_WAIT = 60 s), prove why ports run out after ~47 seconds.
  2. Why does TCP keep closed sockets in TIME_WAIT instead of reusing ports immediately?
  3. Write a helper that checks port exhaustion and sizes a keep-alive pool to cut TIME_WAIT churn to zero.
"CODE · PYTHON"
# 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)
  1. "COUNT LOCAL PORTS"

    Ports 32768 through 60999 inclusive give 60999 - 32768 + 1 = 28,232 local ports for one upstream (dst_ip, dst_port).

  2. "MULTIPLY BY TIME WAIT"

    Each closed socket stays in TIME_WAIT for 60 seconds on Linux. Multiply 600 conn/s * 60 s and compare with 28,232.

  3. "REUSE CONNECTIONS FIRST"

    HTTP keep-alive with connection pooling stops per-request closes. Divide 600 req/s by 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).

"SOLUTION · PYTHON"
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")
"OUTPUT"
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.53 conn/s.
  • Needed ports at 600 conn/s: 600 * 60 = 36,000 slots (7,768 over budget).
  • Time to EADDRNOTAVAIL: 28,232 / 600 = 47.05 seconds.

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.

mitigationeffect at 600 req/srisk or limit
HTTP keep-alive pool (40 conns)0 TIME_WAIT churn/sBest 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 = 1Reuses outbound TIME_WAIT after 1 sNeeds RFC 7323 timestamps; secondary safety net
net.ipv4.tcp_tw_recycleRemoved in Linux 4.12Broke 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_WAIT ports 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_REUSEADDR with outbound port reuse. SO_REUSEADDR lets a listening server rebind a port in TIME_WAIT, not an outbound client reuse an identical 4-tuple.
  • Widening ip_local_port_range without fixing keep-alive. Jumping to 64,512 ports only shifts the crash from 470 conn/s to 1,075 conn/s and still wastes CPU on handshakes.

Follow-up questions

  • "How do you confirm TIME_WAIT exhaustion on a live Linux host?" Run ss -s to check the timewait socket total, and ss -tan state time-wait dst 10.0.1.20:8080 | wc -l to count sockets pinned to that upstream against sysctl net.ipv4.ip_local_port_range.
  • "Why does tcp_tw_reuse stay safe where tcp_tw_recycle broke NAT?" tcp_tw_reuse applies only to outbound client connections and checks RFC 7323 TCP timestamps per connection after at least one second. tcp_tw_recycle affected 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_PORT so 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 →

Original OfferVibe question. Linux IP Sysctl · tcp(7) · RFC 9293: TCP · RFC 7323: TCP Timestamps.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.