gui: the no-activate hook's handle is swapped atomically, and the Windows exit doc says what the unhook leaves to endProcess (#6705)
On top of wizd's unhook (#6705, afe3b3ff7, kept as authored with its test): noActivateHook becomes an atomic.Uintptr that unhookNoActivate swaps to 0, so the three places that unhook (OnShutdown, the main-thread window's WM_DESTROY, Run's return) take it off once without a racy read-then-clear. Nothing hooks it again: hookNoActivate runs its SetWindowsHookEx under noActivateOnce, and noActivateHooked stays true after the unhook. 07dfd0bb8's endProcess (TerminateProcess on itself) and awaitExit are kept and the gui-shell doc says why: they still cover the other DLL detaches, an os.Exit while the app runs (Wails' fatal error, which this unhook doesn't reach), and an update from a version before the fix. Exit paths reviewed: Quit (tray, page) runs Wails' cleanup, whose shutdown tasks run on the main thread (InvokeSync), so OnShutdown unhooks on the hook's thread before Run returns into endProcess. A restart to update quits the same way. Wails' SingleInstance os.Exit in a second instance comes in application.New, before any window exists, so no hook is on. SIGINT/SIGTERM in the app go to Wails (ownSignals), so through OnShutdown. The handover's quitBy exitNow is Mac/Linux only. Run at this tree on base72e06980e(the rebase onto028b07b4badds only routing JS and docs), on xiaomi-gpu (Windows 11 26200, Go 1.26.8) through win-run: - TestUnhookNoActivatePreventsExitDeadlock -count=3: PASS 3/3. - broken by hand (unhookNoActivate returning at once), at the tree before the rebase: unhook and destroy-main both FAIL "process hung in ExitProcess (stderr=fatal error: exitsyscall: syscall frame is no longer valid ...)". - go test ./internal/gui: 11 reds. TestCoreScriptBudget (app.js 10 bytes over its budget) is red the same on macOS at72e06980ewith no script touched here; filed as #6733. The other 10 (TestGatewayHeldByAContainer, TestLodyImportHandoff, TestImportStatusUnreadableIsUnknown, TestAionUiImportRow, TestCherryStudioImportRow, TestProviderSaveClientCert, TestPageAwayLetsStatsGo, TestPluginAgentMissingSaysSo, TestGatewayModeStatsNameTheGatewaySessions, TestUpdateThatWontStart) fail the same on origin/main9cd5f3652run alone there, and are filed in #6068 (also #5324, #5928, #6059). - In the signed-in session (scheduled task /IT, where msctf is loaded): TestRestartBySelfAsksNoAttention (window, tray), TestRestartBySelfComesBackQuietly and TestUnhookNoActivatePreventsExitDeadlock pass. - A real build (go build -tags production -H windowsgui) run as `magpie app` in a sandbox profile (USERPROFILE/APPDATA/LOCALAPPDATA redirected, MAGPIE_ADDR :3461, page on :3462) in the signed-in session, quit four times through POST /api/window/quit?now=1: each exited in 51-62ms. The magpie:// handler it registered was put back afterwards. On mica: gofmt -l clean, go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: ok. Not tried: a restart to update of a real build on Windows (needs a staged update); the wedge itself in a real app (07dfd0bb8's endProcess already ends the normal quit without ExitProcess, so it can't be brought on at will); Windows on arm64.
This commit is contained in:
@@ -1050,7 +1050,13 @@ ended, leaving a process with one thread on port 3425 and the profile and
|
||||
the new window "Not Responding" (BESTLJP0412 on X). A predecessor that
|
||||
still sits with one thread for a second (an older version, stuck so) is
|
||||
ended by the new one (`awaitExit`). See `TestEndProcessSkipsAStuckDetach`
|
||||
and `TestAwaitGoneEndsAMagpieStuckInItsExit` (Windows).
|
||||
and `TestAwaitGoneEndsAMagpieStuckInItsExit` (Windows). One cause of that
|
||||
one thread was magpie's own: the no-activate hook, left on while
|
||||
`ExitProcess` ran, was called from its FLS cleanup and wedged the exit
|
||||
(#6705). It is taken off as the app shuts down (`unhookNoActivate`).
|
||||
`endProcess` and `awaitExit` stay: for the other detaches, for an
|
||||
`os.Exit` while the app still runs (Wails' on a fatal error), and for
|
||||
versions before the fix.
|
||||
|
||||
Two magpies never show at once. On a Mac the old one lets go of its
|
||||
instance lock before it gives the socket (`releaseInstance`), so the new
|
||||
|
||||
@@ -45,7 +45,7 @@ var showingQuietly atomic.Uintptr
|
||||
var (
|
||||
noActivateOnce sync.Once
|
||||
noActivateHooked atomic.Bool
|
||||
noActivateHook w32.HHOOK
|
||||
noActivateHook atomic.Uintptr // the hook's handle, 0 once unhooked
|
||||
)
|
||||
|
||||
// hookNoActivate puts the hook on hwnd's thread the first time a window
|
||||
@@ -59,7 +59,7 @@ func hookNoActivate(hwnd uintptr) {
|
||||
return
|
||||
}
|
||||
noActivateOnce.Do(func() {
|
||||
noActivateHook = w32.SetWindowsHookEx(whCBT, noActivateCBT, 0, w32.DWORD(w32.GetCurrentThreadId()))
|
||||
noActivateHook.Store(uintptr(w32.SetWindowsHookEx(whCBT, noActivateCBT, 0, w32.DWORD(w32.GetCurrentThreadId()))))
|
||||
noActivateHooked.Store(true)
|
||||
})
|
||||
}
|
||||
@@ -72,11 +72,13 @@ func hookNoActivate(hwnd uintptr) {
|
||||
// g1.syscallsp, so cgocallbackg's exitsyscall throws "exitsyscall: syscall
|
||||
// frame is no longer valid", and fatalthrow's runtime.exit(2) self-deadlocks
|
||||
// on runtime.finlock, leaving the process wedged with one thread holding the
|
||||
// single-instance mutex and WebView2 profile (#5922, #6678).
|
||||
// single-instance mutex and WebView2 profile (#5922, #6678). Once taken
|
||||
// off it stays off: hookNoActivate hooks once a run (noActivateOnce). The
|
||||
// handle is swapped, so of OnShutdown's, WM_DESTROY's and Run's calls
|
||||
// only the first unhooks.
|
||||
func unhookNoActivate() {
|
||||
if noActivateHook != 0 {
|
||||
w32.UnhookWindowsHookEx(noActivateHook)
|
||||
noActivateHook = 0
|
||||
if h := noActivateHook.Swap(0); h != 0 {
|
||||
w32.UnhookWindowsHookEx(w32.HHOOK(h))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +86,7 @@ func noActivateCBT(code int, wParam w32.WPARAM, lParam w32.LPARAM) w32.LRESULT {
|
||||
if code == hcbtActivate && quietActivation(wParam) {
|
||||
return 1
|
||||
}
|
||||
return w32.CallNextHookEx(noActivateHook, code, wParam, lParam)
|
||||
return w32.CallNextHookEx(w32.HHOOK(noActivateHook.Load()), code, wParam, lParam)
|
||||
}
|
||||
|
||||
// quietActivation says hwnd, about to be activated, is a magpie window
|
||||
|
||||
@@ -52,7 +52,7 @@ func init() {
|
||||
os.Exit(9)
|
||||
}
|
||||
appWndProc(hwnd, w32.WM_NULL, 0, 0)
|
||||
if noActivateHook == 0 {
|
||||
if noActivateHook.Load() == 0 {
|
||||
os.Stderr.WriteString("noActivateHook was not installed")
|
||||
os.Exit(9)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user