Skip to content

fix(weakevents): Don't keep the first subscriber of a weak event alive (backport #25591) (backport #25730) - #25957

Merged
agneszitte merged 2 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-25730
Nov 3, 2026
Merged

agneszitte merged 2 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-25730

Conversation

@mergify

@mergify mergify Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25590 (for 6.8)

PR Type:

🐞 Bugfix

What changed? 🚀

Backport of #25591 (both commits: the fix 2f9213f42a and ab2bf543bc9) to servicing/6.8. Both cherry-picks apply cleanly.

The leak: WeakEventCollection.Register passed its trim callback as a lambda that shared a closure with the RemoveRegistration local function, which holds the subscriber's delegate. Gen2GcCallback keeps 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. With Brush.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 Gen2GcCallback passes 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 one Uno.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 their DataContext they 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 → a ProgressRing and a TextBlock from the unloaded app. With Uno.dll built from this commit, the same run unloaded all of them.

Tests

  • The cherry-picks bring When_First_Subscriber_Not_Referenced_Then_Collected and ..._With_Default_Trim_Provider (fix), and When_Registered_After_Trim_Stopped_Then_Trim_Callback_Registered and When_Registered_Again_Before_Trim_Then_Single_Trim_Callback (second commit) into Given_WeakEventHelper.
  • Uno.Skia.csproj builds. 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 ✅

🤖 Generated with Claude Code


This is an automatic backport of pull request #25730 done by [Mergify](https://mergify.com).

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)
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@agneszitte
agneszitte merged commit 7b3f7b2 into release/stable/6.7 Nov 3, 2026
40 of 46 checks passed
@agneszitte
agneszitte deleted the mergify/bp/release/stable/6.7/pr-25730 branch November 3, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants