白菜 (Discord #general, 10-19): with magpie on the Desktop, every click on Update says it is restarting and then offers the update again; clicking again never gets it in, only a fresh download from the site does. Cause, as far as could be shown: the install starts the download suspended first (canStart, #894). An antivirus that checks a program as it starts holds the file a while after, and the move into place gave up after asideWaits' ~3s (#2272's fix). The swap failed, the old exe was put back, the state stayed ready and the pill said 更新失败, with the reason only in its tooltip. A click again ran the trial start again, so the antivirus held the same file again and it failed the same way every time. Change: - moveIn and moveAside go on trying, at the last pause (1.5s), while Windows says the file is held open (errors 32/33), up to heldFor (30s) from the first try. Other errors still stop after the short pauses. - InstallBinaryAside trial-starts a download once: the same path, size and mtime that passed isn't started again on a retry. A new download is. - A download still held after that says so ("another program still has the download open (often an antivirus checking it): let magpie through it…"), without the paths, in the version row and the pill's tooltip. It still unwraps to the *os.LinkError and the sharing violation. Tests: - TestMoveAsideWaitsOutALongHold (all OSes): 10 held tries then success. A permission error still stops at 4. - TestInstallBinaryWaitsOutALongHold (Windows): a real handle holds the download for 600ms against 2 short pauses. - TestInstallBinaryTrialsADownloadOnce (Windows): one trial for a retried download, a second for a new one, and the new error text. - Existing tests that hold a file for good set heldFor to 0. What was run: - Broken by hand (no wait past asideWaits, trial on every try): all three new tests red on the Windows box. TestMoveAsideWaitsOutALongHold is red on macOS too. Restored: green. - Windows box (10.0.26200, Defender on), cross-compiled update.test.exe in a sandbox USERPROFILE/APPDATA/TEMP: the whole internal/update package PASS (41 tests). - Real GUI on the box: a production build (v0.1.9000) at C:\Users\yeton\Desktop\magpie-windows-amd64.exe, a sandbox home, a local feed offering v0.1.9001. The .new was held open from PowerShell (FileShare Read) for 12s, then POST /api/update/install {when:now}: - origin/main's build: install failed, "rename … being used by another process", state ready, old process ran on; - this build: the new version started 1s after the hold was let go, and the Desktop exe's hash is 9001's. - The same setup without a hold, origin/main's build, worked: plain Desktop, and a Desktop folder named 白菜 测试 with "(1)" in the exe name. A non-ASCII path or the Desktop alone is not the cause. - gofmt clean. go vet ./... clean for darwin, linux -tags nogui and windows. - HOME=<sandbox> go test -timeout 45m ./... at load ~260-290: every package ok except these. - internal/agent and internal/gateway timed out at 45m. Already filed as #2544 and #2870. - internal/gui's TestSetAnswersWhileDevinAndProvidersHang (19.8s set under load) is filed as #1375. Rerun alone with the update, restart, install and relaunch tests: ok. - internal/update was rerun separately, because its source was swapped to origin/main's for ~1 min mid-run to build the comparison exe: ok. Not tried: - A real third-party antivirus (360, Huorong). The hold is injected. The user's version, antivirus and exact error are still unknown, and were asked for on Discord. - A OneDrive-redirected Desktop.
97 lines
3.0 KiB
Go
97 lines
3.0 KiB
Go
package update
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// What the last update moved aside can still be running (a `magpie serve`
|
|
// started before it), and then can't be removed or replaced: the running
|
|
// exe goes beside it and the new version still goes in.
|
|
func TestInstallBinaryBesideARunningOld(t *testing.T) {
|
|
ping := filepath.Join(os.Getenv("SystemRoot"), "System32", "PING.EXE")
|
|
b, err := os.ReadFile(ping)
|
|
if err != nil {
|
|
t.Skip("no PING.EXE:", err)
|
|
}
|
|
dir := t.TempDir()
|
|
exe := filepath.Join(dir, "magpie.exe")
|
|
for _, f := range []string{exe, exe + ".old"} {
|
|
if err := os.WriteFile(f, b, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cmd := exec.Command(f, "-n", "30", "127.0.0.1")
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { cmd.Process.Kill(); cmd.Wait() })
|
|
}
|
|
if os.Remove(exe+".old") == nil {
|
|
t.Fatal("a running .old could be removed; the test proves nothing")
|
|
}
|
|
staged := exe + ".new"
|
|
os.WriteFile(staged, []byte("new version"), 0o755)
|
|
if err := InstallBinary(staged, exe); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := os.ReadFile(exe); string(got) != "new version" {
|
|
t.Fatalf("exe is not the new version: %d bytes", len(got))
|
|
}
|
|
if _, err := os.Stat(exe + ".old-2"); err != nil {
|
|
t.Fatal("the running exe was not moved beside the old:", err)
|
|
}
|
|
}
|
|
|
|
// holdOpen opens path as an antivirus scan does: reading, without letting
|
|
// it be renamed or removed (no FILE_SHARE_DELETE).
|
|
func holdOpen(t *testing.T, path string) syscall.Handle {
|
|
p, _ := syscall.UTF16PtrFromString(path)
|
|
h, err := syscall.CreateFile(p, syscall.GENERIC_READ, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_EXISTING, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return h
|
|
}
|
|
|
|
// Held open for a moment, the exe still goes aside and the new version in;
|
|
// held for good, the update says what holds it up and keeps the download.
|
|
func TestInstallBinaryHeldOpen(t *testing.T) {
|
|
dir := t.TempDir()
|
|
exe := filepath.Join(dir, "magpie-windows-amd64.exe")
|
|
staged := exe + ".new"
|
|
os.WriteFile(exe, []byte("old version"), 0o755)
|
|
os.WriteFile(staged, []byte("new version"), 0o755)
|
|
|
|
h := holdOpen(t, exe)
|
|
if os.Rename(exe, exe+".x") == nil {
|
|
t.Fatal("a held exe could be renamed; the test proves nothing")
|
|
}
|
|
go func() { time.Sleep(500 * time.Millisecond); syscall.CloseHandle(h) }()
|
|
if err := InstallBinary(staged, exe); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := os.ReadFile(exe); string(got) != "new version" {
|
|
t.Fatalf("exe is %q", got)
|
|
}
|
|
|
|
os.WriteFile(staged, []byte("newer version"), 0o755)
|
|
held := heldFor
|
|
heldFor = 0 // held for good: no use waiting it out
|
|
t.Cleanup(func() { heldFor = held })
|
|
h = holdOpen(t, exe)
|
|
defer syscall.CloseHandle(h)
|
|
err := InstallBinary(staged, exe)
|
|
if err == nil || !strings.Contains(err.Error(), "another program has it open") {
|
|
t.Fatalf("held for good: %v", err)
|
|
}
|
|
t.Log(err)
|
|
if _, err := os.Stat(staged); err != nil {
|
|
t.Fatal("the download was not kept for another try:", err)
|
|
}
|
|
}
|