Skip to content

Unify the helpers that clean up after a collected subscriber #26126

Open

Description

@Xiaoy312

What would you like to be added? 💡

One shared helper for "run this cleanup on the UI thread once this object is collected", used everywhere Uno does that today. There are currently several hand-rolled versions with different rules:

Helper Holds the source Runs at Used by
WeakSourceSubscription<TSource, TState> + SubscriptionCleanup (src/Uno.UI/Helpers/, from #26115) raw weak GCHandle Low, batched (256 items or 3 ms) {Binding} INPC / indexer subscriptions, ItemsSourceView, ItemsRepeater, TransitionManager, ButtonBase / MenuFlyoutItem CanExecuteChanged
DispatcherConditionalDisposable (src/Uno.UI/UI/Xaml/DispatcherConditionalDisposable.cs) ConditionalDisposable (WeakReference) inline on the UI thread, else Idle, one item each DependencyObject.Store property-changed callbacks, so also the DependencyObject-source branch of BindingItem
ImageSource.SubscriptionLease (src/Uno.UI/UI/Xaml/Media/ImageSource.crossruntime.cs) raw weak GCHandle Low, one item each image data subscriptions
BrowserHtmlElement.DomRemovalOnCollect (src/Uno.UI/NativeElementHosting/BrowserHtmlElement.cs) n/a (element id) Idle, one item each DOM element removal on WASM

The proposal: move the remaining three onto WeakSourceSubscription / SubscriptionCleanup (or a shared lower-level primitive under it), so they get the same guarantees:

  • the source is held weakly, so a source collected together with its subscriber is not kept alive by the queued cleanup;
  • removals are batched and time-bounded instead of one dispatcher item each;
  • failures are logged with the source type, and a Debug line reports drained/pending counts.

Why is this needed? 🤔

  • A single {Binding} currently takes two cleanup paths with different priorities, depending on whether its source is an INotifyPropertyChanged or a DependencyObject.
  • Each copy re-solves the same finalizer pitfalls (finalizer ordering of WeakReference vs GCHandle, never throwing from a finalizer, thread affinity of the event source), and fixes made in one don't reach the others.
  • A page with thousands of bindings dropped at once currently queues thousands of Idle items through DispatcherConditionalDisposable.

For which platforms 📱💻🖥️

All platforms 🌍

Anything else we need to know? 💬

Raised during the review of #26115, which introduced WeakSourceSubscription and kept the scope to the subscriptions involved in that fix. Things to check when migrating:

  • DispatcherConditionalDisposable runs the cleanup inline when already on the UI thread: decide whether batching may delay anything that relied on that.
  • Run the leak suites (Given_BindingMemoryLeak, Given_BindingStaleHandlers, Given_ItemsRepeater_Leak, Given_CommandCanExecuteChanged) and the image source tests on every target.

Activity

  1. added
    kind/enhancement
    triage/untriaged
    on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/enhancementtriage/untriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions