Skip to content

fix(binding): Remove stale callbacks and follow indexed collections (backport #26219) - #26257

Merged
agneszitte merged 11 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-26219
Nov 9, 2026
Merged

agneszitte merged 11 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-26219

Conversation

@mergify

@mergify mergify Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes unoplatform/kahua-private#568

PR Type:

🐞 Bugfix

What changed? 🚀

This follows #26115 (kahua-private#566) and fixes three more problems with bindings to a long-lived source. The first four commits are the reporter's patches, ported to master. They are kept as written, and every change on top of them is a separate commit.

1. Callbacks on a DependencyObject source were never removed

A property-changed callback that another object registers on a DependencyObject is held weakly. A {Binding} whose source is a DependencyObject is one example. A ConditionalDisposable tied to the subscriber removes the callback from its finalizer, but only if the source is still alive.

It read the source's liveness from a WeakReference that nothing else referenced. That WeakReference became unreachable in the same collection as the ConditionalDisposable, and finalizers run in any order. When the WeakReference was finalized first, it reported its target as gone, so the callback was never removed. A long-lived source kept one dead callback for every subscriber it ever had.

ConditionalDisposable now holds the source through a weak GCHandle, which has no finalizer and is freed by the ConditionalDisposable itself.

Now that these removals actually happen, each one queued its own dispatcher item from the finalizer. DispatcherConditionalDisposable now queues them through SubscriptionCleanup, like the other removals added in #26115: one dispatcher item at a time, run at Low priority in batches.

2. An indexer path didn't follow a replaced collection

A {Binding} or x:Bind with a path such as Items[0] subscribes to the collection's CollectionChanged. The item before the indexer made that subscription, on the collection it held at that moment. After Items = new ..., the binding kept listening to the old collection and never subscribed to the new one.

The indexer item now makes the subscription, on the collection it indexes into (its DataContext). It resubscribes whenever its DataContext changes. The handler still holds the binding weakly through WeakSourceSubscription (#26115).

The reporter's 6.8 patch also had its own subscription class and SubscriptionCleanup. Those duplicate what #26115 already put on master, and the patch's class held the collection strongly. So only the move to the indexer and the tests are ported, as the commit message says.

3. An indexer showed the collection instead of the item

When the indexed collection raised CollectionChanged, the binding pushed the collection itself to its target. A TextBlock bound to Items[0] then showed the collection's type name. The collection's PropertyChanged raises "Item[]", which doesn't match the "Item[0]" the indexer listens to, so it doesn't help. The indexer now re-reads its value on CollectionChanged, which also updates a longer path such as Items[0].Length.

Tests

Given_BindingSourceStaleHandlers (14 tests) covers:

  • {Binding} and x:Bind OneWay handlers left on a view model and on an indexed collection.
  • Callbacks left on a DependencyObject source.
  • A replaced, emptied and refilled collection.
  • A cleared binding.
  • A raise after the bindings are collected.

It replaces the indexer test in Given_BindingStaleHandlers.

Note for bisecting: the reporter's master port of the tests (b66dbfbe20) was never compiled on master, so Uno.UI.RuntimeTests doesn't build from 886be6812d to b66dbfbe20. 402ac0e9d7 fixes it. Those commits are left as written.

Validation

  • Runtime, Skia desktop (Win32):
    • Before the fix (master's product code with the new tests), 7 of the 14 tests fail: the callbacks left on the DependencyObject source, the collection's type name, and the replaced collection.
    • After it, all 22 tests in Given_BindingSourceStaleHandlers and Given_BindingStaleHandlers pass. The binding suites (Given_Binding*, Given_xBind, Given_XBind*, Windows_UI_Xaml_Data, BindingTests) pass 97 of 97.
    • Wider run over Windows_UI_Xaml.* and Uno_UI_Extensions: 1220 passed, 3 failed. The 3 are theme tests (When_RequestedTheme_Set_Explicitly, When_Secondary_Window_No_Background_*) that expect Light while the machine's OS is in dark mode. The reporter saw the same 3 fail without these changes.
  • Runtime, native WinUI (WinAppSDK): the 6 tests on live bindings (the indexer refresh, the replaced collection, the cleared binding) pass 10 iterations with a single attempt each, so they now run there in CI. That confirms the fixed behavior matches WinUI. The tests that wait for collected bindings stay excluded, as in Given_BindingStaleHandlers, and so does the one that reads Uno's internal callbacks. Each exclusion states its reason.
  • Unit tests: Uno.UI.UnitTests has no new failures. The 13 Given_Calendar failures are the existing time-zone baseline.
  • Compile: the strict desktop build (analyzers on), the WinAppSDK head, and dotnet format whitespace are clean.
  • Not run locally: WASM, Android and iOS. The raise-after-collection tests are skipped on WASM, for the same conservative-stack-scan reason as in Given_BindingStaleHandlers.

Backports to 6.7 and 6.8 will follow once #26192 and #26193 merge, since this builds on #26115.

PR Checklist ✅

🤖 Generated with Claude Code


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

jbourque-kahua and others added 10 commits November 9, 2026 17:09
A property-changed callback registered on a DependencyObject by another
object, like a {Binding} whose source is a DependencyObject, is held
weakly and removed once that object is collected: a ConditionalDisposable
tied to the subscriber removes it from its finalizer, if the source is
still alive.

The source's liveness was read from a WeakReference that only the
ConditionalDisposable references. Both become unreachable in the same
collection and their finalizers run in any order; a WeakReference
finalized first reports its target as collected, so the callback was
never removed. A long-lived source accumulated one dead callback per
subscriber ever registered, for good.

ConditionalDisposable now takes the condition source itself and holds it
through a weak GCHandle, which has no finalizer and is freed by the
ConditionalDisposable. Callers no longer allocate a WeakReference to
pass it in.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 4c71199)
A {Binding} or x:Bind whose path indexes into an INotifyCollectionChanged
value (Items[0]) subscribes to the collection's CollectionChanged. The
subscription was made by the item before the indexer, on the value it
held when it subscribed, and nothing moved it: after Items = new ...,
the binding kept listening to the old collection, and never to the new
one.

The subscription is now made by the indexer item, on the collection it
indexes into (its DataContext). The indexer resubscribes whenever its
DataContext changes, so a replaced collection is followed.

The tests also cover the CollectionChanged handlers of collected
bindings: they are removed, and a raise after the bindings are collected
is safe.

Ported from the servicing/6.8 patch "fix(binding): Don't let collections
pin bindings". Its weakly held handler and finalizer cleanup are already
on master (WeakSourceSubscription), so only the move to the indexer and
the tests are ported.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 886be68)
When the collection a {Binding} path indexes into (Items[0]) raised
CollectionChanged, the binding pushed the collection itself to its
target instead of the indexed value: a TextBlock bound to Items[0]
showed the collection's type name after Items[0] = "x" or an insert.
The collection's PropertyChanged doesn't help, since it raises "Item[]"
and the indexer listens to "Item[0]".

The indexer now re-reads its value on CollectionChanged and propagates
it, as it does on PropertyChanged, which also updates a longer path such
as Items[0].Length. x:Bind, which re-evaluates its whole path, still
follows the collection, through the same propagation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit ed64eef)
{x:Bind Mode=OneWay} goes through the same BindingPath as {Binding}, so
it leaves the same PropertyChanged handlers on a long-lived source when
its element is collected. An indexer path (Items[0]) leaves them both on
the view model and on the collection.

These tests pass only once PropertyChanged handlers of collected
bindings are removed from their source (BindingPath.
SubscribeToNotifyPropertyChanged), which is a separate fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit b66dbfb)
The ported test read a source's property-changed callbacks through
IDependencyObjectStoreProvider, which master no longer has: the store is
merged into DependencyObject. Also keep the Uno-internal read out of the
WinAppSDK build, where the test is excluded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 402ac0e)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 1f48e64)

# Conflicts:
#	src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_BindingSourceStaleHandlers.cs
Also skip the CollectionChanged raise test on WASM, like the
PropertyChanged one: the interpreter's conservative stack scan can keep
the elements alive from stale slots, depending on frame layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 1bf2976)
Given_BindingSourceStaleHandlers checks the CollectionChanged handlers of
collected indexer bindings, loaded and not, and their PropertyChanged
handlers on the view model.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit e85e744)
Now that a ConditionalDisposable reliably removes the callback of a
collected subscriber from a live source, each removal queued its own
Idle dispatcher item from the finalizer. Queue them with the other
removals of collected subscribers instead (SubscriptionCleanup), drained
at Low priority in bounded batches by a single dispatcher item.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit c2a7c08)
Measured on native WinUI with 10 iterations, one attempt each: the six
tests that only follow or count the subscriptions of live bindings (the
indexer refresh, the replaced collection, the cleared binding) pass every
time, so they now run there too, which also confirms the fixed behavior
matches WinUI's.

The tests that wait for collected bindings to be cleaned up stay
excluded, as in Given_BindingStaleHandlers, and so does the one that
reads Uno's property-changed callbacks. Each exclusion now says why.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 0a376a4)
@mergify mergify Bot added the conflicts Automated merge hit conflicts label Nov 9, 2026
@mergify

mergify Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Cherry-pick of 1f48e64 has failed:

On branch mergify/bp/servicing/6.8/pr-26219
Your branch is ahead of 'origin/servicing/6.8' by 5 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 1f48e64.
  (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)

Changes to be committed:
	renamed:    src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindIndexerControl.xaml -> src/Uno.UI.RuntimeTests/BindingTests/XBindIndexerControl.xaml
	renamed:    src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindIndexerControl.xaml.cs -> src/Uno.UI.RuntimeTests/BindingTests/XBindIndexerControl.xaml.cs
	renamed:    src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindOneWayLeakControl.xaml -> src/Uno.UI.RuntimeTests/BindingTests/XBindOneWayLeakControl.xaml
	renamed:    src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindOneWayLeakControl.xaml.cs -> src/Uno.UI.RuntimeTests/BindingTests/XBindOneWayLeakControl.xaml.cs
	deleted:    src/Uno.UI.RuntimeTests/Tests/BindingTests/Given_BindingSourceStaleHandlers.cs

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_BindingSourceStaleHandlers.cs

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

master reads the property details from DependencyObject itself, which
on 6.x is an interface without them: go through
IDependencyObjectStoreProvider, as the original test did.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Xiaoy312 Xiaoy312 removed the conflicts Automated merge hit conflicts label Nov 9, 2026
@unodevops

Copy link
Copy Markdown
Contributor

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

@agneszitte
agneszitte merged commit 8a3c35e into servicing/6.8 Nov 9, 2026
34 of 40 checks passed
@agneszitte
agneszitte deleted the mergify/bp/servicing/6.8/pr-26219 branch November 9, 2026 18:53
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.

5 participants