fix(CmdPal): prevent memory leak when activating extension via global hotkey#47413
Draft
Copilot wants to merge 3 commits into
Draft
fix(CmdPal): prevent memory leak when activating extension via global hotkey#47413Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
…tkey Fixes a race condition where _initializeItemsTask subscribes to COM events on IListItem/IPage objects after SafeCleanup has already run, leaving those subscriptions permanently alive (held by the cross-process COM event registration in the extension process). Changes: - ExtensionObjectViewModel: add IsCleanedUp volatile property, set before UnsafeCleanup() so background threads see it immediately - CommandItemViewModel.InitializeProperties: guard against IsCleanedUp at entry and after subscribing (double-check to close the race window) - PageViewModel.InitializeProperties: same guard pattern for page PropChanged - ListViewModel.UnsafeCleanup: unsubscribe _lastSelectedItem.PropertyChanged to sever the delegate chain that keeps ListViewModel alive - ShellViewModel.LoadPageViewModelAsync: call SafeCleanup() (not just Dispose) when navigation is cancelled; use CancellationToken.None for the UI-thread task so the cleanup lambda always runs even if the token is already cancelled Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/0bf9ec35-b19d-4ea4-971d-803a1d4f305e Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/0bf9ec35-b19d-4ea4-971d-803a1d4f305e Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix memory leak when activating extension via global hotkey
fix(CmdPal): prevent memory leak when activating extension via global hotkey
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of the Pull Request
Each global-hotkey activation of a CmdPal extension creates a new
ListViewModeland starts a background_initializeItemsTaskto fully initialize item properties. A race between that task andUnsafeCleanup()left orphaned cross-process COMPropChangedevent registrations — the extension process held the delegate alive, keepingCommandItemViewModelobjects (and eventually the entireListViewModel) in memory indefinitely.PR Checklist
Detailed Description of the Pull Request / Additional comments
Race condition (primary bug)
_initializeItemsTaskchecksct.IsCancellationRequestedbefore callingSafeInitializeProperties().UnsafeCleanup()(UI thread) cancels the token and iteratesItemsto callitem.SafeCleanup()→model.PropChanged -= handler. If the background task passes the cancellation check just before the token is marked, it then subscribesmodel.PropChanged += handlerwith no cleanup path. The extension process holds that delegate via COM, preventing GC.Fixes
ExtensionObjectViewModel— Addedvolatile bool _isCleanedUp/IsCleanedUpproperty.SafeCleanup()sets it before callingUnsafeCleanup()so background threads observe it immediately.CommandItemViewModel.InitializeProperties()— Double-check guard: bail at entry ifIsCleanedUp; and again after themodel.PropChanged +=/Command.PropertyChanged +=subscriptions to handle the narrow window between the two:PageViewModel.InitializeProperties()— Same double-check pattern for the page-levelPropChangedsubscription.ListViewModel.UnsafeCleanup()— Explicitly unsubscribes_lastSelectedItem.PropertyChangedto break theListItemViewModel → SelectedItemPropertyChanged → ListViewModeldelegate chain.ShellViewModel.LoadPageViewModelAsync()— When navigation is cancelled (rapid back-to-back hotkey presses can cancel an in-flight navigation before it setsCurrentPage), callviewModel.SafeCleanup()instead of onlyDispose(). Also changed the UI-thread cleanup task fromcancellationToken→CancellationToken.Noneso the lambda always executes — if an already-cancelled token is passed toTask.Factory.StartNew, the task is placed in Cancelled state immediately and the action never runs.Validation Steps Performed
_initializeItemsTaskvsUnsafeCleanupacross all affected paths.IsCleanedUpis set atomically (volatile write) before any subscription-removal inUnsafeCleanup, ensuring background threads always see the flag before they can subscribe.CancelAndDisposeTokenSourceusesInterlocked.Exchangeand guards on null; iterating an emptyItemsis a no-op.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
o3svsblobprodcus318.vsblob.vsassets.io/usr/bin/dotnet dotnet build -p:Platform=x64 --no-restore --uid-owner 0 -j ACCEPT(dns block)/usr/bin/dotnet dotnet build src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Microsoft.CmdPal.UI.ViewModels.csproj -p:Platform=x64 --no-restore -p:UseSharedCompilation=false(dns block)If you need me to access, download, or install something from one of these locations, you can either: