Skip to main content
networking

FIN (Finish)

FIN is a TCP flag used to gracefully close a connection. TCP close is a four-way process: FIN from initiator, ACK from peer, FIN from peer, ACK from initiator. The TIME_WAIT state after closing lasts 2*MSL (typically 60s) to handle delayed packets – this can exhaust ports on busy servers.

Definition

The FIN flag signals that the sender has no more data to transmit. TCP close is half-duplex – each direction closes independently. The initiator sends FIN, the peer ACKs it (the peer can still send data). When the peer finishes, it sends its own FIN, which gets ACKed. The initiator then enters TIME_WAIT for 2*MSL (Maximum Segment Lifetime, typically 60 seconds) to handle any delayed packets that might arrive after close. On high-traffic servers (load balancers, proxies), TIME_WAIT sockets can exhaust available local ports. Mitigations include: tcp_tw_reuse (allows reuse of TIME_WAIT sockets for new outgoing connections), SO_LINGER with timeout 0 (sends RST instead of FIN – abrupt close), and connection pooling to avoid frequent open/close cycles.

Examples

  • ss -o state time-wait | wc -l – count TIME_WAIT sockets
  • sysctl net.ipv4.tcp_tw_reuse=1 – allow TIME_WAIT reuse for outgoing
  • shutdown(fd, SHUT_WR) sends FIN while keeping read half open

Related Protocols

Related Terms