Skip to content

fix: prevent use-after-free in PowerMonitor via dangling OS callbacks - #50087

Merged
jkleinsc merged 1 commit into
41-x-yfrom
trop/41-x-y-bp-fix-prevent-use-after-free-in-powermonitor-via-dangling-os-callbacks-1772722847071
Mar 5, 2026
Merged

jkleinsc merged 1 commit into
41-x-yfrom
trop/41-x-y-bp-fix-prevent-use-after-free-in-powermonitor-via-dangling-os-callbacks-1772722847071

Conversation

@trop

@trop trop Bot commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

Backport of #50045

See that PR for details.

Notes: none

PowerMonitor registered OS-level callbacks (HWND UserData and
WTS/suspend notifications on Windows, shutdown handler and lock-screen
observer on macOS) but never cleaned them up in its destructor. The JS
layer also only held the native object in a closure-local variable,
allowing GC to reclaim it while those registrations still referenced
freed memory.

Retain the native PowerMonitor at module level in power-monitor.ts so
it cannot be garbage-collected. Add DestroyPlatformSpecificMonitors()
to properly tear down OS registrations on destruction: on Windows,
unregister WTS and suspend notifications, clear GWLP_USERDATA, and
destroy the HWND; on macOS, remove the emitter from the global
MacLockMonitor and reset the Browser shutdown handler.

Co-authored-by: Shelley Vohr <[email protected]>
@trop
trop Bot requested review from a team and codebytere March 5, 2026 15:01
@trop trop Bot added 41-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes labels Mar 5, 2026
@jkleinsc
jkleinsc merged commit 94bc0ec into 41-x-y Mar 5, 2026
93 of 103 checks passed
@jkleinsc
jkleinsc deleted the trop/41-x-y-bp-fix-prevent-use-after-free-in-powermonitor-via-dangling-os-callbacks-1772722847071 branch March 5, 2026 20:24
@release-clerk

release-clerk Bot commented Mar 5, 2026

Copy link
Copy Markdown

No Release Notes

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.

2 participants