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