skipnothing/HTTP & Networking

Connection Lifecycle

ss -tan can show a socket sitting idle for a full minute after your program moved on: the side that closed first, still answering for a conversation that has already ended.

  • ▸Each direction shuts on its own, though a peer that hangs up at once folds two packets into one.
  • ▸Only the initiating side pays the lingering wait; the other is done the moment its last ACK lands.
  • ▸A pile of stuck-open sockets is never the peer's fault: your own program received the goodbye and never called close().
BUILDS ON
01

One FIN per direction, three or four packets

A live TCP connection is really two byte streams sharing one 4-tuple (the four numbers that name a connection: each side's IP address and port), one flowing each way, and they are independent. So closing is not one act. The client sends a FIN: I have no more bytes for you. The server acknowledges it, and now that one direction is shut, yet the server can still send. Only when the server sends its own FIN, and the client acknowledges that, is the connection fully down.

Count them: FIN, ACK, FIN, ACK. Four jobs, one FIN and one ACK per direction. When the peer closes right away, Linux rides its ACK on its own FIN, so the wire shows three segments; when the peer waits before closing, its ACK goes out alone and you see four; if it sends more bytes first, those cross too before its FIN. Between that opening FIN and the last, the connection sits in a real, named middle state, half-closed: one direction done, the other still carrying data. A single FIN never closes a connection. It closes a direction.

SHUT EACH DIRECTION ON ITS OWN

Keep going, sign up to unlock the rest

4 more parts in this topic, plus 23+ more topics in HTTP & Networking.

Sign up, it's freeSee the full HTTP & Networking