Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

install.ps1: guard the mirror env restore in the outer finally (#12849)

Daniel Han7cb49da1356f
diff --git a/.github/workflows/studio-windows-inference-smoke.yml b/.github/workflows/studio-windows-inference-smoke.yml
index 7d56c22ee3e..85fa9f475f9 100644
--- a/.github/workflows/studio-windows-inference-smoke.yml
+++ b/.github/workflows/studio-windows-inference-smoke.yml
@@ -188,6 +188,8 @@ jobs:
if ($LASTEXITCODE) { exit $LASTEXITCODE }
pwsh -NoProfile -File tests/studio/test_path_probe_access_denied.ps1
if ($LASTEXITCODE) { exit $LASTEXITCODE }
+ pwsh -NoProfile -File tests/studio/test_unsloth_torch_override.ps1
+ if ($LASTEXITCODE) { exit $LASTEXITCODE }
# cross-platform-parity-ci.yml, which is the host it describes.
# uninstall.ps1: native uninstall must keep the shared unsloth.ico while a
diff --git a/install.ps1 b/install.ps1
index bf6b7a99394..0231aa70c4f 100644
--- a/install.ps1
+++ b/install.ps1
@@ -12179,10 +12179,13 @@ try {
}
$script:WoaResolverEnvSaved = $null
}
- foreach ($_mirrorEnvName in @($script:MirrorEnvSaved.Keys)) {
- $_mirrorEnvValue = $script:MirrorEnvSaved[$_mirrorEnvName]
- if ($null -eq $_mirrorEnvValue) { Remove-Item "Env:$_mirrorEnvName" -ErrorAction SilentlyContinue }
- else { Set-Item "Env:$_mirrorEnvName" $_mirrorEnvValue }
+ # Guarded like the block above: @($null.Keys) is one $null item, and indexing it would stop this finally before the cleanup below.
+ if ($script:MirrorEnvSaved) {
+ foreach ($_mirrorEnvName in @($script:MirrorEnvSaved.Keys)) {
+ $_mirrorEnvValue = $script:MirrorEnvSaved[$_mirrorEnvName]
+ if ($null -eq $_mirrorEnvValue) { Remove-Item "Env:$_mirrorEnvName" -ErrorAction SilentlyContinue }
+ else { Set-Item "Env:$_mirrorEnvName" $_mirrorEnvValue }
+ }
}
# UNSLOTH_KEPT_TORCH is a process-scoped handoff, and the session outlives the installer.
Remove-Item Env:UNSLOTH_KEPT_TORCH -ErrorAction SilentlyContinue
diff --git a/tests/studio/test_unsloth_torch_override.ps1 b/tests/studio/test_unsloth_torch_override.ps1
index a236b8def0f..6e56640c350 100644
--- a/tests/studio/test_unsloth_torch_override.ps1
+++ b/tests/studio/test_unsloth_torch_override.ps1
@@ -136,6 +136,8 @@ Set-Content -LiteralPath $leakFile -Encoding ascii -Value @(
"private-pkg @ https://svc:[email protected]/private-1.0-py3-none-any.whl")
$script:TorchOverridesFile = $leakFile
$env:UNSLOTH_KEPT_TORCH = "2.11.0"
+# No mirror fallback ran: nothing was saved, which must not stop the cleanup below.
+$script:MirrorEnvSaved = $null
# Strip the `finally { ... }` wrapper and run the statements: Invoke-Expression uses this scope.
$finallyBody = ($finallyText.Trim() -replace '(?s)^\{', '') -replace '(?s)\}$', ''
try {
@@ -147,6 +149,23 @@ Check "kept-torch handoff still cleared" ($null -eq $env:UNSLOTH_KEPT_TORCH)
Check "tracked path reset so a rerun cannot re-remove it" ($null -eq $script:TorchOverridesFile)
Remove-Item -LiteralPath $leakFile -Force -ErrorAction SilentlyContinue
+Write-Host "outer finally hands the caller's mirror variables back, then still cleans up"
+$mirrorLeak = [System.IO.Path]::GetTempFileName()
+$script:TorchOverridesFile = $mirrorLeak
+$env:UNSLOTH_TEST_MIRROR_ADDED = "https://mirror.example/simple"
+$env:UNSLOTH_TEST_MIRROR_CALLER = "https://mirror.example/simple"
+$script:MirrorEnvSaved = @{ UNSLOTH_TEST_MIRROR_ADDED = $null; UNSLOTH_TEST_MIRROR_CALLER = "caller-value" }
+try {
+ try { throw "simulated terminating error mid-install" }
+ finally { Invoke-Expression $finallyBody }
+} catch { }
+Check "a variable the fallback added is removed" ($null -eq $env:UNSLOTH_TEST_MIRROR_ADDED)
+Check "a variable the caller had is restored" ($env:UNSLOTH_TEST_MIRROR_CALLER -eq "caller-value")
+Check "the overrides file is still removed after the mirror restore" (-not (Test-Path -LiteralPath $mirrorLeak))
+Remove-Item Env:UNSLOTH_TEST_MIRROR_CALLER -ErrorAction SilentlyContinue
+Remove-Item -LiteralPath $mirrorLeak -Force -ErrorAction SilentlyContinue
+$script:MirrorEnvSaved = $null
+
Write-Host "and it deletes it in a scope built only from install.ps1's own top-level definitions"
# The replay above runs in THIS scope, which was handed the guard by the Invoke-Expression higher
# up -- a scope production does not have. A fresh runspace inherits nothing, so seeding it with