{"record":{"id":"eb8ec13575b9b395","repo":"openjdk/jdk","slug":"could-not-connect-to-server-after-attempts-with","errorCode":null,"errorMessage":"Could not connect to server after {} attempts with timeout {}","messagePattern":"Could not connect to server after (.+?) attempts with timeout (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"make/langtools/tools/javacserver/client/Client.java","lineNumber":133,"sourceCode":"     * Makes MAX_CONNECT_ATTEMPTS attempts to connect to server.\n     */\n    private Socket tryConnect() throws IOException, InterruptedException {\n        int attempt = 0;\n\n        while (true) {\n            Log.debug(\"Trying to connect. Attempt \" + (++attempt) + \" of \" + MAX_CONNECT_ATTEMPTS);\n            try {\n                Socket socket = new Socket();\n                InetAddress localhost = InetAddress.getByName(null);\n                InetSocketAddress address = new InetSocketAddress(localhost, conf.portFile().getPort());\n                socket.connect(address, CONNECTION_TIMEOUT);\n                Log.debug(\"Connected\");\n                return socket;\n            } catch (IOException ex) {\n                Log.error(\"Connection attempt failed: \" + ex.getMessage());\n                if (attempt >= MAX_CONNECT_ATTEMPTS) {\n                    Log.error(\"Giving up\");\n                    throw new IOException(\"Could not connect to server after \" + MAX_CONNECT_ATTEMPTS + \" attempts with timeout \" + CONNECTION_TIMEOUT, ex);\n                }\n            }\n            Thread.sleep(WAIT_BETWEEN_CONNECT_ATTEMPTS);\n        }\n    }\n\n    /*\n     * Fork a server process and wait for server to come around\n     */\n    private void startNewServer() throws IOException, InterruptedException {\n        List<String> cmd = new ArrayList<>();\n        // conf.javaCommand() is how to start java in the way we want to run\n        // the server\n        cmd.addAll(Arrays.asList(conf.javaCommand().split(\" \")));\n        // javacserver.server.Server is the server main class\n        cmd.add(Server.class.getName());\n        // and it expects a port file path\n        cmd.add(conf.portFile().getFilename());","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/make/langtools/tools/javacserver/client/Client.java#L115-L151","documentation":"IOException from javacserver Client's connect loop: after MAX_CONNECT_ATTEMPTS TCP connect attempts to the server port (each with CONNECTION_TIMEOUT ms, spaced WAIT_BETWEEN_CONNECT_ATTEMPTS apart) all failed with IOException, the client gives up. Each failed attempt is logged as 'Connection attempt failed: <reason>' before this exception is thrown with the last cause attached.","triggerScenarios":"Client mode of the smart javac compilation server: the port file exists and names a port, but no live server is listening on it — stale port file after a crashed server, server still initializing, or the port being blocked/refused.","commonSituations":"A previous javacserver crashed leaving a stale port file; firewall/SELinux rejecting loopback connections; heavy parallel builds where the server is saturated and never accepts; race between a client reading the port file and the server shutting down.","solutions":["Check the preceding 'Connection attempt failed:' log lines — ECONNREFUSED means nothing is listening (stale port file); timeout means the server is stuck.","Delete the stale port file and let the client start a fresh server.","Kill leftover javacserver processes (they are pooled per-user) and rebuild.","If a firewall blocks loopback, allow connections on the javacserver port range."],"exampleFix":"# before: stale server state blocks the build\nant build\n\n# after: reset server state, then build\npkill -f javacserver || true\nrm -f /tmp/javacserver*.portfile\nant build","handlingStrategy":"retry","validationCode":"// before connecting, verify the port file holds live values\nPortFile pf = PortFile.forFile(portFilePath);\nif (!pf.exists()) {\n    // no server recorded — let the client fork a fresh one rather than retry a dead port\n    client.allowServerStartup(true);\n} else {\n    pf.waitForValidValues(5000); // fail early if the daemon never comes up\n}","typeGuard":null,"tryCatchPattern":"try {\n    socket = client.connectOrSpawn();\n} catch (IOException e) {\n    // stale port file is the dominant cause: reset and retry once with a fresh server\n    if (e.getMessage().startsWith(\"Could not connect\")) {\n        cleanupStalePortFileAndProcesses();\n        socket = client.connectOrSpawn(); // second attempt starts a clean server\n    } else throw e;\n}","preventionTips":["Run one compilation server per user and clean its port file at job start on CI.","Ensure loopback TCP is permitted for the javacserver port range (firewall/SELinux).","Bound the number of parallel clients so the server can accept within the timeout."],"tags":["javacserver","network","connection-refused","build","concurrency"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}