AlgoViz

Opening, owning and closing a TCP connection

medium 7 min

TCP is a full-duplex byte stream with independent closure in each direction.

Video lesson · Chapter 1 of 4

CLOSE_WAIT grows after peer disconnects

0:00 / 2:12

Theory

Read or listen first. About 2 min.

What it is

TCP is a full-duplex byte stream with independent closure in each direction. A peer FIN says it has finished sending. After buffered data is consumed the local application sees EOF and must release its end when finished. TIME_WAIT and CLOSE_WAIT have different owners and meanings, even if both appear after a disconnect.

Why it exists

A service stops opening connections after sustained traffic. Group socket states by owning process and sample twice. Growing CLOSE_WAIT alongside a rising descriptor count suggests an application cleanup path that never closes after peer shutdown. Before changing kernel timers, identify whether the application or the transport owns the next transition.

If TIME_WAIT dominates and descriptors stay stable, investigate churn or outbound port capacity rather than a missing local close.

CLOSE_WAIT grows after peer disconnects

Remote peer → Local TCP stack (send FIN). Local TCP stack → Application owner (deliver EOF). Application owner → Local TCP stack (close descriptor)
Remote peer → Local TCP stack (send FIN). Local TCP stack → Application owner (deliver EOF). Application owner → Local TCP stack (close descriptor)

Inside this chapter

The TCP Header structure
The header carries ports, sequence and acknowledgment numbers, flags and window information. Interpret a flag with its connection state and acknowledgment progress.
Three-way Handshake (SYN, SYN-ACK, ACK)
SYN and SYN-ACK exchange initial sequence information, then ACK completes normal establishment. Connect latency can accumulate before a server handler ever sees a request.
Connection Teardown (FIN, ACK, RST)
A FIN closes one direction cleanly after queued data. A reset aborts the connection. An application should distinguish an orderly end of stream from a truncated operation.
The TCP State Machine: TIME_WAIT, CLOSE_WAIT, ESTABLISHED
ESTABLISHED allows transfer. CLOSE_WAIT awaits local closure after peer FIN. TIME_WAIT generally follows active closure and protects against delayed segments from a prior connection.
Socket Leaks and Ephemeral Port Exhaustion
Outbound connections consume local endpoint combinations and descriptors. Inspect destination distribution, reuse and ownership before treating many sockets in one state as a leak.
TCP Options: Timestamps, SACK, Window Scale
Window scaling negotiates larger advertised windows, SACK identifies received ranges, and timestamps support mechanisms including RTT measurement. Some options are negotiated during establishment.
Nagle's Algorithm and TCP_NODELAY
Nagle’s algorithm can coalesce small writes while unacknowledged data is outstanding. Disabling it changes latency and packet count. Measure the application’s write and flush pattern first.
TCP Keep-Alive mechanics
Keepalives can probe an idle peer when enabled. Their timing is distinct from a request deadline. A successful probe cannot guarantee that a particular application operation will complete.

How it works

Step through the idea, or press Play.

Step 1 of 4

Send FIN. The client finishes writing and sends FIN while it can still receive data.

Understand it

Worked incident

CLOSE_WAIT grows after peer disconnects

ObservationLocal CLOSE_WAIT sockets (sockets)Interpretation
Baseline2Completed peer closes are promptly released.
Incident310An exception path keeps hundreds of peer-closed sockets open.
After intervention4Close on every completion path and repeat failed transfers while monitoring counts.
Illustrative measurements from the same workload at three checkpoints.

Choose the next investigation

If TIME_WAIT dominates and descriptors stay stable, investigate churn or outbound port capacity rather than a missing local close.

Summary

The whole topic in one minute.

A socket in CLOSE_WAIT awaits local close. TIME_WAIT protects a different phase of connection termination.

If TIME_WAIT dominates and descriptors stay stable, investigate churn or outbound port capacity rather than a missing local close.

Sources