{"record":{"id":"31ec3d7d33d96ad4","repo":"LykosAI/StabilityMatrix","slug":"failed-to-remove-existing-venv-for-python-upgrade-ensure","errorCode":null,"errorMessage":"Failed to remove existing venv for Python upgrade. Ensure Forge is not running and retry.","messagePattern":"Failed to remove existing venv for Python upgrade\\. Ensure Forge is not running and retry\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Models/Packages/ForgeClassic.cs","lineNumber":363,"sourceCode":"    }\n\n    private void ResetVenvForPythonUpgrade(string installLocation, Action<ProcessOutput>? onConsoleOutput)\n    {\n        var venvPath = Path.Combine(installLocation, \"venv\");\n        if (!Directory.Exists(venvPath))\n            return;\n\n        try\n        {\n            Directory.Delete(venvPath, recursive: true);\n            onConsoleOutput?.Invoke(\n                ProcessOutput.FromStdOutLine(\"[ForgeClassic] Removed existing venv before Python upgrade.\")\n            );\n        }\n        catch (Exception e)\n        {\n            Logger.Warn(e, \"Failed to remove existing venv during Forge Classic Python upgrade\");\n            throw new InvalidOperationException(\n                \"Failed to remove existing venv for Python upgrade. Ensure Forge is not running and retry.\",\n                e\n            );\n        }\n    }\n\n    private void BackupLegacyConfigFiles(string installLocation, Action<ProcessOutput>? onConsoleOutput)\n    {\n        BackupLegacyConfigFile(installLocation, \"config.json\", onConsoleOutput);\n        BackupLegacyConfigFile(installLocation, \"ui-config.json\", onConsoleOutput);\n    }\n\n    private void BackupLegacyConfigFile(\n        string installLocation,\n        string fileName,\n        Action<ProcessOutput>? onConsoleOutput\n    )\n    {","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Models/Packages/ForgeClassic.cs#L345-L381","documentation":"ResetVenvForPythonUpgrade throws InvalidOperationException when deleting the existing venv (to rebuild it for a Python upgrade) fails. The inner exception is logged as a warning and wrapped, telling the user to make sure Forge is not running.","triggerScenarios":"InstallPackage detects a Python-version change and calls ResetVenvForPythonUpgrade; Directory deletion of the venv throws (files locked by a running Forge/python process, permission issues).","commonSituations":"Forge (or a stray python.exe from the venv) still running while upgrading Python; Windows file locks on venv DLLs; OneDrive/AV locking files in the install folder.","solutions":["Ensure Forge and any python processes from that venv are fully stopped, then retry the install/upgrade.","Manually delete the venv directory in the package folder and re-run the install.","Use Task Manager to kill orphaned python.exe processes holding locks.","Check folder permissions / exclude the install path from antivirus real-time locking."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// ensure no Forge/python processes are running before upgrading python\nvar running = Process.GetProcessesByName(\"python\").Concat(Process.GetProcessesByName(\"forge\")).Any();\nif (running) { /* ask user to close Forge first */ }","typeGuard":null,"tryCatchPattern":"try { await package.InstallPackage(...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"remove existing venv\"))\n{ PromptUserToCloseForgeThenRetry(); }","preventionTips":["Fully close Forge and stray python.exe processes before Python upgrades","Exclude the install directory from antivirus/OneDrive sync locking","Retry after unlocking; the operation is safe to repeat once the venv is removable"],"tags":["venv","file-lock","python-upgrade","windows"],"backgroundTag":"file-write-failed","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}