Sub topic 12
Closing a Connection and TCP States
This chapter assumes you know the three-way handshake, sequence numbers and acknowledgments, and the basic sockets calls. It covers how a TCP connection ends, the states a connection passes through, and the errors that cause trouble in practice.
Why closing takes four steps
A TCP connection carries two independent byte streams, one in each direction. Each direction is closed on its own. A side that has no more data to send sends a segment with the FIN flag set. The other side acknowledges it. The reverse direction stays open until that side also sends its own FIN.
This is called a half-close. It is why the client in the earlier example can call shutdown(socket.SHUT_WR) and still read the server's reply.
A full close usually takes four segments: FIN, ACK, FIN, ACK. The middle two can be merged into one FIN+ACK segment if the second side has nothing left to send.
The states
Each end of a connection is in one state at a time. The operating system tracks it for you.
| State | Meaning |
|---|---|
| LISTEN | A server socket is waiting for incoming connections. |
| SYN_SENT | The client has sent a SYN and is waiting for the reply. |
| SYN_RECEIVED | The server has received a SYN and sent SYN+ACK. |
| ESTABLISHED | The handshake is complete. Data flows both ways. |
| FIN_WAIT_1 | This side sent a FIN and has not yet seen an ACK for it. |
| FIN_WAIT_2 | The FIN was acknowledged. This side waits for the peer's FIN. |
| CLOSE_WAIT | The peer's FIN arrived. This side's application has not closed yet. |
| LAST_ACK | This side sent its own FIN after CLOSE_WAIT and waits for the final ACK. |
| TIME_WAIT | Both FINs are done. This side waits before the connection can be forgotten. |
| CLOSED | No connection exists. |
The side that closes first ends in TIMEWAIT. The side that closes second goes through CLOSEWAIT and LAST_ACK.
TIME_WAIT
The side that sends the last ACK stays in TIME_WAIT for twice the maximum segment lifetime (2 × MSL). Linux uses a fixed 60 seconds. It exists for two reasons:
- If the last ACK is lost, the peer resends its FIN. The waiting side can still answer it.
- Old duplicate segments from this connection expire in the network. They cannot be mistaken for data in a new connection that uses the same address and port pair.
TIMEWAIT is normal. It is the reason the server example sets `SOREUSEADDR before bind`. Without it, restarting a server that closed first can fail with "Address already in use" while old connections sit in TIME_WAIT.
Abrupt close with RST
A segment with the RST flag ends a connection immediately. There is no waiting and no handshake. A host sends RST when a segment arrives for a port with no listener, or when the application aborts. A peer that receives RST sees an error such as ConnectionResetError in Python. Unsent data is discarded.
Common errors
- Piles of CLOSE_WAIT sockets. The peer closed, but your program never called
close(). This is a bug in your application, not in the network. The OS keeps the socket in CLOSE_WAIT until you close it. - Many TIME_WAIT sockets. This is expected on a client or server that opens and closes many short connections. Reusing connections avoids it.
- Ignoring the empty read. When
recvreturnsb"", the peer has sent its FIN. Close your side too. - Assuming
close()delivers all data. Data already queued is normally sent. A close with unread incoming data can trigger an RST, which may discard what you sent.
You can watch states on Linux with ss -tan. The State column shows values like ESTABLISHED and TIME_WAIT.
Exercise
A server shows hundreds of connections stuck in CLOSE_WAIT. Which side has not closed its socket, and what is the likely cause?
Show answer
The server's own application has not closed. CLOSE_WAIT means the peer's FIN arrived and was acknowledged, but the local program never called close(). The likely cause is code that does not close sockets after recv returns an empty bytes object, or one that leaks sockets on error paths.
Exercise
Which side of a connection enters TIME_WAIT, and why does it wait?
Show answer
The side that closes first, which sends the last ACK. It waits so it can re-acknowledge a retransmitted FIN if its ACK was lost, and so old duplicate segments expire before the same address and port pair is reused.