LykosAI/StabilityMatrix · error · InvalidOperationException

Failed to remove existing venv for Python upgrade. Ensure…

Error message

Failed to remove existing venv for Python upgrade. Ensure Forge is not running and retry.

What it means

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.

Solutions

  1. Ensure Forge and any python processes from that venv are fully stopped, then retry the install/upgrade.
  2. Manually delete the venv directory in the package folder and re-run the install.
  3. Use Task Manager to kill orphaned python.exe processes holding locks.
  4. Check folder permissions / exclude the install path from antivirus real-time locking.
Defensive patterns

Strategy: retry

Validate before calling

// ensure no Forge/python processes are running before upgrading python
var running = Process.GetProcessesByName("python").Concat(Process.GetProcessesByName("forge")).Any();
if (running) { /* ask user to close Forge first */ }

Try / catch

try { await package.InstallPackage(...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("remove existing venv"))
{ PromptUserToCloseForgeThenRetry(); }

Prevention

When it happens

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

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

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/31ec3d7d33d96ad4. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Core/Models/Packages/ForgeClassic.cs:363

    }

    private void ResetVenvForPythonUpgrade(string installLocation, Action<ProcessOutput>? onConsoleOutput)
    {
        var venvPath = Path.Combine(installLocation, "venv");
        if (!Directory.Exists(venvPath))
            return;

        try
        {
            Directory.Delete(venvPath, recursive: true);
            onConsoleOutput?.Invoke(
                ProcessOutput.FromStdOutLine("[ForgeClassic] Removed existing venv before Python upgrade.")
            );
        }
        catch (Exception e)
        {
            Logger.Warn(e, "Failed to remove existing venv during Forge Classic Python upgrade");
            throw new InvalidOperationException(
                "Failed to remove existing venv for Python upgrade. Ensure Forge is not running and retry.",
                e
            );
        }
    }

    private void BackupLegacyConfigFiles(string installLocation, Action<ProcessOutput>? onConsoleOutput)
    {
        BackupLegacyConfigFile(installLocation, "config.json", onConsoleOutput);
        BackupLegacyConfigFile(installLocation, "ui-config.json", onConsoleOutput);
    }

    private void BackupLegacyConfigFile(
        string installLocation,
        string fileName,
        Action<ProcessOutput>? onConsoleOutput
    )
    {

View on GitHub (pinned to af93d6ef57)