Skip to content

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

Merged
agneszitte merged 5 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-25965
Nov 4, 2026
Merged

agneszitte merged 5 commits into
servicing/6.8from
mergify/bp/servicing/6.8/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

Note

Backport notes for servicing/6.8

  • The Mergify conflict was only where the test file goes. 6.x keeps runtime tests under Tests/Windows_UI_Xaml_Controls/, and the cherry-pick already put the file there. The fix files are identical to fix: Subscribe to Command.CanExecuteChanged weakly #25965.
  • One more strong subscription is made weak on this branch. BindableButton.Android.cs is native-Android code that no longer exists on master. Its ClickCommand.CanExecuteChanged subscription was strong and was only removed on detach, so a button that was never attached stayed alive. It now uses the same WeakCanExecuteChangedSubscription. Two NativeAndroid tests cover it: a never-attached button is collected, and a live button keeps following CanExecute through a GC.
  • Compile: Uno.UI for net10.0-android builds, and the runtime tests build for Skia. The native-Android runtime tests rely on CI.

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)

# Conflicts:
#	src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_CommandCanExecuteChanged.cs
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)
@mergify mergify Bot added the conflicts Automated merge hit conflicts label Nov 3, 2026
@mergify

This comment was marked as resolved.

Xiaoy312 and others added 2 commits November 3, 2026 17:46
BindableButton held its ClickCommand.CanExecuteChanged subscription
strongly and only removed it when detached from the window, so a
long-lived command kept alive a button that was never attached. It
now uses the same weak subscription as ButtonBase and MenuFlyoutItem.

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:

@github-actions github-actions Bot added the platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform label Nov 3, 2026
@Xiaoy312 Xiaoy312 removed the conflicts Automated merge hit conflicts label Nov 3, 2026
@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 243457 did not find any UI Test snapshots differences.

@mergify

mergify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@agneszitte
agneszitte merged commit 0ce37ec into servicing/6.8 Nov 4, 2026
42 checks passed
@agneszitte
agneszitte deleted the mergify/bp/servicing/6.8/pr-25965 branch November 4, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants