fix: ensureDaemon 自动检测并重启过期守护进程

问题: 重新构建后 IPv6 等新功能不生效,客户端仍运行旧代码。
根因: ensureDaemon() 通过 IPC socket 拨号时,若旧 daemon 进程仍在
运行(重建后未退出),GUI 会直接复用旧进程,永不启动新二进制。
日志证实旧 daemon 处理连接(init 日志无 ip6 字段),新代码从未执行。

解决方案: 新增版本握手协议,GUI 启动时查询 daemon 构建版本,
版本不匹配则自动关闭旧进程并启动新的。

- 新建 internal/version 包,GUI 与 daemon 共享同一 Version 变量,
  均通过 Makefile ldflags 注入
- IPC 新增 CmdVersion 命令,daemon 返回 version.Version;
  Response 增加 Version 字段,Client 新增 QueryVersion() 方法
- ensureDaemon() 拨号后查询版本,不匹配则发 CmdShutdown、等待
  socket 释放、重新启动 daemon;旧 daemon 不识别 CmdVersion 时
  返回错误,同样触发重启
- Makefile LDFLAGS 改为注入 lmvpn/internal/version.Version
This commit is contained in:
2026-07-07 12:42:23 +08:00
parent a332857973
commit 4420532a66
6 changed files with 75 additions and 10 deletions
+22 -3
View File
@@ -10,6 +10,7 @@ import (
"lmvpn/internal/ipc"
"lmvpn/internal/log"
"lmvpn/internal/paths"
"lmvpn/internal/version"
)
// ensureDaemon checks if the daemon is running and launches it (as
@@ -25,9 +26,27 @@ import (
// The --daemon-bin flag tells daemon-launch where to find lmvpnd. If
// not given, daemon-launch auto-detects it relative to its own path.
func ensureDaemon() (*ipc.Client, error) {
// Fast path: daemon already running.
// Fast path: a daemon is already running. Verify its build version
// matches ours; if not, it's a stale process from a previous build
// and must be restarted so the new code takes effect.
if c, err := ipc.Dial(); err == nil {
return c, nil
if ver, qerr := c.QueryVersion(); qerr == nil && ver == version.Version {
return c, nil
} else {
log.L().Info("stale daemon detected, restarting",
"got", ver, "expected", version.Version, "query_err", qerr)
_ = ipc.SendShutdown(c)
c.Close()
// Wait for the old daemon to exit and release the socket.
for i := 0; i < 30; i++ {
time.Sleep(100 * time.Millisecond)
if cc, err := ipc.Dial(); err == nil {
cc.Close()
} else {
break // socket gone
}
}
}
}
exe, err := os.Executable()
@@ -78,7 +97,7 @@ func ensureDaemon() (*ipc.Client, error) {
}
// shellQuote wraps a string in single quotes for shell safety.
// Embedded single quotes are escaped using the '\'' pattern.
// Embedded single quotes are escaped using the '\ pattern.
func shellQuote(s string) string {
result := "'"
for _, r := range s {
+5 -1
View File
@@ -1,3 +1,7 @@
package ui
var Version = "dev"
import "lmvpn/internal/version"
// Version is the application version, sourced from the shared version
// package so the GUI and daemon report the same string.
var Version = version.Version