Skip to content

fix: Subscribe to Command.CanExecuteChanged weakly - #25965

Merged
Xiaoy312 merged 3 commits into
masterfrom
dev/xygu/20261102/command-canexecute-leak
Nov 3, 2026
Merged

Xiaoy312 merged 3 commits into
masterfrom
dev/xygu/20261102/command-canexecute-leak

Conversation

@Xiaoy312

@Xiaoy312 Xiaoy312 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25969, closes unoplatform/kahua-private#562

PR Type:

🐞 Bugfix

What changed? 🚀

ButtonBase and MenuFlyoutItem subscribed to Command.CanExecuteChanged with a strong handler. They only removed it when the command changed or in LeaveImpl. So a control that never entered the live tree stayed alive for as long as its command, for example:

  • the items of a MenuFlyout that is never opened, which in turn keep the whole screen alive through DataContext and their parents;
  • a button that is created but never shown.

WinUI holds this subscription weakly (ctl::WeakEventPtr).

This PR does the same with a new internal helper, WeakCanExecuteChangedSubscription:

  • The command no longer holds the control. It only references a small subscription object, which holds the control through a weak reference.
  • The subscription is removed once the control is collected. The control keeps a handle; when the control is collected, the handle's finalizer removes the subscription from the command. The removal runs on the UI thread, since an ICommand isn't required to be thread-safe.
  • Explicit removal is unchanged. Changing the command, or LeaveImpl, removes the subscription right away and skips the finalizer.
  • A raise on behalf of a collected control does nothing. The subscription doesn't unsubscribe itself mid-raise, so a command that raises over its live handler list keeps working.

AppBarButton, ToggleMenuFlyoutItem, RadioMenuFlyoutItem and the other subclasses get the fix through their base class. No other CanExecuteChanged subscription remains in Uno.UI.

Behaviour change: if a command's remove accessor throws, the exception is now logged at Error level. Previously it propagated from the Command setter or from LeaveImpl.

Not changed: subscribing only while the control is in the live tree would avoid the finalizer. However, WinUI keeps a never-loaded control's IsEnabled in sync with CanExecute; the new tests confirm this on native WinUI. Doing so would therefore break WinUI parity.

Validation

  • Runtime, Skia desktop (Win32):
    • Given_CommandCanExecuteChanged: 18/18 pass. Before the fix, the leak tests fail.
    • Temporarily breaking the handle's ownership makes exactly the "live control survives a GC" tests fail.
    • Regression run over the Button, AppBarButton, MenuFlyout, SplitMenuFlyoutItem, CommandBar, RadioButton, HyperlinkButton, XamlUICommand, StandardUICommand, FrameworkElement leak, SplitButton, CommandBarFlyout and the related integration suites: 291 passed, 0 failed.
  • Runtime, native WinUI (WinAppSDK): the 8 functional tests pass. They cover IsEnabled tracking for loaded, never-loaded and still-alive-after-a-GC controls, unsubscribing on leave, and command replacement. The GC-based leak tests are Uno-only.
  • Reporter's repro, against this build: controls and screens still alive drop from 100/100 screens and 300/300 items to 0, and subscribers drop to 0, in all four scenarios.
  • Compile: strict Uno.UI build, the WinAppSDK head, and dotnet format whitespace are clean.
  • Not run locally: WASM and mobile. The GC tests follow the existing leak-test pattern for those hosts (the Task.Yield collection loop, ignored on NativeAOT); CI will confirm.

PR Checklist ✅

🤖 Generated with Claude Code

Xiaoy312 and others added 2 commits November 3, 2026 02:17
A control that never enters the live tree is kept alive by a long-lived
command through its CanExecuteChanged subscription.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ButtonBase and MenuFlyoutItem now hold their CanExecuteChanged
subscription weakly, as WinUI does (WeakEventPtr), so a long-lived
command no longer keeps alive a control that never entered the live
tree, e.g. an item of a MenuFlyout that is never opened.

The command only references a small subscription that holds the
control through a weak reference. The control keeps a handle whose
finalizer removes that subscription from the command, on the UI
thread.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@Xiaoy312
Xiaoy312 enabled auto-merge November 3, 2026 19:20
@nventive-devops

Copy link
Copy Markdown
Contributor

The build 243119 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png
    • AppBarButtonWithIconTest.png-dark
    • AppBarToggleButtonTest.png
    • Application_DispatcherShutdownMode.png
    • AppXamlDefinedResources.png-dark
    • Attributed_text_Simple.png
    • AutoBorderStretchwithbottommargin.png-dark
    • AutoBorderStretchwithtopmargin.png
    • AutomationProperties_Relations.png-dark
    • AutomationProperties_Relations.png
    • AutoSuggestBox_Icons.png-dark
    • AutoSuggestBox_Icons.png
    • BasicEnability.png-dark
    • BasicEnability.png
    • BenchmarkDotNet.png-dark
    • BenchmarkDotNet.png
    • BitmapIcon_Monochromatic.png-dark
    • BitmapIcon_Monochromatic.png
    • BorderCenteredwithmargins.png-dark
    • BorderCenteredwithmargins.png
  • skia-windows-screenshots: 2458 changed over 2492

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AppBarButtonTest.png
    • ApplicationViewSizing.png-dark
    • AppWindowClosing.png-dark
    • AppWindowPositionAndSize.png-dark
    • Arrange_Performance01.png-dark
    • Attributed_text_Simple.png
    • AutoBorderStretchwithbottommargin.png-dark
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • AutoSizedPathCentered.png-dark
    • AutoSizedPathCentered.png
    • AutoSuggestBox_Reason.png-dark
    • AutoSuggestBox_Reason.png
    • BasicAutoSuggestBox.png-dark
    • BasicAutoSuggestBox.png
    • Basics_Automated.png-dark
    • Basics_Automated.png
    • BezierSegment.png-dark
    • BezierSegment.png

@Xiaoy312
Xiaoy312 merged commit 09b5f7a into master Nov 3, 2026
51 of 52 checks passed
@Xiaoy312
Xiaoy312 deleted the dev/xygu/20261102/command-canexecute-leak branch November 3, 2026 21:21
@Xiaoy312

Xiaoy312 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@Mergifyio backport release/stable/6.7 servicing/6.8 release/stable/7.0

@mergify

mergify Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.7 servicing/6.8 release/stable/7.0

✅ Backports have been created

Details

Cherry-pick of fc1ef84 has failed:

On branch mergify/bp/release/stable/6.7/pr-25965
Your branch is up to date with 'origin/release/stable/6.7'.

You are currently cherry-picking commit fc1ef84.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	added by them:   src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_CommandCanExecuteChanged.cs

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Cherry-pick of fc1ef84 has failed:

On branch mergify/bp/servicing/6.8/pr-25965
Your branch is up to date with 'origin/servicing/6.8'.

You are currently cherry-picking commit fc1ef84.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	added by them:   src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_CommandCanExecuteChanged.cs


It took 2.03 seconds to enumerate untracked files.
See 'git help status' for information on how to improve this.

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

agneszitte added a commit that referenced this pull request Nov 4, 2026
…r-25965

fix: Subscribe to Command.CanExecuteChanged weakly (backport #25965)
agneszitte added a commit that referenced this pull request Nov 4, 2026
…6.7/pr-25965

fix: Subscribe to Command.CanExecuteChanged weakly (backport #25965)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Skia] ButtonBase and MenuFlyoutItem: a long-lived Command keeps never-loaded controls alive through CanExecuteChanged

4 participants