Skip to content

fix: Subscribe to Command.CanExecuteChanged weakly (backport #25965) - #26004

Closed
mergify[bot] wants to merge 3 commits into
release/stable/7.0from
mergify/bp/release/stable/7.0/pr-25965
Closed

mergify[bot] wants to merge 3 commits into
release/stable/7.0from
mergify/bp/release/stable/7.0/pr-25965

Conversation

@mergify

@mergify mergify Bot 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


This is an automatic backport of pull request #25965 done by [Mergify](https://mergify.com).

Xiaoy312 and others added 3 commits November 3, 2026 22:24
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]>
(cherry picked from commit fc1ef84)
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]>
(cherry picked from commit eff7885)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit e6e4535)
@unodevops

Copy link
Copy Markdown
Contributor

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

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 243446 did not find any UI Test snapshots differences.

@mergify

mergify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Queued — the merge queue status continues in this comment ↓.

@mergify

mergify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

🚫 Left the queue · pull request dequeued

Less than a minute in the queue, with no time running CI.

So far

  • ✅ Entered the queue Wed 4 Nov, 03:32 · rule default · triggered by @Xiaoy312 with the merge queue checkbox
  • ✅ Checks skipped · PR is already up-to-date
  • 🚫 Left the queue 03:33 · 600f6d7

Reason

Pull request #26004 has been dequeued

GitHub refused to merge the pull request. Repository rule violations found

Cannot update this protected ref. This is usually enforced by a branch protection or ruleset rule.

To get back in the queue

  • Push a change if one is needed: the pull request is queued again once its queue conditions match.
  • If no change is needed, tick the box below to queue it again as it is.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request
2 merge conditions met
  • github-review-approved · required by GitHub repository ruleset rule release/stable - PR requirements
  • any of · required by GitHub repository ruleset rule release/stable - PR requirements:
    • check-success = Uno.UI - CI
    • check-neutral = Uno.UI - CI
    • check-skipped = Uno.UI - CI

Times in UTC · dashboard · how the merge queue works

@MartinZikmund

MartinZikmund commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Covered in sync PR: #26026

@MartinZikmund
MartinZikmund deleted the mergify/bp/release/stable/7.0/pr-25965 branch November 12, 2026 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants