magpie icon indicating copy to clipboard operation
magpie copied to clipboard

gui, proc: on Windows the take-over ends the older magpie and then says "stuck", with the port already free

Open JasonBuildAI opened this issue 1 month ago • 3 comments

Who is hit

On Windows the Gateway page's take-over never completes. With an older magpie holding the gateway's port, the button ends that magpie, and then answers {ok:false, reason:"stuck"} naming the process it just ended, so the page still shows the older magpie as serving the gateway while nobody serves it. The user has to press the button again, and the second press works (nothing answers by then, so it starts the gateway here). Every Windows take-over, on an idle machine or a loaded one.

What happens

The read behind it, ListeningOn (listener_windows.go), asks Get-NetTCPConnection … -ErrorAction SilentlyContinue and returns the command's error as an error. When nothing listens on the port the cmdlet writes a non-terminating error and exits 1, so the read answers exit status 1 — where the macOS one (listener_darwin.go, lsof) and the Linux one (listener_linux.go, /proc) answer no processes and no error for the same case.

The take-over's letGo (gateway_takeover.go) needs exactly that answer to know an older magpie let the port go (err == nil && !contains(now, pids)). With an error it waits out its deadline — 5s, then 3s after the kill — and quitMagpieOnPort answers Reason:"stuck".

Repro

Windows 11 (10.0.26200), Windows PowerShell 5.1.26100.9711 (the powershell the read runs), Go 1.26.8, main at 8e8316ed.

PowerShell's own read, with nothing on port 1:

PS> powershell -NoProfile -NonInteractive -Command "Get-NetTCPConnection -State Listen -LocalPort 1 -ErrorAction SilentlyContinue | ForEach-Object { $_.OwningProcess }"
PS> $LASTEXITCODE
1

Nothing on stdout, nothing on stderr, exit 1. On a port with a listener: exit 0 and its pid.

Through the reader (this is the new test in the fix below):

$ go test -tags nogui -count=1 -run '^TestNothingListeningIsNoError$' -v ./internal/proc/
    listener_test.go:53: nothing listens on 58003: exit status 1
--- FAIL: TestNothingListeningIsNoError (12.09s)

The user-visible one (internal/gui):

$ go test -tags nogui -count=1 -run '^TestGatewayFixQuitsOnlyAMagpie$' -v ./internal/gui/
    gateway_takeover_test.go:131: take-over: {OK:false Reason:stuck PID:27104 Path:…\magpie.exe Port:50209 Version:0.1.550 Error:}

That test caps the take-over's waits at 3s and 2s, so I ran the same test with them at 30s and 20s, on the same code: still Reason:stuck, 77s in.

Expected / actual

  • Expected: a port nothing listens on is no processes and no error, as macOS and Linux already answer, so the take-over sees the older magpie let the port go and serves the gateway here in one press.
  • Actual: exit status 1, so the take-over ends the older magpie and reports stuck.

Relation to #1827

This is the TestGatewayFixQuitsOnlyAMagpie red in #1827, group C, whose body says "No cause below has been traced in code" and whose later triage reads it as test isolation ("This is mostly the test's budget, though it may also be slow for real on a loaded machine"). The 30s/20s run shows it is the product: whenever nothing listens the read is an error, so no wait is long enough. The other red in that test — TempDir RemoveAll cleanup: unlinkat …\magpie.exe: Access is denied — is separate and stays, as triaged there (the test hard-links the running binary).

Fix

fix/ag7c2f-windows-listening-none on the fork JasonBuildAI/magpie (head 85a8c8c0, one commit on main at 8e8316ed) — the read counts the cmdlet's exit 1 with nothing written as "none found" for as long as the caller's deadline hasn't passed, which keeps a read cut short an error (a killed PowerShell also exits 1 with nothing written). The same guard goes on listener_darwin.go, where an lsof cut short was read as none found too (listener_linux.go already answers the deadline with ctx.Err()). The tests are TestNothingListeningIsNoError and TestListeningOnCutShortDoesNotAnswerNone, the second failing when only the deadline guard is removed.

No pull request can be opened from this account: GitHub refuses the call (JasonBuildAI does not have the correct permissions to execute CreatePullRequest, and a REST POST /repos/yetone/magpie/pulls answers 404). The branch is offered in a comment below for review and cherry-pick, the way #1827's branches landed.

AI disclosure: reported with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 14 '26 16:10 JasonBuildAI

Updated at 85a8c8c0, rebased onto main at 8e8316ed, after two independent review rounds of the diff.

The branch is here, since this account can't open a pull request on this repo (GitHub refuses the create call: JasonBuildAI does not have the correct permissions to execute CreatePullRequest; a REST POST /repos/yetone/magpie/pulls answers 404):

  • branch fix/ag7c2f-windows-listening-none on the fork JasonBuildAI/magpie, one commit on main at 8e8316ed, head 85a8c8c0
  • git fetch https://github.com/JasonBuildAI/magpie fix/ag7c2f-windows-listening-none && git cherry-pick 85a8c8c0

Files: internal/proc/listener.go, internal/proc/listener_windows.go, internal/proc/listener_darwin.go, internal/proc/listener_test.go — 60 insertions, 4 deletions.

What changes: on Windows the read counts Get-NetTCPConnection's exit 1 with nothing on either stream, within the caller's deadline, as "none found" — the answer lsof and /proc already give. listener_darwin.go gets the deadline guard it was missing, so an lsof cut short is an error on a Mac too (lsof's stderr is not consulted there: whether it writes any on a genuine none found is unmeasured without a Mac); listener_linux.go already answers the deadline with ctx.Err(). The contract is written on ListeningOn.

The four cases, measured on the cmdlet itself on Windows 11 (10.0.26200), Windows PowerShell 5.1.26100.9711:

what err stdout stderr ctx
none found exit status 1 empty empty alive
a listener is there none its pid empty alive
the cmdlet isn't there exit status 1 empty 544 bytes alive
cut short at 1.5s / 3s exit status 1 empty empty deadline exceeded

So a read that failed loudly and a read cut short both stay errors. What the same shape cannot tell apart is a failure PowerShell keeps quiet about — it exits 1 for any error record it suppresses, and a process killed by something other than the caller looks the same — which reads as none found now where it read as an error before. That narrowing by stderr is the trade this makes, and the commit says so rather than leaving it for someone to find later.

Tests: TestNothingListeningIsNoError (red without the change: listener_test.go:53: nothing listens on 58003: exit status 1) and TestListeningOnCutShortDoesNotAnswerNone, which pins the deadline guard — its read's deadline is 300ms, under the 1.6s a bare 1 costs PowerShell here, so the read is cut short, and with only && ctx.Err() == nil removed it fails (a read cut short answered [] with no error, while this process holds the port). Its deadline is 300ms, under the 1.6s a bare 1 costs PowerShell here, so on this machine the read is really cut short for either direction of the change; on a machine that answers inside 300ms the assertion has nothing to bite. Where it bites differs by platform: on macOS it is red before the change, because lsof tolerates exit 1 without a deadline check there; on Windows upstream's reader already answers a cut-short read with an error, so there it pins the guard rather than demonstrating the fix - it goes red only when the tolerance is in place without the deadline check, the state this change must not regress into.

Evidence, all on Windows 11 (10.0.26200), Windows PowerShell 5.1.26100.9711, Go 1.26.8, rebased onto main at 8e8316ed, the package tests under a temporary HOME with GOPATH, GOMODCACHE and GOCACHE pinned:

  • internal/proc: TestListeningOn, TestNothingListeningIsNoError and TestListeningOnCutShortDoesNotAnswerNone pass — ok github.com/yetone/magpie/internal/proc 13.429s, and the whole package ok 41.169s.
  • the symptom with the change reverted: gateway_takeover_test.go:131: take-over: {OK:false Reason:stuck PID:27104 Path:…\magpie.exe Port:50209 Version:0.1.550}. That test caps the take-over's waits at 3s and 2s, so I ran it with them at 30s and 20s on the same code: still Reason:stuck, 77s in. No wait is long enough, so this is the product, not the test's budget. With the change the take-over assertion passes; that test's TempDir RemoveAll cleanup: unlinkat …\magpie.exe: Access is denied red stays, as triaged in #1827.
  • TestSetPortMovesTheGateway and TestSetPortServesWhenTheOldOneWasTaken pass. TestGatewayHeldByAContainer fails on that same cleanup red, and on its line-111 another magpie serves the gateway it fails on the unpatched code as well.
  • gofmt -l on the changed files prints nothing. go vet ./..., GOOS=windows go vet ./..., GOOS=linux go vet -tags nogui ./... exit 0; go build ./..., GOOS=windows go build ./..., GOOS=linux go build -tags nogui ./... exit 0; go test -run '^$' ./... (the step Windows CI compiles with) exits 0. GOOS=darwin go vet ./internal/proc/ and GOOS=darwin go build ./internal/proc/ exit 0 — a cross-build is not a test, and no Mac was available here, so the darwin line rests on the Windows measurement.
  • go test -tags nogui ./..., whole suite, at the head before this one: not readable on this box, which runs several other Go suites at once. Six packages hit the 30m package timeout (agent, cli, davsync, gateway, gui, library), and every non-timeout failure it reported (backup, pluginrt, provider, sessions, update, usage) is one the unpatched baseline at 87d83e7a reported too, with three 20m timeouts of its own. Nothing in internal/proc failed in either.

AI disclosure: reported with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 14 '26 17:10 JasonBuildAI

Thanks @JasonBuildAI, this is fixed in b25659344, with a test-only follow-up in 4f020a95f.

Cause. When nothing listens on the port, Get-NetTCPConnection fails with ObjectNotFound, and PowerShell exits 1 even with -ErrorAction SilentlyContinue. ListeningOn therefore returned "exit status 1" where lsof and /proc return "no pids". The take-over waits for exactly that "no listener" answer, so it ran out its deadline and said "stuck" while the port was already free.

Fix. The command now catches the error and exits 0 only for ObjectNotFound. Your 85a8c8c0 instead treated "exit 1 with both streams empty" as none found. I went with catching the category so that a silent failure of another kind (a missing cmdlet, or a PowerShell cut short) still reads as an error. Your separate point about the darwin reader having no deadline guard is not part of this change.

Test fix (4f020a95f). TestGatewayFixQuitsOnlyAMagpie hard-linked the running test binary. On Windows the temp folder's cleanup then failed with "Access is denied". The test now copies the binary there instead.

What was run on the Windows box:

  • The new TestNothingListeningIsNoError: it fails on the old code with "exit status 1" and passes with the fix.
  • TestGatewayFixQuitsOnlyAMagpie: on the old code it says "stuck" after 12.5s. With both commits it passed 3 of 3 runs, about 7s each.

On macOS, go test -tags nogui ./... passes, and go vet is clean on all three OSes.

This ships in the next release after v0.1.1231. I'll leave this open until you confirm.

yetone avatar Oct 14 '26 19:10 yetone

Confirmed on main (82f48f09), this Windows box, Go 1.26.8:

> go test -tags nogui -count=1 -run TestNothingListeningIsNoError ./internal/proc/
ok  github.com/yetone/magpie/internal/proc  15.555s

On the review point: I agree the ObjectNotFound catch is the better rule than 85a8c8c0's "exit 1 with both streams empty". A read cut short has exactly that shape - measured here with the same call shape (exec.CommandContext + Output, a PowerShell given 300ms and then 1.5s to live): both deadlines return exit status 1 as an *exec.ExitError with stdout and stderr both empty, and errors.Is(err, context.DeadlineExceeded) false. So the streams alone cannot tell a silent failure from a cut-short one, and the failure category can; my version narrowed on the wrong side. Yours is the one to keep.

One point of mine does survive your commit, and it is macOS-only. internal/proc/listener_darwin.go:22 accepts that same shape - an *exec.ExitError with empty stdout - as none found, and a command killed by its deadline reports through an *ExitError rather than ctx.Err() (what I measured above; on macOS a killed lsof reports as signal: killed, the same shape, which I cannot measure here - no Mac). So an lsof the deadline cut short on a loaded Mac can read as "no listener", the same direction of error the take-over just lost; listener_linux.go is the one reader that answers ctx.Err(). Small, macOS-only, and I have not filed it - your call whether it is worth tracking, and I am happy to write it up with a test if you want it.

Closing this one: the reported "stuck" take-over is fixed, and b25659344 with 4f020a95f is on main but not in v0.1.1242, so it ships in the next release. My branch fix/ag7c2f-windows-listening-none (85a8c8c0) stays unmerged.

AI disclosure: this verification was produced with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 14 '26 21:10 JasonBuildAI