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:
- Both peers sit in
ESTABLISHED. The Client's application callsclose(); Client becomes the active closer, Server the passive one. - The Client sends segment 1:
FIN, seq = u. Client:ESTABLISHED → FIN_WAIT_1. - The Server receives the
FINand now knows the Client will send no more data. Server:ESTABLISHED → CLOSE_WAIT. - The Server sends segment 2:
ACK, ack = u+1, acknowledging theFINright away. - The Client receives that
ACK. Client:FIN_WAIT_1 → FIN_WAIT_2. The Client→Server pipe is now closed. - Half-close. The Server may still have data to deliver while the Client only listens. Its
ACK(already sent) and itsFIN(not yet sent) stay separate; this beat is why the close takes four segments. - The Server finishes sending and emits segment 3:
FIN, seq = w, ack = u+1. Server:CLOSE_WAIT → LAST_ACK. - The Client receives the Server's
FIN. Client:FIN_WAIT_2 → TIME_WAIT. - The Client sends segment 4, the final
ACK, ack = w+1. - The Server receives it. Server:
LAST_ACK → CLOSED. The Server is finished and reachesCLOSEDbefore the Client. - The Client lingers in
TIME_WAITfor2·MSL, ready to re-acknowledge a retransmittedFINand letting any stray duplicates of this connection's segments expire in the network. - The timer elapses. Client:
TIME_WAIT → CLOSED. - A closing beat holds both ladders at
CLOSED. Four segments plus theTIME_WAIThold 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
FINfromLAST_ACK, and the Client re-ACKs fromTIME_WAITand restarts the timer. - Simultaneous close. Both applications call
close()at once; each side sends aFINbefore seeing the other's and goesFIN_WAIT_1 → CLOSING → TIME_WAIT. This case is not animated here because there is no passive closer to contrast against. FIN_WAIT_2starvation. If the passive side never sends itsFIN, the active closer would wait forever inFIN_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
RSTskips the four-way exchange: there is no half-close orTIME_WAIT, and any in-flight data is discarded.
Common Mistakes
- Expecting three segments, like the handshake. The middle
ACKand the secondFINcannot 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
CLOSEDtogether. The passive closer is done the instant the finalACKlands (beat 10); the active closer holdsTIME_WAITfor2·MSLfirst. In this normal-close scenario, the Server finishes before the Client. - Treating
TIME_WAITas waste. It is what prevents a lost finalACKfrom ending in aRST-terminated close; it is also why servers that actively close thousands of connections accumulate sockets inTIME_WAIT. - Putting
TIME_WAITon the wrong peer. In this normal-close scenario it belongs to the active closer, not the passive peer. Simultaneous close can put both endpoints inTIME_WAIT. - Mixing up the acknowledgment arithmetic. A
FINconsumes one sequence number despite carrying no data, so the ACKs areu+1andw+1, notuandw.
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.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Found a mistake or something unclear?