2

I am getting port in use cannot bind on process restarts. I am trying to find out when a port is considered being used?

Is it only when in LISTEN mode? Can it also be if a connection is open to the port in TIME_WAIT state (or any other state in the TCP finite state machine)?

tcp 0 0 127.0.0.1:7199 0.0.0.0:* LISTEN 30099/java tcp 0 0 192.168.1.2:9160 0.0.0.0:* LISTEN 30099/java tcp 0 0 192.168.1.2:58263 192.168.1.2:9042 TIME_WAIT - tcp 0 0 192.168.1.2:58262 192.168.1.2:9042 TIME_WAIT - tcp 0 0 ::ffff:192.168.1.2:9042 :::* LISTEN 30099/java tcp 0 0 ::ffff:192.168.1.2:9042 ::ffff:192.168.1.2:57191 ESTABLISHED 30099/java tcp 0 0 ::ffff:192.168.1.2:9042 ::ffff:192.168.1.2:57190 ESTABLISHED 30099/java tcp 0 0 ::ffff:192.168.1.2:9042 ::ffff:10.176.70.226:37105 ESTABLISHED 30099/java tcp 0 0 ::ffff:127.0.0.1:42562 ::ffff:127.0.0.1:7199 TIME_WAIT - tcp 0 0 ::ffff:192.168.1.2:57190 ::ffff:192.168.1.2:9042 ESTABLISHED 30138/java tcp 0 0 ::ffff:192.168.1.2:57198 ::ffff:192.168.1.2:9042 ESTABLISHED 30138/java tcp 0 0 ::ffff:192.168.1.2:9042 ::ffff:10.176.70.226:37106 ESTABLISHED 30099/java tcp 0 0 ::ffff:192.168.1.2:57197 ::ffff:192.168.1.2:9042 ESTABLISHED 30138/java tcp 0 0 ::ffff:192.168.1.2:57191 ::ffff:192.168.1.2:9042 ESTABLISHED 30138/java tcp 0 0 ::ffff:192.168.1.2:9042 ::ffff:192.168.1.2:57198 ESTABLISHED 30099/java tcp 0 0 ::ffff:192.168.1.2:9042 ::ffff:192.168.1.2:57197 ESTABLISHED 30099/java tcp 0 0 ::ffff:127.0.0.1:42567 ::ffff:127.0.0.1:7199 TIME_WAIT - 

The process in question is a Java process which exposes JMX port. And there are a few monitoring agents that send request to that port to get information. I want to make sure that when the restart happens (after stop and before start) the ports are free so it won't run into the port bind issue. If a pending TIME_WAIT connection on that port is considered for the port being used, then I will add waits in between stop and start for those TIME_WAIT states to get cleaned up before the process starts. Unless there is another preferred option.

Thanks

1 Answer 1

5

A port is considered "in use" whenever there are any sockets bound to it. They don't have to be in LISTEN state, just bound. Therefore the TIME_WAIT sockets that you see do count.

It gets a little bit more complicated if any sockets are bound to addresses and ports. Different sockets are allowed to be bound to the same port if they're bound to different addresses. But if there is any socket bound to the wildcard address (INADDR_ANY, shown by netstat as *) for that port, it prevents other sockets from binding to any address and that same port.

Normally, most software that uses listening sockets sets the SO_REUSEADDR socket option on those sockets. This option relaxes the rules. With the option set, only a bound and listening socket prevents another socket from binding to the same address. This means that any lingering TIME_WAIT and other sockets will not prevent the software from restarting and rebinding immediately to the same port.

Your Java program is most likely not setting SO_REUSEADDR on its socket, but it should.

4
  • Where can I read more about this? Thanks Commented Jan 28, 2016 at 21:42
  • Try the socket manpage for quick reference info. The venerable textbook for this information is TCP/IP illustrated. I'm probably dating myself by recommending that, there might be something good that's more recent, but then again you can't go wrong with Stevens. Commented Jan 28, 2016 at 23:14
  • A socket is identified by IP address and port number. Commented Jan 29, 2016 at 2:12
  • @vonbrand, what do you mean by that? A socket is not really "identified" by that since not all sockets are necessarily bound. Commented Jan 29, 2016 at 15:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.