Best on a larger screen — the animation and controls have more room to breathe.
Inputs

TCP Connection Teardown

TCP connection teardown is shown here as the four-segment exchange (FIN, ACK, FIN, ACK) that gracefully closes an established TCP connection. Each direction of the connection closes independently: the active closer walks FIN_WAIT_1 → FIN_WAIT_2 → TIME_WAIT → CLOSED, the passive closer walks CLOSE_WAIT → LAST_ACK → CLOSED, and the closer that sent the first FIN lingers in TIME_WAIT for twice the maximum segment lifetime.

Ending a TCP connection works like ending a phone call: you say you are done talking, the other person acknowledges it, finishes what they were saying, says they are done, and you acknowledge that in turn. You then stay on the line briefly in case your last acknowledgment was lost; that extra wait is TIME_WAIT.

How TCP Connection Teardown Works

Opening a connection takes three segments; the close shown here takes four. The asymmetry comes from what a TCP connection is: two independent one-way byte streams (Client→Server and Server→Client) sharing one socket pair, with each stream closing on its own schedule.

One peer is the active closer (its application calls close() first; here that is the Client), and the other is the passive closer. The active closer sends a FIN ("my stream is finished") and the passive closer acknowledges it immediately. Because the passive closer's own stream may still have buffered data or a response being generated, its ACK and its own FIN are two separate segments, possibly far apart in time. That gap is the half-close: one pipe shut, the other still flowing. The handshake fuses SYN and ACK because the server's own SYN is ready the moment it acknowledges the client's. The teardown's middle ACK and FIN share a segment only when the passive closer is already done sending, giving FIN, FIN+ACK, ACK; when it still has data, as here, they stay apart and the close takes four segments.

Each state change is driven by an endpoint event: a local close() that sends a FIN, the receipt of a segment, or a timer expiring. The active closer descends ESTABLISHED → FIN_WAIT_1 → FIN_WAIT_2 → TIME_WAIT → CLOSED; the passive closer descends ESTABLISHED → CLOSE_WAIT → LAST_ACK → CLOSED. Together, the two ladders trace the full normal-close state machine.

The lesson uses symbolic sequence numbers (the Client's FIN carries seq = u, the Server's carries seq = w) rather than real 32-bit byte counters, and a FIN consumes one sequence number even though it carries no data. That is why the acknowledgments are u+1 and w+1, not u and w.

TCP Connection Teardown Step by Step

The default scenario is the Normal close, and it runs in thirteen beats:

  1. Both peers sit in ESTABLISHED. The Client's application calls close(); Client becomes the active closer, Server the passive one.
  2. The Client sends segment 1: FIN, seq = u. Client: ESTABLISHED → FIN_WAIT_1.
  3. The Server receives the FIN and now knows the Client will send no more data. Server: ESTABLISHED → CLOSE_WAIT.
  4. The Server sends segment 2: ACK, ack = u+1, acknowledging the FIN right away.
  5. The Client receives that ACK. Client: FIN_WAIT_1 → FIN_WAIT_2. The Client→Server pipe is now closed.
  6. Half-close. The Server may still have data to deliver while the Client only listens. Its ACK (already sent) and its FIN (not yet sent) stay separate; this beat is why the close takes four segments.
  7. The Server finishes sending and emits segment 3: FIN, seq = w, ack = u+1. Server: CLOSE_WAIT → LAST_ACK.
  8. The Client receives the Server's FIN. Client: FIN_WAIT_2 → TIME_WAIT.
  9. The Client sends segment 4, the final ACK, ack = w+1.
  10. The Server receives it. Server: LAST_ACK → CLOSED. The Server is finished and reaches CLOSED before the Client.
  11. The Client lingers in TIME_WAIT for 2·MSL, ready to re-acknowledge a retransmitted FIN and letting any stray duplicates of this connection's segments expire in the network.
  12. The timer elapses. Client: TIME_WAIT → CLOSED.
  13. A closing beat holds both ladders at CLOSED. Four segments plus the TIME_WAIT hold end the connection.

On the canvas, each segment is a chip crossing the channel between the two lifelines, and every state change drops a badge onto its peer's ladder as it happens; older rungs fade so the current state stands out. The packet in flight and the TIME_WAIT countdown run at slow-motion beats so you can read the seq/ack arithmetic mid-crossing.

Why TIME_WAIT Exists

Once both FINs have been sent and acknowledged, TIME_WAIT can look like dead air. The Client stays in TIME_WAIT because a pure ACK is not itself acknowledged, including this final ACK, so the Client cannot know it arrived.

Switch the scenario selector to Lost final ACK to watch the failure that justifies the wait. The Client sends ACK, ack = w+1, and the network drops it. The Server, still in LAST_ACK, hears nothing; its retransmission timer fires and it re-sends FIN, seq = w. Because the Client is still in TIME_WAIT, it re-ACKs with ack = w+1 and restarts its 2·MSL timer. The Server receives the ACK this time and moves to CLOSED; the Client serves out its linger and closes too. Both reach the same ending after one extra round trip.

Now consider the counterfactual. If the Client had closed immediately after beat 9, the retransmitted FIN would arrive at a socket that no longer exists, and the Client's stack would answer with a RST instead of an ACK. The Server would end on an error rather than a clean close. The 2·MSL figure (twice the maximum segment lifetime) bounds how long a segment can survive in the network; it does not bound when the Server's retransmission timer fires. A late retransmitted FIN is covered because each one that arrives during TIME_WAIT restarts the timer. Waiting the full interval also lets old segments from this connection expire before the same socket pair is reused, although some stacks reuse the pair earlier under extra checks such as timestamps.

Edge Cases

  • Lost final ACK. In the selectable second scenario traced above, the Server retransmits its FIN from LAST_ACK, and the Client re-ACKs from TIME_WAIT and restarts the timer.
  • Simultaneous close. Both applications call close() at once; each side sends a FIN before seeing the other's and goes FIN_WAIT_1 → CLOSING → TIME_WAIT. This case is not animated here because there is no passive closer to contrast against.
  • FIN_WAIT_2 starvation. If the passive side never sends its FIN, the active closer would wait forever in FIN_WAIT_2; real stacks may apply cleanup timeouts, especially after the application has abandoned the connection; the exact policy depends on the implementation and socket state.
  • Abortive close. A RST skips the four-way exchange: there is no half-close or TIME_WAIT, and any in-flight data is discarded.

Common Mistakes

  • Expecting three segments, like the handshake. The middle ACK and the second FIN cannot merge in general because the passive closer may still be sending. The half-close window is why teardown normally takes four segments rather than three.
  • Believing both sides reach CLOSED together. The passive closer is done the instant the final ACK lands (beat 10); the active closer holds TIME_WAIT for 2·MSL first. In this normal-close scenario, the Server finishes before the Client.
  • Treating TIME_WAIT as waste. It is what prevents a lost final ACK from ending in a RST-terminated close; it is also why servers that actively close thousands of connections accumulate sockets in TIME_WAIT.
  • Putting TIME_WAIT on the wrong peer. In this normal-close scenario it belongs to the active closer, not the passive peer. Simultaneous close can put both endpoints in TIME_WAIT.
  • Mixing up the acknowledgment arithmetic. A FIN consumes one sequence number despite carrying no data, so the ACKs are u+1 and w+1, not u and w.

A Note on Simplification

This model simplifies several details: symbolic u/w instead of real byte counters, no data segments interleaved, and timers shown as single beats. RFC 9293 specifies the full state machine.