Skip to content

fix: preserve mouse hook handle when UnhookWindowsHookEx fails - #51419

Merged
ckerr merged 2 commits into
41-x-yfrom
trop/41-x-y-bp-fix-preserve-mouse-hook-handle-when-unhookwindowshookex-fails-1777577693988
Apr 30, 2026
Merged

ckerr merged 2 commits into
41-x-yfrom
trop/41-x-y-bp-fix-preserve-mouse-hook-handle-when-unhookwindowshookex-fails-1777577693988

Conversation

@trop

@trop trop Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Backport of #51098

See that PR for details.

Notes: Fixed an issue on Windows where a transient UnhookWindowsHookEx failure in setIgnoreMouseEvents(true, { forward: true }) teardown could cause duplicate low-level mouse hooks to be installed on the next activation.

trop Bot and others added 2 commits April 30, 2026 19:34
NativeWindowViews::SetForwardMouseMessages() installs a low-level mouse
hook when mouse forwarding begins and unhooks it once no window needs
forwarding. The previous code reset the shared `mouse_hook_` handle to
`nullptr` unconditionally after calling UnhookWindowsHookEx, even when
the unhook call failed.

When unhooking fails, the hook is still installed in the system. Because
`mouse_hook_` is nulled out anyway, the next call to
SetForwardMouseMessages(true) evaluates `if (!mouse_hook_)` as true and
installs a second, duplicate hook via SetWindowsHookEx, so every mouse
message is processed by MouseHookProc multiple times.

Check the return value of UnhookWindowsHookEx and only null the handle
on success. When the call fails, leave `mouse_hook_` pointing at the
existing hook so the next activation reuses it rather than stacking a
new one on top, and log the failure via PLOG to surface the underlying
Windows error.

Fixes: #51064
Signed-off-by: Asish Kumar <[email protected]>

Co-authored-by: Asish Kumar <[email protected]>
Signed-off-by: Asish Kumar <[email protected]>

Co-authored-by: Asish Kumar <[email protected]>
@trop trop Bot added 41-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes labels Apr 30, 2026
@ckerr
ckerr merged commit 6037d59 into 41-x-y Apr 30, 2026
121 of 123 checks passed
@ckerr
ckerr deleted the trop/41-x-y-bp-fix-preserve-mouse-hook-handle-when-unhookwindowshookex-fails-1777577693988 branch April 30, 2026 23:40
@release-clerk

release-clerk Bot commented Apr 30, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed an issue on Windows where a transient UnhookWindowsHookEx failure in setIgnoreMouseEvents(true, { forward: true }) teardown could cause duplicate low-level mouse hooks to be installed on the next activation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

41-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant