How does TCP close a connection, and what is TIME_WAIT?
IntermediateConceptNetwork Models, TCP, UDP & Ports
Answer
A graceful close is four segments: each side sends FIN and the other ACKs it, so each direction closes independently. The side that closes first enters TIME_WAIT for 2×MSL (60 seconds on Linux) to make sure the last ACK arrived and stale segments from the old connection die out.
Thousands of TIME_WAIT sockets are normal on a busy client or proxy. They only hurt if you run out of ephemeral ports, which you see as Cannot assign requested address. The fix is connection pooling or keep-alive, not blindly enabling tcp_tw_reuse everywhere.
More on Network Models, TCP, UDP & Ports
What are the layers of the OSI model, and why does a DevOps engineer care?BeginnerHow does the TCP/IP model map to the OSI model?BeginnerWhat is the difference between TCP and UDP?BeginnerDescribe the TCP three-way handshake.BeginnerWhat is a port, and what are well-known, registered and ephemeral ranges?BeginnerWhich ports should you know by heart for DevOps work?Beginner