Repository navigation
fix(binding): Remove stale callbacks and follow indexed collections (backport #26219) - #26256
Merged
agneszitte merged 11 commits intoNov 10, 2026
Merged
Conversation
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)
Contributor
Author
|
Cherry-pick of 1f48e64 has failed: 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 |
2 of 5 tasks
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]>
Contributor
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
ajpinedam
approved these changes
Nov 9, 2026
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. |
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.
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
DependencyObjectsource were never removedA property-changed callback that another object registers on a
DependencyObjectis held weakly. A{Binding}whose source is aDependencyObjectis one example. AConditionalDisposabletied 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
WeakReferencethat nothing else referenced. ThatWeakReferencebecame unreachable in the same collection as theConditionalDisposable, and finalizers run in any order. When theWeakReferencewas 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.ConditionalDisposablenow holds the source through a weakGCHandle, which has no finalizer and is freed by theConditionalDisposableitself.Now that these removals actually happen, each one queued its own dispatcher item from the finalizer.
DispatcherConditionalDisposablenow queues them throughSubscriptionCleanup, 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}orx:Bindwith a path such asItems[0]subscribes to the collection'sCollectionChanged. The item before the indexer made that subscription, on the collection it held at that moment. AfterItems = 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 itsDataContextchanges. The handler still holds the binding weakly throughWeakSourceSubscription(#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. ATextBlockbound toItems[0]then showed the collection's type name. The collection'sPropertyChangedraises"Item[]", which doesn't match the"Item[0]"the indexer listens to, so it doesn't help. The indexer now re-reads its value onCollectionChanged, which also updates a longer path such asItems[0].Length.Tests
Given_BindingSourceStaleHandlers(14 tests) covers:{Binding}andx:BindOneWay handlers left on a view model and on an indexed collection.DependencyObjectsource.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, soUno.UI.RuntimeTestsdoesn't build from886be6812dtob66dbfbe20.402ac0e9d7fixes it. Those commits are left as written.Validation
DependencyObjectsource, the collection's type name, and the replaced collection.Given_BindingSourceStaleHandlersandGiven_BindingStaleHandlerspass. The binding suites (Given_Binding*,Given_xBind,Given_XBind*,Windows_UI_Xaml_Data,BindingTests) pass 97 of 97.Windows_UI_Xaml.*andUno_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.Given_BindingStaleHandlers, and so does the one that reads Uno's internal callbacks. Each exclusion states its reason.Uno.UI.UnitTestshas no new failures. The 13Given_Calendarfailures are the existing time-zone baseline.dotnet format whitespaceare clean.Given_BindingStaleHandlers.Backports to 6.7 and 6.8 will follow once #26192 and #26193 merge, since this builds on #26115.
PR Checklist ✅
Screenshots Compare Test Runresults.🤖 Generated with Claude Code
This is an automatic backport of pull request #26219 done by [Mergify](https://mergify.com).