Interview Prep/Networking/Network Models, TCP, UDP & Ports

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.

All Networking questions Roadmap: Networking for DevOps

More on Network Models, TCP, UDP & Ports