Fix Windows service mode: it could neither start nor stop.

- Start: WinSW's NetX.out.log/err.log in data\runtime are the same files as
  start_netx.ps1's netx.out.log (case-insensitive) and stay open, so NetX
  could not truncate its log and every start failed. WinSW logs now go to
  data\runtime\service.
- Stop: nothing told the service_run.ps1 supervisor to exit, so it kept
  restarting NetX and WinSW waited forever. stop_netx_app.ps1 writes the
  service.stop flag (-KeepSupervisor for the supervisor's own restarts),
  service_run checks it every second, and runtime_ctl / service_lifecycle
  write it before stopping the service.
- WinSW launched powershell.exe as stop hook never reached the script
  (stuck on inherited std handles); run it via cmd with NUL stdin and output
  to data\runtime\service\stopexec.log. stop_netx_app.ps1 keeps a
  timestamped trace there.

Offline Windows Sandbox: service install -> healthy in 28s, stop in 20s.
Existing service installs need "install service" re-run to pick this up.

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 c8806c38bd
commit a7cc1f502b
4 changed files with 53 additions and 9 deletions

View file

@ -65,11 +65,21 @@ function Ensure-WinSW {
$stopPs1 = ConvertTo-WinSWPath (Join-Path $PSScriptRoot "stop_netx_app.ps1")
$progFs = ConvertTo-WinSWPath $prog
$dataFs = ConvertTo-WinSWPath $data
$logPath = ConvertTo-WinSWPath (Join-Path $data "data\runtime")
if (-not (Test-Path (Join-Path $data "data\runtime"))) {
New-Item -ItemType Directory -Path (Join-Path $data "data\runtime") -Force | Out-Null
# Own folder: WinSW writes <id>.out.log / <id>.err.log = NetX.out.log, which on Windows is
# the same file as start_netx.ps1's netx.out.log. WinSW keeps it open, so NetX could not
# truncate its log and every service start failed.
$svcLogDir = Join-Path $data "data\runtime\service"
$logPath = ConvertTo-WinSWPath $svcLogDir
if (-not (Test-Path $svcLogDir)) {
New-Item -ItemType Directory -Path $svcLogDir -Force | Out-Null
}
# Stop hook via cmd: NUL stdin + output to a file. Launched by WinSW directly,
# powershell.exe never reached the script's first line (stuck on the inherited
# std handles), so the supervisor was never told to exit and stops hung.
$stopLog = Join-Path $svcLogDir "stopexec.log"
$stopCmd = "/d /s /c `"powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File `"$stopPs1`" -ProgramRoot `"$progFs`" -DataRoot `"$dataFs`" &lt;NUL &gt;&gt;`"$stopLog`" 2&gt;&amp;1`""
$xml = @"
<service>
<id>$svcName</id>
@ -86,8 +96,8 @@ function Ensure-WinSW {
<onfailure action="restart" delay="30 sec"/>
<resetfailure>1 hour</resetfailure>
<stoptimeout>60sec</stoptimeout>
<stopexecutable>powershell.exe</stopexecutable>
<stoparguments>-NoProfile -ExecutionPolicy Bypass -File "$stopPs1" -ProgramRoot "$progFs" -DataRoot "$dataFs"</stoparguments>
<stopexecutable>cmd.exe</stopexecutable>
<stoparguments>$stopCmd</stoparguments>
<workingdirectory>$progFs</workingdirectory>
</service>
"@