openjdk/jdk · error · IOException

Could not connect to server after {} attempts with timeout {

Error message

Could not connect to server after {} attempts with timeout {}

What it means

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.

Source

Thrown at make/langtools/tools/javacserver/client/Client.java:133

     * Makes MAX_CONNECT_ATTEMPTS attempts to connect to server.
     */
    private Socket tryConnect() throws IOException, InterruptedException {
        int attempt = 0;

        while (true) {
            Log.debug("Trying to connect. Attempt " + (++attempt) + " of " + MAX_CONNECT_ATTEMPTS);
            try {
                Socket socket = new Socket();
                InetAddress localhost = InetAddress.getByName(null);
                InetSocketAddress address = new InetSocketAddress(localhost, conf.portFile().getPort());
                socket.connect(address, CONNECTION_TIMEOUT);
                Log.debug("Connected");
                return socket;
            } catch (IOException ex) {
                Log.error("Connection attempt failed: " + ex.getMessage());
                if (attempt >= MAX_CONNECT_ATTEMPTS) {
                    Log.error("Giving up");
                    throw new IOException("Could not connect to server after " + MAX_CONNECT_ATTEMPTS + " attempts with timeout " + CONNECTION_TIMEOUT, ex);
                }
            }
            Thread.sleep(WAIT_BETWEEN_CONNECT_ATTEMPTS);
        }
    }

    /*
     * Fork a server process and wait for server to come around
     */
    private void startNewServer() throws IOException, InterruptedException {
        List<String> cmd = new ArrayList<>();
        // conf.javaCommand() is how to start java in the way we want to run
        // the server
        cmd.addAll(Arrays.asList(conf.javaCommand().split(" ")));
        // javacserver.server.Server is the server main class
        cmd.add(Server.class.getName());
        // and it expects a port file path
        cmd.add(conf.portFile().getFilename());

View on GitHub (pinned to 88dfb74bbe)

Solutions

  1. Check the preceding 'Connection attempt failed:' log lines — ECONNREFUSED means nothing is listening (stale port file); timeout means the server is stuck.
  2. Delete the stale port file and let the client start a fresh server.
  3. Kill leftover javacserver processes (they are pooled per-user) and rebuild.
  4. If a firewall blocks loopback, allow connections on the javacserver port range.

Example fix

# before: stale server state blocks the build
ant build

# after: reset server state, then build
pkill -f javacserver || true
rm -f /tmp/javacserver*.portfile
ant build
Defensive patterns

Strategy: retry

Validate before calling

// before connecting, verify the port file holds live values
PortFile pf = PortFile.forFile(portFilePath);
if (!pf.exists()) {
    // no server recorded — let the client fork a fresh one rather than retry a dead port
    client.allowServerStartup(true);
} else {
    pf.waitForValidValues(5000); // fail early if the daemon never comes up
}

Try / catch

try {
    socket = client.connectOrSpawn();
} catch (IOException e) {
    // stale port file is the dominant cause: reset and retry once with a fresh server
    if (e.getMessage().startsWith("Could not connect")) {
        cleanupStalePortFileAndProcesses();
        socket = client.connectOrSpawn(); // second attempt starts a clean server
    } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Related errors


AI-assisted analysis of openjdk/jdk@88dfb74bbe (2026-08-14). Data as JSON: /api/errors/eb8ec13575b9b395. Report an issue: GitHub.