{"record":{"id":"f5bc33be38eb0509","repo":"HMCL-dev/HMCL","slug":"cannot-attach-vm-lvmid","errorCode":null,"errorMessage":"Cannot attach VM ${lvmid}","messagePattern":"Cannot attach VM (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameDumpGenerator.java","lineNumber":161,"sourceCode":"            execute(vm, \"Thread.print -l\", writer);\n\n    }\n\n    private static VirtualMachine attachVM(String lvmid, Writer writer) throws IOException, InterruptedException {\n        for (int i = 0; i < RETRY_TIME; i++) {\n            try {\n                return VirtualMachine.attach(lvmid);\n            } catch (Throwable e) {\n                LOG.warning(\"An exception encountered while attaching vm \" + lvmid, e);\n                writer.write(StringUtils.getStackTrace(e));\n                writer.write('\\n');\n                Thread.sleep(3000);\n            }\n        }\n\n        String message = \"Cannot attach VM \" + lvmid;\n        writer.write(message);\n        throw new IOException(message);\n    }\n\n    private static void execute(VirtualMachine vm, String command, Appendable target) throws IOException {\n        try (Reader reader = new InputStreamReader(executeJVMCommand(vm, command), OperatingSystem.NATIVE_CHARSET)) {\n            char[] data = new char[256];\n            CharBuffer cb = CharBuffer.wrap(data);\n            int len;\n            while ((len = reader.read(data)) > 0) { // Directly read the data into a CharBuffer would cause useless array copy actions.\n                target.append(cb, 0, len);\n            }\n        } catch (Throwable throwable) {\n            LOG.warning(\"An exception encountered while executing jcmd \" + vm.id(), throwable);\n            target.append(StringUtils.getStackTrace(throwable));\n            target.append('\\n');\n        }\n    }\n\n    private static InputStream executeJVMCommand(VirtualMachine vm, String command) throws IOException, AttachNotSupportedException {","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCLCore/src/main/java/org/jackhuang/hmcl/game/GameDumpGenerator.java#L143-L179","documentation":"GameDumpGenerator.attachVM fails to attach to the target JVM via the attach API and, after retrying, writes 'Cannot attach VM <lvmid>' to the dump writer and throws IOException. This happens when the HotSpotVirtualMachine attach or its load/instrument command cannot complete within the retry loop. The dump file will contain the same message.","triggerScenarios":"vm() requesting a dump for a lvmid whose process has exited, is not a Java process, or whose JVM refuses attach (different user, permissions, attach disabled via -XX:+DisableAttachMechanism), or repeated attach attempts failing after the 3-second retry sleeps.","commonSituations":"Target game process crashed between listing and attach; attaching to a process owned by another user; JDK tools not available (no attach provider); security software blocking the attach socket; attach mechanism disabled in the target JVM.","solutions":["Confirm the target process is alive (jps / process list) and re-list lvmids before attaching","Run the launcher and the target JVM under the same user account","Remove -XX:+DisableAttachMechanism from the target JVM's options and ensure it is a HotSpot JVM with attach support","Check OS permissions/security software blocking the attach mechanism, then retry"],"exampleFix":"// before\nnew GameDumpGenerator().vm(lvmid); // lvmid may be stale\n// after\nif (ProcessHandle.of(lvmid).isPresent()) {\n    new GameDumpGenerator().vm(lvmid);\n} else {\n    LOG.warning(\"Process \" + lvmid + \" exited; skipping dump\");\n}","handlingStrategy":"try-catch","validationCode":"if (ProcessHandle.of(lvmid).isEmpty()) { skip; } // and verify same user account","typeGuard":"static boolean canAttach(long lvmid) {\n    return ProcessHandle.of(lvmid).map(p -> p.isAlive()).orElse(false);\n}","tryCatchPattern":"try { dump = generator.vm(lvmid); }\ncatch (IOException e) { if (e.getMessage().startsWith(\"Cannot attach VM\")) { LOG.warning(\"Attach failed for \" + lvmid); } else throw e; }","preventionTips":["Re-enumerate live JVMs immediately before attaching","Ensure the target JVM allows attach (no -XX:+DisableAttachMechanism) and runs as the same user","Run on a JDK that includes attach providers; add retries with backoff"],"tags":["jvm","attach","diagnostics"],"backgroundTag":"jvm-attach-failed","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}