{"record":{"id":"15a8e999471a0ee0","repo":"karatelabs/karate","slug":"port-not-available-after-attempts","errorCode":null,"errorMessage":"port {}:{} not available after {} attempts","messagePattern":"port (.+?):(.+?) not available after (.+?) attempts","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"karate-core/src/main/java/io/karatelabs/process/PortUtils.java","lineNumber":74,"sourceCode":"     * @param stillAlive Supplier that returns false if we should stop waiting (e.g., process died)\n     * @return true if port is available, false if timed out or process died\n     */\n    public static boolean waitForPort(String host, int port, int attempts, int intervalMs, BooleanSupplier stillAlive) {\n        for (int i = 0; i < attempts; i++) {\n            if (stillAlive != null && !stillAlive.getAsBoolean()) {\n                logger.warn(\"process died while waiting for port {}:{}\", host, port);\n                return false;\n            }\n            try (Socket socket = new Socket()) {\n                socket.connect(new InetSocketAddress(host, port), 1000);\n                logger.debug(\"port {}:{} is available after {} attempts\", host, port, i + 1);\n                return true;\n            } catch (Exception e) {\n                logger.trace(\"port {}:{} not yet available (attempt {})\", host, port, i + 1);\n                sleep(intervalMs);\n            }\n        }\n        logger.warn(\"port {}:{} not available after {} attempts\", host, port, attempts);\n        return false;\n    }\n\n    /**\n     * Wait for an HTTP endpoint to return 200.\n     */\n    public static boolean waitForHttp(String url, int attempts, int intervalMs, BooleanSupplier stillAlive) {\n        HttpClient client = HttpClient.newBuilder()\n                .connectTimeout(Duration.ofSeconds(5))\n                .build();\n        for (int i = 0; i < attempts; i++) {\n            if (stillAlive != null && !stillAlive.getAsBoolean()) {\n                logger.warn(\"process died while waiting for HTTP {}\", url);\n                return false;\n            }\n            try {\n                HttpRequest request = HttpRequest.newBuilder()\n                        .uri(URI.create(url))","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/process/PortUtils.java#L56-L92","documentation":"`PortUtils.waitForPort` exhausted all attempts without a successful TCP connection to host:port. It logs this warning and returns false, meaning the service never became reachable within the configured attempt budget.","triggerScenarios":"`waitForPort('localhost', 8080, 30, 500, stillAlive)` where nothing listens on 8080; server started but bound to a different port/interface (e.g. 127.0.0.1 vs container IP); attempts/interval too small for a slow-starting service.","commonSituations":"Firewall or network policy blocking the port; service binding only to a specific interface; docker port mapping mistakes; under-provisioned timeout for slow JVM/framework startup in CI.","solutions":["Confirm the service actually started and which port/interface it bound (logs, `netstat`/`ss`)","Increase `attempts` or `intervalMs` to cover slow startup","Verify host is correct (localhost vs container hostname vs external IP)","Check firewall/security-group rules blocking the port"],"exampleFix":"// before\nboolean up = PortUtils.waitForPort(\"localhost\", 8080, 10, 200, alive);\n// after\nboolean up = PortUtils.waitForPort(\"localhost\", 8080, 60, 500, alive);","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if (!PortUtils.waitForPort(host, port, 60, 500, alive)) {\n    throw new RuntimeException(\"port \" + host + \":\" + port + \" never opened; check service logs\");\n}","preventionTips":["Size attempts*intervalMs above worst-case startup time (CI is slower)","Confirm the bind address matches the host you poll","Check port conflicts and firewall rules before the wait"],"tags":["network","port","timeout","startup"],"backgroundTag":"connection-refused","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}