Skip to content

The agent that grows with you

fix(win): CIM-based PS in-use check and same-volume staging swap

David Metcalfed6a711c783e9
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index f9e481cd32c..d310fc15f1c 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -958,8 +958,20 @@ function Test-ManagedNodeInUse {
# tree open, and rewriting it then fails with WinError 5 (Access denied)
# on npm.cmd (#80926). Cheap pre-check used to skip destructive steps;
# the rename/move itself remains the authoritative guard.
- return @(Get-Process -ErrorAction SilentlyContinue |
- Where-Object { $_.Path -like "$NodeDir\*" }).Count -gt 0
+ #
+ # Check the executable path AND the command line: a cmd.exe wrapper
+ # running npm.cmd from the tree reports its own exe (cmd.exe lives in
+ # System32) while the tree path appears only in the command line.
+ # Win32_Process.CommandLine is available on Windows PowerShell 5.1 and
+ # 7+ (the Get-Process .CommandLine ETS property is 7.4+ only), and a
+ # single CIM query beats a per-process property access loop.
+ return @(
+ Get-CimInstance Win32_Process -ErrorAction SilentlyContinue |
+ Where-Object {
+ ($_.ExecutablePath -like "$NodeDir\*") -or
+ ($_.CommandLine -like "*$NodeDir*")
+ }
+ ).Count -gt 0
}
# Re-discover uv without re-installing it. Cross-process stage drivers
@@ -1579,11 +1591,26 @@ function Test-Node {
Where-Object { $_.LastWriteTime -lt (Get-Date).AddMinutes(-10) } |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
$stamp = [Guid]::NewGuid().ToString("N")
+ $staged = "$HermesHome\node.new-$stamp"
+ $backup = "$HermesHome\node.old-$stamp"
+ # Stage to a sibling directory so the final swap is a
+ # same-volume rename (atomic), not a cross-volume Move-Item
+ # (copy+delete, non-atomic -- a partial copy would leave a
+ # broken tree). Move from $env:TEMP here, rename below.
+ try {
+ Move-Item $extractedDir.FullName $staged -ErrorAction Stop
+ } catch {
+ Write-Warn "Failed to stage the new Node.js tree; aborting the Node upgrade."
+ Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
+ Remove-Item -Force $tmpZip -ErrorAction SilentlyContinue
+ return $false
+ }
if (Test-Path "$HermesHome\node") {
try {
- Rename-Item "$HermesHome\node" "$HermesHome\node.old-$stamp" -ErrorAction Stop
+ Rename-Item "$HermesHome\node" $backup -ErrorAction Stop
} catch {
Write-Warn "Hermes-managed Node.js is in use by a running app; deferring its upgrade. Close the app and re-run the update."
+ Remove-Item -Recurse -Force $staged -ErrorAction SilentlyContinue
Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
Remove-Item -Force $tmpZip -ErrorAction SilentlyContinue
return $false
@@ -1593,24 +1620,31 @@ function Test-Node {
# the litter-sweep cutoff to a concurrent heal. Touch it
# (best-effort) so the in-flight backup is never swept.
try {
- (Get-Item "$HermesHome\node.old-$stamp").LastWriteTime = Get-Date
+ (Get-Item $backup).LastWriteTime = Get-Date
} catch { }
- }
- try {
- Move-Item $extractedDir.FullName "$HermesHome\node" -ErrorAction Stop
- } catch {
- # Restore the live tree before bailing. A cross-volume
- # Move-Item may have left a partial target, so clear it
- # first (best-effort) before renaming the old tree back.
- if (Test-Path "$HermesHome\node.old-$stamp") {
- Remove-Item -Recurse -Force "$HermesHome\node" -ErrorAction SilentlyContinue
- Rename-Item "$HermesHome\node.old-$stamp" "$HermesHome\node" -ErrorAction SilentlyContinue
+ try {
+ Rename-Item $staged "$HermesHome\node" -ErrorAction Stop
+ } catch {
+ # Restore the live tree before bailing. The swap is a
+ # same-volume rename, so a failure leaves no partial
+ # target to clear.
+ Rename-Item $backup "$HermesHome\node" -ErrorAction SilentlyContinue
+ Remove-Item -Recurse -Force $staged -ErrorAction SilentlyContinue
+ Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
+ Remove-Item -Force $tmpZip -ErrorAction SilentlyContinue
+ return $false
+ }
+ Remove-Item -Recurse -Force $backup -ErrorAction SilentlyContinue
+ } else {
+ try {
+ Rename-Item $staged "$HermesHome\node" -ErrorAction Stop
+ } catch {
+ Remove-Item -Recurse -Force $staged -ErrorAction SilentlyContinue
+ Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
+ Remove-Item -Force $tmpZip -ErrorAction SilentlyContinue
+ return $false
}
- Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
- Remove-Item -Force $tmpZip -ErrorAction SilentlyContinue
- return $false
}
- Remove-Item -Recurse -Force "$HermesHome\node.old-$stamp" -ErrorAction SilentlyContinue
# Session PATH so the rest of this run sees node/npm.
$env:Path = "$HermesHome\node;$env:Path"