Skip to content

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

Merged
jkleinsc merged 2 commits into
electron:mainfrom
officialasishkumar:fix/native-window-views-unhook-mouse-hook
Apr 30, 2026
Merged

jkleinsc merged 2 commits into
electron:mainfrom
officialasishkumar:fix/native-window-views-unhook-mouse-hook

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

Description of Change

NativeWindowViews::SetForwardMouseMessages() installs a low-level mouse hook via SetWindowsHookEx(WH_MOUSE_LL, ...) 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:

if (forwarding_windows_->empty()) {
  UnhookWindowsHookEx(mouse_hook_);
  mouse_hook_ = nullptr;
}

Per the UnhookWindowsHookEx documentation, a failure (FALSE return) means the hook is still installed in the system. Because mouse_hook_ was nulled out regardless, the next call to SetForwardMouseMessages(true) would evaluate if (!mouse_hook_) as true and install a second hook via SetWindowsHookEx, leading to duplicate MouseHookProc invocations for every mouse message.

This PR checks the return value of UnhookWindowsHookEx and only nulls mouse_hook_ on success. When the call fails, the handle is left pointing at the still-installed hook so the next activation reuses it instead of stacking a new one on top, and the failure is logged via PLOG(WARNING) to surface the underlying Windows error.

Fixes #51064

Checklist

Release Notes

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.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Apr 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR contains unsigned commits. This repository enforces commit signatures
for all incoming PRs. To get your PR merged, please sign those commits
(git rebase --exec 'git commit -S --amend --no-edit -n' @{upstream}) and force push them to this branch
(git push --force-with-lease)

For more information on signing commits, see GitHub's documentation on Telling Git about your signing key.

@github-actions github-actions Bot added the needs-signed-commits Currently some or all of the commits in this PR are not signed label Apr 16, 2026
@officialasishkumar
officialasishkumar force-pushed the fix/native-window-views-unhook-mouse-hook branch from 94454e1 to 9ea0694 Compare April 16, 2026 18:17
@github-actions github-actions Bot removed the needs-signed-commits Currently some or all of the commits in this PR are not signed label Apr 16, 2026
@qiaohang

Copy link
Copy Markdown

Suggestion:For better robustness, when UnhookWindowsHookEx fails, we should verify whether the error code is ERROR_INVALID_HOOK_HANDLE.

@codebytere codebytere left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! Once we handle ERROR_INVALID_HOOK_HANDLE we should be able to move this forward.

@officialasishkumar
officialasishkumar force-pushed the fix/native-window-views-unhook-mouse-hook branch from 9ea0694 to bf1f2b7 Compare April 18, 2026 17:49
@officialasishkumar

officialasishkumar commented Apr 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Handled ERROR_INVALID_HOOK_HANDLE in bf1f2b7: if UnhookWindowsHookEx reports an invalid hook handle, the cached hook is cleared; other unhook failures keep the handle and log the specific Windows error.

@github-actions github-actions Bot added the needs-signed-commits Currently some or all of the commits in this PR are not signed label Apr 18, 2026
@codebytere

Copy link
Copy Markdown
Member

@officialasishkumar commits need to be signed

@officialasishkumar
officialasishkumar force-pushed the fix/native-window-views-unhook-mouse-hook branch from bf1f2b7 to ae8ad98 Compare April 22, 2026 08:21
@github-actions github-actions Bot removed the needs-signed-commits Currently some or all of the commits in this PR are not signed label Apr 22, 2026
@codebytere codebytere added semver/patch backwards-compatible bug fixes target/41-x-y PR should also be added to the "41-x-y" branch. target/42-x-y PR should also be added to the "42-x-y" branch. labels Apr 22, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Apr 22, 2026
@jkleinsc

Copy link
Copy Markdown
Member

@officialasishkumar can you rebase your PR with the latest from main? Hopefully that will clear the failing Windows arm64 test.

@officialasishkumar
officialasishkumar force-pushed the fix/native-window-views-unhook-mouse-hook branch from ae8ad98 to 53cbf2b Compare April 28, 2026 18:36
@officialasishkumar

Copy link
Copy Markdown
Contributor Author

Rebased on latest main and pushed (53cbf2b).

@github-actions github-actions Bot added the needs-signed-commits Currently some or all of the commits in this PR are not signed label Apr 28, 2026
@codebytere

Copy link
Copy Markdown
Member

@officialasishkumar please fix unsigned commits

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: electron#51064
Signed-off-by: Asish Kumar <[email protected]>
@officialasishkumar
officialasishkumar force-pushed the fix/native-window-views-unhook-mouse-hook branch from 53cbf2b to b55cf38 Compare April 29, 2026 21:28
@github-actions github-actions Bot removed the needs-signed-commits Currently some or all of the commits in this PR are not signed label Apr 29, 2026
@officialasishkumar

Copy link
Copy Markdown
Contributor Author

Signed the commits and force-pushed b55cf38; the signed-commit check is passing now.

@jkleinsc
jkleinsc merged commit d0612e2 into electron:main Apr 30, 2026
69 checks passed
@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.

@trop

trop Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "42-x-y", please check out #51418

@trop

trop Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "41-x-y", please check out #51419

@trop trop Bot added in-flight/42-x-y in-flight/41-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/41-x-y PR was merged to the "41-x-y" branch. and removed target/42-x-y PR should also be added to the "42-x-y" branch. target/41-x-y PR should also be added to the "41-x-y" branch. in-flight/42-x-y in-flight/41-x-y labels Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged/41-x-y PR was merged to the "41-x-y" branch. merged/42-x-y PR was merged to the "42-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unchecked return value of UnhookWindowsHookEx in NativeWindowViews::SetForwardMouseMessages leads to duplicate mouse hooks

6 participants