Skip to content

fix: preserve mouse hook handle when UnhookWindowsHookEx fails#51418

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

fix: preserve mouse hook handle when UnhookWindowsHookEx fails#51418
ckerr merged 2 commits into
42-x-yfrom
trop/42-x-y-bp-fix-preserve-mouse-hook-handle-when-unhookwindowshookex-fails-1777577693356

Conversation

@trop
Copy link
Copy Markdown
Contributor

@trop trop Bot commented Apr 30, 2026

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 <officialasishkumar@gmail.com>

Co-authored-by: Asish Kumar <officialasishkumar@gmail.com>
Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>

Co-authored-by: Asish Kumar <officialasishkumar@gmail.com>
@trop trop Bot added 42-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes labels Apr 30, 2026
@ckerr ckerr merged commit f04ad9d into 42-x-y Apr 30, 2026
126 of 128 checks passed
@ckerr ckerr deleted the trop/42-x-y-bp-fix-preserve-mouse-hook-handle-when-unhookwindowshookex-fails-1777577693356 branch April 30, 2026 23:20
@release-clerk
Copy link
Copy Markdown

release-clerk Bot commented Apr 30, 2026

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

42-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