Skip to content

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

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

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

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 38-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes labels Mar 5, 2026
@jkleinsc
jkleinsc merged commit 18ee76c into 38-x-y Mar 5, 2026
128 of 152 checks passed
@jkleinsc
jkleinsc deleted the trop/38-x-y-bp-fix-prevent-use-after-free-in-powermonitor-via-dangling-os-callbacks-1772722851555 branch March 5, 2026 20:52
@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

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