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.

Unsloth Studio Installer: hold the install lock as a file as well as a mutex (#11094)

Daniel Han66c0811b7251
diff --git a/.github/workflows/cross-platform-parity-ci.yml b/.github/workflows/cross-platform-parity-ci.yml
index 971fa05fd0c..03eeddca52a 100644
--- a/.github/workflows/cross-platform-parity-ci.yml
+++ b/.github/workflows/cross-platform-parity-ci.yml
@@ -98,6 +98,7 @@ on:
- 'tests/studio/test_amd_name_arch_r9700.ps1'
- 'tests/studio/test_nvidia_library_inventory.ps1'
- 'tests/studio/test_nvidia_smi_discovery.ps1'
+ - 'tests/studio/test_install_lock_excludes_aliases.ps1'
- 'tests/studio/test_application_control_cli_fallback.ps1'
# test_application_control_cli_fallback.ps1 pins install.ps1's trampoline against
# process.rs's, so a change to either spelling has to run here.
@@ -178,6 +179,7 @@ on:
- 'tests/studio/test_amd_name_arch_r9700.ps1'
- 'tests/studio/test_nvidia_library_inventory.ps1'
- 'tests/studio/test_nvidia_smi_discovery.ps1'
+ - 'tests/studio/test_install_lock_excludes_aliases.ps1'
- 'tests/studio/test_application_control_cli_fallback.ps1'
# test_application_control_cli_fallback.ps1 pins install.ps1's trampoline against
# process.rs's, so a change to either spelling has to run here.
@@ -319,6 +321,16 @@ jobs:
if: runner.os == 'Windows'
shell: powershell
run: powershell -NoProfile -File tests\studio\test_nvidia_smi_discovery.ps1
+ # Spawns a real holder process and takes the lock through a junction (Windows) or a
+ # symbolic link (Linux), so it needs both legs: the alias mechanisms differ per OS and
+ # .NET enforces FileShare.None through a different mechanism on each.
+ - name: Install lock alias-exclusion tests
+ shell: pwsh
+ run: pwsh -NoProfile -File tests/studio/test_install_lock_excludes_aliases.ps1
+ - name: Install lock alias-exclusion tests (Windows PowerShell 5.1)
+ if: runner.os == 'Windows'
+ shell: powershell
+ run: powershell -NoProfile -File tests\studio\test_install_lock_excludes_aliases.ps1
# AST-extracted helpers plus a real .cmd written to a temp dir, so no venv and no
# Windows needed. The 1260 classification is exercised against crafted exceptions:
# a hosted runner has no Application Control policy to be denied by. The enforced
diff --git a/install.ps1 b/install.ps1
index 0a236892a65..80912891144 100644
--- a/install.ps1
+++ b/install.ps1
@@ -2789,15 +2789,28 @@ exit 1
Join-Path $env:LOCALAPPDATA "Unsloth Studio"
} else { $null }
+ # Cleared per invocation: script scope outlives a run, and under `irm | iex` one session can
+ # install twice. A stale $true makes the lock skip claiming a root it just created.
+ $script:StudioEnvRootPath = $null
+ $script:StudioEnvRootExisted = $false
+
if ($envOverride) {
# Tilde expansion: env vars aren't subject to it when quoted on assignment.
if ($envOverride -eq "~" -or $envOverride -like "~/*" -or $envOverride -like "~\*") {
$envOverride = (Join-Path $env:USERPROFILE $envOverride.Substring(1).TrimStart('/','\'))
}
try {
+ # BEFORE the create: this is the only moment anyone can still tell. The lock runs
+ # thousands of lines below and asks whether the root existed, which by then is yes for
+ # every env-mode root, so its ownership branch would never fire for the roots it was
+ # written for.
+ $script:StudioEnvRootPath = $envOverride
+ $script:StudioEnvRootExisted = [System.IO.Directory]::Exists($envOverride)
# .NET API: New-Item -Path treats brackets as wildcards (no -LiteralPath on PS 5.1).
[System.IO.Directory]::CreateDirectory($envOverride) | Out-Null
$StudioHome = (Resolve-Path -LiteralPath $envOverride).Path
+ # Re-recorded against the resolved spelling, which is what the lock is handed.
+ $script:StudioEnvRootPath = $StudioHome
} catch {
Write-StudioLine "ERROR: $envOverrideVar=$envOverride cannot be created or accessed." -ForegroundColor Red
# Same as the --tauri rejection above: still before the lock finally.
@@ -2888,6 +2901,10 @@ exit 1
# The emptiness test is inline, not Test-DirectoryHasEntries, which is defined below this
# function's first caller: the name error would land in the catch and skip the claim in
# silence. An unreadable root counts as occupied, so failure means "do not claim".
+ # Above its earliest reader, not beside the lock helpers: Write-StudioRootOwnerMarker filters
+ # this name out of its emptiness test thousands of lines earlier, and a $null filters nothing.
+ $script:StudioInstallLockFileName = ".unsloth-install.lock"
+
function Write-StudioRootOwnerMarker {
param([Parameter(Mandatory = $true)][string]$Root)
try {
@@ -2898,7 +2915,10 @@ exit 1
if (Test-Path -LiteralPath $Root) {
$occupied = $true
try {
+ # The lock's own file is not occupancy: it is created in this root before
+ # anything else, so counting it makes a fresh root look like somebody else's.
$occupied = @(Get-ChildItem -LiteralPath $Root -Force -ErrorAction Stop |
+ Where-Object { $_.Name -ne $script:StudioInstallLockFileName } |
Select-Object -First 1).Count -gt 0
} catch { $occupied = $true }
$claimable = (
@@ -2920,8 +2940,23 @@ exit 1
# Get-Item -Force, not Test-Path: the latter follows a dangling link and answers
# false, after which WriteAllText follows the link and writes outside the root.
Remove-Item -LiteralPath $marker -Force -ErrorAction SilentlyContinue
- if (Get-Item -LiteralPath $marker -Force -ErrorAction SilentlyContinue) { return }
- [System.IO.File]::WriteAllText($marker, "")
+ # CreateNew, not check-then-write: the pair left a window to plant a link at the name
+ # and have the write truncate its target. Residual, stated rather than implied away: a
+ # DANGLING link is still followed and creates a zero-byte file at its target, which
+ # cannot destroy an existing one. FILE_FLAG_OPEN_REPARSE_POINT is the complete answer
+ # and .NET does not expose it here.
+ $markerStream = $null
+ try {
+ $markerStream = [System.IO.File]::Open($marker,
+ [System.IO.FileMode]::CreateNew,
+ [System.IO.FileAccess]::Write,
+ [System.IO.FileShare]::None)
+ } catch {
+ # No marker is fine; the venv writes its own later.
+ return
+ } finally {
+ if ($markerStream) { try { $markerStream.Dispose() } catch {} }
+ }
} catch { }
}
@@ -5188,6 +5223,183 @@ exit 0
try { $Mutex.ReleaseMutex() } catch {} finally { $Mutex.Dispose() }
}
+ # Mutex AND file, because they exclude different things. The mutex is named from a hash of the
+ # resolved path, so two spellings of one directory fail to exclude each other whenever the
+ # resolver is inexact; a file inside the destination has no such problem, because the
+ # filesystem resolves the alias. Both are kept: a released installer only knows the mutex, so
+ # dropping it would stop an old run and a new one seeing each other during an upgrade.
+
+ function Enter-StudioInstallLock {
+ param([Parameter(Mandatory = $true)][string]$Path)
+ $mutex = Enter-StudioInstallMutex -Path $Path
+ if ($null -eq $mutex) { return $null }
+ $stream = $null
+ try {
+ # Race-safe: concurrent creators both succeed, and only one opens the file exclusively
+ # below. A throw here surfaces as the caller's install-lock error.
+ $existedBefore = [System.IO.Directory]::Exists($Path)
+ # An env-mode root is created thousands of lines above, so the check just above answers
+ # yes for every one of them; that site recorded what it found, so prefer its answer for
+ # this same directory.
+ #
+ # One direction only. It may say "I created this root, so treat it as new", never the
+ # opposite: a record claiming the root existed cannot be true while the root is absent
+ # now, so it is a leftover from an earlier run in the same session.
+ if ($script:StudioEnvRootPath -and
+ $script:StudioEnvRootPath.Equals($Path, [System.StringComparison]::OrdinalIgnoreCase) -and
+ -not $script:StudioEnvRootExisted) {
+ $existedBefore = $false
+ }
+ $null = [System.IO.Directory]::CreateDirectory($Path)
+ $lockPath = Join-Path $Path $script:StudioInstallLockFileName
+ # A link planted at the lock path is not a lock, it is a way to point our exclusive
+ # handle at somebody else's file: File.Open follows it, so the target would be held
+ # open with FileShare.None for the whole install even though nothing is written to it.
+ # Remove the link itself, never its target, then let the open below create a real
+ # file. Get-Item -Force rather than Test-Path, which follows a dangling link and
+ # answers false; Write-StudioRootOwnerMarker guards the same hazard the same way.
+ $existingLock = $null
+ try {
+ $existingLock = Get-Item -LiteralPath $lockPath -Force -ErrorAction SilentlyContinue
+ } catch { $existingLock = $null }
+ if ($existingLock -and
+ ($existingLock.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) {
+ # A detected link that will NOT go must stop the install, not be shrugged off.
+ #
+ # Not an empty catch: a link that can be inspected but not deleted would carry on
+ # into File.Open, which follows it, and a zero-byte target passes the length check
+ # too. The throw reaches the outer handler, which drops the mutex and reports a
+ # lock-creation failure rather than a phantom second installer.
+ try {
+ Remove-Item -LiteralPath $lockPath -Force -ErrorAction Stop
+ } catch {
+ throw "A link is planted at the install lock path $lockPath and cannot be removed."
+ }
+ }
+ # A HARD link carries no reparse attribute, so the check above cannot see it and
+ # File.Open follows it. The discriminator is the opened stream's Length: nothing is
+ # ever written here, so ours is always zero bytes and a planted alias worth denying
+ # access to is not. Read from the handle, which also closes the check-to-open window.
+ #
+ # At most one repair, and CreateNew rather than OpenOrCreate: the mutex only serialises
+ # identical spellings, so if somebody re-created the entry in between, this must fail
+ # rather than hand out a second lock.
+ $repaired = $false
+ while ($true) {
+ if ($repaired) { $mode = [System.IO.FileMode]::CreateNew }
+ else { $mode = [System.IO.FileMode]::OpenOrCreate }
+ try {
+ $stream = [System.IO.File]::Open(
+ $lockPath,
+ $mode,
+ [System.IO.FileAccess]::ReadWrite,
+ [System.IO.FileShare]::None)
+ } catch [System.IO.IOException] {
+ # Only a sharing or lock violation means "another installer holds it"; a
+ # storage fault reported that way would send the user hunting a second
+ # installer that does not exist. HResult's low 16 bits carry the Win32 code: 32
+ # ERROR_SHARING_VIOLATION, 33 ERROR_LOCK_VIOLATION. Off Windows .NET implements
+ # FileShare with flock and reports errno 11 (EAGAIN) instead, measured rather
+ # than assumed. 80, 183 and 17 are how the CreateNew reopen reports losing the
+ # race, which means the same thing to the caller.
+ $code = $_.Exception.HResult -band 0xFFFF
+ $lost = $repaired -and ($code -eq 80 -or $code -eq 183 -or $code -eq 17)
+ if (-not $lost -and $code -ne 32 -and $code -ne 33 -and $code -ne 11) { throw }
+ if ($stream) { $stream.Dispose() }
+ Exit-StudioInstallMutex -Mutex $mutex
+ return $null
+ }
+ # The reparse test above ran on the PATHNAME, so a link can be swapped in before
+ # this open, and a zero-byte target slips past the Length test below. Re-read now
+ # the handle is held. This narrows the window rather than closing it: closing it
+ # needs FILE_FLAG_OPEN_REPARSE_POINT, and removing native imports is the point of
+ # this workstream. The residual needs a root another user can already write to.
+ if ($stream.Length -eq 0) {
+ $entry = $null
+ try { $entry = Get-Item -LiteralPath $lockPath -Force -ErrorAction Stop } catch { $entry = $null }
+ if ($entry -and (($entry.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0)) {
+ $stream.Dispose()
+ $stream = $null
+ throw "A link was planted at the install lock path $lockPath while it was being opened."
+ }
+ break
+ }
+ if ($repaired) {
+ # A file this run just created cannot be non-empty. Fail loudly, not in a loop.
+ throw "The install lock file at $lockPath is not empty after being replaced."
+ }
+ # Move the entry ASIDE rather than delete it, and let the reopen create a real file
+ # at the freed name.
+ #
+ # Deleting a planted hard link costs only its own directory entry, which is why it
+ # looked safe, but an ORDINARY non-empty file at this name loses its contents
+ # merely because somebody started the installer. Refusing instead lets a planted
+ # hard link block every install. The discriminator is the link count and .NET does
+ # not expose it here, so there is no cheap way to tell them apart. A rename needs
+ # no discrimination: the planted link leaves the lock
+ # path either way, and the ordinary file keeps its bytes under a name that says
+ # what happened to it.
+ $stream.Dispose()
+ $stream = $null
+ $displaced = "$lockPath.displaced-" + (Get-Date -Format "yyyyMMddHHmmss") +
+ "-" + [guid]::NewGuid().ToString("N").Substring(0, 8)
+ try {
+ Move-Item -LiteralPath $lockPath -Destination $displaced -Force -ErrorAction Stop
+ } catch {
+ # Same discrimination and the same codes as the open below: only a sharing or
+ # lock violation is a concurrent install. The code is dug out of the exception
+ # chain because a cmdlet failure arrives wrapped.
+ $mvCode = 0
+ $mvEx = $_.Exception
+ while ($mvEx) {
+ if ($mvEx -is [System.IO.IOException]) {
+ $mvCode = $mvEx.HResult -band 0xFFFF
+ break
+ }
+ $mvEx = $mvEx.InnerException
+ }
+ Exit-StudioInstallMutex -Mutex $mutex
+ if ($mvCode -ne 32 -and $mvCode -ne 33 -and $mvCode -ne 11) { throw }
+ return $null
+ }
+ $repaired = $true
+ }
+ # Residual, stated plainly: a hard link to a genuinely EMPTY file is indistinguishable
+ # from our own and is used as the lock. Nothing is written to it, so the only thing
+ # denied for the install is access to a zero-byte file.
+ # Nothing is written to the handle on purpose: holding it open exclusively IS the
+ # lock, and File.Open follows a link, so a planted lock file would have its TARGET
+ # truncated merely by starting the installer.
+ # The lock is now the first thing that can create the root, so without this marker a
+ # run that died before the first real write leaves a root scripts/uninstall.ps1's
+ # _IsStudioRoot refuses to remove as somebody else's.
+ #
+ # Only when this call created the directory: a UNSLOTH_STUDIO_HOME pointed at one the
+ # user already had must never be claimed, or uninstall would delete it.
+ #
+ # Through the helper, not a bare WriteAllText, which follows a planted link just as
+ # File.Open does. The helper removes the entry and confirms its absence first, with
+ # Get-Item -Force so a dangling link is not mistaken for nothing there.
+ if (-not $existedBefore) {
+ Write-StudioRootOwnerMarker -Root $Path
+ }
+ return [pscustomobject]@{ Mutex = $mutex; Stream = $stream; Path = $lockPath }
+ } catch {
+ if ($stream) { try { $stream.Dispose() } catch {} }
+ Exit-StudioInstallMutex -Mutex $mutex
+ throw
+ }
+ }
+
+ function Exit-StudioInstallLock {
+ param($Lock)
+ if ($null -eq $Lock) { return }
+ # Closing the handle IS the release, so a dead process leaves nothing to clean up. The
+ # file stays: deleting it would race another installer that has just opened it.
+ if ($Lock.Stream) { try { $Lock.Stream.Dispose() } catch {} }
+ Exit-StudioInstallMutex -Mutex $Lock.Mutex
+ }
+
function Test-StudioProtectedPathMatch {
param(
[Parameter(Mandatory = $true)][string]$Candidate,
@@ -5393,12 +5605,12 @@ exit 0
}
}
try {
- $studioInstallMutex = Enter-StudioInstallMutex -Path $StudioHome
+ $studioInstallLock = Enter-StudioInstallLock -Path $StudioHome
} catch {
Write-StudioLine "[ERROR] Could not create the Unsloth install lock: $($_.Exception.Message)" -ForegroundColor Red
return (Exit-InstallFailure "Could not create the Unsloth install lock")
}
- if ($null -eq $studioInstallMutex) {
+ if ($null -eq $studioInstallLock) {
Write-StudioLine "[ERROR] Another Unsloth Studio install or repair is already running." -ForegroundColor Red
Write-StudioLine " Wait for it to finish, then re-run install.ps1." -ForegroundColor Yellow
return (Exit-InstallFailure "Another Unsloth Studio install or repair is already running")
@@ -10336,7 +10548,7 @@ sys.exit(2 if conflict else (0 if installed else 1))
for ($i = $studioRuntimeMutexes.Count - 1; $i -ge 0; $i--) {
Exit-StudioInstallMutex -Mutex $studioRuntimeMutexes[$i]
}
- Exit-StudioInstallMutex -Mutex $studioInstallMutex
+ Exit-StudioInstallLock -Lock $studioInstallLock
# Matters for `irm | iex`, where these are the user's own session variables.
Restore-StudioTempEnvironment
}
diff --git a/tests/python/test_windows_installer_concurrency_guard.py b/tests/python/test_windows_installer_concurrency_guard.py
index b41c35d665e..284aa11a505 100644
--- a/tests/python/test_windows_installer_concurrency_guard.py
+++ b/tests/python/test_windows_installer_concurrency_guard.py
@@ -866,7 +866,7 @@ def test_runtime_path_hash_is_defined_before_custom_root_lock_uses_it():
def test_guard_and_mutex_precede_rollback_and_release_after_restore():
source = INSTALL_PS1.read_text(encoding = "utf-8")
- acquire = source.index("$studioInstallMutex = Enter-StudioInstallMutex -Path $StudioHome")
+ acquire = source.index("$studioInstallLock = Enter-StudioInstallLock -Path $StudioHome")
root_match = source.index("$studioTauriRootMatch =", acquire)
managed_root = source.index("$studioUsesTauriManagedRoot =", root_match)
runtime_lock_needed = source.index("$studioNeedsRuntimeLock =", managed_root)
@@ -890,7 +890,9 @@ def test_guard_and_mutex_precede_rollback_and_release_after_restore():
prompt = source.index("Start Unsloth Studio now?", restore)
autostart = source.index("Start-Process -FilePath $VenvPython", prompt)
release_runtime = source.rindex("Exit-StudioInstallMutex -Mutex $studioRuntimeMutexes[$i]")
- release_install = source.rindex("Exit-StudioInstallMutex -Mutex $studioInstallMutex")
+ # The install lock is a mutex AND a file now; the runtime locks above are still plain mutexes,
+ # which is why only this one changed spelling. The ordering contract is unchanged.
+ release_install = source.rindex("Exit-StudioInstallLock -Lock $studioInstallLock")
wait_for_exit = source.rindex("$studioAutoStartProcess.WaitForExit()")
assert (
diff --git a/tests/studio/test_install_lock_excludes_aliases.ps1 b/tests/studio/test_install_lock_excludes_aliases.ps1
new file mode 100644
index 00000000000..06a908366e6
--- /dev/null
+++ b/tests/studio/test_install_lock_excludes_aliases.ps1
@@ -0,0 +1,659 @@
+#!/usr/bin/env pwsh
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+# The install lock must exclude two installers that reach one destination by different spellings.
+#
+# The named mutex alone cannot: its name hashes the resolved path, so an inexact resolver leaves a
+# junction and its target with two names that do not exclude each other (install.ps1's
+# Get-StudioPathHash). A lock file inside the destination has no such failure mode, because the
+# filesystem resolves the alias.
+#
+# Only the FILE half is exercised here, with the mutex stubbed. The mutex half is unchanged by this
+# work and is covered by tests/python/test_windows_installer_concurrency_guard.py.
+# Run: pwsh -NoProfile -File tests/studio/test_install_lock_excludes_aliases.ps1
+
+$ErrorActionPreference = "Stop"
+$root = (Resolve-Path ([System.IO.Path]::Combine($PSScriptRoot, "..", ".."))).Path
+$installPs1 = Join-Path $root "install.ps1"
+
+$failures = 0
+function Check($name, $cond) {
+ if ($cond) { Write-Host " PASS $name" }
+ else { Write-Host " FAIL $name" -ForegroundColor Red; $script:failures++ }
+}
+
+function Get-HelperSources($path, $names) {
+ $tokens = $null; $errors = $null
+ $ast = [System.Management.Automation.Language.Parser]::ParseFile($path, [ref]$tokens, [ref]$errors)
+ if ($errors) { $errors | ForEach-Object { $_.ToString() }; throw "$path has parse errors" }
+ $out = @()
+ foreach ($name in $names) {
+ $fn = $ast.FindAll({ param($n)
+ $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $name
+ }, $true)
+ if ($fn.Count -lt 1) { throw "expected $name in $path, found none" }
+ $out += $fn[0].Extent.Text
+ }
+ return $out
+}
+
+# The lock file's name is read out of install.ps1 rather than repeated here, so renaming it there
+# without updating the uninstaller or this test shows up as a failure instead of a silent pass.
+$installText = Get-Content -Raw -LiteralPath $installPs1
+$nameMatch = [regex]::Match($installText,
+ '\$script:StudioInstallLockFileName\s*=\s*"([^"]+)"')
+if (-not $nameMatch.Success) { throw "could not find `$script:StudioInstallLockFileName in install.ps1" }
+$lockFileName = $nameMatch.Groups[1].Value
+Write-Host " lock file name: $lockFileName"
+
+# Stubs for the mutex half. Always granting it isolates the file half: every refusal this test
+# observes is the file lock's doing, never the mutex's.
+function Enter-StudioInstallMutex { param([string]$Path) return "stub-mutex" }
+function Exit-StudioInstallMutex { param($Mutex) }
+
+foreach ($src in (Get-HelperSources $installPs1 @(
+ "Enter-StudioInstallLock", "Exit-StudioInstallLock",
+ "Test-StudioPlainFile", "Write-StudioRootOwnerMarker"))) {
+ Invoke-Expression $src
+}
+$script:StudioInstallLockFileName = $lockFileName
+
+$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("instlock-" + [guid]::NewGuid().ToString("N"))
+New-Item -ItemType Directory -Force -Path $tmp | Out-Null
+
+# A child process is the only honest way to test exclusion: .NET tracks handles per process, so a
+# same-process second open would be refused even if the OS were not enforcing anything.
+$holderPs1 = Join-Path $tmp "holder.ps1"
+@"
+param([string]`$LockPath, [string]`$ReadyFile, [int]`$HoldSeconds = 25)
+`$s = [System.IO.File]::Open(`$LockPath, [System.IO.FileMode]::OpenOrCreate,
+ [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
+New-Item -ItemType File -Force -Path `$ReadyFile | Out-Null
+Start-Sleep -Seconds `$HoldSeconds
+`$s.Dispose()
+"@ | Set-Content -LiteralPath $holderPs1 -Encoding UTF8
+
+function Start-Holder($lockPath) {
+ $ready = Join-Path $tmp ("ready-" + [guid]::NewGuid().ToString("N"))
+ $exe = (Get-Process -Id $PID).Path
+ # -WindowStyle is not supported by pwsh off Windows, so only ask for it where it exists.
+ $startArgs = @{
+ FilePath = $exe
+ ArgumentList = @("-NoProfile", "-File", $holderPs1, $lockPath, $ready)
+ PassThru = $true
+ }
+ if ($IsWindows -or $env:OS -eq "Windows_NT") { $startArgs["WindowStyle"] = "Hidden" }
+ $proc = Start-Process @startArgs
+ $deadline = [DateTime]::UtcNow.AddSeconds(30)
+ while (-not (Test-Path -LiteralPath $ready) -and [DateTime]::UtcNow -lt $deadline) {
+ Start-Sleep -Milliseconds 100
+ }
+ if (-not (Test-Path -LiteralPath $ready)) {
+ try { $proc.Kill() } catch {}
+ throw "the holder process never took the lock"
+ }
+ return $proc
+}
+
+function Stop-Holder($proc) {
+ try { if (-not $proc.HasExited) { $proc.Kill() } } catch {}
+ try { $proc.WaitForExit(10000) | Out-Null } catch {}
+}
+
+try {
+ $real = Join-Path $tmp "real"
+ New-Item -ItemType Directory -Force -Path $real | Out-Null
+
+ # 1. A destination that does not exist yet is created by taking the lock, since the installer
+ # locks before it has written anything.
+ $fresh = Join-Path $tmp "not-created-yet"
+ $freshLock = Enter-StudioInstallLock -Path $fresh
+ Check "a destination that does not exist is created" (Test-Path -LiteralPath $fresh)
+ Check "the lock is granted on a fresh destination" ($null -ne $freshLock)
+ Check "the lock file lands inside the destination" (
+ Test-Path -LiteralPath (Join-Path $fresh $lockFileName))
+ Check "a root the lock created identifies itself to the uninstaller" (
+ Test-Path -LiteralPath (Join-Path $fresh ".unsloth-studio-owned") -PathType Leaf)
+ Exit-StudioInstallLock -Lock $freshLock
+ Check "releasing does not delete the lock file" (
+ Test-Path -LiteralPath (Join-Path $fresh $lockFileName))
+
+ # The safety half: uninstall.ps1 deletes what the marker names, so a UNSLOTH_STUDIO_HOME
+ # pointed at a directory the user already had must never carry it.
+ $preexisting = Join-Path $tmp "users-own-directory"
+ New-Item -ItemType Directory -Force -Path $preexisting | Out-Null
+ "keep me" | Set-Content -LiteralPath (Join-Path $preexisting "important.txt")
+ $preLock = Enter-StudioInstallLock -Path $preexisting
+ Check "an existing directory is NOT claimed as an Unsloth root" (
+ -not (Test-Path -LiteralPath (Join-Path $preexisting ".unsloth-studio-owned")))
+ Check "an existing directory's contents are untouched" (
+ (Get-Content -Raw -LiteralPath (Join-Path $preexisting "important.txt")).Trim() -eq "keep me")
+ Exit-StudioInstallLock -Lock $preLock
+
+ # 2. Released means re-takeable. A lock that leaked would block every later run.
+ $again = Enter-StudioInstallLock -Path $fresh
+ Check "the lock can be taken again after release" ($null -ne $again)
+ Exit-StudioInstallLock -Lock $again
+
+ # 3. The control. Two genuinely different destinations must NOT exclude each other, or every
+ # check below would pass for the trivial reason that the lock always refuses.
+ $other = Join-Path $tmp "other"
+ New-Item -ItemType Directory -Force -Path $other | Out-Null
+ $holder = Start-Holder (Join-Path $real $lockFileName)
+ try {
+ $otherLock = Enter-StudioInstallLock -Path $other
+ Check "a different destination is not blocked (control)" ($null -ne $otherLock)
+ if ($otherLock) { Exit-StudioInstallLock -Lock $otherLock }
+
+ # 4. Same destination, same spelling: refused.
+ $sameLock = Enter-StudioInstallLock -Path $real
+ Check "the same destination is refused while held" ($null -eq $sameLock)
+ if ($sameLock) { Exit-StudioInstallLock -Lock $sameLock }
+
+ # 5. The point of the change. Same destination reached through an ALIAS: also refused.
+ # A junction on Windows and a symlink elsewhere; neither needs elevation for a
+ # directory junction, and the symlink path is what the Linux lane exercises.
+ $alias = Join-Path $tmp "alias"
+ $madeAlias = $false
+ try {
+ if ($IsWindows -or $env:OS -eq "Windows_NT") {
+ New-Item -ItemType Junction -Path $alias -Target $real -ErrorAction Stop | Out-Null
+ } else {
+ New-Item -ItemType SymbolicLink -Path $alias -Target $real -ErrorAction Stop | Out-Null
+ }
+ $madeAlias = $true
+ } catch {
+ Write-Host " SKIP could not create a directory alias: $($_.Exception.Message)" -ForegroundColor Yellow
+ }
+ if ($madeAlias) {
+ # Guard against a vacuous pass: if the alias does not actually reach the same
+ # directory, the refusal below would prove nothing.
+ $probe = Join-Path $real ("probe-" + [guid]::NewGuid().ToString("N"))
+ New-Item -ItemType File -Force -Path $probe | Out-Null
+ $viaAlias = Join-Path $alias (Split-Path -Leaf $probe)
+ Check "the alias really reaches the same directory" (Test-Path -LiteralPath $viaAlias)
+ Remove-Item -LiteralPath $probe -Force -ErrorAction SilentlyContinue
+
+ $aliasLock = Enter-StudioInstallLock -Path $alias
+ Check "an ALIAS of the held destination is refused" ($null -eq $aliasLock)
+ if ($aliasLock) { Exit-StudioInstallLock -Lock $aliasLock }
+
+ # Not vacuous: these two spellings differ, so the mutex name hashed from them differs
+ # too and the mutex alone would have let both installers through.
+ $spellReal = [System.IO.Path]::GetFullPath($real).ToUpperInvariant()
+ $spellAlias = [System.IO.Path]::GetFullPath($alias).ToUpperInvariant()
+ Check "the two spellings differ, so a name derived from them cannot exclude (bites)" (
+ $spellReal -ne $spellAlias)
+ }
+ } finally {
+ Stop-Holder $holder
+ }
+
+ # 6. A planted link at the lock path must not have its target touched. File.Open follows one,
+ # so anything written through the handle lands in the TARGET. Taking the lock writes
+ # nothing at all, which is why this holds.
+ $victimDir = Join-Path $tmp "victim-root"
+ New-Item -ItemType Directory -Force -Path $victimDir | Out-Null
+ $victim = Join-Path $tmp "precious.txt"
+ $precious = "do not truncate me"
+ $precious | Set-Content -LiteralPath $victim
+ $planted = Join-Path $victimDir $lockFileName
+ $plantedOk = $false
+ try {
+ New-Item -ItemType SymbolicLink -Path $planted -Target $victim -ErrorAction Stop | Out-Null
+ $plantedOk = $true
+ } catch {
+ Write-Host " SKIP could not plant a file link: $($_.Exception.Message)" -ForegroundColor Yellow
+ }
+ if ($plantedOk) {
+ $plantedLock = $null
+ try { $plantedLock = Enter-StudioInstallLock -Path $victimDir } catch {}
+ # WHILE the lock is held, which is the point: following the link would hold the victim
+ # with FileShare.None for the whole install, unreadable here despite nothing being written.
+ $victimReadable = $false
+ try { $victimReadable = ((Get-Content -Raw -LiteralPath $victim).Trim() -eq $precious) } catch {}
+ Check "a planted link's target stays readable while the lock is held" $victimReadable
+ Check "the lock file is a real file, not the planted link" (
+ $null -ne (Get-Item -LiteralPath $planted -Force -ErrorAction SilentlyContinue) -and
+ ((Get-Item -LiteralPath $planted -Force).Attributes -band
+ [System.IO.FileAttributes]::ReparsePoint) -eq 0)
+ if ($plantedLock) { Exit-StudioInstallLock -Lock $plantedLock }
+ Check "a planted link's target is not truncated" (
+ (Get-Content -Raw -LiteralPath $victim).Trim() -eq $precious)
+ }
+
+ # 6b. The same hazard planted as a HARD link. A hard link is a second directory entry for an
+ # existing file, so it carries no reparse point attribute and the attributes check cannot
+ # see it at all; File.Open follows it just the same and would hold the victim with
+ # FileShare.None for the whole install. That is a denial of service on somebody else's
+ # file, reached by a route the reparse check does not cover.
+ $hardDir = Join-Path $tmp "hardlink-root"
+ New-Item -ItemType Directory -Force -Path $hardDir | Out-Null
+ $hardVictim = Join-Path $tmp "precious-hard.txt"
+ $hardPrecious = "do not deny me"
+ $hardPrecious | Set-Content -LiteralPath $hardVictim -NoNewline
+ $hardVictimLength = (Get-Item -LiteralPath $hardVictim).Length
+ $hardPlanted = Join-Path $hardDir $lockFileName
+ $hardOk = $false
+ try {
+ New-Item -ItemType HardLink -Path $hardPlanted -Target $hardVictim -ErrorAction Stop | Out-Null
+ $hardOk = $true
+ } catch {
+ Write-Host " SKIP this host cannot create a hard link: $($_.Exception.Message)" -ForegroundColor Yellow
+ }
+ if ($hardOk) {
+ # Proof the planted entry really is a hard link and not something the existing defence
+ # already catches. Without this the checks below could pass for the wrong reason.
+ $hardItem = Get-Item -LiteralPath $hardPlanted -Force
+ Check "a planted hard link carries NO reparse attribute, so the attributes check is blind to it (bites)" (
+ ($hardItem.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -eq 0)
+ Check "the planted hard link really reaches the victim's contents" (
+ $hardItem.Length -eq $hardVictimLength -and $hardVictimLength -gt 0)
+
+ $hardLock = $null
+ try { $hardLock = Enter-StudioInstallLock -Path $hardDir } catch {}
+ Check "the lock is still granted with a hard link planted" ($null -ne $hardLock)
+ # WHILE the lock is held. Get-Content is not enough on its own, since a shared read can
+ # succeed against a handle opened for reading; ask for the installer's own exclusivity.
+ $victimFree = $false
+ try {
+ $probeStream = [System.IO.File]::Open($hardVictim, [System.IO.FileMode]::Open,
+ [System.IO.FileAccess]::Read, [System.IO.FileShare]::None)
+ $probeStream.Dispose()
+ $victimFree = $true
+ } catch {}
+ Check "a planted hard link's target is NOT held open by the lock" $victimFree
+ Check "the lock file is a fresh empty file, not the victim" (
+ (Get-Item -LiteralPath $hardPlanted -Force).Length -eq 0)
+ if ($hardLock) { Exit-StudioInstallLock -Lock $hardLock }
+ Check "a planted hard link's target keeps its contents" (
+ (Get-Content -Raw -LiteralPath $hardVictim) -eq $hardPrecious)
+ Check "a planted hard link's target keeps its length" (
+ (Get-Item -LiteralPath $hardVictim).Length -eq $hardVictimLength)
+
+ # 6c. Removing a non-empty entry must never steal a lock somebody else holds. This file is
+ # both non-empty and held by a real second process, so a fix that removed first and
+ # asked afterwards would hand out a second lock for one destination.
+ $stealDir = Join-Path $tmp "steal-root"
+ New-Item -ItemType Directory -Force -Path $stealDir | Out-Null
+ $stealLockPath = Join-Path $stealDir $lockFileName
+ $stealBody = "held by somebody else"
+ $stealBody | Set-Content -LiteralPath $stealLockPath -NoNewline
+ $stealHolder = Start-Holder $stealLockPath
+ try {
+ $stolen = "not-run"
+ try { $stolen = Enter-StudioInstallLock -Path $stealDir } catch {}
+ Check "a held non-empty lock is refused, not removed and retaken" ($null -eq $stolen)
+ if ($stolen -and $stolen -ne "not-run") { Exit-StudioInstallLock -Lock $stolen }
+ Check "the held lock file still exists after the refusal" (
+ Test-Path -LiteralPath $stealLockPath)
+ } finally {
+ Stop-Holder $stealHolder
+ }
+ # Its contents are only readable once the holder has let go, since the holder takes it
+ # with FileShare.None exactly as the installer does.
+ Check "the held lock file survives the refusal intact" (
+ (Get-Content -Raw -LiteralPath $stealLockPath) -eq $stealBody)
+ }
+
+ # 7. A real I/O fault must not read as "another installer is running". The lock path is a
+ # DIRECTORY here, so the open fails for a reason that is nobody's concurrent install.
+ $blockedRoot = Join-Path $tmp "blocked-root"
+ New-Item -ItemType Directory -Force -Path (Join-Path $blockedRoot $lockFileName) | Out-Null
+ $blockedThrew = $false
+ $blockedResult = "not-run"
+ try { $blockedResult = Enter-StudioInstallLock -Path $blockedRoot } catch { $blockedThrew = $true }
+ Check "a non-sharing failure is not silently reported as a busy lock" (
+ $blockedThrew -or $null -ne $blockedResult)
+ if ($blockedResult -and $blockedResult -ne "not-run") { Exit-StudioInstallLock -Lock $blockedResult }
+
+ # 8. A holder that dies without releasing must not wedge the destination: the OS closes the
+ # handle, so the next run takes the lock. This is why the file is not deleted on release.
+ $crashDir = Join-Path $tmp "crashed"
+ New-Item -ItemType Directory -Force -Path $crashDir | Out-Null
+ $crashHolder = Start-Holder (Join-Path $crashDir $lockFileName)
+ Stop-Holder $crashHolder
+ $afterCrash = $null
+ $deadline = [DateTime]::UtcNow.AddSeconds(15)
+ while ($null -eq $afterCrash -and [DateTime]::UtcNow -lt $deadline) {
+ $afterCrash = Enter-StudioInstallLock -Path $crashDir
+ if ($null -eq $afterCrash) { Start-Sleep -Milliseconds 200 }
+ }
+ Check "a killed holder leaves no stale lock" ($null -ne $afterCrash)
+ if ($afterCrash) { Exit-StudioInstallLock -Lock $afterCrash }
+
+ # The lock file must not make a fresh root look like somebody else's directory. It is created
+ # inside $StudioHome before anything else runs, and the owner marker refuses a root that is not
+ # empty, so counting it means a fresh UNSLOTH_STUDIO_HOME never gets its marker and a run that
+ # dies before the venv marker leaves a root the uninstaller will not remove.
+ $StudioRedirectMode = 'env'
+ $freshRoot = Join-Path $tmp "fresh-env-root"
+ New-Item -ItemType Directory -Force -Path $freshRoot | Out-Null
+ $freshLock = Enter-StudioInstallLock -Path $freshRoot
+ Check "the fresh root took its lock" ($null -ne $freshLock)
+ Write-StudioRootOwnerMarker -Root $freshRoot
+ Check "a fresh env-mode root carrying only the lock file is still claimed" (
+ Test-Path -LiteralPath (Join-Path $freshRoot ".unsloth-studio-owned") -PathType Leaf)
+ if ($freshLock) { Exit-StudioInstallLock -Lock $freshLock }
+
+ # Stale env-root state from an EARLIER run in the same session must not answer for this one.
+ # Script scope outlives a run and `irm | iex` can install twice, so a stale $true makes the
+ # lock skip claiming a root it just created.
+ $StudioRedirectMode = 'env'
+ $staleRoot = Join-Path $tmp "stale-env-root"
+ # Deliberately NOT created: this is the run where the directory is genuinely new.
+ Check "control: the stale root really is absent" (-not (Test-Path -LiteralPath $staleRoot))
+ $script:StudioEnvRootPath = $staleRoot
+ $script:StudioEnvRootExisted = $true
+ $staleLock = Enter-StudioInstallLock -Path $staleRoot
+ Check "the run still took its lock" ($null -ne $staleLock)
+ Check "a root created by THIS run is claimed despite the stale record" (
+ Test-Path -LiteralPath (Join-Path $staleRoot ".unsloth-studio-owned") -PathType Leaf)
+ if ($staleLock) { Exit-StudioInstallLock -Lock $staleLock }
+ $script:StudioEnvRootPath = $null
+ $script:StudioEnvRootExisted = $false
+
+ # And the reset itself has to run before the override is read, or it would wipe the record
+ # the override site just made. Read the installer rather than trusting the ordering.
+ $srcAll = [System.IO.File]::ReadAllText((Join-Path $root "install.ps1"))
+ $resetAt = $srcAll.IndexOf('$script:StudioEnvRootPath = $null')
+ $recordAt = $srcAll.IndexOf('$script:StudioEnvRootPath = $envOverride')
+ Check "the per-invocation reset exists" ($resetAt -gt 0)
+ Check "the reset clears the existed flag too" (
+ $srcAll -match '\$script:StudioEnvRootExisted = \$false')
+ Check "the reset runs before the override records anything" (
+ $resetAt -gt 0 -and $recordAt -gt 0 -and $resetAt -lt $recordAt)
+
+ # Bites control for the same code path: a root holding somebody else's file is NOT claimed,
+ # so the check above is about the lock file specifically and not about env mode claiming
+ # everything it is pointed at.
+ $occupiedRoot = Join-Path $tmp "occupied-env-root"
+ New-Item -ItemType Directory -Force -Path $occupiedRoot | Out-Null
+ [System.IO.File]::WriteAllText((Join-Path $occupiedRoot "someone-elses-notes.txt"), "mine")
+ $occupiedLock = Enter-StudioInstallLock -Path $occupiedRoot
+ Write-StudioRootOwnerMarker -Root $occupiedRoot
+ Check "control: an env-mode root holding another file is NOT claimed" (
+ -not (Test-Path -LiteralPath (Join-Path $occupiedRoot ".unsloth-studio-owned")))
+ if ($occupiedLock) { Exit-StudioInstallLock -Lock $occupiedLock }
+
+ # The owner marker must not follow a link either. Between the directory appearing and the
+ # marker being written, another user who can write to the root can plant a link there, and a
+ # bare WriteAllText truncates its target under the installer's rights.
+ $StudioRedirectMode = 'default'
+ $markerVictim = Join-Path $tmp "marker-victim.txt"
+ [System.IO.File]::WriteAllText($markerVictim, "keep me")
+ $markerRoot = Join-Path $tmp "marker-root"
+ New-Item -ItemType Directory -Force -Path $markerRoot | Out-Null
+ $plantedMarker = Join-Path $markerRoot ".unsloth-studio-owned"
+ $madeLink = $false
+ try {
+ New-Item -ItemType SymbolicLink -Path $plantedMarker -Target $markerVictim -ErrorAction Stop | Out-Null
+ $madeLink = $true
+ } catch {}
+ if (-not $madeLink) {
+ # Windows needs Developer Mode or elevation for this. Say so rather than reporting a pass.
+ Write-Host " SKIP cannot create a symbolic link on this host" -ForegroundColor Yellow
+ } else {
+ Check "the planted marker link really reaches the victim (bites)" (
+ (Get-Content -Raw -LiteralPath $plantedMarker) -eq "keep me")
+ Write-StudioRootOwnerMarker -Root $markerRoot
+ Check "the planted marker link's target is not truncated" (
+ (Get-Content -Raw -LiteralPath $markerVictim) -eq "keep me")
+ $written = Get-Item -LiteralPath $plantedMarker -Force -ErrorAction SilentlyContinue
+ # Three checks, not one conjunction: this row failed on 5.1 while passing on pwsh 7, and a
+ # single Check would report "the marker is wrong" without saying which part.
+ Check "the marker exists after the claim" ($null -ne $written)
+ Check "the marker is empty, as this installer only ever creates it" (
+ $null -ne $written -and $written.Length -eq 0)
+ # The ReparsePoint attribute, not .Target: .Target is $null on pwsh 7 but an EMPTY
+ # COLLECTION on 5.1, which is not $null, so the old spelling failed on an ordinary file.
+ Check "the marker is a real file, not the planted link" (
+ $null -ne $written -and
+ (($written.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -eq 0))
+ }
+
+ # And no window between the two: delete, confirm, write leaves a moment to plant the link
+ # again. The creation must FAIL on an entry already there rather than overwrite it. Driven by
+ # leaving a link in place with no delete in front of it, which is what the window produces.
+ $raceVictim = Join-Path $tmp "race-victim.txt"
+ [System.IO.File]::WriteAllText($raceVictim, "still here")
+ $raceRoot = Join-Path $tmp "race-root"
+ New-Item -ItemType Directory -Force -Path $raceRoot | Out-Null
+ $raceMarker = Join-Path $raceRoot ".unsloth-studio-owned"
+ $madeRaceLink = $false
+ try {
+ New-Item -ItemType SymbolicLink -Path $raceMarker -Target $raceVictim -ErrorAction Stop | Out-Null
+ $madeRaceLink = $true
+ } catch {}
+ if (-not $madeRaceLink) {
+ Write-Host " SKIP cannot create a symbolic link on this host" -ForegroundColor Yellow
+ } else {
+ $marker = $raceMarker
+ $createThrew = $false
+ $s = $null
+ try {
+ $s = [System.IO.File]::Open($marker, [System.IO.FileMode]::CreateNew,
+ [System.IO.FileAccess]::Write, [System.IO.FileShare]::None)
+ } catch { $createThrew = $true } finally { if ($s) { $s.Dispose() } }
+ Check "creating the marker refuses a name that is already taken" ($createThrew -eq $true)
+ Check "and the link's target is untouched by the refusal" (
+ (Get-Content -Raw -LiteralPath $raceVictim) -eq "still here")
+ }
+
+ # Read the helper too: the drive above exercises CreateNew directly, so it would still pass
+ # with the helper back on a check followed by a WriteAllText.
+ $markerFn = @(Get-HelperSources $installPs1 @("Write-StudioRootOwnerMarker"))[0]
+ Check "the marker helper creates with CreateNew" ($markerFn -match 'FileMode\]::CreateNew')
+ Check "and no longer writes the marker through WriteAllText" (
+ $markerFn -notmatch 'WriteAllText\(\$marker')
+
+ # A non-empty entry at the lock path is moved aside, never destroyed.
+ #
+ # The length check exists because a planted HARD link would otherwise be held open with
+ # FileShare.None for the whole install. Deleting the entry handled that safely, since a hard
+ # link loses only its own directory entry. It did not handle an ORDINARY non-empty file at
+ # the same name that is its own only link: that one lost its contents merely because somebody
+ # started the installer, and nothing this installer writes can produce such a file.
+ #
+ # Refusing instead would hand the first case back as a denial of service, and the two cannot
+ # be told apart cheaply: the discriminator is the link count and .NET does not expose it
+ # here. A rename needs no discrimination, so that is what this asserts.
+ $keepRoot = Join-Path $tmp "non-empty-lock-root"
+ New-Item -ItemType Directory -Force -Path $keepRoot | Out-Null
+ $keepLock = Join-Path $keepRoot $lockFileName
+ [System.IO.File]::WriteAllText($keepLock, "somebody else's bytes")
+ $keepLockObj = Enter-StudioInstallLock -Path $keepRoot
+ Check "an install still starts with a non-empty entry at the lock path" ($null -ne $keepLockObj)
+ Check "the lock file it ends up holding is a fresh empty one" (
+ (Get-Item -LiteralPath $keepLock -Force).Length -eq 0)
+ $displaced = @(Get-ChildItem -LiteralPath $keepRoot -Force |
+ Where-Object { $_.Name -like "$lockFileName.displaced-*" })
+ Check "the original was moved aside rather than deleted" ($displaced.Count -eq 1)
+ Check "and it still has its contents" (
+ $displaced.Count -eq 1 -and
+ (Get-Content -Raw -LiteralPath $displaced[0].FullName) -eq "somebody else's bytes")
+ if ($keepLockObj) { Exit-StudioInstallLock -Lock $keepLockObj }
+
+ # The env-mode ownership branch must not be dead code. The override root is created thousands
+ # of lines before the lock, so the lock's "did it exist" question always answers yes and the
+ # claim never fires for the roots it was written for. The earlier checks call
+ # Write-StudioRootOwnerMarker themselves, so they prove the helper works and say nothing about
+ # whether the lock reaches it; this one takes the lock and then looks, with nothing between.
+ $envFresh = Join-Path $tmp "env-root-created-earlier"
+ [System.IO.Directory]::CreateDirectory($envFresh) | Out-Null # the early override create
+ $script:StudioEnvRootPath = $envFresh
+ $script:StudioEnvRootExisted = $false # it did NOT exist before that
+ $envLock = Enter-StudioInstallLock -Path $envFresh
+ Check "a root the override created this run is still claimed by the lock" (
+ Test-Path -LiteralPath (Join-Path $envFresh ".unsloth-studio-owned") -PathType Leaf)
+ if ($envLock) { Exit-StudioInstallLock -Lock $envLock }
+
+ # Bites control, and it is the safety half: a directory the USER already had must never be
+ # claimed, because the uninstaller deletes what the marker names.
+ $envOwned = Join-Path $tmp "env-root-user-already-had"
+ [System.IO.Directory]::CreateDirectory($envOwned) | Out-Null
+ $script:StudioEnvRootPath = $envOwned
+ $script:StudioEnvRootExisted = $true # it was there beforehand
+ $ownedLock = Enter-StudioInstallLock -Path $envOwned
+ Check "control: a root that existed before the run is NOT claimed" (
+ -not (Test-Path -LiteralPath (Join-Path $envOwned ".unsloth-studio-owned")))
+ if ($ownedLock) { Exit-StudioInstallLock -Lock $ownedLock }
+ $script:StudioEnvRootPath = $null
+
+ # And the lock's own fresh-root branch has to go THROUGH that helper. A second copy of the
+ # write inline would pass every check above while carrying the hazard, because the checks
+ # drive the helper directly. Read the branch instead of trusting it.
+ # @() around the call, not just an index: a single-element return unrolls to a bare string,
+ # and [0] on a string is its first CHARACTER, which matches neither pattern and passes the
+ # negative check for free. Observed here before it was fixed.
+ $lockFn = @(Get-HelperSources $installPs1 @("Enter-StudioInstallLock"))[0]
+ Check "the lock claims a fresh root through Write-StudioRootOwnerMarker" (
+ $lockFn -match 'Write-StudioRootOwnerMarker -Root \$Path')
+ Check "and does not write the marker itself" (
+ $lockFn -notmatch 'WriteAllText\([^)]*\.unsloth-studio-owned')
+
+ # A detected link that will not go must STOP the install. Under the old empty catch the run
+ # carried on into File.Open, which follows it, and a zero-byte target passed the length check
+ # too: the exact denial of service the removal exists to prevent. Driven by making it fail.
+ $undeletableRoot = Join-Path $tmp "undeletable-link-root"
+ New-Item -ItemType Directory -Force -Path $undeletableRoot | Out-Null
+ $linkVictim = Join-Path $tmp "link-victim-empty.txt"
+ [System.IO.File]::WriteAllText($linkVictim, "")
+ $undeletableLock = Join-Path $undeletableRoot $lockFileName
+ $madeUndeletable = $false
+ try {
+ New-Item -ItemType SymbolicLink -Path $undeletableLock -Target $linkVictim -ErrorAction Stop | Out-Null
+ $madeUndeletable = $true
+ } catch {}
+ if (-not $madeUndeletable) {
+ Write-Host " SKIP cannot create a symbolic link on this host" -ForegroundColor Yellow
+ } else {
+ # Remove-Item is what the lock calls; make it refuse for this one path only, which is what
+ # an ACL that permits inspection but not deletion looks like from in here.
+ $savedRemove = ${function:Remove-Item}
+ function Remove-Item {
+ param(
+ [Parameter(ValueFromPipeline = $true)]$InputObject,
+ [string]$LiteralPath, [string]$Path, [switch]$Force, [switch]$Recurse,
+ [string]$ErrorAction
+ )
+ if ($LiteralPath -and $LiteralPath.EndsWith($script:StudioInstallLockFileName)) {
+ throw [System.UnauthorizedAccessException]::new("access denied by test")
+ }
+ }
+ $linkThrew = $false
+ $linkResult = "unset"
+ try { $linkResult = Enter-StudioInstallLock -Path $undeletableRoot } catch { $linkThrew = $true }
+ ${function:Remove-Item} = $savedRemove
+ Check "an undeletable link at the lock path fails the install" (
+ $linkThrew -eq $true -and $linkResult -eq "unset")
+ Check "control: it did not quietly report a busy lock instead" ($linkResult -ne $null -or $linkThrew)
+ Check "and the link's target was never held or replaced" (
+ (Get-Item -LiteralPath $undeletableLock -Force).Target -eq $linkVictim)
+ }
+
+ # A rename that fails for a reason other than contention must surface: calling a read-only
+ # directory or a storage fault "already running" sends the user hunting a phantom installer.
+ $mvRoot = Join-Path $tmp "unmovable-lock-root"
+ New-Item -ItemType Directory -Force -Path $mvRoot | Out-Null
+ [System.IO.File]::WriteAllText((Join-Path $mvRoot $lockFileName), "not empty")
+ $savedMove = ${function:Move-Item}
+ function Move-Item {
+ param([string]$LiteralPath, [string]$Destination, [switch]$Force, [string]$ErrorAction)
+ throw [System.UnauthorizedAccessException]::new("rename denied by test")
+ }
+ $mvThrew = $false
+ $mvResult = "unset"
+ try { $mvResult = Enter-StudioInstallLock -Path $mvRoot } catch { $mvThrew = $true }
+ ${function:Move-Item} = $savedMove
+ Check "a rename denied by permissions is surfaced, not called a busy lock" (
+ $mvThrew -eq $true -and $mvResult -eq "unset")
+
+ # Bites control: a rename that fails with a SHARING violation is still the busy path, so the
+ # check above is about the error class and not about every failure now throwing.
+ [System.IO.File]::WriteAllText((Join-Path $mvRoot $lockFileName), "not empty")
+ function Move-Item {
+ param([string]$LiteralPath, [string]$Destination, [switch]$Force, [string]$ErrorAction)
+ # The two-argument constructor sets HResult; the field is not settable from here.
+ throw [System.IO.IOException]::new("sharing violation", 32)
+ }
+ $busyThrew = $false
+ $busyResult = "unset"
+ try { $busyResult = Enter-StudioInstallLock -Path $mvRoot } catch { $busyThrew = $true }
+ ${function:Move-Item} = $savedMove
+ Check "control: a sharing violation on the rename still reports a busy lock" (
+ $busyThrew -eq $false -and $null -eq $busyResult)
+} finally {
+ Remove-Item -LiteralPath $tmp -Recurse -Force -ErrorAction SilentlyContinue
+}
+
+Write-Host ""
+# The swapped-link race. The reparse test runs on the pathname BEFORE the open, so a link can be
+# swapped in between, and a target that is itself zero bytes slips past the Length test too. Drive
+# the SHIPPED expression, not a retyped copy, so a change to the real one cannot leave this passing.
+$lockSrc = [System.IO.File]::ReadAllText((Join-Path $root "install.ps1"))
+$guardAt = $lockSrc.IndexOf('A link was planted at the install lock path')
+Check "the post-open guard is present" ($guardAt -gt 0)
+
+$openAt = $lockSrc.IndexOf('$stream = [System.IO.File]::Open(')
+Check "the guard runs after the open, not before it" ($openAt -gt 0 -and $guardAt -gt $openAt)
+# Guarded: with the guard gone $guardAt is -1 and Substring would raise. A suite that dies
+# reports nothing at all.
+$guardBlock = if ($openAt -gt 0 -and $guardAt -gt $openAt) { $lockSrc.Substring($openAt, $guardAt - $openAt) } else { "" }
+Check "the guard releases the handle before it throws" ($guardBlock -match '\$stream\.Dispose\(\)[\s\S]*\$stream = \$null')
+
+$predicate = @($lockSrc -split "`n" | Where-Object {
+ $_ -match '\$entry -and \(\(\$entry\.Attributes -band \[System\.IO\.FileAttributes\]::ReparsePoint\) -ne 0\)' })
+Check "the predicate is one line of the shipped source" ($predicate.Count -eq 1)
+# Degrade to a failed check, not an indexing error: "the run crashed" is not a test result.
+$test = $null
+if ($predicate.Count -eq 1) {
+ # Two statements, not one expression: [scriptblock]::Create(A -replace B, C) binds C as a
+ # second ARGUMENT to Create rather than to -replace, and fails on the overload.
+ $rewritten = ($predicate[0].Trim() -replace '^if \(', 'return (') -replace '\) \{$', ')'
+ $test = [scriptblock]::Create($rewritten)
+}
+
+$raceDir = Join-Path ([System.IO.Path]::GetTempPath()) ("unsloth-lockrace-" + [guid]::NewGuid().ToString("N"))
+New-Item -ItemType Directory -Path $raceDir | Out-Null
+try {
+ # The exact case the Length test cannot see: a link whose target is itself zero bytes.
+ $victim = Join-Path $raceDir "someone-elses.lock"
+ [System.IO.File]::WriteAllText($victim, "")
+ $planted = Join-Path $raceDir "install.lock"
+ New-Item -ItemType SymbolicLink -Path $planted -Target $victim | Out-Null
+
+ $entry = Get-Item -LiteralPath $planted -Force
+ Check "a zero-byte target is still zero bytes, so the Length test alone would accept it" (
+ ([System.IO.FileInfo]$victim).Length -eq 0)
+ Check "the guard refuses a link swapped in under the lock path" ($null -ne $test -and (& $test) -eq $true)
+
+ # The control that makes the row above mean something: an ordinary empty lock file is fine.
+ $ordinary = Join-Path $raceDir "ordinary.lock"
+ [System.IO.File]::WriteAllText($ordinary, "")
+ $entry = Get-Item -LiteralPath $ordinary -Force
+ Check "control: the guard leaves an ordinary empty lock file alone" ($null -ne $test -and (& $test) -eq $false)
+
+ # A missing entry must not be read as a link either, or a lock the installer just created and
+ # an antivirus momentarily hid would fail the install instead of proceeding.
+ $entry = $null
+ Check "control: a vanished entry is not a link" ($null -ne $test -and (& $test) -eq $false)
+} finally {
+ Remove-Item -LiteralPath $raceDir -Recurse -Force -ErrorAction SilentlyContinue
+}
+
+# The gate has to be the LAST thing before the success line: checks appended after it print FAIL
+# and the suite exits 0 anyway, so CI reports green while the regression is live. That happened to
+# the swapped-link checks below, so the shape is asserted rather than trusted.
+$selfText = [System.IO.File]::ReadAllText($PSCommandPath)
+$gateAt = $selfText.LastIndexOf('if ($failures -gt 0)')
+$passAt = $selfText.LastIndexOf('All install-lock alias checks passed')
+$lastCheckAt = $selfText.LastIndexOf("`nCheck ")
+if ($gateAt -lt 0 -or $passAt -lt $gateAt -or $lastCheckAt -gt $gateAt) {
+ Write-Host " FAIL the failure gate is not the last thing before the success line" -ForegroundColor Red
+ exit 1
+}
+
+if ($failures -gt 0) { Write-Host "$failures check(s) failed" -ForegroundColor Red; exit 1 }
+Write-Host "All install-lock alias checks passed"
+exit 0