update: the Windows trial start leaves the download held, so the move right after it fails and the first install of every update fails (TestTrialStart red there; the Windows CI step does not run the trial tests)
internal/update, the Windows install path: InstallBinaryAside (#894, 9ca64d10, 2026-10-05) starts the downloaded magpie as a trial (canStart -> trialStart, Windows checks a program when it creates its process) and then moves that same file in with os.Rename(staged, exe) microseconds later. The trial leaves the file held for a moment - whatever reads a program as it starts (antivirus, indexer, a sync client: the hold moveAside already knows) - so the move fails with The process cannot access the file because it is being used by another process, and the first install of every download fails on such a machine. Measured on a plain Windows 11 box (10.0.26200, Go 1.26.8, no changes to the repo): the first install of a fresh download failed 10 of 10 times; a second try - the same file, already scanned once - installed it 10 of 10 times. TestTrialStart, the repo's own test of exactly this property ("ended, not left running: its file can go", internal/update/install_blocked_windows_test.go:56), is red there since the trial landed. The Windows CI step "Windows update install" does not run it (its -run names TestInstallBinary* and TestMoveAside* only), so nothing shows it, and #1827's Windows sweep didn't catch it (that box let the file go in time).
Who pays: Windows users of the app whose machine holds a program that has just started. They click Restart to update, nothing restarts, and Settings shows the download with a raw sharing-violation error next to it; a second click installs it. Every new download repeats the pattern, so the app's own updater is a failed attempt each time instead of an update, and a user who doesn't click again stays on the old version (the update is the only way fixes reach them). The trial is the newest step in a path the repo already treats as hold-prone: moveAside and putBack both wait the same hold out, internal/update/update.go:465 - the trial's move-in was the one move left without it.
Reproduce
At main 5618ade1 on Windows 11, Go 1.26.8, sandboxed home (XDG_CONFIG_HOME/TEMP under a temp folder; the repo untouched):
go test -count=1 -v ./internal/update/ -run '^TestTrialStart$'
=== RUN TestTrialStart
install_blocked_windows_test.go:56: still held: remove C:\Temp\...\magpie.exe.new: The process cannot access the file because it is being used by another process.
--- FAIL: TestTrialStart (0.05s)
FAIL github.com/yetone/magpie/internal/update 0.304s
The install path itself, with a real trial (canStart = trialStart) and a real program under the .new name the download has:
=== RUN TestInstallRightAfterTheTrial
install_blocked_windows_test.go:147: install after the trial: rename ...\magpie.exe.new ...\magpie.exe: The process cannot access the file because it is being used by another process.
--- FAIL: TestInstallRightAfterTheTrial (0.05s)
and the retry the GUI offers, on the same file (3 attempts, fresh files, 10 rounds):
first attempt installed 0/10, second (after a first failure) 10/10
The hold, measured with trialStart's own flags (start suspended with CREATE_SUSPENDED|CREATE_NO_WINDOW, Kill, Wait) on a copy of PING.EXE, 5 runs each, and a control that is copied but never started:
ping 25KB released after [14.7ms 14.8ms 16.6ms 15.8ms 15.0ms]
ping+32MB zeros released after [228ms 241ms 234ms 235ms 231ms]
gateway.test 43MB released after [299ms 301ms 315ms 304ms 293ms]
B: copy not started, rename err: <nil>
So the file is not held by the write but by the start, it is held for 15ms at 25KB and ~300ms at 43MB, and the move in InstallBinaryAside happens within ~1ms of the trial returning: the move cannot win that race on a machine where the hold exists. The second attempt only succeeds because the second start of a file that was already scanned is held briefly enough that the move gets through.
Cause
trialStart (internal/update/proc_windows.go) ends the trial process and returns as if the file were free; InstallBinaryAside (internal/update/update.go:428) relies on that and renames the staged file in directly. Nothing in between waits for the file to be let go, unlike every other move of a file that may be held in this package (moveAside, update.go:477; putBack, blocked.go:143, both over asideWaits). The test the trial landed with asserts the property that is missing, and it is red on any machine that holds the file.
A file still held when trialStart returns is not a reason to fail the trial: Windows did run the program, so the verdict stays. It is the move that must wait for the file, and the trial is where the hold is created.
What was run
gofmt -l internal/update- clean.go vet ./internal/update/(Windows),GOOS=linux go vet -tags nogui ./internal/update/,GOOS=darwin go vet ./internal/update/- clean.go test -count=1 ./internal/update/on Windows 11 - red before, green after;TestTrialStart0.05s FAIL before / 0.13s PASS after; the two new tests 0.10s and 0.05s FAIL before, 0.33s and 0.13s PASS after.go test -tags nogui -run '^$' ./...(the Windows CI compile step) - all packages compile.- The CI step the fix extends, run by hand:
go test -v -count=1 -timeout 2m ./internal/update -run '^(TestInstallBinary|TestMoveAside|TestTrialStart|TestInstallRightAfterTheTrial)'- 4.7s, all pass.
Sibling check
canStart has one caller (InstallBinaryAside); trialStart has no other caller. The other places that move a just-started file already wait: putBack (the relaunch that didn't come up) retries over asideWaits before giving up, and moveAside retries the running exe the same way; TestInstallBinaryRestoresAfterStagedRenameFails (PR #632) already pins the "a scanner holds the download" failure and its rollback, which is why a failed first attempt restores the exe instead of leaving nothing. Nothing else in the package moves a file it started itself.
Fix
trialStart waits for the file to be let go before returning, over the pauses moveAside already uses for the same hold; a file still held after them is left to the move that fails on it, whose error says why, so the trial's verdict is untouched. Two tests pin it: TestTrialStartWaitsForTheFile holds the file itself (until 150ms in) and fails if trialStart returns while it is still held, and TestInstallRightAfterTheTrial runs the install with the real canStart (the order InstallBinaryAside makes) and fails before the wait. The Windows CI step's -run pattern now names the trial tests, which it did not.
Branch fix/windows-trial-hold-b7f2d on the shared fork JasonBuildAI/magpie, two commits on 5618ade1 (4b748a73 the fix, 42f1e3c0 the CI step), diff:
--- a/internal/update/proc_windows.go
+++ b/internal/update/proc_windows.go
@@ -57,7 +57,11 @@
// trialStart starts path suspended and ends it at once: Windows checks a
// program when it creates its process, so one Smart App Control or
// another application control policy won't run fails here, and none of
-// one it will run runs.
+// one it will run runs. A program's file stays held a moment after the
+// process ends - whatever scans a program as it starts - and the caller
+// moves it in right after (InstallBinaryAside): wait for it to be let go
+// first, as moveAside waits out the same hold, so the trial doesn't make
+// that move fail with a sharing violation it caused itself (#2272).
func trialStart(path string) error {
@@
cmd.Process.Kill()
cmd.Wait()
+ awaitMovable(path)
return nil
}
+
+// movable reports whether path may be renamed or removed now: a handle
+// another program holds without sharing delete - a scanner reading a
+// program that just started - makes opening it for delete fail with a
+// sharing violation. Any other failure is not about the hold, and the
+// move that fails on it says what it is.
+func movable(path string) bool {
+ p, err := syscall.UTF16PtrFromString(path)
+ if err != nil {
+ return true
+ }
+ h, err := syscall.CreateFile(p, deleteAccess, syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE, nil, syscall.OPEN_EXISTING, 0, 0)
+ if err == nil {
+ syscall.CloseHandle(h)
+ return true
+ }
+ return !errors.Is(err, errSharingViolation) && !errors.Is(err, errLockViolation)
+}
+
+// awaitMovable waits for the file at path to be let go, over the pauses
+// moveAside uses: the hold the trial's own start caused is a moment at
+// most, and a file still held after them is left to the move that fails
+// on it, whose error says why.
+func awaitMovable(path string) {
+ for i := 0; ; i++ {
+ if movable(path) || i == len(asideWaits) {
+ return
+ }
+ time.Sleep(asideWaits[i])
+ }
+}
The tests (install_blocked_windows_test.go) and the CI line (test.yml: -run '^(TestInstallBinary|TestMoveAside|TestTrialStart|TestInstallRightAfterTheTrial)') are in the same two commits.
Raw after, same box, at 4b748a73:
=== RUN TestTrialStart
--- PASS: TestTrialStart (0.13s)
=== RUN TestTrialStartWaitsForTheFile
--- PASS: TestTrialStartWaitsForTheFile (0.33s)
=== RUN TestInstallRightAfterTheTrial
--- PASS: TestInstallRightAfterTheTrial (0.13s)
PASS
ok github.com/yetone/magpie/internal/update 0.703s
and the whole package, same box:
ok github.com/yetone/magpie/internal/update 15.575s
No PR travels with this for this account: the README says the repository doesn't take pull requests, and the API answers 404 "not the correct permissions to execute CreatePullRequest". The branch above is on the shared fork, and the maintainers are welcome to it.
AI disclosure: this change was drafted with AI assistance at my request, under my direction and review; I set the constraint that the evidence be a raw before/after run on the machine it fails on, and I reviewed the diff and this report before posting. The Windows runs above were made in this session on this machine.
Two additions after an independent review pass of the branch, from a fresh clone at 5618ade1 with the new tests copied in:
TestTrialStartWaitsForTheFile: base 0/10 pass,42f1e3c010/10 pass.TestInstallRightAfterTheTrial: base 0/10, head 10/10. BaseTestTrialStartred again (9/10), so the hold is a race, not a constant.- Head re-checked by hand:
gofmt -l internal/updateclean,go vet ./internal/update/clean,GOOS=linuxbuild/vet andGOOS=darwinvet clean, and the CI step's exact-runpattern green (ok github.com/yetone/magpie/internal/update 4.451s).
Known limits of the fix, for the record:
- A file still held after the five pauses (~3s) is left to the move that fails on it: same sharing violation as before, so the trial's verdict is untouched and the only cost is the wait on an install that was failing anyway.
- The
os.Rename(staged, exe)itself still isn't retried. The hold observed here and by the reporter begins right afterWaitand lasts 15ms-300ms, so the wait catches it; a hold that only begins after the last successful probe can still fail the move - the same windowmoveAsidetolerates today. TestTrialStartWaitsForTheFileholds the file itself until 150ms, so it would false-pass on a machine where a baretrialStarttakes longer than that (it is ~40ms here).
Fixed in c644d9f1d, thanks @JasonBuildAI. InstallBinaryAside now moves the download into place (and puts the old exe back on failure) through moveIn. While Windows says the file is held (errors 32 and 33), moveIn tries again over 100 ms to 1.5 s, the same waits moveAside already uses for the running exe. The Windows CI step now runs TestTrialStart and the new trial tests too.
What was run on a Windows 10.0.26200 x64 box (Defender real-time on), using cross-compiled test binaries in a temp dir with a sandbox USERPROFILE, APPDATA and LOCALAPPDATA:
- New
TestInstallBinaryWaitsForHeldDownload(holds the download open for 150 ms):- old code: FAIL, "The process cannot access the file because it is being used by another process";
- new code: PASS.
- The whole update package passes, and so does the CI pattern
^(TestInstallBinary|TestMoveAside|TestTrialStart).
What I could not reproduce on that box was the hold itself:
- origin/main's
TestTrialStartpassed 3 of 3. - The real trial-then-install path succeeded 10 of 10 with a 12 MB exe and 10 of 10 with a 74 MB magpie.exe.
- A real
magpie updatefrom v0.1.1243 to 0.1.1244 in two sandbox homes succeeded 2 of 2.
So the fix is verified with an injected hold, not an antivirus one. If you still see the first install fail after the next release, please post the exact error. The fix ships in the next release, and I'm keeping this open until then.
Same box class as the report (Windows 11 10.0.26200 x64), magpie 0.1.2364 updating to 0.1.2404, and the hold still lands - but one step earlier than c644d9f1d covers, and what follows it is worse than "click again".
1. The trial's own fork/exec fails, before any move
magpie 0.1.2404 is out (you have 0.1.2364) · https://github.com/yetone/magpie-releases/releases/tag/v0.1.2404
downloading magpie-windows-amd64.exe through https://gh-proxy.com …
magpie: the new version couldn't start: fork/exec C:\Users\…\magpie\update\238366835f09\magpie.exe.new: The process cannot access the file because it is being used by another process.; download it from https://usemagpie.ai and run it yourself
the new version couldn't start: … wraps couldn't start %s: %w - the trial's CreateProcess, not moveIn's rename. There was no file to move yet, so moveIn's 100ms-1.5s retries never ran. The 74,204,672-byte file had been written seconds before by the same run, so whatever reads a freshly written exe held it before the start: the reverse order of the hold this issue measured, and the one awaitMovable after the start cannot see.
2. After that, every later attempt dies on 416
Second click and every one after it:
magpie: download magpie.exe.new through the mirror https://gh-proxy.com/: 416 Requested Range Not Satisfiable
2.7 s, no bytes, no retry. internal/update/update.go at main 2cca1117:
download()opens withos.Remove(path)(update.go:1019) to drop the leftover and ignores the error - the leftover is complete and held, so it survives.fetch()stats it (update.go:1076) and sendsRange: bytes=<have>-(update.go:1083).haveis exactly the asset's size, so the range starts at EOF: unsatisfiable by definition (RFC 7233), 416 from any host, mirror or not.- 416 goes to the default branch (
update.go:1110) and becomes aMirrorError;fail()'sos.Remove(update.go:1087) hits the same hold;download()stops on the first error that isn'tNetwork(err). The next click repeats it byte for byte.
So the "a second attempt installs it, 10/10" behaviour does not hold on this box: the second attempt is a permanent 416 whose message names a mirror status rather than the hold, and the file cannot be cleared by hand while held. The only way out here was quitting the app and replacing the exe manually; magpie update cannot reach that state.
3. What would have saved it
- If
have == a.Size(the feed carries the size), skip the request and go straight to the hash check and the trial. - Check the
os.Removeresults atupdate.go:1019/1034/1087and report the hold ("… is held by another process") instead of turning it into a 416. - Treat 416 as "this file is unusable": truncate or rename it aside and start over once.
4. For scale
rm on the leftover answered Device or resource busy for at least three minutes while the app was running, which is well past the ~3 s the waits top out at. Defender's realtime service answers 0x800106ba on this box, so the reader is something else that touches a new exe (indexer or sync client).
AI disclosure: found and written up with AI assistance at my request, on my machine, from the raw error strings and the code above; I checked the bytes against the release SHA256SUMS before replacing the binary by hand.