A server handles a burst of short requests, an admin runs ss -tan, and the screen fills with hundreds of connections in a state called TIME-WAIT. The next restart of the service then fails with “Address already in use,” or a client that makes many quick connections starts throwing “Cannot assign requested address.” Both errors trace back to the same deliberate pause built into TCP. Tests in an isolated Linux network namespace on kernel 6.18 show which side of a connection holds that pause, how long it lasts, and which of the commonly suggested fixes do anything.
What TIME_WAIT is for
A deliberate wait after closing
RFC 9293, the 2022 consolidated specification of TCP, describes TIME-WAIT as “waiting for enough time to pass to be sure the remote TCP peer received the acknowledgment” of its close request, and “to avoid new connections being impacted by delayed segments from previous connections.” RFC 9293
Both jobs matter. If the last acknowledgment of a close gets lost, the other side will resend its FIN, and someone has to be around to answer it. And if a brand-new connection reused the exact same pair of addresses and ports a moment later, a stray packet from the old connection still wandering the network could be mistaken for new data.
How long the standard says
Section 3.6.1 of the RFC states that when a connection is closed actively, “it MUST linger in the TIME-WAIT state for a time 2xMSL.” MSL, the maximum segment lifetime, is defined in section 3.4.2: “For this specification the MSL is taken to be 2 minutes,” which the document calls “an engineering choice” that could change with experience. Twice two minutes is four minutes.

Which side ends up in TIME_WAIT
The side that closes first
The key word in the RFC is “actively.” Only the endpoint that sends the first FIN, the active closer, enters TIME-WAIT. The other side passes through different states and finishes almost immediately.
The test set up a small Python server and client on the loopback interface inside a fresh network namespace, then ran 1,000 short connections two ways:
- Client closes first: 999 TIME-WAIT sockets, all on the client side, zero on the server.
- Server closes first: 999 TIME-WAIT sockets on the server side, holding the server’s port 9001 as their local port.
Why it matters for servers
A web server that closes connections itself after each response keeps a TIME-WAIT socket for every one of them. A client that closes first takes that cost on its own side instead. HTTP keep-alive and connection pooling reduce the number of closes altogether, which is the most effective fix of all because there is simply less to wait on.
How long it lasts on Linux
Measuring it
Linux does not use the RFC’s four minutes. The test opened 200 connections with the client closing first, then counted TIME-WAIT sockets every few seconds:
t=5s TIME_WAIT=200
t=40s TIME_WAIT=200
t=58s TIME_WAIT=200
t=61s TIME_WAIT=200
t=63s TIME_WAIT=0
All 200 disappeared between 61 and 63 seconds after closing. On this kernel the wait was about 60 seconds, with a little timer granularity.
The tcp_fin_timeout myth
A large number of tuning guides tell administrators to lower net.ipv4.tcp_fin_timeout to shorten TIME-WAIT. The measurement above was taken with tcp_fin_timeout already set to 5 seconds. It made no difference.
The kernel’s own documentation explains why. That setting is “The length of time an orphaned (no longer referenced by any application) connection will remain in the FIN_WAIT_2 state before it is aborted at the local end.” Linux kernel documentation, IP Sysctl FIN_WAIT_2 is a different state, the one a connection sits in while waiting for the other side’s FIN. The TIME-WAIT duration on Linux is not controlled by it.

Error one: Address already in use
The restart failure
After the server-closes-first run, 999 TIME-WAIT sockets still had port 9001 as their local port. The server process was then stopped and restarted on the same port. Without any socket option, bind() failed:
bind: [Errno 98] Address already in use
With SO_REUSEADDR set before binding, the same restart worked immediately, while the TIME-WAIT sockets were still present.
What SO_REUSEADDR allows
The Linux socket manual page describes the option as allowing “reuse of local addresses,” and for IPv4: “a socket may bind, except when there is an active listening socket bound to the address.” socket(7) Leftover TIME-WAIT sockets are not an active listener, so the bind succeeds. A second copy of a running server still cannot grab the same port.
That is why most server frameworks set this option by default. A hand-written server in Python, C or Go that lacks it will hit the error after any restart that follows real traffic, usually for about a minute, which is exactly the TIME-WAIT period measured above.
Error two: Cannot assign requested address
Running out of ports
Every outgoing connection needs a local port, and a client that closes first holds each port in TIME-WAIT for that minute. The kernel documentation gives the default range for those ports as 32768 to 60999, roughly 28,000 ports. A client hammering one server address and port at more than about 470 new connections per second, every second, will run through that range within the minute.
To show the effect without generating that much traffic, the test shrank the range inside the namespace to 100 ports, 40000 to 40099. The client opened 100 connections and closed each one. On the next attempt it failed:
ok 100 err [Errno 99] Cannot assign requested address after 100
Two seconds later, with tcp_tw_reuse off, a second batch could not open a single connection.
More destinations, more ports
The limit applies per destination, not per machine. With the same 100-port range and reuse turned off, a second server was started on port 9401 next to the one on 9400. The client exhausted its ports against 9400 after 100 connections, as before, and then opened another 100 to 9401 without any trouble. The TIME-WAIT list afterward showed each local port, such as 40099 and 40098, appearing twice, once for each destination.
This is because a TCP connection is identified by four values: local address, local port, remote address and remote port. Two connections can share a local port as long as something else in the four differs. A proxy or load balancer that talks to a single backend address and port is the worst case, since every connection competes for the same pool. Adding backend ports, backend addresses or extra source addresses on the client multiplies the pool, which is often the most practical fix for a proxy that keeps hitting error 99.
What tcp_tw_reuse does
The kernel setting net.ipv4.tcp_tw_reuse will “Enable reuse of TIME-WAIT sockets for new connections when it is safe from protocol viewpoint.” Its values are 0 to disable, 1 for global enable, and 2 to enable for loopback traffic only, which is the default. The documentation adds that it “should not be changed without advice/request of technical experts.”
Repeating the test with tcp_tw_reuse=1 changed the second batch’s result completely. All 100 connections succeeded, reusing ports still listed in TIME-WAIT.
The one-second catch
The first attempt to test reuse failed, and the reason is in the same documentation. A setting called tcp_tw_reuse_delay sets “The delay in milliseconds before a TIME-WAIT socket can be reused,” with a default of 1,000. The original test opened its second batch within milliseconds of the first, so no TIME-WAIT socket was old enough to reuse, and all three settings (0, 1 and 2) failed identically. Only after waiting two seconds did reuse work. A client that exhausts its ports in a tight burst will not be rescued by this setting.

What to change and what to leave alone
Fixes that work
- Keep connections open. HTTP keep-alive, database connection pools and persistent gRPC channels cut the number of closes, and therefore TIME-WAIT sockets, by orders of magnitude.
- Set
SO_REUSEADDRon listening sockets so restarts are not blocked. - On busy clients, widen
ip_local_port_rangeor spread connections across more destination addresses or ports, since each distinct destination gets its own set of local ports. - Consider
tcp_tw_reuse=1for outbound-heavy hosts, mindful of the kernel’s advice and the one-second delay.
Settings to leave alone
tcp_fin_timeout does not shorten TIME-WAIT, as measured above. tcp_max_tw_buckets caps how many TIME-WAIT sockets the system holds, and beyond it the kernel destroys them immediately and prints a warning; its documentation is blunt that administrators “must not lower the limit artificially,” and that the limit exists only to prevent simple denial-of-service attacks. The default on the test VM was 32,768.
Reading ss output
ss -tan state time-wait lists only the waiting sockets, and ss -s prints a one-line summary including a TIME-WAIT count. A few thousand on a busy web server is normal and costs very little memory. The number only matters when it lines up with one of the two errors above. To see where they point, ss -tan state time-wait | awk 'NR>1{print $4}' | sort | uniq -c | sort -rn | head counts the waiting sockets by remote address and port; one destination dominating the list is the signature of the port-exhaustion problem.
Sources
- RFC 9293, Transmission Control Protocol (TCP), August 2022: https://www.rfc-editor.org/rfc/rfc9293.html
- The Linux kernel documentation, IP Sysctl: https://docs.kernel.org/networking/ip-sysctl.html
- socket(7), Linux manual page: https://man7.org/linux/man-pages/man7/socket.7.html
