{"record":{"id":"1506c21401f1c559","repo":"openjdk/jdk","slug":"failed-to-delete-file","errorCode":null,"errorMessage":"Failed to delete file.","messagePattern":"Failed to delete file\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"make/langtools/tools/javacserver/shared/PortFile.java","lineNumber":183,"sourceCode":"        rwfile.writeLong(cookie);\n        myServerPort = port;\n        myServerCookie = cookie;\n    }\n\n    /**\n     * Delete the port file.\n     */\n    public void delete() throws IOException, InterruptedException {\n        if (!file.exists()) { // file deleted already\n            return;\n        }\n        // Keep trying until file has been deleted, otherwise we\n        // might shutdown the server and prevent another one from starting.\n        for (int i = 0; i < 10 && file.exists() && !file.delete(); i++) {\n            Thread.sleep(1000);\n        }\n        if (file.exists()) {\n            throw new IOException(\"Failed to delete file.\");\n        }\n        // allow some time for late clients to connect\n        Thread.sleep(1000);\n    }\n\n    /**\n     * Is the port file still there?\n     */\n    public boolean exists() throws IOException {\n        return file.exists();\n    }\n\n    /**\n     * Is a stop file there?\n     */\n    public boolean markedForStop() throws IOException {\n        if (stopFile.exists()) {\n            try {","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/make/langtools/tools/javacserver/shared/PortFile.java#L165-L201","documentation":"IOException from PortFile.delete(): the server-side port file could not be deleted after 10 attempts spaced 1 second apart. The retry loop exists deliberately — an undeleted port file would make subsequent clients try to talk to a dead server, so the deletion failure is treated as fatal for the shutdown sequence.","triggerScenarios":"Server shutdown calling delete() when another process holds the file open with semantics that block unlink (Windows), the containing directory is not writable, or an external tool (antivirus, indexer) keeps recreating/locking the file.","commonSituations":"Windows builds where a concurrent client has the port file mapped/locked; NFS semantics preventing deletion; the port file directory owned by root after a sudo build.","solutions":["Ensure the user running the shutdown owns the port file's directory and has write permission on it.","Terminate all javacserver clients and processes before shutting the server down.","Manually remove the port file (rm) once no processes reference it, and retry the build.","On Windows, close any IDEs/builds that may hold the file open."],"exampleFix":"# before: shutdown fails because the port file is locked\nserver-stop\n\n# after: release holders, then remove manually\npkill -f javacserver\nrm -f /path/to/javacserver.port\nserver-stop","handlingStrategy":"retry","validationCode":"// before delete(): ensure nothing else is holding the file\nif (file.exists() && !file.canWrite())\n    throw new IOException(\"port file not deletable (permissions): \" + file);","typeGuard":null,"tryCatchPattern":"try {\n    portFile.delete();\n} catch (IOException e) {\n    if (System.getProperty(\"os.name\").toLowerCase().contains(\"win\")) {\n        // Windows lock: schedule deletion on exit rather than failing shutdown\n        file.deleteOnExit();\n    } else {\n        Files.deleteIfExists(file.toPath()); // one NIO attempt with real errno\n        throw e;\n    }\n}","preventionTips":["Stop all clients before shutting the compilation server down.","Keep the port-file directory user-owned, on a local disk, off NFS.","On Windows, expect delete contention; rely on the delete-on-exit escape hatch."],"tags":["javacserver","file-deletion","locking","windows","shutdown"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}