InstallBinaryAside starts the download suspended (trialStart), ends
it, sets the running exe aside and renames the download over it. A
program that checks an executable as its process starts (an antivirus)
can hold the file a moment after the process has ended, and the rename
then fails with a sharing violation; the report says the first install
of every update failed this way. The rename (and the put-back of the
old exe on failure) now goes through moveIn, which tries again over
asideWaits (100 ms to 1.5 s) while Windows says the file is held
(errors 32/33), as moveAside already does for the running exe. Any
other failure is returned at once. The Windows CI step now runs the
trial tests too (TestTrialStart and the new ones).
What was run:
- Windows box (Windows 10.0.26200 x64, Defender real-time on, a
cross-compiled update.test.exe in a temp dir with a sandbox
USERPROFILE/APPDATA/LOCALAPPDATA/TEMP):
- TestInstallBinaryWaitsForHeldDownload (new; the download held open
for 150 ms): old update.go FAIL "rename ...magpie.exe.new
...magpie.exe: The process cannot access the file because it is
being used by another process."; new PASS (0.31 s).
- TestInstallBinaryRightAfterTheTrial (new; real trialStart, then the
install) and TestTrialStart pass on old and new code there.
- The whole update package passes; the CI pattern
^(TestInstallBinary|TestMoveAside|TestTrialStart) passes.
- Rollback tests now use 1 ms waits so a file held for good fails
fast.
- The real update, in a sandbox: v0.1.1243's magpie-windows-amd64.exe
run as `magpie update` twice in separate sandbox homes: both updated
to 0.1.1244, exit 0.
- macOS: gofmt -l clean; go vet darwin, GOOS=linux -tags nogui,
GOOS=windows clean; go test -p 4 ./... (see the last commit of this
push for the result).
Not reproduced: the hold itself. On this box the real trial-then-
install path succeeded every time (10/10 with a 12 MB exe, 10/10 with
a 74 MB magpie.exe, 2/2 real updates), and origin/main's TestTrialStart
passed 3/3. The fix is verified with an injected hold, not a real
antivirus one.
151 lines
4.6 KiB
Go
151 lines
4.6 KiB
Go
package update
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func rollbackFiles(t *testing.T) (string, string) {
|
|
t.Helper()
|
|
exe := filepath.Join(t.TempDir(), "magpie.exe")
|
|
staged := exe + ".new"
|
|
for path, content := range map[string]string{exe: "old version", staged: "new version"} {
|
|
if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return exe, staged
|
|
}
|
|
|
|
// shortWaits has moveIn and moveAside give up on a file held for good
|
|
// at once.
|
|
func shortWaits(t *testing.T) {
|
|
was := asideWaits
|
|
asideWaits = []time.Duration{time.Millisecond}
|
|
t.Cleanup(func() { asideWaits = was })
|
|
}
|
|
|
|
func holdForRollbackTest(t *testing.T, path string) func() {
|
|
t.Helper()
|
|
h := holdOpen(t, path)
|
|
close := sync.OnceFunc(func() {
|
|
if err := syscall.CloseHandle(h); err != nil {
|
|
t.Error("close held file:", err)
|
|
}
|
|
})
|
|
t.Cleanup(close)
|
|
return close
|
|
}
|
|
|
|
func wantInstallFile(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if got, err := os.ReadFile(path); err != nil || string(got) != content {
|
|
t.Errorf("%s = %q (%v), want %q", filepath.Base(path), got, err, content)
|
|
}
|
|
}
|
|
|
|
// A scanner holding the download must not leave the installed path missing.
|
|
// An earlier update's held .old means the actual backup is .old-2 instead.
|
|
func TestInstallBinaryRestoresAfterStagedRenameFails(t *testing.T) {
|
|
for _, heldOld := range []bool{false, true} {
|
|
name := "first update"
|
|
if heldOld {
|
|
name = "earlier old still held"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
shortWaits(t)
|
|
exe, staged := rollbackFiles(t)
|
|
aside := exe + ".old"
|
|
if heldOld {
|
|
if err := os.WriteFile(aside, []byte("earlier version"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
holdForRollbackTest(t, aside)
|
|
if err := os.Remove(aside); !errors.Is(err, errSharingViolation) {
|
|
t.Fatalf("fixture: removing held .old = %v, want sharing violation", err)
|
|
}
|
|
aside = exe + ".old-2"
|
|
}
|
|
closeStaged := holdForRollbackTest(t, staged)
|
|
if err := os.Rename(staged, staged+".probe"); !errors.Is(err, errSharingViolation) {
|
|
t.Fatalf("fixture: renaming held download = %v, want sharing violation", err)
|
|
}
|
|
err := InstallBinary(staged, exe)
|
|
var renameErr *os.LinkError
|
|
if !errors.Is(err, errSharingViolation) || !errors.As(err, &renameErr) || renameErr.Old != staged || renameErr.New != exe {
|
|
t.Fatalf("InstallBinary = %v, want held staged-to-exe rename failure", err)
|
|
}
|
|
wantInstallFile(t, exe, "old version")
|
|
wantInstallFile(t, staged, "new version")
|
|
if _, err := os.Stat(aside); !os.IsNotExist(err) {
|
|
t.Errorf("restored backup remains at %s: %v", filepath.Base(aside), err)
|
|
}
|
|
if heldOld {
|
|
wantInstallFile(t, exe+".old", "earlier version")
|
|
}
|
|
|
|
closeStaged()
|
|
if err := InstallBinary(staged, exe); err != nil {
|
|
t.Fatal("retry after the scanner closes:", err)
|
|
}
|
|
wantInstallFile(t, exe, "new version")
|
|
wantInstallFile(t, aside, "old version")
|
|
if _, err := os.Stat(staged); !os.IsNotExist(err) {
|
|
t.Errorf("download remains after successful retry: %v", err)
|
|
}
|
|
if heldOld {
|
|
wantInstallFile(t, exe+".old", "earlier version")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// If a scanner takes the old file just after it moves aside, restoration
|
|
// can fail too. Report both failures and keep both versions for recovery.
|
|
func TestInstallBinaryReportsFailedRestore(t *testing.T) {
|
|
shortWaits(t)
|
|
exe, staged := rollbackFiles(t)
|
|
holdForRollbackTest(t, staged)
|
|
rename := renameFile
|
|
t.Cleanup(func() { renameFile = rename })
|
|
var aside string
|
|
renameFile = func(from, to string) error {
|
|
err := os.Rename(from, to)
|
|
if err == nil && from == exe {
|
|
aside = to
|
|
holdForRollbackTest(t, aside)
|
|
}
|
|
return err
|
|
}
|
|
err := InstallBinary(staged, exe)
|
|
if err == nil || aside == "" {
|
|
t.Fatalf("fixture did not reach the second rename: %v, backup %q", err, aside)
|
|
}
|
|
wantInstallFile(t, aside, "old version")
|
|
wantInstallFile(t, staged, "new version")
|
|
if !strings.Contains(err.Error(), "restore") {
|
|
t.Errorf("failure does not explain that restoring the executable failed: %v", err)
|
|
}
|
|
var both interface{ Unwrap() []error }
|
|
if !errors.As(err, &both) {
|
|
t.Fatalf("failure does not retain both causes: %v", err)
|
|
}
|
|
var installFailed, restoreFailed bool
|
|
for _, cause := range both.Unwrap() {
|
|
var linkErr *os.LinkError
|
|
if errors.As(cause, &linkErr) && errors.Is(cause, errSharingViolation) {
|
|
installFailed = installFailed || linkErr.Old == staged && linkErr.New == exe
|
|
restoreFailed = restoreFailed || linkErr.Old == aside && linkErr.New == exe
|
|
}
|
|
}
|
|
if !installFailed || !restoreFailed {
|
|
t.Errorf("missing typed install/restore failure: %v", err)
|
|
}
|
|
}
|