Impact of socket.SO_REUSEADDR

This is often used to get around an “address already in use” error. If you know the previous server program has been shutdown, you can set this socket option prior to the initial server bind, and then restart your program. However, there is risk. A TIME_WAIT status on a socket is designed to provide safety when a “late” packet arrives on a host:port combo from a previous incarnation of a program using that combo. The OS will normally retain a file handle to the host:port combo for a time twice that of the MSL (Max Segment Lifetime) to ensure this is not the case.

Again, if you know your program and are sure there will be no issues with “late” packets such as those described arriving to a subsequent incarnation, you can set this on the server socket and get your program started more quickly. If you can wait for the number of seconds defined as a timeout for the TIME_WAIT status, you can simply wait, restart, then get a successful bind without setting the REUSEADDR parameter. This value in seconds can be obtained with what is below…

$cat /proc/sys/net/ipv4/tcp_fin_timeout

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.