Skip to content

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

Merged
agneszitte merged 2 commits into
servicing/6.8from
dev/nr/weak-event-first-subscriber-6.8
Oct 28, 2026
Merged

agneszitte merged 2 commits into
servicing/6.8from
dev/nr/weak-event-first-subscriber-6.8

Conversation

@nickrandolph

@nickrandolph nickrandolph commented Oct 27, 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

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)
Copilot AI balanced review requested due to automatic review settings October 27, 2026 06:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@unodevops

Copy link
Copy Markdown
Contributor

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

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 241613 has failed on Uno.UI - CI.

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

agneszitte commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

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 perf, but it closes a related build-up in the same code. A collection registered a new trim callback each time it went from empty to one registration, and a callback only stops once it runs and finds the collection empty. So when a collection was emptied and subscribed to again before the next gen 2 GC, the old callback kept running next to the new one, and more piled up with each such cycle. The collection now tracks whether its callback is still running and only registers a new one once the previous one stopped.

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 Given_WeakEventHelper tests passing. Keeping 6.8-dev identical to master here also lets a later 6.7 backport copy 6.8 as is.

@agneszitte agneszitte changed the title fix(weakevents): Don't keep the first subscriber of a weak event alive (6.8) fix(weakevents): Don't keep the first subscriber of a weak event alive (backport #25591) Oct 28, 2026
@agneszitte
agneszitte merged commit 61446ea into servicing/6.8 Oct 28, 2026
11 of 20 checks passed
@agneszitte
agneszitte deleted the dev/nr/weak-event-first-subscriber-6.8 branch October 28, 2026 04:19
@agneszitte

Copy link
Copy Markdown
Member

@Mergifyio backport release/stable/6.7

@mergify

mergify Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.7

✅ Backports have been created

Details

agneszitte added a commit that referenced this pull request Nov 3, 2026
…6.7/pr-25730

fix(weakevents): Don't keep the first subscriber of a weak event alive (backport #25591) (backport #25730)
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.

6 participants