title: Timeout on the address used in socket accept
body: <p>Probably the most asked-about subject in all of stackoverflow world: &quot;Address in in use&quot; errors. There are literally thousands of questions on the topic. This question ask for a specific detail.</p>
  <p>The specific detail: how long is an address that has been <code>bind/accept</code>-ed held for after a process terminates abnormally?</p>
  <p>I'm writing a HTTP server demon. It accepts socket connections, obviously. If the process is terminated abnormally (either by the debugger, or an actual crash), the socket address on which <code>accept</code> has been called is held for some time before it can be reused (on Debian Linux). Restarting the server immediately, and attempting to <code>bind</code> to the same endpoint results in a &quot;Address already in use&quot; error. The address appears to be reserved by the OS for some period of time on the order of 60 seconds after the process terminates. It may be dependent on whether there was an open client connection when the previous process was terminated.</p>
  <p>If anyone can recommend an up-to-date accurate state diagram for TCP/IP connections, I could probably work this out myself. (Please do).</p>
  <p>It seems to me that I need to write code in my server to retry the bind/accept operation for some period of time before giving up and exiting abnormally with an actual &quot;Address already in use&quot; error. This seems necessary in order to allow the server to be restarted. The code currently retries for 140 seconds for no other reason than that's a timeout for a bunch of TCP operations; but I'd like the peace of mind of having an actual fact-informed value.</p>
  <p>So that's the question. How long do I need to retry for before giving up on an attempt to bind a port used to accept connections? What is the timeout on the reservation of an address that has been previously used to accept socket connections by a process that has been abnormally terminated? Is this possibly an Operating-System-dependent question? The ideal answer would reference a standards-imposed timeout in the TCP/IP socket state diagram.</p>