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.

  • Look into SO_REUSEADDR.
    – phemmer
    Oct 14 '14 at 12:45
  • @Patrick That doesn't explain why sshd apparently can rebind the port when another process can't. However, an understanding of SO_REUSEADDR and SO_LINGER/linger time (see man 7 socket, and here) is probably necessary to filling in the details which might make this question answerable. Right now methinks it is not.
    – goldilocks
    Oct 14 '14 at 13:41
  • @goldilocks sure it does. sshd is using SO_REUSEADDR, this other application isn't.
    – phemmer
    Oct 14 '14 at 14:46
  • @Patrick Hmmm, I've always assumed that 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 and sshd did.
    – goldilocks
    Oct 14 '14 at 15:27
  • @Patrick Well, SO_REUSEADDR works, but man 7 socket doesn't do a good job of explaining what the 'issue' is here. Is the "reuse of a local address" part the relevant bit? Does it count as "reuse of a local address" if there's already a connection there, even though it's not "reusing" the bind/listen?
    – ss23
    Oct 15 '14 at 7:35

While I don't understand all of the semantics (I'm either looking in the wrong place, or the documentation is lacking), I believe that for a certain amount of time after closing a connection (perhaps set by SO_LINGER), no process can open a new socket with the same details unless they have SO_REUSEADDR set.

This is to prevent someone reconnecting a second after a connection is closed and the process having to deal with packets that were meant for the previous process, as I understand it.

man 7 socket doesn't document this as part of SO_REUSEADDR which made this answer hard to figure out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.