Probably the most asked-about subject in all of stackoverflow world: "Address in in use" errors. There are literally thousands of questions on the topic. This question ask for a specific detail.
The specific detail: how long is an address that has been bind/accept
-ed held for after a process terminates abnormally?
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 accept
has been called is held for some time before it can be reused (on Debian Linux). Restarting the server immediately, and attempting to bind
to the same endpoint results in a "Address already in use" 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.
If anyone can recommend an up-to-date accurate state diagram for TCP/IP connections, I could probably work this out myself. (Please do).
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 "Address already in use" 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.
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.