Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

update: on Windows a download an antivirus holds is waited for up to 30s, and a restart tried again doesn't trial-start it again (白菜 on Discord)

yetone40dee517519e
diff --git a/internal/update/aside_test.go b/internal/update/aside_test.go
index a27ec5788..687fdc343 100644
--- a/internal/update/aside_test.go
+++ b/internal/update/aside_test.go
@@ -11,8 +11,9 @@ import (
// fakeRename has renameFile fail with err the first n times, then rename.
func fakeRename(t *testing.T, n int, err error) *int {
calls := 0
- waits, rename := asideWaits, renameFile
+ waits, rename, held := asideWaits, renameFile, heldFor
asideWaits = []time.Duration{time.Millisecond, time.Millisecond, time.Millisecond}
+ heldFor = 0
renameFile = func(from, to string) error {
calls++
if calls <= n {
@@ -20,7 +21,7 @@ func fakeRename(t *testing.T, n int, err error) *int {
}
return os.Rename(from, to)
}
- t.Cleanup(func() { asideWaits, renameFile = waits, rename })
+ t.Cleanup(func() { asideWaits, renameFile, heldFor = waits, rename, held })
return &calls
}
@@ -78,3 +79,31 @@ func TestMoveAsideSaysWhy(t *testing.T) {
t.Fatalf("%d tries of a missing exe", *calls)
}
}
+
+// An antivirus checking a new program can hold a file longer than the
+// short pauses (白菜 on Discord: every restart failed the same way): a
+// file held open is waited for on, up to heldFor, rather than given up on
+// after three seconds.
+func TestMoveAsideWaitsOutALongHold(t *testing.T) {
+ exe := filepath.Join(t.TempDir(), "magpie-windows-amd64.exe")
+ os.WriteFile(exe, []byte("v1"), 0o755)
+ calls := fakeRename(t, 10, errSharingViolation)
+ heldFor = 5 * time.Second
+ if _, err := moveAside(exe); err != nil {
+ t.Fatal(err)
+ }
+ if *calls != 11 {
+ t.Fatalf("%d tries, want 11", *calls)
+ }
+
+ // what isn't a file held open isn't waited for past the short pauses
+ os.WriteFile(exe, []byte("v2"), 0o755)
+ calls = fakeRename(t, 100, os.ErrPermission)
+ heldFor = 5 * time.Second
+ if _, err := moveAside(exe); err == nil {
+ t.Fatal("moved")
+ }
+ if *calls != 4 {
+ t.Fatalf("%d tries of a refused move, want 4", *calls)
+ }
+}
diff --git a/internal/update/install_blocked_windows_test.go b/internal/update/install_blocked_windows_test.go
index 38ad499d6..fc4cfa11a 100644
--- a/internal/update/install_blocked_windows_test.go
+++ b/internal/update/install_blocked_windows_test.go
@@ -4,6 +4,7 @@ import (
"errors"
"os"
"path/filepath"
+ "strings"
"testing"
"time"
)
@@ -150,3 +151,52 @@ func TestStartCheckedPutsBackForReal(t *testing.T) {
})
}
}
+
+// 白菜 on Discord: a download an antivirus holds longer than the short
+// pauses after the trial start goes in once it is let go, and a restart
+// that failed anyway, tried again, doesn't start the same download again
+// (which had the antivirus hold it again) but does a new one.
+func TestInstallBinaryWaitsOutALongHold(t *testing.T) {
+ waits, held := asideWaits, heldFor
+ asideWaits, heldFor = []time.Duration{time.Millisecond, 20 * time.Millisecond}, 5*time.Second
+ t.Cleanup(func() { asideWaits, heldFor = waits, held })
+ exe, staged := rollbackFiles(t)
+ release := holdForRollbackTest(t, staged)
+ go func() { time.Sleep(600 * time.Millisecond); release() }()
+ if _, err := InstallBinaryAside(staged, exe); err != nil {
+ t.Fatal("install while the download is held past the short pauses:", err)
+ }
+ wantInstallFile(t, exe, "new version")
+}
+
+func TestInstallBinaryTrialsADownloadOnce(t *testing.T) {
+ waits, held, was := asideWaits, heldFor, canStart
+ asideWaits, heldFor = []time.Duration{time.Millisecond}, 0
+ trials := 0
+ canStart = func(string) error { trials++; return nil }
+ t.Cleanup(func() { asideWaits, heldFor, canStart = waits, held, was })
+ exe, staged := rollbackFiles(t)
+ release := holdForRollbackTest(t, staged)
+ _, err := InstallBinaryAside(staged, exe)
+ if err == nil || !strings.Contains(err.Error(), "another program still has the download open") {
+ t.Fatalf("held for good: %v", err)
+ }
+ wantInstallFile(t, exe, "old version")
+ release()
+ if _, err := InstallBinaryAside(staged, exe); err != nil {
+ t.Fatal("tried again:", err)
+ }
+ wantInstallFile(t, exe, "new version")
+ if trials != 1 {
+ t.Fatalf("%d trial starts of one download, want 1", trials)
+ }
+
+ // a new download is tried
+ os.WriteFile(staged, []byte("newer version"), 0o755)
+ if _, err := InstallBinaryAside(staged, exe); err != nil {
+ t.Fatal(err)
+ }
+ if trials != 2 {
+ t.Fatalf("%d trial starts after a new download, want 2", trials)
+ }
+}
diff --git a/internal/update/install_rollback_windows_test.go b/internal/update/install_rollback_windows_test.go
index 2be597157..029eb910d 100644
--- a/internal/update/install_rollback_windows_test.go
+++ b/internal/update/install_rollback_windows_test.go
@@ -26,9 +26,9 @@ func rollbackFiles(t *testing.T) (string, string) {
// 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 })
+ was, held := asideWaits, heldFor
+ asideWaits, heldFor = []time.Duration{time.Millisecond}, 0
+ t.Cleanup(func() { asideWaits, heldFor = was, held })
}
func holdForRollbackTest(t *testing.T, path string) func() {
diff --git a/internal/update/install_windows_test.go b/internal/update/install_windows_test.go
index 7fc42c2e1..e4a406127 100644
--- a/internal/update/install_windows_test.go
+++ b/internal/update/install_windows_test.go
@@ -80,6 +80,9 @@ func TestInstallBinaryHeldOpen(t *testing.T) {
}
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)
diff --git a/internal/update/update.go b/internal/update/update.go
index 0d4d64d1f..976afa814 100644
--- a/internal/update/update.go
+++ b/internal/update/update.go
@@ -26,6 +26,7 @@ import (
"runtime"
"strconv"
"strings"
+ "sync"
"syscall"
"time"
@@ -440,9 +441,15 @@ func InstallBinary(staged, exe string) error {
func InstallBinaryAside(staged, exe string) (string, error) {
var old string
if runtime.GOOS == "windows" {
- if err := canStart(staged); err != nil {
- os.Remove(staged)
- return "", &BlockedError{Err: err}
+ // tried once per download: a restart clicked again after a failed
+ // one doesn't start it again, which would have an antivirus that
+ // checks a program as it starts hold it again for the move below
+ if !trialled.passed(staged) {
+ if err := canStart(staged); err != nil {
+ os.Remove(staged)
+ return "", &BlockedError{Err: err}
+ }
+ trialled.note(staged)
}
// A running .exe cannot be overwritten, but it can be moved aside.
// What the last update moved aside may still be running too (a
@@ -456,6 +463,11 @@ func InstallBinaryAside(staged, exe string) (string, error) {
}
}
if err := moveIn(staged, exe); err != nil {
+ if heldOpen(err) {
+ // the reason and what to do, shown as is in the version row
+ // and the Update pill's tooltip, without the paths
+ err = &saidError{fmt.Sprintf("couldn't put the new version in place of %s: another program still has the download open (often an antivirus checking it): let magpie through it and restart to update again, or download the new version and put it in place of this one", filepath.Base(exe)), err}
+ }
if old != "" {
if restoreErr := moveIn(old, exe); restoreErr != nil {
return "", errors.Join(err, fmt.Errorf("couldn't restore %s from %s: %w", filepath.Base(exe), old, restoreErr))
@@ -468,6 +480,16 @@ func InstallBinaryAside(staged, exe string) (string, error) {
return old, nil
}
+// saidError is err said in other words: what is shown, err what
+// errors.Is and errors.As find.
+type saidError struct {
+ said string
+ err error
+}
+
+func (e *saidError) Error() string { return e.said }
+func (e *saidError) Unwrap() error { return e.err }
+
// renameFile is os.Rename; tests make it fail.
var renameFile = os.Rename
@@ -476,6 +498,69 @@ var renameFile = os.Rename
// open, which Windows won't rename under, for a moment, and lets go.
var asideWaits = []time.Duration{100 * time.Millisecond, 200 * time.Millisecond, 400 * time.Millisecond, 800 * time.Millisecond, 1500 * time.Millisecond}
+// heldFor is how long a file Windows says another program holds open is
+// waited for, past asideWaits, the last pause repeated: an antivirus that
+// checks a new program as it starts may hold a 50 MB download longer than
+// asideWaits' three seconds, and a restart that gave up then would fail
+// the same way on every click, each click's trial start having it checked
+// again (白菜 on Discord). The user clicked restart and sees "Restarting…"
+// meanwhile. A var for tests.
+var heldFor = 30 * time.Second
+
+// waitHeld is whether to try again after try i (from 0) failed with err,
+// begun at start, sleeping first when so: through asideWaits for any error
+// retry allows, then on while err is a file held open, up to heldFor.
+func waitHeld(i int, err error, start time.Time, retry bool) bool {
+ if !retry {
+ return false
+ }
+ if i < len(asideWaits) {
+ time.Sleep(asideWaits[i])
+ return true
+ }
+ if !heldOpen(err) || len(asideWaits) == 0 {
+ return false
+ }
+ pause := asideWaits[len(asideWaits)-1]
+ if time.Since(start)+pause > heldFor {
+ return false
+ }
+ time.Sleep(pause)
+ return true
+}
+
+// trialled is the download canStart last let through, by path, size and
+// time written, so a restart tried again on it doesn't start it again.
+var trialled trialNote
+
+type trialNote struct {
+ mu sync.Mutex
+ path string
+ size int64
+ mod time.Time
+}
+
+func (n *trialNote) passed(path string) bool {
+ fi, err := os.Stat(path)
+ if err != nil {
+ return false
+ }
+ n.mu.Lock()
+ defer n.mu.Unlock()
+ return n.path == path && n.size == fi.Size() && n.mod.Equal(fi.ModTime())
+}
+
+func (n *trialNote) note(path string) {
+ fi, err := os.Stat(path)
+ n.mu.Lock()
+ defer n.mu.Unlock()
+ if err != nil {
+ n.path = ""
+ return
+ }
+ n.path, n.size, n.mod = path, fi.Size(), fi.ModTime()
+}
+
// Windows' errors for a file another program holds open without letting
// it be renamed.
const (
@@ -490,12 +575,12 @@ const (
// after the process has ended, and a move made at once failed the first
// install of every update (#2272). Any other failure is returned at once.
func moveIn(from, exe string) error {
- var err error
+ start := time.Now()
for i := 0; ; i++ {
- if err = os.Rename(from, exe); err == nil || i == len(asideWaits) || !heldOpen(err) {
+ err := os.Rename(from, exe)
+ if err == nil || !waitHeld(i, err, start, heldOpen(err)) {
return err
}
- time.Sleep(asideWaits[i])
}
}
@@ -512,18 +597,18 @@ func heldOpen(err error) bool {
// so a failed installation can put the running exe back.
func moveAside(exe string) (string, error) {
var err error
+ start := time.Now()
for i := 0; ; i++ {
old := oldName(exe)
if err = renameFile(exe, old); err == nil {
return old, nil
}
- if errors.Is(err, fs.ErrNotExist) || i == len(asideWaits) {
- break
- }
if i == 0 {
os.Chmod(exe, 0o755) // a read-only exe: the attribute off
}
- time.Sleep(asideWaits[i])
+ if !waitHeld(i, err, start, !errors.Is(err, fs.ErrNotExist)) {
+ break
+ }
}
why := err.Error()
var le *os.LinkError