Files
magpie/internal/gui/webview2_lock_windows_test.go
T
yetone 93906ba755 gui: a second launch on Windows hands over at once, not after 30s (shangyuan0818 on X)
On Windows, opening magpie from the Start menu while it already ran took
over 10s to show the window, longer than a cold start. fb6b4de0a made
every start wait, up to 30s, for the WebView2 profile folder's lockfile
before the app is made. A second launch hands over to the running magpie
only as the app is made (Wails' single instance, in application.New), and
the running magpie's WebView2 holds that folder for as long as it runs,
so every second launch waited the whole 30s, then handed over.

Now the wait is skipped when Wails' single instance mutex for this config
(wails-app-<id>-sim) is held: a magpie runs, and the launch goes straight
on to hand over. A restart after an update or a failed WebView2 start
still waits: the magpie before has ended by then (AwaitPredecessor), so
the mutex is free and only its msedgewebview2 can still hold the folder.

Run:
- Reproduced on xiaomi-gpu (Windows 11) with builds of 406ad72f1 and of
  this commit, sandbox USERPROFILE/APPDATA/LOCALAPPDATA, MAGPIE_ADDR
  :3461, HKCU magpie/ccswitch/App Paths/Uninstall keys exported and put
  back. ssh has no desktop (the real app crashes in uxtheme there, and no
  one was signed in for an interactive task), so a stand-in held what a
  running magpie holds: the single instance mutex for the sandbox's
  config and EBWebView\lockfile without sharing. Launching magpie.exe
  with no arguments, as the Start menu does, three times each: before,
  the launch quit after 30240, 30232, 30234 ms ("still held by another
  process after 30s"); after, 189, 161, 166 ms. That Wails took the
  launch for a second one also shows the mutex name matches Wails'.
- TestWebView2ReadyDoesNotWaitOnTheRunningMagpie (Windows): red with the
  held check turned off by hand (waited 20.06s), green with it.
- go test -count=1 ./internal/gui on mote: ok (150s). On xiaomi-gpu: 9
  reds, the same 9 at origin/main 633ab58e9 and all filed (#6068, #5324,
  #5928, #6059).
- gofmt -l; go vet ./internal/gui on darwin, windows, linux -tags nogui
  and windows -tags nogui (on mote: mica is out of disk).
Not tried: the real tray app with a desktop session and a Start menu
click, for the reason above.
2026-11-13 14:01:50 +08:00

81 lines
2.4 KiB
Go

//go:build windows && !nogui
package gui
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
"golang.org/x/sys/windows"
)
// Chromium holds EBWebView\lockfile without sharing while its browser
// process runs; the start waits on it (@ljlwlcz on X).
func TestWebView2FreeSeesTheHeldLockfile(t *testing.T) {
dir := t.TempDir()
if !webView2Free(dir) {
t.Fatal("no lockfile is free")
}
lock := filepath.Join(dir, "EBWebView", "lockfile")
if err := os.MkdirAll(filepath.Dir(lock), 0o755); err != nil {
t.Fatal(err)
}
h, err := windows.CreateFile(windows.StringToUTF16Ptr(lock), windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.CREATE_ALWAYS, windows.FILE_ATTRIBUTE_NORMAL, 0)
if err != nil {
t.Fatal(err)
}
if webView2Free(dir) {
t.Fatal("a lockfile held without sharing is free")
}
windows.CloseHandle(h)
if !webView2Free(dir) {
t.Fatal("a released lockfile is held")
}
}
// A second launch, which hands over to the magpie running, doesn't wait
// for that magpie's WebView2 to let go of the folder: it never does while
// it runs (shangyuan0818 on X: the Start menu took over 10s to show the
// window).
func TestWebView2ReadyDoesNotWaitOnTheRunningMagpie(t *testing.T) {
dir := t.TempDir()
lock := filepath.Join(dir, "EBWebView", "lockfile")
if err := os.MkdirAll(filepath.Dir(lock), 0o755); err != nil {
t.Fatal(err)
}
f, err := windows.CreateFile(windows.StringToUTF16Ptr(lock), windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.CREATE_ALWAYS, windows.FILE_ATTRIBUTE_NORMAL, 0)
if err != nil {
t.Fatal(err)
}
defer windows.CloseHandle(f)
id := fmt.Sprintf("test.%d.%d", os.Getpid(), time.Now().UnixNano())
// no magpie runs on it: the folder held is waited on
start := time.Now()
webView2Ready(dir, id, 2*time.Second)
if d := time.Since(start); d < 2*time.Second {
t.Fatalf("a start with no magpie running waited %s for a held folder, want 2s", d)
}
// Wails' mutex for the id, as the running magpie holds it
m, err := windows.CreateMutex(nil, false, windows.StringToUTF16Ptr(instanceMutex(id)))
if err != nil {
t.Fatal(err)
}
defer windows.CloseHandle(m)
if !instanceHeld(id) {
t.Fatal("the mutex held reads as no magpie running")
}
if instanceHeld(id + "x") {
t.Fatal("another config's id reads as running")
}
start = time.Now()
webView2Ready(dir, id, 20*time.Second)
if d := time.Since(start); d > 10*time.Second {
t.Fatalf("a second launch waited %s on the running magpie's folder", d)
}
}