BESTLJP0412 on X (Windows 11 26200, 0.1.4179 to 0.1.4193, WebView2 157):
after an update's restart the window stayed "Not Responding", and a
process with a single thread sat on port 3425 and the WebView2 profile.
A Go process has several threads while it runs; one left means it is in
ExitProcess (os.Exit on Windows), which ends every other thread and then
runs each loaded DLL's detach on the last one. A detach that waits for a
thread already ended (WebView2's, COM's) never returns, so the old magpie
never ends, its handles stay open, and the new one waited its 60s
(awaitGone), went on beside it, and hung on the held port and profile.
Both ends fixed:
- the old one: cli.Main ends through endProcess, on Windows
TerminateProcess on itself instead of ExitProcess. Everything magpie
writes is written by then; the kernel closes every handle and no
detach runs.
- the new one: awaitGone's wait for the old magpie (awaitExit) looks at
its thread count every 250ms; one thread for a second is a magpie stuck
in its exit, and it is ended (opened with PROCESS_TERMINATE). This is
what fixes the update from 0.1.4193 and older, which still exit through
ExitProcess.
docs/subsystems/gui-shell.md says how a Windows restart waits and ends.
Tests (Windows, real processes; the stuck detach is a fiber-local storage
callback set to kernel32's Sleep with INFINITE, which ExitProcess runs on
its last thread and TerminateProcess doesn't):
- cli TestEndProcessSkipsAStuckDetach: first checks the stand-in is real
(os.Exit there hangs with exactly 1 thread left), then that
endProcess(3) ends with code 3. Broken by hand (TerminateProcess taken
out): red, "endProcess hung in a DLL's detach, as os.Exit does".
- update TestAwaitGoneEndsAMagpieStuckInItsExit: a predecessor stuck that
way is ended in ~1.4s. Broken by hand (the thread check skipped): red,
"the start went on after 20.022s with the old magpie still stuck".
Run:
- win-run on xiaomi-gpu (Windows 11): go test ./internal/cli ok (179s);
./internal/update all ok but TestPackagedByDpkg, red the same on
origin/main 442964d70 (#4603, a ':' file name on NTFS).
- mote: gofmt; go vet, GOOS=windows go vet, GOOS=linux -tags nogui go vet
of internal/cli and internal/update ok.
- mica: full go test ./... -timeout 45m started at this tree; still running
at push (ship-at-once rule), result to follow on the issue thread if red.
Not tried: a real update restart of the app on Windows with a stuck
WebView2 detach (the hang can't be brought on at will there); the fix is
checked with the FLS stand-in above.
22 lines
848 B
Go
22 lines
848 B
Go
package cli
|
|
|
|
import (
|
|
"os"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// endProcess ends magpie with code. On Windows os.Exit is ExitProcess,
|
|
// which ends every other thread and then runs each loaded DLL's detach on
|
|
// the one left: WebView2's, COM's and the GPU driver's included. One of
|
|
// those waiting on a thread already ended never returns, and the process
|
|
// stays with that one thread, still holding the gateway's port and its
|
|
// WebView2 profile, so the magpie an update restarted into hung "Not
|
|
// Responding" (BESTLJP0412 on X, 0.1.4179 to 0.1.4193). Everything magpie
|
|
// writes is written by now, and the kernel lets go of every handle, so
|
|
// the process ends at once instead (TerminateProcess skips the detaches).
|
|
func endProcess(code int) {
|
|
_ = windows.TerminateProcess(windows.CurrentProcess(), uint32(code))
|
|
os.Exit(code) // not reached
|
|
}
|