Never wait on the process tree when starting NetX.

Windows PowerShell 5.1 Start-Process -Wait (and `& exe` with captured
output) waits for every descendant / until the stdout pipe closes. The
postgres and NetX processes a start leaves running made runtime_ctl.ps1
never return: tray stuck on "Starting", Start Menu "Start NetX" never
opened the UI. Start-NetxPowerShell -Wait, the UAC relaunch and the Setup
wait in check_update now wait for the process itself (WaitForExit).

Service mode in runtime_ctl: start via sc.exe without blocking on
SERVICE_RUNNING; stop writes the supervisor stop flag, polls up to 120s,
then kills the WinSW tree as a last resort.

Verified in an offline Windows Sandbox (no VC++ redist, no Python).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
oliver 2026-10-14 00:46:44 +08:00
parent ce9a78b5ec
commit c8806c38bd
3 changed files with 61 additions and 13 deletions

View file

@ -16,19 +16,47 @@ $prog = Get-NetxProgramRoot -Override $ProgramRoot
$data = Get-NetxDataRoot -ProgramRoot $prog -Override $DataRoot
function Invoke-NetxChildScript([string]$Name, [string[]]$Extra = @()) {
& powershell.exe -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot $Name) `
-ProgramRoot $prog -DataRoot $data @Extra
return $LASTEXITCODE
# Never read the child's stdout: postgres / NetX processes it starts inherit the
# pipe, so `& powershell.exe` with captured output blocks until they exit (forever).
# Start-Process -Wait is no good either (it waits for the whole process tree).
$p = Start-NetxPowerShell -File (Join-Path $PSScriptRoot $Name) `
-Arguments (@("-ProgramRoot", $prog, "-DataRoot", $data) + $Extra) `
-WorkingDirectory $prog -WindowStyle Hidden -PassThru
$null = $p.Handle # keep a handle so ExitCode is available after exit
$p.WaitForExit()
return $p.ExitCode
}
function Stop-NetxService {
# Stop-Service waits forever on a service that will not stop; poll with a deadline
# and kill the WinSW process tree as a last resort.
$svc = Get-Service -Name "NetX" -ErrorAction SilentlyContinue
if (-not $svc -or $svc.Status -eq "Stopped") { return }
# Ask service_run.ps1 to exit ourselves rather than relying only on WinSW's stop hook.
Write-NetxServiceStopFlag -DataRoot $data
& sc.exe stop NetX | Out-Null
$deadline = (Get-Date).AddSeconds(120)
while ((Get-Date) -lt $deadline) {
Start-Sleep -Seconds 1
$svc.Refresh()
if ($svc.Status -eq "Stopped") { return }
}
$wmi = Get-CimInstance Win32_Service -Filter "Name='NetX'" -ErrorAction SilentlyContinue
if ($wmi -and $wmi.ProcessId) { & taskkill.exe /PID $wmi.ProcessId /T /F | Out-Null }
$null = Invoke-NetxChildScript "stop_netx_app.ps1"
}
function Start-NetxService {
# Don't block on SERVICE_RUNNING: callers wait for /health instead.
$svc = Get-Service -Name "NetX" -ErrorAction SilentlyContinue
if ($svc -and $svc.Status -eq "Stopped") { & sc.exe start NetX | Out-Null }
}
$runner = Get-NetxManagedRunner
switch ($runner) {
"service" {
switch ($Action) {
"start" { Start-Service -Name "NetX" }
"stop" { Stop-Service -Name "NetX" -Force }
"restart" { Restart-Service -Name "NetX" -Force }
}
if ($Action -ne "start") { Stop-NetxService }
if ($Action -ne "stop") { Start-NetxService }
}
"task" {
if ($Action -ne "start") {