{"record":{"id":"321c727d80f1a8ca","repo":"apache/druid","slug":"unable-to-find-open-port-between-d-and-d","errorCode":null,"errorMessage":"Unable to find open port between [%d] and [%d]","messagePattern":"Unable to find open port between \\[(.+?)\\] and \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/common/utils/SocketUtil.java","lineNumber":52,"sourceCode":"  {\n    final int startPort = basePort < 0 ? -1 : ThreadLocalRandom.current().nextInt(0x7fff) + basePort;\n    return findOpenPortFrom(startPort);\n  }\n\n  public static int findOpenPortFrom(int startPort)\n  {\n    int currPort = startPort;\n\n    while (currPort < 0xffff) {\n      try (ServerSocket ignoredSocket = new ServerSocket(currPort)) {\n        return currPort;\n      }\n      catch (IOException e) {\n        ++currPort;\n      }\n    }\n\n    throw new ISE(\"Unable to find open port between [%d] and [%d]\", startPort, currPort);\n  }\n}\n","sourceCodeStart":34,"sourceCodeEnd":55,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/common/utils/SocketUtil.java#L34-L55","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Stop the processes occupying the port range (lsof -i :<port> or netstat to identify them), then retry.","Change the starting port to a different range (SocketUtil.findOpenPort(20000)).","Wait for TIME_WAIT sockets to expire or enable SO_REUSEADDR-style rebinding in the conflicting service.","Serialize test runs or randomize start ports across CI workers to avoid contention."],"exampleFix":"// before\nint port = SocketUtil.findOpenPort(8080);\n// after\nint port = SocketUtil.findOpenPort(28888); // move away from heavily-used range","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"int port;\ntry {\n  port = SocketUtil.findOpenPort(basePort);\n} catch (ISE e) {\n  port = SocketUtil.findOpenPort(basePort + 1000); // retry in another range\n}","preventionTips":["Randomize or spread start ports across concurrent CI workers","Check lsof/netstat for port conflicts before launching tests","Ensure crashed tests shut down their servers to free sockets"],"tags":["network","ports","socket"],"backgroundTag":"address-already-in-use","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}