fix(update): let legacy clients complete tray shutdown before replacing them
This commit is contained in:
1 parent
d41b29e345
commit
717b324cf2
8 files changed
+56
-31
No files matched your search
@@ -28,10 +28,6 @@ func (a *App) afterStartup() {
|
||||
|
||||
// onSecondInstance handles another `reims-go.exe` started while this one runs.
|
||||
func (a *App) onSecondInstance(data options.SecondInstanceData) {
|
||||
if len(data.Args) == 1 && data.Args[0] == cli.QuitForUpdateArg {
|
||||
a.QuitApp()
|
||||
return
|
||||
}
|
||||
w, ok := cli.ParseWindowArgs(data.Args)
|
||||
if !ok {
|
||||
// Command-line mode never gets here (it does not take the lock), so
|
||||
|
||||
@@ -11,7 +11,7 @@ func findWindow() uintptr { return 0 }
|
||||
|
||||
func ForwardVMClose(string) bool { return false }
|
||||
|
||||
func WindowForUpdate() (uint32, func()) { return 0, func() {} }
|
||||
func WindowForUpdate() uint32 { return 0 }
|
||||
|
||||
func DesktopWindowPID() uint32 { return 0 }
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ func findWindow() uintptr {
|
||||
|
||||
// WindowForUpdate identifies the client with Wails' single-instance window.
|
||||
// A missing window may mean the older client has just acquired its lock.
|
||||
func WindowForUpdate() (uint32, func()) {
|
||||
func WindowForUpdate() uint32 {
|
||||
hwnd := findWindow()
|
||||
if hwnd == 0 {
|
||||
lockName := "wails-app-" + InstanceID + "sim"
|
||||
@@ -76,12 +76,7 @@ func WindowForUpdate() (uint32, func()) {
|
||||
}
|
||||
}
|
||||
}
|
||||
pid := windowPID(hwnd)
|
||||
return pid, func() {
|
||||
if pid != 0 && windowPID(hwnd) == pid {
|
||||
_ = sendToWindow(hwnd, []string{QuitForUpdateArg}, 500)
|
||||
}
|
||||
}
|
||||
return windowPID(hwnd)
|
||||
}
|
||||
|
||||
// DesktopWindowPID returns the current Wails single-instance owner, if any.
|
||||
|
||||
@@ -5,9 +5,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// QuitForUpdateArg is an IPC-only request to exit without stopping VMs.
|
||||
const QuitForUpdateArg = "--quit-for-update"
|
||||
|
||||
// The desktop window's own flags. `vm start` launches the window with them
|
||||
// (or sends them to a window already running), so they are the whole
|
||||
// protocol between command-line mode and the window. Each flag is one
|
||||
|
||||
@@ -4,6 +4,6 @@ package selfupdate
|
||||
|
||||
func LockDesktopStartup(string) (func(), error) { return func() {}, nil }
|
||||
|
||||
func Takeover(uint32, func(), []string, func() uint32) (bool, error) { return false, nil }
|
||||
func Takeover(uint32, []string, func() uint32) (bool, error) { return false, nil }
|
||||
|
||||
func ReportTakeoverError(error) {}
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/i18n"
|
||||
"github.com/Nwflower/reims-go/internal/tray"
|
||||
"github.com/Nwflower/reims-go/internal/version"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
@@ -38,7 +39,7 @@ func LockDesktopStartup(instanceID string) (func(), error) {
|
||||
return
|
||||
}
|
||||
defer windows.CloseHandle(h)
|
||||
event, err := windows.WaitForSingleObject(h, 30_000)
|
||||
event, err := windows.WaitForSingleObject(h, 45_000)
|
||||
if err == nil && event != windows.WAIT_OBJECT_0 && event != windows.WAIT_ABANDONED {
|
||||
err = fmt.Errorf("could not acquire the Reims-Go startup mutex (%#x)", event)
|
||||
}
|
||||
@@ -59,13 +60,13 @@ func LockDesktopStartup(instanceID string) (func(), error) {
|
||||
|
||||
// Takeover replaces the running client's executable at the exact path used by
|
||||
// its shortcuts. The caller holds the desktop startup lock.
|
||||
func Takeover(pid uint32, requestExit func(), relaunchArgs []string, windowPID func() uint32) (bool, error) {
|
||||
func Takeover(pid uint32, relaunchArgs []string, windowPID func() uint32) (bool, error) {
|
||||
// The compatibility probe runs the old client's version command. Never
|
||||
// launch that executable with elevated privileges inherited from this one.
|
||||
if pid == 0 || pid == uint32(os.Getpid()) || windows.GetCurrentProcessToken().IsElevated() {
|
||||
return false, nil
|
||||
}
|
||||
process, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION|windows.PROCESS_TERMINATE|windows.SYNCHRONIZE, false, pid)
|
||||
process, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION|windows.SYNCHRONIZE, false, pid)
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
@@ -111,8 +112,7 @@ func Takeover(pid uint32, requestExit func(), relaunchArgs []string, windowPID f
|
||||
if !fileMatches(target, oldFile) {
|
||||
return false, nil
|
||||
}
|
||||
requestExit()
|
||||
if err := stopClient(process); err != nil {
|
||||
if err := closeClient(process, pid); err != nil {
|
||||
return false, fmt.Errorf("close old Reims-Go: %w", err)
|
||||
}
|
||||
if !fileMatches(target, oldFile) {
|
||||
@@ -235,13 +235,22 @@ func fileMatches(path string, expected os.FileInfo) bool {
|
||||
return err == nil && os.SameFile(current, expected)
|
||||
}
|
||||
|
||||
func stopClient(process windows.Handle) error {
|
||||
if event, err := windows.WaitForSingleObject(process, 3000); event == windows.WAIT_OBJECT_0 || err != nil {
|
||||
return err
|
||||
func closeClient(process windows.Handle, pid uint32) error {
|
||||
// The Wails instance window can appear before startup creates the tray.
|
||||
// A successful post only requests exit. Wait for all shutdown hooks.
|
||||
for i := 0; i < 20; i++ {
|
||||
if event, err := windows.WaitForSingleObject(process, 100); event == windows.WAIT_OBJECT_0 || err != nil {
|
||||
return err
|
||||
}
|
||||
if tray.RequestQuit(pid) {
|
||||
event, err := windows.WaitForSingleObject(process, 15_000)
|
||||
if event == windows.WAIT_OBJECT_0 || err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.New("the older client did not finish closing")
|
||||
}
|
||||
}
|
||||
// Legacy clients do not know the quit request. Stop only the verified
|
||||
// client process using the original handle, never a reused PID or tree.
|
||||
return terminateClient(process)
|
||||
return errors.New("the older client's tray window is unavailable")
|
||||
}
|
||||
|
||||
func terminateClient(process windows.Handle) error {
|
||||
|
||||
@@ -17,6 +17,8 @@ var (
|
||||
user32 = syscall.NewLazyDLL("user32.dll")
|
||||
procRegisterClassExW = user32.NewProc("RegisterClassExW")
|
||||
procCreateWindowExW = user32.NewProc("CreateWindowExW")
|
||||
procFindWindowExW = user32.NewProc("FindWindowExW")
|
||||
procGetWindowThreadProcessID = user32.NewProc("GetWindowThreadProcessId")
|
||||
procDestroyWindow = user32.NewProc("DestroyWindow")
|
||||
procDefWindowProcW = user32.NewProc("DefWindowProcW")
|
||||
procGetMessageW = user32.NewProc("GetMessageW")
|
||||
@@ -51,6 +53,9 @@ var (
|
||||
)
|
||||
|
||||
const (
|
||||
trayWindowClass = "ReimsGoTrayWindowClass"
|
||||
trayWindowTitle = "Reims-Go Tray"
|
||||
|
||||
wmDestroy = 0x0002
|
||||
wmClose = 0x0010
|
||||
wmNull = 0x0000
|
||||
@@ -95,6 +100,29 @@ const (
|
||||
trayIconUID = 1
|
||||
)
|
||||
|
||||
// RequestQuit invokes the tray's normal Exit command for the verified client
|
||||
// process. The class, title and command ID also exist in older releases.
|
||||
func RequestQuit(pid uint32) bool {
|
||||
if pid == 0 {
|
||||
return false
|
||||
}
|
||||
class := windows.StringToUTF16Ptr(trayWindowClass)
|
||||
title := windows.StringToUTF16Ptr(trayWindowTitle)
|
||||
var hwnd uintptr
|
||||
for {
|
||||
hwnd, _, _ = procFindWindowExW.Call(0, hwnd, uintptr(unsafe.Pointer(class)), uintptr(unsafe.Pointer(title)))
|
||||
if hwnd == 0 {
|
||||
return false
|
||||
}
|
||||
var owner uint32
|
||||
procGetWindowThreadProcessID.Call(hwnd, uintptr(unsafe.Pointer(&owner)))
|
||||
if owner == pid {
|
||||
ok, _, _ := procPostMessageW.Call(hwnd, wmCommand, cmdQuit, 0)
|
||||
return ok != 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type wndClassExW struct {
|
||||
CbSize uint32
|
||||
Style uint32
|
||||
@@ -305,7 +333,7 @@ func (m *Manager) runLoop(ready chan<- error) {
|
||||
defer close(m.done)
|
||||
|
||||
hInst, _, _ := procGetModuleHandle.Call(0)
|
||||
className, _ := syscall.UTF16PtrFromString("ReimsGoTrayWindowClass")
|
||||
className := windows.StringToUTF16Ptr(trayWindowClass)
|
||||
tbMsgName, _ := syscall.UTF16PtrFromString("TaskbarCreated")
|
||||
tbMsg, _, _ := procRegisterWindowMessageW.Call(uintptr(unsafe.Pointer(tbMsgName)))
|
||||
m.taskbarCreated = uint32(tbMsg)
|
||||
@@ -329,7 +357,7 @@ func (m *Manager) runLoop(ready chan<- error) {
|
||||
}
|
||||
_, _, _ = procRegisterClassExW.Call(uintptr(unsafe.Pointer(&wc)))
|
||||
|
||||
winTitle, _ := syscall.UTF16PtrFromString("Reims-Go Tray")
|
||||
winTitle := windows.StringToUTF16Ptr(trayWindowTitle)
|
||||
hwnd, _, err := procCreateWindowExW.Call(
|
||||
0,
|
||||
uintptr(unsafe.Pointer(className)),
|
||||
|
||||
@@ -77,10 +77,10 @@ func main() {
|
||||
releaseStartup = release
|
||||
defer release()
|
||||
if !launch.Background && launch.WaitPID == 0 && launch.StartVM == "" && launch.ResumeVM == "" {
|
||||
pid, requestExit := cli.WindowForUpdate()
|
||||
pid := cli.WindowForUpdate()
|
||||
relaunch := launch
|
||||
relaunch.TakeoverRelaunch = true
|
||||
restarted, err := selfupdate.Takeover(pid, requestExit, relaunch.Args(), cli.DesktopWindowPID)
|
||||
restarted, err := selfupdate.Takeover(pid, relaunch.Args(), cli.DesktopWindowPID)
|
||||
if restarted {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user