The TCP State Machine and TIME_WAIT
The eleven TCP states and the events that move a connection between them, watched live with ss on both ends of a real connection; why the active closer waits in TIME_WAIT, the quiet time rule, FIN_WAIT_2 timeouts, simultaneous close, and how Linux lets a new connection reuse a TIME_WAIT 4-tuple.
kvi5hnuCode Sprout team member14 min readOn this page
Articles 59 and 60 followed the segments of a connection. Each endpoint also keeps a state for the connection, which decides how it reacts to the next segment or application call. RFC 9293 defines the states and the transitions between them as a state machine. Knowing it lets you read ss output (and that of netstat, the older tool ss replaced on Linux), explain stuck connections, and understand the most misunderstood state of all, TIME_WAIT.
The states
The diagram below is the whole state machine: each box is a state, and each arrow is labeled with the event (an application call or an arriving segment) that moves a connection along it. The table after it gives each state's meaning.

| State | Meaning |
|---|---|
| CLOSED | No connection. Not really a state, but where every connection starts and ends. |
| LISTEN | A server socket waiting for SYNs. |
| SYN-SENT | Sent a SYN, waiting for a SYN-ACK. |
| SYN-RECEIVED | Received a SYN and answered with a SYN-ACK, waiting for the ACK. |
| ESTABLISHED | The connection is open and data flows both ways. |
| FIN-WAIT-1 | This side closed and sent a FIN, waiting for its ACK. |
| FIN-WAIT-2 | The FIN was acknowledged, waiting for the peer's FIN. |
| CLOSE-WAIT | Received the peer's FIN, waiting for the local application to close. |
| LAST-ACK | Closed after the peer did, waiting for the ACK of this side's FIN. |
| CLOSING | Both sides sent FINs at the same time, waiting for the ACK of this side's FIN. |
| TIME-WAIT | Done, but waiting before forgetting the connection. |
The left column of the diagram is the active closer, usually the client, and the right is the passive closer. Which side is which depends only on who calls close() first. A web server that closes idle keep-alive connections is the active closer for those connections.
Watching the states change
In the lab, a script ran ss every few milliseconds on both hosts while a connection ran through its whole life, with 25 ms of delay each way. The client sent a request, received a reply, and closed 200 ms later. The server closed 300 ms after it got the client's FIN.
ss uses short names for some states: ESTAB for ESTABLISHED and SYN-RECV for SYN-RECEIVED. What the client's ss reported:
16 ms to 62 ms SYN-SENT
67 ms to 314 ms ESTAB
319 ms to 408 ms FIN-WAIT-1
412 ms to 667 ms FIN-WAIT-2
672 ms to 1400 ms TIME-WAIT
What the server's ss reported:
43 ms to 89 ms SYN-RECV
94 ms to 339 ms ESTAB
346 ms to 641 ms CLOSE-WAIT
646 ms to 689 ms LAST-ACK
The chart below draws both lists as bars on one time axis, so you can see which client state lines up with which server state.

Every row of the diagram shows up:
- The client was in SYN-SENT for one round trip, and the server was in SYN-RECV from receiving the SYN until the client's ACK arrived.
- After the client's
close(), it spent about 90 ms in FIN-WAIT-1. That's a round trip, plus the server's delayed ACK of the FIN (article 68). - The client sat in FIN-WAIT-2 while the server sat in CLOSE-WAIT, for as long as the server's application waited before closing: 300 ms here.
- The server's LAST-ACK lasted one trip of its FIN to the client and the ACK back.
- The server's socket then disappeared. The client's moved to TIME-WAIT, where it stays for 60 seconds.
TIME_WAIT
The active closer doesn't forget a connection when the final exchange ends. It stays in TIME-WAIT for twice the Maximum Segment Lifetime (2 MSL). The MSL is the longest time a segment is assumed to survive in the network. The original specification set it to 2 minutes, while Linux uses a fixed 60 seconds for the whole TIME-WAIT, which isn't configurable.
In the lab, the server did the active close and then listed its TIME-WAIT socket with its timer (with a state filter, ss leaves out the State column):
0 0 198.51.100.10:8080 192.0.2.10:46858 timer:(timewait,59sec,0)
The two 0 columns are the receive and send queues, then come the local and remote address. timer:(timewait,59sec,0) names the timer that's running, the time left on it, and how many times it has fired (retransmissions). TIME-WAIT has two jobs.
- Deliver the final ACK reliably. The last segment of a close is the active closer's ACK of the peer's FIN. If it's lost, the peer resends its FIN. A host in TIME-WAIT still knows the connection and can ACK the FIN again. A host that had forgotten it would answer with a reset, and the peer's application would see an error on a connection that closed normally.
- Let old segments die. While the 4-tuple is held in TIME-WAIT, no new connection with the same addresses and ports can start, so a delayed segment from the old connection can't be accepted by a new one. Waiting 2 MSL means any such segment, and any reply to it, has expired.
Only one side pays: the active closer. That's why busy clients, proxies and load balancers that open many short outgoing connections accumulate thousands of TIME-WAIT sockets. A TIME-WAIT socket uses a small amount of kernel memory and no file descriptor, so large numbers are normal. Linux caps them with net.ipv4.tcp_max_tw_buckets and removes extras early, with a log warning.
Restarting a server that holds TIME-WAIT sockets
If a server closes connections itself, its listening port also appears in the TIME-WAIT sockets' 4-tuples. A restarted server then may not be able to bind the port. In the lab, right after the TIME-WAIT above:
server (noreuse): bind() failed: [Errno 98] Address already in use
server (reuse): bind() OK
Without SO_REUSEADDR, bind() failed with "Address already in use", even though nothing was listening. With SO_REUSEADDR, it succeeded. Almost every server sets SO_REUSEADDR on its listening socket for this reason. It doesn't allow two programs to listen on the same port at once; that's what SO_REUSEPORT is for (article 63).
Reusing a TIME-WAIT 4-tuple for a new outgoing connection
Can a client open a new connection with exactly the same 4-tuple as one still in TIME-WAIT? In the lab, a client bound a fixed local port, connected, closed (leaving TIME-WAIT), and tried the same 4-tuple again 300 ms later. The test ran with and without timestamps, and with and without SO_REUSEADDR on the client:
timestamps=1 SO_REUSEADDR=1
first: connect() OK
before 2nd: ['TIME-WAIT']
second: connect() OK
after 2nd: ['FIN-WAIT-1']
timestamps=1 SO_REUSEADDR=0
first: connect() OK
before 2nd: ['TIME-WAIT']
second: bind() failed: [Errno 98] Address already in use
after 2nd: ['TIME-WAIT']
timestamps=0 SO_REUSEADDR=1
first: connect() OK
before 2nd: ['TIME-WAIT']
second: connect() failed: [Errno 99] Cannot assign requested address
after 2nd: ['TIME-WAIT']
timestamps=0 SO_REUSEADDR=0
first: connect() OK
before 2nd: ['TIME-WAIT']
second: bind() failed: [Errno 98] Address already in use
after 2nd: ['TIME-WAIT']
The four results:
- Without
SO_REUSEADDR,bind()itself refused the port, because it was held by the TIME-WAIT socket. - With
SO_REUSEADDRand timestamps, the new connection was allowed, and the TIME-WAIT socket was replaced. (Whensslooked again, only the new connection, already closing, remained.) Timestamps make this safe: with PAWS (article 60), segments from the old connection carry older timestamps and are rejected. This is the approach RFC 6191 describes. - With
SO_REUSEADDRbut no timestamps,connect()failed withEADDRNOTAVAIL, "Cannot assign requested address", because nothing would protect the new connection from the old one's segments.
The same rule decides what happens when a client runs out of ephemeral ports. When every port to a given server is held in TIME-WAIT, connect() fails with EADDRNOTAVAIL. The sysctl net.ipv4.tcp_tw_reuse controls whether Linux reuses TIME-WAIT sockets for ordinary outgoing connections that use ephemeral ports:
tcp_tw_reuse | Effect (for outgoing connections only, and only with timestamps) |
|---|---|
| 0 | Never reuse |
| 1 | Reuse a TIME-WAIT socket once it's at least 1 second old (adjustable with tcp_tw_reuse_delay, in milliseconds, since Linux 6.14) |
| 2 | Reuse only for loopback traffic (the default since Linux 4.19) |
An older option, tcp_tw_recycle, broke clients behind NAT and was removed in Linux 4.12. If you find advice to enable it, the advice is outdated. The best fix for TIME-WAIT pressure is usually at the application level: reuse connections (HTTP keep-alive, connection pools) instead of opening one per request.
The quiet time
TIME-WAIT protects new connections only as long as the host remembers the old ones. If a host crashes and reboots, its TIME-WAIT state is gone. The original specification therefore asked a rebooted host to wait one MSL before creating any connections, which is called the quiet time. Hardly any implementation does this: hosts take longer than that to reboot, and random ISNs (article 59) make collisions with old connections very unlikely anyway.
FIN-WAIT-2 and orphaned connections
FIN-WAIT-2 has no timer in the state machine: the active closer waits for the peer's FIN as long as it takes. If the peer's application never closes, the connection would wait forever. That's correct for a half-close, where the application still wants to read. But when the application has fully closed its socket, nobody will ever read that data.
Linux handles such orphaned connections with a timer: net.ipv4.tcp_fin_timeout, 60 seconds by default. In the lab, the client closed a connection whose server application kept its socket open without ever closing:
net.ipv4.tcp_fin_timeout = 60
after 1 s: FIN-WAIT-2 0 0 192.0.2.10:50740 198.51.100.10:7000 timer:(timewait,58sec,0)
after 30 s: FIN-WAIT-2 0 0 192.0.2.10:50740 198.51.100.10:7000 timer:(timewait,29sec,0)
after 59 s: FIN-WAIT-2 0 0 192.0.2.10:50740 198.51.100.10:7000 timer:(timewait,832ms,0)
after 62 s: (no socket)
The server side was still in CLOSE-WAIT throughout, with the client's FIN counted as 1 byte in its receive queue:
LISTEN 0 128 0.0.0.0:7000 0.0.0.0:*
CLOSE-WAIT 1 0 198.51.100.10:7000 192.0.2.10:50740
After 60 seconds, the client dropped the connection. It never reached TIME-WAIT. The timer:(timewait,...) in the output is how Linux implements the orphan's countdown internally. A half-closed socket the application still holds open isn't affected by this timer.
Simultaneous close
When both applications close at the same moment, each side sends its FIN while in FIN-WAIT-1. The FINs cross, each side receives a FIN before the ACK of its own, sends an ACK, and enters CLOSING. When the ACK of its own FIN arrives, it enters TIME-WAIT. In the lab, both sides called close() at the same instant with 25 ms of delay each way:
30 ms after close(): client: CLOSING server: CLOSING
130 ms after close(): client: TIME-WAIT server: TIME-WAIT
Both sides passed through CLOSING, and both ended in TIME-WAIT, because both did an active close. Simultaneous open is the mirror image: SYN-SENT to SYN-RECEIVED on both sides, as in article 59.
Common questions
Why can't TIME-WAIT just be shorter?
It can be, safely, only when something else protects new connections from old segments. Linux's 60 seconds is already shorter than the original 4 minutes. Timestamps and PAWS let Linux reuse a TIME-WAIT 4-tuple early, which is the safe way to get the same benefit.
Are thousands of TIME-WAIT sockets a problem?
Usually not. They use little memory and no file descriptors. They become a problem only when a client makes so many short connections to one server that it runs out of ephemeral ports, which shows up as EADDRNOTAVAIL errors. Reusing connections is the real fix.
Why do I see TIME-WAIT on my server and not on clients?
Because your server is closing connections first, for example an HTTP server closing idle keep-alive connections or after each response. The active closer always holds TIME-WAIT.
What's the difference between CLOSE-WAIT and TIME-WAIT?
CLOSE-WAIT means the peer closed and the local application hasn't, and it lasts until the application acts. TIME-WAIT means both sides closed and this side did so first, and it lasts a fixed 60 seconds on Linux. Many CLOSE-WAIT sockets point to an application bug. Many TIME-WAIT sockets are normal.
Does SO_REUSEADDR let two servers share a port?
Not on Linux for TCP. It lets a server bind a port that has TIME-WAIT connections, but a second listener still fails. SO_REUSEPORT lets several sockets, all with the option, listen on the same port and share incoming connections.
Can I kill a TIME-WAIT socket?
Not with ss -K on Linux, which closes only full sockets. You don't need to: it expires on its own. If one must go away immediately, sending it a valid RST would remove it, which is the TIME-WAIT assassination problem in article 62.
What's the 2 MSL in seconds?
It depends on the system. The original MSL was 2 minutes, making 2 MSL 4 minutes. Linux uses a fixed 60 seconds for the whole TIME-WAIT, and other systems choose their own values. Measure with ss -tano state time-wait, which shows the remaining time.
Summary
- RFC 9293 defines eleven states. The active closer passes through FIN-WAIT-1, FIN-WAIT-2 and TIME-WAIT, and the passive closer through CLOSE-WAIT and LAST-ACK.
- The lab's
sssampling showed every state on both sides of a real connection, with durations set by round trips, delayed ACKs and application pauses. - TIME-WAIT, held by the active closer, lets it re-acknowledge a lost final FIN and keeps old segments out of a new connection with the same 4-tuple. On Linux it lasts 60 seconds.
SO_REUSEADDRlets a server bind a port with TIME-WAIT connections. With timestamps, Linux even lets a new connection replace a TIME-WAIT 4-tuple. Without timestamps,connect()fails withEADDRNOTAVAIL.tcp_tw_reuse(default 2, loopback only) controls reuse for outgoing connections.tcp_tw_recycleis gone.- Orphaned FIN-WAIT-2 connections are dropped after
tcp_fin_timeout(60 s). CLOSE-WAIT lasts until the application closes. - Crossing FINs lead both sides through CLOSING into TIME-WAIT.
Practice questions
1. A server application closes every connection right after sending its response. On which machine do TIME-WAIT sockets accumulate?
Answer
On the server, because it does the active close. Each connection leaves a TIME-WAIT socket there for 60 seconds on Linux.
2. Why does the active closer, and not the passive closer, need TIME-WAIT?
Answer
The active closer sends the very last segment, the ACK of the peer's FIN. If that ACK is lost, the peer resends its FIN and needs a reply. The passive closer's last action is waiting for an ACK, which it can retransmit for with its FIN. It doesn't need to linger once the ACK arrives.
3. A server shows 3,000 sockets in CLOSE-WAIT that never go away. What's happening, and where is the fix?
Answer
Clients closed their sides, but the server application never called close() on those sockets. TCP doesn't time out CLOSE-WAIT. The fix is in the application: close the socket when recv() returns 0 or an error.
4. A load generator opens 50,000 short connections per minute to one server port from one IP address and starts getting EADDRNOTAVAIL. Explain the error and suggest two fixes.
Answer
Its connections close first, so each leaves a TIME-WAIT socket for 60 seconds, holding a 4-tuple. With the default ephemeral range of 32768 to 60999 (28,232 ports), the client runs out of free ports to that server. Fixes: reuse connections (keep-alive or pooling), enable net.ipv4.tcp_tw_reuse=1 (requires timestamps), widen ip_local_port_range, or use more client IP addresses.
5. In the lab's simultaneous close, why did both sides end in TIME-WAIT?
Answer
Both called close() before receiving the other's FIN, so both were active closers. Each passed through FIN-WAIT-1 and CLOSING, and each received the ACK of its own FIN, which leads to TIME-WAIT.
6. A client closes a connection with close(), and the server application hangs and never closes its socket. What happens to the client's side, with Linux defaults?
Answer
The client gets the server's ACK of its FIN and moves to FIN-WAIT-2. Because the client's socket is orphaned (fully closed by the application), Linux drops it after tcp_fin_timeout, 60 seconds, without entering TIME-WAIT. The server stays in CLOSE-WAIT until its application closes.
Previous: TCP Options: MSS, Window Scale, SACK, Timestamps, User Timeout, TCP-AO and Fast Open
Next: TCP Resets: Closed Ports, Aborts, Half-Open Connections and TIME-WAIT Assassination

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.