Skip to content

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

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

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

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 39-x-y backport This is a backport PR semver/patch backwards-compatible bug fixes labels Mar 5, 2026
@jkleinsc
jkleinsc merged commit 3679fd5 into 39-x-y Mar 5, 2026
139 of 150 checks passed
@jkleinsc
jkleinsc deleted the trop/39-x-y-bp-fix-prevent-use-after-free-in-powermonitor-via-dangling-os-callbacks-1772722850816 branch March 5, 2026 22:20
@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

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