apache/druid · error · IllegalStateException

Unable to find open port between

Error message

Unable to find open port between [%d] and [%d]

What it means

SocketUtil.findOpenPort iterates a port range trying to bind a ServerSocket, incrementing on IOException (typically BindException: address in use). If every port from startPort to the current cursor is occupied, it throws an IllegalStateException stating no open port was found in the range. Druid uses this mainly in tests to locate a free port.

Solutions

  1. Stop the processes occupying the port range (lsof -i :<port> or netstat to identify them), then retry.
  2. Change the starting port to a different range (SocketUtil.findOpenPort(20000)).
  3. Wait for TIME_WAIT sockets to expire or enable SO_REUSEADDR-style rebinding in the conflicting service.
  4. Serialize test runs or randomize start ports across CI workers to avoid contention.

Example fix

// before
int port = SocketUtil.findOpenPort(8080);
// after
int port = SocketUtil.findOpenPort(28888); // move away from heavily-used range
Defensive patterns

Strategy: retry

Try / catch

int port;
try {
  port = SocketUtil.findOpenPort(basePort);
} catch (ISE e) {
  port = SocketUtil.findOpenPort(basePort + 1000); // retry in another range
}

Prevention

When it happens

Trigger: Calling SocketUtil.findOpenPort(startPort) when all ports in [startPort, some upper limit] are already bound by other processes, so every ServerSocket bind attempt throws IOException.

Common situations: Running Druid test suites concurrently with other services occupying the default test port range; a previous crashed test leaving sockets in TIME_WAIT; CI machines running many workers competing for the same fixed port range.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/321c727d80f1a8ca. Report an issue: GitHub.

Appendix: source

Thrown at processing/src/main/java/org/apache/druid/common/utils/SocketUtil.java:52

  {
    final int startPort = basePort < 0 ? -1 : ThreadLocalRandom.current().nextInt(0x7fff) + basePort;
    return findOpenPortFrom(startPort);
  }

  public static int findOpenPortFrom(int startPort)
  {
    int currPort = startPort;

    while (currPort < 0xffff) {
      try (ServerSocket ignoredSocket = new ServerSocket(currPort)) {
        return currPort;
      }
      catch (IOException e) {
        ++currPort;
      }
    }

    throw new ISE("Unable to find open port between [%d] and [%d]", startPort, currPort);
  }
}

View on GitHub (pinned to 9b90983fd2)