Skip to content

Commit b68c8b3

Browse files
committed
fix(windows): tighten backend cleanup process matching
1 parent f411d41 commit b68c8b3

1 file changed

Lines changed: 51 additions & 27 deletions

File tree

src-tauri/windows/kill-backend-processes.ps1

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,70 @@ if ([string]::IsNullOrWhiteSpace($installRoot)) {
2121

2222
$installRootWithSep = $installRoot + [string][char]92
2323
$currentPid = $PID
24-
$commandLineFallbackNames = @(
24+
$targetProcessNames = @(
2525
"python.exe",
2626
"pythonw.exe",
2727
"astrbot-desktop-tauri.exe",
2828
"astrbot.exe"
2929
)
3030

31-
Get-CimInstance Win32_Process |
31+
function Test-IsUnderInstallRoot {
32+
param(
33+
[Parameter(Mandatory = $false)]
34+
[string]$PathValue
35+
)
36+
37+
if ([string]::IsNullOrWhiteSpace($PathValue)) {
38+
return $false
39+
}
40+
41+
try {
42+
$normalized = [System.IO.Path]::GetFullPath($PathValue).TrimEnd([char]92).ToLowerInvariant()
43+
return $normalized -eq $installRoot -or $normalized.StartsWith($installRootWithSep)
44+
} catch {
45+
return $false
46+
}
47+
}
48+
49+
function Get-CommandExecutablePath {
50+
param(
51+
[Parameter(Mandatory = $false)]
52+
[string]$CommandLine
53+
)
54+
55+
if ([string]::IsNullOrWhiteSpace($CommandLine)) {
56+
return $null
57+
}
58+
59+
$trimmed = $CommandLine.Trim()
60+
if ($trimmed.StartsWith('"')) {
61+
$endQuote = $trimmed.IndexOf('"', 1)
62+
if ($endQuote -gt 1) {
63+
return $trimmed.Substring(1, $endQuote - 1)
64+
}
65+
return $null
66+
}
67+
68+
$firstSpace = $trimmed.IndexOf(' ')
69+
if ($firstSpace -gt 0) {
70+
return $trimmed.Substring(0, $firstSpace)
71+
}
72+
return $trimmed
73+
}
74+
75+
$nameFilter = ($targetProcessNames | ForEach-Object { "Name='$_'" }) -join " OR "
76+
77+
Get-CimInstance Win32_Process -Filter $nameFilter |
3278
ForEach-Object {
3379
if ($_.ProcessId -eq $currentPid) {
3480
return
3581
}
3682

37-
$shouldStop = $false
38-
39-
try {
40-
if ($_.ExecutablePath) {
41-
$exePath = [System.IO.Path]::GetFullPath($_.ExecutablePath).TrimEnd([char]92).ToLowerInvariant()
42-
if ($exePath -eq $installRoot -or $exePath.StartsWith($installRootWithSep)) {
43-
$shouldStop = $true
44-
}
45-
}
46-
} catch {
47-
# Ignore per-process executable path errors and continue cleanup.
48-
}
83+
$shouldStop = Test-IsUnderInstallRoot -PathValue $_.ExecutablePath
4984

5085
if (-not $shouldStop) {
51-
try {
52-
$name = [string]$_.Name
53-
$nameLower = $name.ToLowerInvariant()
54-
if ($_.CommandLine -and ($commandLineFallbackNames -contains $nameLower)) {
55-
$commandLine = [string]$_.CommandLine
56-
$commandLineLower = $commandLine.ToLowerInvariant()
57-
if ($commandLineLower.Contains($installRootWithSep) -or $commandLineLower.Contains($installRoot)) {
58-
$shouldStop = $true
59-
}
60-
}
61-
} catch {
62-
# Ignore per-process command line errors and continue cleanup.
63-
}
86+
$commandExePath = Get-CommandExecutablePath -CommandLine $_.CommandLine
87+
$shouldStop = Test-IsUnderInstallRoot -PathValue $commandExePath
6488
}
6589

6690
if ($shouldStop) {

0 commit comments

Comments
 (0)