How does TCP session termination work?
A four-way exchange: FIN → ACK → FIN → ACK. Each direction is closed independently, so it needs four segments (vs. three for setup); either host can initiate, and the two middle steps can sometimes combine into one FIN+ACK.
* FIN, ACK, FIN, ACK: each direction is closed independently, so teardown takes four segments. *
TCP Session Termination (Four-Way Handshake):
Host A Host B
│ │
│──────── FIN ─────────────────────→│ Step 1: A has no more data
│ │
│←─────── ACK ──────────────────────│ Step 2: B acknowledges FIN
│ │
│←─────── FIN ──────────────────────│ Step 3: B has no more data
│ │
│──────── ACK ─────────────────────→│ Step 4: A acknowledges FIN
│ │
│ Connection Closed │
Four Steps:
| Step | Action | Purpose |
|---|---|---|
| 1 | Client sends FIN | "I have no more data to send" |
| 2 | Server sends ACK | Acknowledges client's FIN |
| 3 | Server sends FIN | "I have no more data to send" |
| 4 | Client sends ACK | Acknowledges server's FIN |
Note: Steps 2 and 3 can occur in the same segment (FIN+ACK combined).
Key insight: Either host can initiate termination. Both directions must be closed independently.
Why four segments instead of three? During setup, the server can combine SYN+ACK because both sides agree to open at once. During teardown, when one side sends FIN the other may still have data to send, so its ACK and its own FIN are often separate - giving four steps: "I'm done" → "OK" → "Me too" → "OK".
Go deeper:
TCP Connection Termination — Four-Way Handshake — walks through the FIN / ACK / FIN / ACK exchange that closes each direction independently.
RFC 9293 §3.6 — Closing a Connection — the authoritative teardown sequence, including the TIME-WAIT state.
TCP — Connection termination — Wikipedia's four-way handshake summary for the carousel.