Files
magpie/internal/update/predecessor_windows_test.go
T
yetone 07dfd0bb8f update: a magpie stuck in its exit on Windows no longer holds the restarted one (BESTLJP0412 on X)
BESTLJP0412 on X (Windows 11 26200, 0.1.4179 to 0.1.4193, WebView2 157):
after an update's restart the window stayed "Not Responding", and a
process with a single thread sat on port 3425 and the WebView2 profile.

A Go process has several threads while it runs; one left means it is in
ExitProcess (os.Exit on Windows), which ends every other thread and then
runs each loaded DLL's detach on the last one. A detach that waits for a
thread already ended (WebView2's, COM's) never returns, so the old magpie
never ends, its handles stay open, and the new one waited its 60s
(awaitGone), went on beside it, and hung on the held port and profile.

Both ends fixed:
- the old one: cli.Main ends through endProcess, on Windows
  TerminateProcess on itself instead of ExitProcess. Everything magpie
  writes is written by then; the kernel closes every handle and no
  detach runs.
- the new one: awaitGone's wait for the old magpie (awaitExit) looks at
  its thread count every 250ms; one thread for a second is a magpie stuck
  in its exit, and it is ended (opened with PROCESS_TERMINATE). This is
  what fixes the update from 0.1.4193 and older, which still exit through
  ExitProcess.

docs/subsystems/gui-shell.md says how a Windows restart waits and ends.

Tests (Windows, real processes; the stuck detach is a fiber-local storage
callback set to kernel32's Sleep with INFINITE, which ExitProcess runs on
its last thread and TerminateProcess doesn't):
- cli TestEndProcessSkipsAStuckDetach: first checks the stand-in is real
  (os.Exit there hangs with exactly 1 thread left), then that
  endProcess(3) ends with code 3. Broken by hand (TerminateProcess taken
  out): red, "endProcess hung in a DLL's detach, as os.Exit does".
- update TestAwaitGoneEndsAMagpieStuckInItsExit: a predecessor stuck that
  way is ended in ~1.4s. Broken by hand (the thread check skipped): red,
  "the start went on after 20.022s with the old magpie still stuck".

Run:
- win-run on xiaomi-gpu (Windows 11): go test ./internal/cli ok (179s);
  ./internal/update all ok but TestPackagedByDpkg, red the same on
  origin/main 442964d70 (#4603, a ':' file name on NTFS).
- mote: gofmt; go vet, GOOS=windows go vet, GOOS=linux -tags nogui go vet
  of internal/cli and internal/update ok.
- mica: full go test ./... -timeout 45m started at this tree; still running
  at push (ship-at-once rule), result to follow on the issue thread if red.

Not tried: a real update restart of the app on Windows with a stuck
WebView2 detach (the hang can't be brought on at will there); the fix is
checked with the FLS stand-in above.
2026-11-15 02:31:56 +08:00

161 lines
4.8 KiB
Go

package update
import (
"bufio"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"golang.org/x/sys/windows"
)
// The magpie before is gone but its WebView2 isn't yet: the start waits
// for the msedgewebview2 processes it left (vsoullai on X, 0.1.4096: the
// new magpie's WebView2 joined the old browser going down and hung). The
// old magpie here is this test binary; its "WebView2" is a copy of it named
// msedgewebview2.exe that ends 3s after it.
const testPredecessorEnv = "MAGPIE_TEST_PREDECESSOR"
func init() {
v := os.Getenv(testPredecessorEnv)
if v == "" {
return
}
role, arg, _ := strings.Cut(v, ",")
switch role {
case "old": // arg: the web copy, then its done file
exe, done, _ := strings.Cut(arg, ",")
web := exec.Command(exe, "-test.run=^$")
web.Env = append(os.Environ(), testPredecessorEnv+"=web,"+done)
if err := web.Start(); err != nil {
os.Exit(2)
}
os.Stdout.WriteString("ready\n")
io.Copy(io.Discard, os.Stdin) // the test lets it go
os.Exit(0)
case "stuck": // quits into a DLL detach that never returns
runtime.LockOSThread()
k := windows.NewLazySystemDLL("kernel32.dll")
sleep := k.NewProc("Sleep")
if sleep.Find() != nil {
os.Exit(9)
}
// a fiber-local storage callback runs on ExitProcess's last thread:
// Sleep(INFINITE)
idx, _, _ := k.NewProc("FlsAlloc").Call(sleep.Addr())
if idx == 0xFFFFFFFF {
os.Exit(9)
}
if ok, _, _ := k.NewProc("FlsSetValue").Call(idx, 0xFFFFFFFF); ok == 0 {
os.Exit(9)
}
os.Exit(0)
case "web": // arg: the file it writes as it ends
time.Sleep(3 * time.Second)
os.WriteFile(arg, []byte("done"), 0o644)
os.Exit(0)
}
}
func TestAwaitGoneWaitsForTheOldWebView(t *testing.T) {
self, err := os.Executable()
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
web := filepath.Join(dir, "msedgewebview2.exe")
b, err := os.ReadFile(self)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(web, b, 0o755); err != nil {
t.Fatal(err)
}
done := filepath.Join(dir, "web-done")
old := exec.Command(self, "-test.run=^$")
old.Env = append(os.Environ(), testPredecessorEnv+"=old,"+web+","+done)
in, _ := old.StdinPipe()
out, _ := old.StdoutPipe()
if err := old.Start(); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
in.Close()
old.Wait()
// the web copy ends by itself within 3s; the folder goes after it
for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(100 * time.Millisecond) {
if _, err := os.Stat(done); err == nil {
break
}
}
time.Sleep(200 * time.Millisecond)
})
if line, _ := bufio.NewReader(out).ReadString('\n'); line != "ready\n" {
t.Fatalf("old: %q", line)
}
if got := webViewProcs(uint32(old.Process.Pid)); len(got) != 1 {
t.Fatalf("the old one's WebView2 processes: %v, want its one", got)
}
go func() {
time.Sleep(300 * time.Millisecond)
in.Close() // the old magpie quits; its WebView2 runs on 3s
}()
t.Setenv("MAGPIE_REPLACES", strconv.Itoa(old.Process.Pid))
start := time.Now()
AwaitPredecessor()
if _, err := os.Stat(done); err != nil {
t.Fatalf("the start went on after %s, while the old magpie's WebView2 still ran", time.Since(start).Round(time.Millisecond))
}
}
// The magpie before stuck in its exit, one thread left on the gateway's
// port and the WebView2 profile, is ended rather than waited for until the
// new one goes on beside it and hangs (BESTLJP0412 on X, 0.1.4179 to
// 0.1.4193: "Not Responding" after an update's restart). Its stand-in quits
// into a fiber-local storage callback that sleeps forever, which
// ExitProcess runs on its last thread as it would a DLL's detach.
func TestAwaitGoneEndsAMagpieStuckInItsExit(t *testing.T) {
self, err := os.Executable()
if err != nil {
t.Fatal(err)
}
old := exec.Command(self, "-test.run=^$")
old.Env = append(os.Environ(), testPredecessorEnv+"=stuck")
if err := old.Start(); err != nil {
t.Fatal(err)
}
exited := make(chan struct{})
go func() { old.Wait(); close(exited) }()
t.Cleanup(func() {
old.Process.Kill()
<-exited
})
pid := uint32(old.Process.Pid)
for deadline := time.Now().Add(10 * time.Second); threads(pid) != 1; time.Sleep(50 * time.Millisecond) {
if time.Now().After(deadline) {
t.Fatalf("the old magpie never got stuck in its exit: %d threads", threads(pid))
}
}
saved := predecessorMax
predecessorMax = 20 * time.Second
t.Cleanup(func() { predecessorMax = saved })
t.Setenv("MAGPIE_REPLACES", strconv.Itoa(old.Process.Pid))
start := time.Now()
AwaitPredecessor()
took := time.Since(start)
select {
case <-exited:
case <-time.After(5 * time.Second):
t.Fatalf("the start went on after %s with the old magpie still stuck in its exit", took.Round(time.Millisecond))
}
if took >= predecessorMax {
t.Fatalf("waited the whole %s for a magpie stuck in its exit", predecessorMax)
}
}