1

I have a situation where I kill a process, but when restarting it, I get the Address already in use error.

I am running sudo netstat -lntp to get the process ID, which returns, for example:

tcp        0      0 0.0.0.0:8002            0.0.0.0:*               LISTEN       475/valhalla_servic

I then run sudo kill -9 475

After running this, netstat no longer shows the process, but when I try to restart I receive the Address already in use error and I'm not sure what I'm doing wrong?

1 Answers1

1

I'm not sure what I'm doing wrong?

Good news: nothing.

The TCP state machine has timeout conditions. For that reason, ports linger a while, by default.

As a user of some software, there's not much you can do about that - not letting arbitrary software instantly rebind a port is, in the end, also a security feature.

As a developer of the software, you can set SO_LINGER to 0. It is, in most cases, not the right thing to do – especially not when you do actually kill the owning process.

  • Thanks for the reply - I don't think that's the issue in my case as I can leave it for many hours and still see Address already in use – Gary Wright Jan 11 '22 at 14:55
  • then you didn't kill the process(es) that hold the socket – Marcus Müller Jan 11 '22 at 14:55
  • If it isn't killed, it should be shown in the above call to netstat shouldn't it? Once I issue the kill command, there is nothing showing there. (Thanks again for the answer / follow up) – Gary Wright Jan 12 '22 at 09:02
  • good point. does anything show up with -a instead of -l? if so, what does its STATE column say? (btw, netstat is a bit old by now, ss is the "moderner" alternative on Linux, at least) – Marcus Müller Jan 12 '22 at 09:41
  • Nope, it is like all trace of the process is gone and yet I still receive the error on restart – Gary Wright Jan 12 '22 at 13:56