TIME-WAIT

term

networkingprogramming

Kept in this browser · My desk

The last state of a Transmission Control Protocol (TCP) connection on the side that closed first: the connection is finished, but its five-tuple is held for twice the Maximum Segment Lifetime before it can be used again.

TIME-WAIT is the insurance of the Transmission Control Protocol (TCP) against two failures: the peer never receiving the final acknowledgement, and a delayed segment from an old connection being accepted by a new one with the same addresses and ports. Request for Comments (RFC) 9293 requires the end that closes actively to stay in it for 2 x MSL (Maximum Segment Lifetime), and takes the MSL to be 2 minutes, so the specification's figure is 240 seconds. It costs memory and, more often, ports: a client or a proxy that opens and closes many short connections to one server holds each five-tuple for the whole period, which caps the rate of new connections to that destination. Seeing thousands of sockets in TIME-WAIT is usually normal; seeing connection failures because of them is a design problem, solved by who closes first, by longer-lived connections, by more addresses or ports, or by the timestamp-based reuse the standards allow.

What it is. Request for Comments (RFC) 9293, the specification of the Transmission Control Protocol (TCP) published in August 2022 as Internet Standard 7, lists the states a connection passes through, LISTEN, SYN-SENT, SYN-RECEIVED, ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, LAST-ACK and TIME-WAIT, plus the fictional CLOSED that stands for no connection, and defines TIME-WAIT as waiting for enough time to pass to be sure the remote peer received the acknowledgement of its connection termination request, and to avoid new connections being affected by delayed segments from previous connections. The state belongs to the end that closes first. In the normal close of section 3.6, the active closer goes through FIN-WAIT-1 and FIN-WAIT-2, receives the peer's FIN (finish) segment, acknowledges it and enters TIME-WAIT, while the peer, having passed through CLOSE-WAIT and LAST-ACK, goes straight to CLOSED on receiving that acknowledgement; in a simultaneous close both ends pass through CLOSING and both end in TIME-WAIT. The original specification of September 1981, RFC 793, already had the state and the same 2-minute lifetime.

How long, and why. Section 3.6.1 says that when a connection is closed actively it MUST linger in TIME-WAIT for 2 x MSL (Maximum Segment Lifetime), and section 3.4.2 takes the MSL to be 2 minutes, calling it an engineering choice that may change if experience shows it desirable. With the specification's numbers the wait is 240 seconds. Both reasons in the definition are about the network holding packets longer than the endpoints do. If the final acknowledgement is lost, the peer retransmits its FIN; an end still in TIME-WAIT can acknowledge it again, and the segment-arrival rules of section 3.10.7.4 say that the only thing that can arrive in this state is a retransmission of the remote FIN, to be acknowledged while the 2 MSL timeout restarts. And because RFC 9293 defines a connection by its pair of sockets, a new connection with the same addresses and ports is a new incarnation of the old one, so a stray segment from the old one could be accepted as valid data. Section 3.4.1 states the consequence in one line: TIME-WAIT limits the rate of connection reuse. When the timer expires, section 3.10.8 deletes the connection's control block and enters CLOSED.

The exceptions the standard allows. Section 3.6.1 permits an end in TIME-WAIT to accept a new SYN (synchronize) segment from the same peer and reopen the connection directly, provided it chooses an initial sequence number larger than any it used in the previous incarnation and goes back to TIME-WAIT if the SYN turns out to be an old duplicate. When TCP timestamps are in use, RFC 9293 points to the improved algorithm of RFC 6191 (April 2011, Best Current Practice 159, by Fernando Gont) and says it SHOULD be implemented, because timestamps are common and reducing TIME-WAIT benefits busy servers. Section 3.5.2 adds that the side issuing a reset should enter TIME-WAIT, citing the analysis of T. Faber, J. Touch and W. Yui at the Institute of Electrical and Electronics Engineers (IEEE) INFOCOM conference of March 1999 on the effect of TIME-WAIT on busy servers.

What it costs, worked. The cost is the five-tuple. A client whose ephemeral range is Linux's default, 32768 to 60999, has 28,232 source ports towards any one server address and port; if it opens short connections to that server and closes them first, each port is held for 240 seconds after the close, so the steady state allows 28,232 divided by 240, about 117.6 new connections a second, towards that destination. With the Windows range of 49152 to 65535 the figure is 16,384 divided by 240, about 68.3. RFC 6056 (January 2011) describes the reverse collision: when a web server performs the active close, it keeps the five-tuple in TIME-WAIT while the client has already forgotten it, and if the client reuses the same port for a new connection to the same server the attempt collides, with effects ranging from connection failures to TIME-WAIT assassination, the hazard named in RFC 1337. Operating systems choose their own effective durations and reuse rules, so these are the standard's numbers, not any product's.

In practice on Linux. ss -tan state time-wait lists the sockets in the state. The kernel's documentation describes the related controls: tcp_tw_reuse allows reusing TIME-WAIT sockets for new connections when safe from the protocol's point of view, with 0 to disable, 1 to enable globally and 2 for loopback traffic only, default 2, and a reuse delay, tcp_tw_reuse_delay, of 1,000 milliseconds by default; the older tcp(7) manual page still lists the option as disabled by default, so the two documents disagree and the running kernel's value is the one to read. tcp_max_tw_buckets caps the number of TIME-WAIT sockets, exists only to prevent simple denial-of-service attacks, and the documentation insists it must not be lowered artificially. tcp_tw_recycle, present from Linux 2.4 to 4.11, was not recommended because hosts behind Network Address Translation (NAT) may not send monotonically increasing timestamps. ip(7) adds that a bound TCP local address stays unavailable for some time after closing unless SO_REUSEADDR is set, which makes TCP less reliable.

What goes wrong. The most common misreading is tcp_fin_timeout: the kernel documentation defines it as the time an orphaned connection stays in FIN_WAIT_2 before it is aborted, not the duration of TIME-WAIT, and tcp(7) gives its default as 60 seconds. The second is treating a large TIME-WAIT count as a fault; it is the expected trace of many short connections, and becomes a problem only when the ports run out. Remedies follow from the arithmetic: keep connections open and reuse them, let the side with more ports to spare close first, add source addresses or destination ports, or rely on the timestamp-based reuse that RFC 6191 standardizes. Middleboxes that open server-side connections on behalf of many clients, source address translators and load balancers among them, meet the limit first.

Neighbours. The five-tuple is what TIME-WAIT holds; the ephemeral port is the field it ties up; the three-way handshake is how a new incarnation begins; the socket is the object the host keeps in the state.

Also known as: time_wait, timewait, time wait, 2msl, estado time-wait

Sources

← Back to T Back to the glossary