修复微信与 WhatsApp 停止脚本在进程已退出时的误报异常。

统一使用静默 taskkill 包装函数处理 sidecar 进程清理,避免 NativeCommandError 噪声并修正 PowerShell 只读变量名冲突,保证 start_all/status_all/stop_all 链路稳定结束。

Made-with: Cursor
This commit is contained in:
oliver 2026-04-29 01:19:00 +08:00
parent 853dc1812f
commit 26562b1ca4
3 changed files with 55 additions and 40 deletions

View file

@ -36,16 +36,19 @@ function Get-SidecarProcesses {
} }
} }
function Invoke-TaskKillQuiet {
param(
[Parameter(Mandatory = $true)]
[string]$ProcessId
)
cmd.exe /c "taskkill /PID $ProcessId /T /F 1>nul 2>nul" | Out-Null
return $LASTEXITCODE
}
function Stop-SidecarProcesses { function Stop-SidecarProcesses {
$procs = @(Get-SidecarProcesses | Sort-Object ProcessId -Descending) $procs = @(Get-SidecarProcesses | Sort-Object ProcessId -Descending)
$oldNativePref = $PSNativeCommandUseErrorActionPreference foreach ($proc in $procs) {
try { [void](Invoke-TaskKillQuiet -ProcessId ([string]$proc.ProcessId))
$PSNativeCommandUseErrorActionPreference = $false
foreach ($proc in $procs) {
taskkill.exe /PID $proc.ProcessId /T /F 2>$null | Out-Null
}
} finally {
$PSNativeCommandUseErrorActionPreference = $oldNativePref
} }
return $procs.Count return $procs.Count
} }

View file

@ -37,27 +37,33 @@ function Get-SidecarProcesses {
} }
} }
function Invoke-TaskKillQuiet {
param(
[Parameter(Mandatory = $true)]
[string]$ProcessId,
[switch]$ForceKill
)
$forceArg = ""
if ($ForceKill) { $forceArg = " /F" }
cmd.exe /c "taskkill /PID $ProcessId /T$forceArg 1>nul 2>nul" | Out-Null
return $LASTEXITCODE
}
function Stop-SidecarProcesses { function Stop-SidecarProcesses {
param( param(
[switch]$ForceKill [switch]$ForceKill
) )
$procs = @(Get-SidecarProcesses | Sort-Object ProcessId -Descending) $procs = @(Get-SidecarProcesses | Sort-Object ProcessId -Descending)
$oldNativePref = $PSNativeCommandUseErrorActionPreference foreach ($proc in $procs) {
try { if ($ForceKill) {
$PSNativeCommandUseErrorActionPreference = $false [void](Invoke-TaskKillQuiet -ProcessId ([string]$proc.ProcessId) -ForceKill)
foreach ($proc in $procs) { continue
if ($ForceKill) { }
taskkill.exe /PID $proc.ProcessId /T /F 2>$null | Out-Null $code = Invoke-TaskKillQuiet -ProcessId ([string]$proc.ProcessId)
continue if ($code -ne 0) {
} # Some process trees require force kill on Windows.
taskkill.exe /PID $proc.ProcessId /T 2>$null | Out-Null [void](Invoke-TaskKillQuiet -ProcessId ([string]$proc.ProcessId) -ForceKill)
if ($LASTEXITCODE -ne 0) {
# Some process trees require force kill on Windows.
taskkill.exe /PID $proc.ProcessId /T /F 2>$null | Out-Null
}
} }
} finally {
$PSNativeCommandUseErrorActionPreference = $oldNativePref
} }
return $procs.Count return $procs.Count
} }
@ -79,20 +85,14 @@ if (-not $procId) {
exit 0 exit 0
} }
$oldNativePref = $PSNativeCommandUseErrorActionPreference if ($Force) {
try { [void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill)
$PSNativeCommandUseErrorActionPreference = $false } else {
if ($Force) { $code = Invoke-TaskKillQuiet -ProcessId ([string]$procId)
taskkill.exe /PID $procId /T /F 2>$null | Out-Null if ($code -ne 0) {
} else { # Retry with /F to avoid noisy parent/child kill failures.
taskkill.exe /PID $procId /T 2>$null | Out-Null [void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill)
if ($LASTEXITCODE -ne 0) {
# Retry with /F to avoid noisy parent/child kill failures.
taskkill.exe /PID $procId /T /F 2>$null | Out-Null
}
} }
} finally {
$PSNativeCommandUseErrorActionPreference = $oldNativePref
} }
$killed = Stop-SidecarProcesses -ForceKill:$Force $killed = Stop-SidecarProcesses -ForceKill:$Force

View file

@ -19,15 +19,27 @@ $oclawRoot = Resolve-RepoRoot
$sidecarRoot = Join-Path $oclawRoot "data\\channel_sidecar\\$ChannelId" $sidecarRoot = Join-Path $oclawRoot "data\\channel_sidecar\\$ChannelId"
$sidecarPidFile = Join-Path $sidecarRoot "pid.txt" $sidecarPidFile = Join-Path $sidecarRoot "pid.txt"
function Invoke-TaskKillQuiet {
param(
[Parameter(Mandatory = $true)]
[string]$ProcessId,
[switch]$ForceKill
)
$forceArg = ""
if ($ForceKill) { $forceArg = " /F" }
cmd.exe /c "taskkill /PID $ProcessId /T$forceArg 1>nul 2>nul" | Out-Null
return $LASTEXITCODE
}
if (Test-Path $sidecarPidFile) { if (Test-Path $sidecarPidFile) {
$procId = (Get-Content -Path $sidecarPidFile -ErrorAction SilentlyContinue | Select-Object -First 1) $procId = (Get-Content -Path $sidecarPidFile -ErrorAction SilentlyContinue | Select-Object -First 1)
if ($procId) { if ($procId) {
if ($Force) { if ($Force) {
taskkill.exe /PID $procId /T /F 2>$null | Out-Null [void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill)
} else { } else {
taskkill.exe /PID $procId /T 2>$null | Out-Null $code = Invoke-TaskKillQuiet -ProcessId ([string]$procId)
if ($LASTEXITCODE -ne 0) { if ($code -ne 0) {
taskkill.exe /PID $procId /T /F 2>$null | Out-Null [void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill)
} }
} }
} }