About Linux sockets in TIME_WAIT status
We recently came across an application used a significant number of open/close TCP connections under Redhat Enterprise Linux 5.4. As a result the system ran out of local ports to open new connections due a number of sockets that were stuck in TIME_WAIT status. Using the ‘netstat’ command from a shell we could see that it was this particular application causing the problem because the destination port numbers were all the same.
Sockets which are listed in CLOSE_WAIT status are different and many of these usually means that a particular application isn’t closing it’s socket file descriptors off correctly.
More about fixing Linux socket TIME_WAIT status
This problem was causing issues for all network related software on the system. To alleviate this problem we set the following kernel parameters as root and rebooted:
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
After this change the number of sockets in TIME_WAIT state were drastically reduced, problem solved!