Repository navigation
fix(weakevents): Don't keep the first subscriber of a weak event alive (backport #25591) (backport #25730) - #25957
Merged
agneszitte merged 2 commits intoNov 3, 2026
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 #25590 (cherry picked from commit 2f9213f) (cherry picked from commit 2132ab4)
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. (cherry picked from commit ab2bf54) (cherry picked from commit 288b497)
Merged
2 of 5 tasks
Contributor
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
agneszitte
approved these changes
Nov 3, 2026
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 #25590 (for 6.8)
PR Type:
🐞 Bugfix
What changed? 🚀
Backport of #25591 (both commits: the fix
2f9213f42aandab2bf543bc9) toservicing/6.8. Both cherry-picks apply cleanly.The leak:
WeakEventCollection.Registerpassed its trim callback as a lambda that shared a closure with theRemoveRegistrationlocal function, which holds the subscriber's delegate.Gen2GcCallbackkeeps that callback alive and resurrects itself on every gen 2 GC, so the first subscriber of a collection stayed in memory for as long as anything else was subscribed. WithBrush.RegisterInvalidateRender, a long-lived theme brush keeps the first element that used it alive, along with whatever that element references.The fix: the trim callback is a static lambda that gets the collection from the target
Gen2GcCallbackpasses back, so it captures nothing.Also included: #25591's second commit,
perf(weakevents): Run one trim callback per weak event collection(cherry-picked as 288b497), so 6.8 matches master. Without it, trim callbacks pile up when a collection is emptied and subscribed to again before the next gen 2 GC.Where 6.8 hits it
A desktop host that loads apps into collectible
AssemblyLoadContexts and unloads them shares oneUno.dll, and its theme brushes, with every app. Elements from an unloaded app that were first to subscribe to a shared brush stay pinned, and through theirDataContextthey keep the whole context from unloading.In such a host, loading and unloading four apps left 2 contexts that never unloaded. A heap dump traced both to
Gen2GcCallback→WeakEventCollection+<>c__DisplayClass10_0.actualTarget→ aProgressRingand aTextBlockfrom the unloaded app. WithUno.dllbuilt from this commit, the same run unloaded all of them.Tests
When_First_Subscriber_Not_Referenced_Then_Collectedand..._With_Default_Trim_Provider(fix), andWhen_Registered_After_Trim_Stopped_Then_Trim_Callback_RegisteredandWhen_Registered_Again_Before_Trim_Then_Single_Trim_Callback(second commit) intoGiven_WeakEventHelper.Uno.Skia.csprojbuilds. I did not run the runtime tests locally; they ran on master in fix(weakevents): Don't keep the first subscriber of a weak event alive #25591.PR Checklist ✅
Screenshots Compare Test Runresults.🤖 Generated with Claude Code
This is an automatic backport of pull request #25730 done by [Mergify](https://mergify.com).