mycf_711 (Win11 26200, 0.1.2364 -> 0.1.2404): the first try failed at the
trial start with "fork/exec ...\magpie.exe.new: The process cannot access
the file because it is being used by another process", and every try
after it failed at once with 416. Both confirmed on xiaomi-gpu with the
old code and a real handle held on the download.
Cause: something (often an antivirus) holds the staged magpie.exe.new.
CreateProcess fails with a sharing violation and that was reported as a
BlockedError (a policy block, which stops the clock checks). download()
dropped os.Remove's error, so the held, complete file stayed, and fetch
resumed it with "Range: bytes=<size>-", which no host can satisfy.
Changes in internal/update:
- download clears the earlier file through clearFile; a held file gives a
clear error ("another program has it open...") instead of being ignored,
and every other Remove error is returned, not dropped.
- fetch: a leftover that is already whole and matches the hash is used with
no request; one that is whole but of other bytes starts over; a 416 starts
over from zero once.
- binaryStage takes a fresh name (magpie.exe.new-*) when the usual one is
held, and stagedFiles/RemoveStaleNew find and clear those too.
- the trial start waits while the download is held (waitHeld, as the aside
moves do); still held after the wait is an ordinary error that keeps the
download, not a BlockedError.
These run on macOS and Linux too (the held case can't happen there, the 416
and whole-leftover cases can).
Run at 003e55115 (on origin/main 7428f3229):
- mote: gofmt -l clean; go vet ./... for darwin, linux and windows; builds;
go test -tags nogui -count=1 -timeout 45m ./...: every package ok except
internal/gui, red on TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestColdTurnsTaken (#4510), both filed and in files this change doesn't
touch.
- xiaomi-gpu (win-run go test ./internal/update/...): 88 PASS, -count=3 of
the new tests ok; TestPackagedByDpkg red, the same on main (#4603: the
fixture name magpie:amd64.list is an NTFS stream).
- xiaomi-gpu internal/gui update tests: TestUpdateThatWontStart red, the
same on main (#4115).
- wsl-run (magpie-desktop) go test -tags nogui ./internal/update/...: ok.
Rebased on origin/main e40675cc1 (9 commits, none in internal/update) and
rerun there: mote vet (darwin, windows) and go test ./internal/update/... ok;
xiaomi-gpu the same, red only on TestPackagedByDpkg (#4603).
Broken by hand: origin/main's update.go with only the removeFile shim added
(scratchpad oldcode copy). mote: 5 red (WholeFileAsksNoRangePastTheEnd,
WholeFileOfOtherBytesStartsOver, 416StartsOverOnce, SaysAHeldLeftover,
StageStepsRoundAHeldLeftover). Windows: 8 red, those 5 plus
StageStepsRoundAFileHeldOpen ("416 ... asked [bytes=19-]") and both Trial
tests, with the reporter's own fork/exec message as a BlockedError.
Not tried: a real release update end to end on the reporter's machine, or
with a real antivirus holding the file; which program held it isn't known.
macOS Stage shares only the fetch changes.
Users already on 0.1.2364-0.1.2421 run the old updater: after a reboot (the
hold ends) Check for updates usually gets through; otherwise download
magpie from https://usemagpie.ai and open it.
135 lines
4.3 KiB
Go
135 lines
4.3 KiB
Go
package update
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// holdWritable opens path as a scanner checking a new program may: for
|
|
// writing, sharing no delete, so Windows neither removes it nor starts a
|
|
// process from it while the handle is open.
|
|
func holdWritable(t *testing.T, path string) func() {
|
|
t.Helper()
|
|
p, _ := syscall.UTF16PtrFromString(path)
|
|
h, err := syscall.CreateFile(p, syscall.GENERIC_READ|syscall.GENERIC_WRITE, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_EXISTING, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
closed := false
|
|
letGo := func() {
|
|
if !closed {
|
|
closed = true
|
|
syscall.CloseHandle(h)
|
|
}
|
|
}
|
|
t.Cleanup(letGo)
|
|
return letGo
|
|
}
|
|
|
|
func pingBytes(t *testing.T) []byte {
|
|
t.Helper()
|
|
b, err := os.ReadFile(filepath.Join(os.Getenv("SystemRoot"), "System32", "PING.EXE"))
|
|
if err != nil {
|
|
t.Skip("no PING.EXE:", err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
// A real open handle on the earlier download: Windows won't remove it
|
|
// (what mycf_711's update dropped the error of), and the next download
|
|
// goes under a name of its own instead of asking for bytes past its end.
|
|
func TestStageStepsRoundAFileHeldOpen(t *testing.T) {
|
|
quickRetries(t)
|
|
f := newDesktopFixture(t)
|
|
srv, ranges := releaseHost(t, f.body, nil)
|
|
f.asset = asset(srv.URL, f.body)
|
|
d := cacheStage(f.exe)
|
|
os.MkdirAll(d, 0o755)
|
|
left := filepath.Join(d, "magpie.exe.new")
|
|
os.WriteFile(left, f.body, 0o755) // the whole of it, as the reporter's
|
|
holdWritable(t, left)
|
|
if os.Remove(left) == nil {
|
|
t.Fatal("a held file could be removed; the test proves nothing")
|
|
}
|
|
staged, err := stageBinaryFor(context.Background(), f.asset, f.exe)
|
|
if err != nil {
|
|
t.Fatalf("stage: %v (asked %q)", err, ranges())
|
|
}
|
|
if staged == left {
|
|
t.Fatalf("staged at the held %s", left)
|
|
}
|
|
if b, _ := os.ReadFile(staged); !bytes.Equal(b, f.body) {
|
|
t.Fatalf("staged %q", b)
|
|
}
|
|
if err := download(context.Background(), f.asset, left); err == nil || !strings.Contains(err.Error(), "another program has it open") {
|
|
t.Fatalf("download into the held file: %v, want it said held", err)
|
|
}
|
|
}
|
|
|
|
// The trial start of a download a scanner still holds (Windows: "being
|
|
// used by another process") waits for it to let go, as moveIn does, and
|
|
// the update goes in: it was never refused.
|
|
func TestTrialWaitsForAHeldDownload(t *testing.T) {
|
|
body := pingBytes(t)
|
|
was := canStart
|
|
canStart = trialStart
|
|
t.Cleanup(func() { canStart = was })
|
|
waits, held := asideWaits, heldFor
|
|
asideWaits, heldFor = []time.Duration{50 * time.Millisecond}, 20*time.Second
|
|
t.Cleanup(func() { asideWaits, heldFor = waits, held })
|
|
|
|
dir := t.TempDir()
|
|
exe := filepath.Join(dir, "magpie.exe")
|
|
staged := filepath.Join(dir, "magpie.exe.new")
|
|
os.WriteFile(exe, []byte("old version"), 0o755)
|
|
os.WriteFile(staged, body, 0o755)
|
|
letGo := holdWritable(t, staged)
|
|
if err := trialStart(staged); !heldOpen(err) {
|
|
t.Fatalf("trial start of a held file: %v, want it held; the test proves nothing", err)
|
|
}
|
|
go func() { time.Sleep(700 * time.Millisecond); letGo() }()
|
|
if _, err := InstallBinaryAside(staged, exe); err != nil {
|
|
t.Fatalf("InstallBinaryAside: %v", err)
|
|
}
|
|
if b, _ := os.ReadFile(exe); !bytes.Equal(b, body) {
|
|
t.Fatal("the new version isn't in place")
|
|
}
|
|
}
|
|
|
|
// Held past the wait, the update says what holds it and keeps the
|
|
// download for a later restart; it isn't noted as a version this computer
|
|
// won't start (*BlockedError), which stopped the automatic checks.
|
|
func TestTrialHeldForGoodIsNotBlocked(t *testing.T) {
|
|
body := pingBytes(t)
|
|
was := canStart
|
|
canStart = trialStart
|
|
t.Cleanup(func() { canStart = was })
|
|
shortWaits(t)
|
|
|
|
dir := t.TempDir()
|
|
exe := filepath.Join(dir, "magpie.exe")
|
|
staged := filepath.Join(dir, "magpie.exe.new")
|
|
os.WriteFile(exe, []byte("old version"), 0o755)
|
|
os.WriteFile(staged, body, 0o755)
|
|
letGo := holdWritable(t, staged)
|
|
_, err := InstallBinaryAside(staged, exe)
|
|
if err == nil || errors.As(err, new(*BlockedError)) || !strings.Contains(err.Error(), "another program still has the download open") {
|
|
t.Fatalf("InstallBinaryAside = %v, want it said held, not blocked", err)
|
|
}
|
|
letGo()
|
|
wantInstallFile(t, exe, "old version")
|
|
if b, _ := os.ReadFile(staged); !bytes.Equal(b, body) {
|
|
t.Fatal("the download wasn't kept for another try")
|
|
}
|
|
if _, err := InstallBinaryAside(staged, exe); err != nil {
|
|
t.Fatalf("the restart after it let go: %v", err)
|
|
}
|
|
}
|