Files
MyGoNavi/internal/nativewindow/parent_lifecycle_windows.go
Syngnat b80989cb7b 🐛 fix(nativewindow): 修复主进程退出后子窗口残留
- 启动独立窗口时传递并校验主进程 PID
- 按平台监控父进程并在其退出后关闭子窗口
- 补充 PID 解析、真实父进程退出和异常探测回归测试
2026-07-27 08:43:12 +08:00

52 lines
1.1 KiB
Go

//go:build windows
package nativewindow
import (
"fmt"
"golang.org/x/sys/windows"
)
type windowsDetachedParentWatcher struct {
handle windows.Handle
}
func newDetachedParentWatcher(pid int) (detachedParentWatcher, error) {
if pid <= 1 {
return nil, fmt.Errorf("invalid detached parent process ID: %d", pid)
}
handle, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
if err != nil {
return nil, fmt.Errorf("open detached parent process %d: %w", pid, err)
}
return &windowsDetachedParentWatcher{handle: handle}, nil
}
func (w *windowsDetachedParentWatcher) Alive() (bool, error) {
if w == nil || w.handle == 0 {
return false, nil
}
result, err := windows.WaitForSingleObject(w.handle, 0)
if err != nil {
return true, err
}
switch result {
case windows.WAIT_OBJECT_0:
return false, nil
case uint32(windows.WAIT_TIMEOUT):
return true, nil
default:
return true, fmt.Errorf("unexpected detached parent wait result: %d", result)
}
}
func (w *windowsDetachedParentWatcher) Close() error {
if w == nil || w.handle == 0 {
return nil
}
err := windows.CloseHandle(w.handle)
w.handle = 0
return err
}