{"record":{"id":"b197fcc0819fa148","repo":"HMCL-dev/HMCL","slug":"game-instance-already-exists","errorCode":null,"errorMessage":"Game instance already exists: ","messagePattern":"Game instance already exists: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/multimc/MultiMCModpackInstallTask.java","lineNumber":183,"sourceCode":"                repository.removeInstanceFromDisk(instanceId);\n        });\n    }\n\n    @Override\n    public boolean doPreExecute() {\n        return true;\n    }\n\n    /// Reserves the instance in a repository draft before preparing tasks that write its root.\n    @Override\n    public void preExecute() throws Exception {\n        DefaultGameRepositoryDraft openedDraft = repository.openDraft();\n        draft = openedDraft;\n        try {\n            // Construction fixes the mode; the captured snapshot only verifies that it is still valid.\n            boolean targetExists = openedDraft.getBaseSnapshot().hasInstance(instanceId);\n            if (this.updateTarget == null && targetExists) {\n                throw new IllegalStateException(\"Game instance already exists: \" + instanceId);\n            }\n            if (this.updateTarget != null && !targetExists) {\n                throw new IllegalStateException(\"Game instance no longer exists: \" + instanceId);\n            }\n\n            openedDraft.put(new GameInstanceManifest(instanceId));\n            newInstallationReserved = this.updateTarget == null;\n        } catch (IOException | RuntimeException e) {\n            try {\n                openedDraft.abort();\n            } catch (IOException cleanupFailure) {\n                e.addSuppressed(cleanupFailure);\n            }\n            draft = null;\n            throw e;\n        }\n\n        // Stage #0: General Setup","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCLCore/src/main/java/org/jackhuang/hmcl/modpack/multimc/MultiMCModpackInstallTask.java#L165-L201","documentation":"preExecute reserves the target instance inside a repository draft; in install mode (updateTarget == null) the instance must not already exist. Thrown when the base snapshot reports the instance ID as present, preventing an accidental overwrite of an existing instance.","triggerScenarios":"Running a new-installation MultiMCModpackInstallTask (no DefaultGameInstance update target) whose instanceId already exists in the repository at execution time.","commonSituations":"Installing the same modpack twice under the same instance name; a race where another task created the instance between task construction and execution.","solutions":["Choose a different instanceId for the new installation.","Delete or rename the existing instance first.","Use the update-mode constructor with the existing DefaultGameInstance to update in place."],"exampleFix":"// before\ntask = new MultiMCModpackInstallTask(dm, zip, modpack, cfg, existingId);\n// after\nif (!repository.hasInstance(existingId)) {\n    task = new MultiMCModpackInstallTask(dm, zip, modpack, cfg, existingId);\n} else {\n    task = new MultiMCModpackInstallTask(dm, zip, modpack, cfg, repository.getInstance(existingId)); // update\n}","handlingStrategy":"validation","validationCode":"if (repository.hasInstance(instanceId)) { /* use update mode or pick another id */ }","typeGuard":null,"tryCatchPattern":"try { task.run(); } catch (IllegalStateException e) { if (e.getMessage().startsWith(\"Game instance already exists\")) { /* rename or delete */ } }","preventionTips":["Generate unique instance IDs per install","Check instance existence before launching install tasks","Clean up failed installs before retrying"],"tags":["java","modpack","duplicate","illegal-state"],"backgroundTag":"file-already-exists","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}