Consider a parent process which completes a socket/bind/accept
, and will fork children with that socket open for them to communicate with, while the parent continues accepting connections. That parent process is then killed.
Another process now attempts to bind to the same address the parent process was bound to, on the same port, but receives an EADDRINUSE error.
However, when you complete this process with sshd
, it seems sshd
is able to rebind to the port that was closed, while during the restart window (where the sshd parent process is not running), a different program (running as a different user) just gets EADDRINUSE.
What are the semantics behind this? Why can sshd
rebind, but another users process cannot?
Additionally, I can confirm that the netstat -a | grep PORT
output from during the time only the child process is running (when the other process can't bind
), the only connection is the ESTABLISHED
one, none in LISTEN
state.
SO_REUSEADDR
.sshd
apparently can rebind the port when another process can't. However, an understanding ofSO_REUSEADDR
andSO_LINGER
/linger time (seeman 7 socket
, and here) is probably necessary to filling in the details which might make this question answerable. Right now methinks it is not.sshd
is usingSO_REUSEADDR
, this other application isn't.SO_REUSEADDR
must be set by both the process which owned the now defunct socket and the new one...then I further assumed that the original process was coded by the OP in ignorance of SO_REUSEADDR. But you're right, this is almost certainly the explanation; only the new process needs to use it andsshd
did.