Skip to content

fix(composition): Prune dead context entries on add - #26124

Merged
Xiaoy312 merged 6 commits into
masterfrom
dev/xygu/20261105/composition-context-prune
Nov 6, 2026
Merged

Xiaoy312 merged 6 commits into
masterfrom
dev/xygu/20261105/composition-context-prune

Conversation

@Xiaoy312

@Xiaoy312 Xiaoy312 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes unoplatform/kahua-private#569

PR Type:

🐞 Bugfix

What changed? 🚀

A composition brush keeps a weak "context" entry (a WeakReference and its GC handle) for every visual or shape that uses it, so it can propagate its changes. A consumer only removes its entry when its brush property is reassigned. Consumers that are simply unloaded and collected left their entry on the brush, and only a change of the brush pruned them. Shared theme/resource brushes rarely change, so their list grew without bound: 3 entries per Border + Rectangle that ever used the brush.

CompositionObject.AddContext now sweeps dead entries once the list reaches a threshold, then resets the threshold to twice the surviving count (minimum 16). The sweep is skipped while PropagateChanged walks the same list.

  • Bounded, not eagerly reclaimed: dead entries stay until the next add or change of that brush, at most about twice the peak number of live consumers.
  • Why the brush sweeps, and not the consumer: composition objects suppress finalization, and a weak reference is cleared before finalizers run, so a collected consumer can't remove its own entry. Only the list owner can see it died. WinUI has no equivalent list; native composition releases references deterministically.
  • Tests: 3 Skia runtime tests in Given_CompositionObject_Context. They fail without the fix (1000 / 1050 / 303 entries) and pass with it. The expected counts include any consumers the GC failed to collect (conservative stack scanning on WASM / mobile), and a test fails visibly if more than 10% stay alive. The XAML test also checks that the live Border and Rectangle still render a brush change after the sweep.
  • Manual sample: CompositionBrushContextLeak in SamplesApp.

The fix commit is the reporter's patch, kept verbatim; follow-ups are separate commits.

PR Checklist ✅

🤖 Generated with Claude Code

jbourque-kahua and others added 6 commits November 5, 2026 20:16
CompositionObject.AddContext appended a weak ContextEntry per consumer but never swept dead ones. A consumer (BorderVisual, CompositionSpriteShape, SpriteVisual) only removes its entry when its brush property is reassigned, so consumers that are simply collected left their entry on the brush. Shared theme/resource brushes rarely raise changes (PropagateChanged is the only other place that prunes), so their list and its WeakReference GC handles grew without bound (3 entries per Border+Rectangle using the brush).

AddContext now sweeps dead entries once the list reaches a threshold, then resets the threshold to twice the surviving count (min 16): O(live consumers) size, amortized O(1) per add. The sweep is skipped while PropagateChanged is iterating the same list.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conservative GC (WASM, mobile) can keep a consumer alive, which then
legitimately keeps its entry. Derive the expected counts from the
measured survivors instead of assuming every consumer was collected,
and fail visibly if most of them were not.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The XAML leak test now asserts the live Border and Rectangle keep
their entries and still render a brush change after the sweep, and
resets the window content.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added area/automation Categorizes an issue or PR as relevant to project automation kind/documentation labels Nov 6, 2026
@Xiaoy312

Xiaoy312 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@Mergifyio backport release/stable/6.7 servicing/6.8

@mergify

mergify Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.7 servicing/6.8

✅ Backports have been created

Details

Cherry-pick of b393072 has failed:

On branch mergify/bp/release/stable/6.7/pr-26124
Your branch is ahead of 'origin/release/stable/6.7' by 3 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit b393072.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   specs/lessons.md

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Cherry-pick of b393072 has failed:

On branch mergify/bp/servicing/6.8/pr-26124
Your branch is ahead of 'origin/servicing/6.8' by 3 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit b393072.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   specs/lessons.md

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@unodevops

Copy link
Copy Markdown
Contributor

@unodevops

Copy link
Copy Markdown
Contributor

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

@Xiaoy312 Xiaoy312 added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. do-not-merge/breaking-change Indicates that a PR should not merge because it contains breaking changes. and removed do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. do-not-merge/breaking-change Indicates that a PR should not merge because it contains breaking changes. labels Nov 6, 2026
@nventive-devops

Copy link
Copy Markdown
Contributor

The build 244220 found UI Test snapshots differences: skia-linux-screenshots: 2458, skia-windows-screenshots: 2458

Details
  • skia-linux-screenshots: 2458 changed over 2494

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AnimatedBackVisualSourceSamplePage.png-dark
    • AndroidWindowInsets.png
    • AppBarButtonWithIconTest.png
    • AppBarButtonTest.png
    • ApplicationViewMode.png
    • ApplicationViewMode.png-dark
    • AppWindowClosing.png
    • Application_DispatcherShutdownMode.png
    • AppWindowTitleBar Properties.png-dark
    • AppXamlDefinedResources.png-dark
    • Arrange_Performance01.png-dark
    • Arrange_Performance01.png
    • Attributed_text_Supserscript.png
    • AuthenticationBroker_Cancel.png-dark
    • AutoBorderStretchwithtopmargin.png
    • AutoBorderStretchwithtopmargin.png-dark
    • AutomationProperties_Name.png-dark
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_Name.png
    • AutomationProperties_AutomationId.png
  • skia-windows-screenshots: 2458 changed over 2494

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png
    • AnimatedBackVisualSourceSamplePage.png
    • AppBarButtonTest.png
    • AppBar_KeyBoard.png
    • ApplicationViewSpanningRectsPage.png-dark
    • AppWindowClosing.png-dark
    • AppWindowPositionAndSize.png
    • AppXamlDefinedResources.png
    • Attributed_text_FontSize_Changing.png
    • Attributed_text_Simple.png-dark
    • AutoBorderStretchwithbottommargin.png
    • AutoBorderStretchwithleftmargin.png-dark
    • AutomationProperties_Name.png
    • AutomationProperties_Relations.png-dark
    • AutoSuggestBoxChosenSuggestion.png-dark
    • AutoSuggestBoxChosenSuggestion.png
    • AutoSuggestBox_Icons.png-dark
    • AutoSuggestBox_Icons.png
    • BasicAcrylicBrushTest2.png-dark
    • BasicAcrylicBrushTest2.png

@mergify

mergify Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

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.

@Xiaoy312
Xiaoy312 merged commit 6aaf325 into master Nov 6, 2026
55 checks passed
@Xiaoy312
Xiaoy312 deleted the dev/xygu/20261105/composition-context-prune branch November 6, 2026 17:33
Xiaoy312 added a commit that referenced this pull request Nov 8, 2026
…r-26124

fix(composition): Prune dead context entries on add (backport #26124)
agneszitte added a commit that referenced this pull request Nov 9, 2026
…6.7/pr-26124

fix(composition): Prune dead context entries on add (backport #26124)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/automation Categorizes an issue or PR as relevant to project automation kind/documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants