diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index d00c8ac47..97f5fb982 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -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 diff --git a/internal/gui/noactivate_windows.go b/internal/gui/noactivate_windows.go index d4c043d8f..e3fa05955 100644 --- a/internal/gui/noactivate_windows.go +++ b/internal/gui/noactivate_windows.go @@ -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 diff --git a/internal/gui/noactivate_windows_test.go b/internal/gui/noactivate_windows_test.go index 7f19de994..74e17f728 100644 --- a/internal/gui/noactivate_windows_test.go +++ b/internal/gui/noactivate_windows_test.go @@ -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) }