Repository navigation
fix(weakevents): Don't keep the first subscriber of a weak event alive (backport #25591) - #25730
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)
There was a problem hiding this comment.
🟢 Approval recommended
The narrowly scoped fix preserves the callback contract and includes regression coverage for both retention scenarios.
No open issues 🎉
What changed in this PR
Backports the weak-event subscriber leak fix for #25590 to servicing/6.8, without the separate trim-callback optimization.
Changes:
- Uses a static trim callback to prevent unintended retention of the first subscriber.
- Adds regression tests covering strongly retained callbacks and the default finalizer-based trim provider.
No critical, moderate, or minor issues identified.
| File | Description |
|---|---|
src/Uno.UWP/UI/Core/WeakEventHelper.cs |
Removes subscriber capture from the trim callback. |
src/Uno.UI.RuntimeTests/Tests/Windows_UI/Given_WeakEventHelper.cs |
Adds collection tests for both trim-provider scenarios. |
Review effort: Balanced
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
|
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)
|
Added #25591's second commit, ab2bf54 (cherry-picked here as 288b497), so this 6.8 backport now matches master, where #25591 has just been merged. The "Not included" note in the description no longer applies and I adjusted the pr description on this. Why: the commit is labelled It's small (10 product lines plus 66 test lines) and applies cleanly with only the 6.x path change. mikernet ran both commits on a 6.8 build, with all 12 |
|
@Mergifyio backport release/stable/6.7 |
✅ Backports have been createdDetails
|
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