Repository navigation
fix(weakevents): Don't keep the first subscriber of a weak event alive - #25591
agneszitte merged 2 commits into
Conversation
WeakEventCollection.Register passed its trim callback as a lambda that the compiler put on the same closure class as the RemoveRegistration local function, which holds the subscriber's delegate. Gen2GcCallback keeps that callback strongly referenced and re-registers itself on every gen 2 GC while the collection has a live registration. So the subscriber that registered on an empty collection stayed in memory as long as the event source lived and anything else was subscribed, unless its registration was disposed. With Brush.RegisterInvalidateRender, a long-lived theme brush kept the first TextBlock that used it alive, along with its whole page. Short weak references to such an object are cleared even though the callback's finalizer keeps it in memory, which hid the leak from tests. The trim callback is now a static lambda that gets the collection from the target Gen2GcCallback passes back, so it captures nothing. Fixes unoplatform#25590
A collection registered a trim callback each time it went from empty to one registration. A callback only stops when it runs and finds the collection empty, so when the collection was emptied and subscribed to again before the next gen 2 GC, the earlier callback kept running next to the new one, and more piled up with each such cycle. The collection now tracks under its lock whether its callback is still running, and only registers a new one once the previous one stopped.
There was a problem hiding this comment.
🟢 Approval recommended
The focused changes preserve lifecycle synchronization and include targeted regression coverage, with no unresolved findings.
No open issues 🎉
What changed in this PR
Fixes #25590 by preventing weak-event trim callbacks from retaining the first subscriber.
Changes:
- Uses a non-capturing static trim callback.
- Tracks callback registration to prevent duplicate active callbacks.
- Adds regression tests for subscriber collection and callback lifecycle.
| File | Description |
|---|---|
| src/Uno.WinRT/UI/Core/WeakEventHelper.cs | Removes subscriber capture and prevents duplicate trim callbacks. |
| src/Uno.UI.RuntimeTests/Windows/UI/Given_WeakEventHelper.cs | Tests collection with simulated and default providers, plus callback restart behavior. |
Review effort: Balanced
…e (port) Port of the first commit of unoplatform#25591 (2f9213f), for unoplatform#25590. WeakEventCollection.Register passed its trim callback as a lambda that the compiler put on the closure of the RemoveRegistration local function, which holds the subscriber's delegate. Gen2GcCallback keeps that callback alive and re-registers itself on every gen 2 GC while the collection has a live registration, so the first subscriber of a weak event stayed in memory as long as the source lived and anything else was subscribed. In DropDoc's WebAssembly app, theme brushes kept the first TextBlocks that used them alive with their pages (108 MB). The trim callback is now a static lambda that captures nothing. - Tests: Given_WeakEventHelper.When_First_Subscriber_Not_Referenced_Then_Collected and its variant with the real Gen2GcCallback.
Port of the second commit of unoplatform#25591 (ab2bf54). A collection registered another trim callback each time it went from empty to one registration, and the earlier ones kept running while it had registrations. It now only registers a new one once the previous one stopped.
|
Could this be backported to |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
The build 240474 found UI Test snapshots differences: Details
|
|
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. |
…bscriber-6.8 fix(weakevents): Don't keep the first subscriber of a weak event alive (backport #25591)
GitHub Issue: closes #25590
PR Type:
🐞 Bugfix
What changed? 🚀
WeakEventCollection.Registerpassed its trim callback as_ => Trim(). The compiler put that lambda on the same closure class as theRemoveRegistrationlocal function, which holds the subscriber's own delegate (actualTarget).Gen2GcCallbackkeeps its callback strongly referenced and re-registers itself on every gen 2 GC while the collection has a live registration. So the subscriber that registered on an empty collection stayed in memory for as long as the event source lived and anything else was subscribed to it, unless its registration was disposed.With
Brush.RegisterInvalidateRender, a long-lived theme brush kept the firstTextBlockthat used it alive, along with its whole page. In a WebAssembly app, five such callbacks held 108 MB of a 282 MB managed heap: three pages the user had already left (see the issue for the heap dump chain).Short weak references to an object held this way are cleared, even though the callback's finalizer keeps resurrecting it, so leak tests built on
new WeakReference(obj)don't see it. The new tests use a resurrection-tracking reference where they go through the realGen2GcCallback.Gen2GcCallbackpasses back, so it captures nothing.perfcommit.Tests
New tests in
Given_WeakEventHelper:When_First_Subscriber_Not_Referenced_Then_Collected: uses the existingTestPlatformProvider, which keeps the trim callback strongly referenced asGen2GcCallbackdoes. A first subscriber that owns its undisposed registration, asBrush.SetupBrushChangedcallers do, is collected while a second one stays subscribed...._With_Default_Trim_Provider: the same scenario with the realGen2GcCallback, checked through aWeakReferencewithtrackResurrection: true.When_Registered_Again_Before_Trim_Then_Single_Trim_CallbackandWhen_Registered_After_Trim_Stopped_Then_Trim_Callback_Registered: check the single-callback behaviour, using a provider that simulatesGen2GcCallback.Results on Skia desktop:
Expected:<1>. Actual:<2>.Given_WeakEventHelperpass.Related classes, run with three attempts:
Given_SolidColorBrush_UITest,Given_GradientBrush,Given_BindingMemoryLeak,Given_FrameworkElement_And_Leak,Given_ProgressRing,Given_HighContrastAdjustment,Given_RichTextBlock.When_Foreground_Changed_DynamicallyandGiven_ElementTheme.Given_ProgressRing.When_Stretch_Fill(Expected:<100,50>. Actual:<100,50.4>), which fails the same way on master on this machine because of display scaling.The other
RegisterTrimCallbackusers were checked as well.DefaultArrayPoolandLinearArrayPoolalready pass non-capturing lambdas.LRUCachecaptures only the cache that owns the target, so it outlives the cache by one gen 2 GC at most.PR Checklist ✅
Screenshots Compare Test Runresults.