Opscraft
The TCP handshake · Lesson 2 of 8 · 5 min read

Closing connections and socket states

Graceful close: FIN

Each direction closes independently (TCP is full duplex):

Enlarge diagramClientcloses firstServerFIN_WAIT_1CLOSE_WAIT: the app must call close()FIN_WAIT_2LAST_ACKTIME_WAIT for 60 s, then CLOSEDCLOSEDFIN: I have finished sendingACKFIN (often sent together with the ACK)ACK
Figure 1. Closing politely: each side sends its own FIN. The side that closes first waits in TIME_WAIT; a side stuck in CLOSE_WAIT has an application that never called close().
Swipe sideways to see all of it, or tap to open it full screen.

Often the server's ACK and FIN are combined, giving three packets instead of four.

  • FIN means "I've finished sending" — a polite goodbye after all data.
  • A half-close is legal: one side sends FIN but keeps receiving until the other closes (shutdown(SHUT_WR)), e.g. a client that finished uploading and waits for the response.

Abort: RST

RST slams the door: the connection is destroyed immediately, unsent/unread data is discarded, and the peer's next call fails with "connection reset by peer" (ECONNRESET). RSTs are sent when:

  • a SYN arrives for a port with no listener → client sees "connection refused";
  • a segment arrives for a connection that doesn't exist (e.g. after a server restart or a NAT/LB forgot the flow);
  • an application closes a socket with unread data, or uses SO_LINGER with timeout 0;
  • a firewall/proxy/load balancer actively rejects or kills a connection (idle timeout).

One-line summary: FIN is a polite goodbye; RST is hanging up mid-sentence.

All the states

State Meaning
LISTEN Server socket waiting for connections
SYN_SENT Client sent SYN, waiting for SYN-ACK
SYN_RECV Server got SYN, sent SYN-ACK, waiting for final ACK (half-open, in the SYN queue)
ESTABLISHED Open; data flows
FIN_WAIT_1 Active closer sent FIN
FIN_WAIT_2 Active closer's FIN acknowledged; waiting for peer's FIN
CLOSE_WAIT Peer closed; local application hasn't called close() yet
LAST_ACK Passive closer sent its FIN, waiting for the final ACK
TIME_WAIT Active closer waits ~2×MSL before freeing the 5-tuple
CLOSING Both sides sent FIN simultaneously
CLOSED No connection
ss -tan                                   # all TCP sockets with state
ss -tan state time-wait | wc -l
ss -tan state close-wait
ss -s                                     # summary by state
ss -tanp state close-wait                 # which process owns them

TIME_WAIT: purpose and problems

The side that closes first stays in TIME_WAIT for twice the maximum segment lifetime (Linux uses a fixed 60 seconds). Why:

  1. If the final ACK is lost, the peer retransmits its FIN; the TIME_WAIT socket can re-ACK it.
  2. Delayed segments from the old connection die out before the same 5-tuple can be reused, so they can't corrupt a new connection.

TIME_WAIT is normal. It becomes a problem when a host makes many short-lived outbound connections to the same destination (a proxy to a backend, an app to a database without pooling, a load test client): thousands of TIME_WAIT sockets each hold an ephemeral port for 60 s → ephemeral port exhaustion → Cannot assign requested address.

Fixes, best first:

Enlarge diagramNew connection perrequestproxy → backend, app →databaseEach close leavesTIME_WAIT60 s per connectionPorts used up~28,000 perdestination"Cannot assignrequested address"new connections failFixReuse connections first: keep-alive, connection pools, upstream keepalive in Nginx. tcp_tw_reuse and a widerport range only buy time.
Figure 2. How TIME_WAIT turns into an outage: a client that opens a new connection for every request to the same backend runs out of local ports.
Swipe sideways to see all of it, or tap to open it full screen.
  • Reuse connections — HTTP keep-alive, connection pools, upstream keepalive in Nginx (keepalive 64; in the upstream block plus HTTP/1.1 and clearing the Connection header).
  • Let the server close (then TIME_WAIT lands on the server, which isn't limited by client ports).
  • net.ipv4.tcp_tw_reuse=1 — lets new outbound connections reuse TIME_WAIT sockets safely (uses timestamps).
  • Widen ip_local_port_range; add more source IPs / destination IPs.
  • (tcp_tw_recycle was removed from Linux 4.12 — it broke clients behind NAT. Don't recommend it.)

CLOSE_WAIT: always an application bug

CLOSE_WAIT means the peer closed and your application never closed its side. A steadily growing number of CLOSE_WAIT sockets on your server is a socket leak in the app (missing close() in an error path, a connection pool that doesn't discard dead connections). It eventually exhausts file descriptors ("Too many open files"). The kernel can't fix it; find the code path (ss -tanp state close-wait → process; thread dumps), and restart as a stop-gap.