SonarSource/sonarqube · error · IllegalStateException

Fail to find an available port on

Error message

Fail to find an available port on 

What it means

getNextAvailablePort tries portAllocator.getAvailable for candidate ports and gives up if none yields a valid free port, throwing IllegalStateException. It means no usable (non-privileged, unallocated) port could be found for the address.

Solutions

  1. Check the process for leaked sockets/ports that stay in PORTS_ALREADY_ALLOCATED
  2. Restart the process to clear the in-memory allocation cache
  3. Verify the OS allows binding on the given address and ephemeral port range
  4. Inspect the wrapped cause from the inner allocator for bind failures

Example fix

null
Defensive patterns

Strategy: retry

Try / catch

try {
  int port = networkUtils.getNextAvailablePort(addr);
} catch (IllegalStateException e) {
  LOGGER.error("No available port; check leaked sockets and port range", e);
}

Prevention

When it happens

Trigger: Repeated calls exhausting the allocator's candidates; isValidPort rejecting ports <=1023 or already allocated by this process; the OS denying socket binds.

Common situations: Long-lived process allocating many ports over time; restricted environments limiting ephemeral ports; all candidates in PORTS_ALREADY_ALLOCATED.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/7d8af7184fa3e5be. Report an issue: GitHub.

Appendix: source

Thrown at server/sonar-process/src/main/java/org/sonar/process/NetworkUtilsImpl.java:80

      .map(t -> getNextAvailablePort(t, PortAllocator.INSTANCE))
      .orElseThrow(() -> new IllegalArgumentException(format("Can not resolve address %s", hostOrAddress))));
  }

  /**
   * Warning - the allocated ports are kept in memory and are never clean-up. Besides the memory consumption,
   * that means that ports already allocated are never freed. As a consequence
   * no more than ~64512 calls to this method are allowed.
   */
  @VisibleForTesting
  static int getNextAvailablePort(InetAddress address, PortAllocator portAllocator) {
    for (int i = 0; i < PORT_MAX_TRIES; i++) {
      int port = portAllocator.getAvailable(address);
      if (isValidPort(port)) {
        PORTS_ALREADY_ALLOCATED.add(port);
        return port;
      }
    }
    throw new IllegalStateException("Fail to find an available port on " + address);
  }

  private static boolean isValidPort(int port) {
    return port > 1023 && !PORTS_ALREADY_ALLOCATED.contains(port);
  }

  static class PortAllocator {

    private static final PortAllocator INSTANCE = new PortAllocator();

    int getAvailable(InetAddress address) {
      try (ServerSocket socket = new ServerSocket(0, 50, address)) {
        return socket.getLocalPort();
      } catch (IOException e) {
        throw new IllegalStateException("Fail to find an available port on " + address, e);
      }
    }
  }

View on GitHub (pinned to 184c821202)