Skip to content

fix(responsive): release hosts that never load from HardSelfReferences - #1684

Merged
kazo0 merged 3 commits into
mainfrom
dev/nr/responsive-extension-unloaded-host-leak
Oct 27, 2026
Merged

kazo0 merged 3 commits into
mainfrom
dev/nr/responsive-extension-unloaded-host-leak

Conversation

@nickrandolph

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): closes #1683

PR Type

  • Bugfix

What is the current behavior?

  • ResponsiveExtension adds itself to the static HardSelfReferences list (the uno#14502 workaround).
  • If its element never loads, the extension never connects and is never removed from that list.
  • Its unsubscribe closure holds the element strongly, so the element and everything reachable from it stay in memory. An app in a collectible AssemblyLoadContext can't unload.

What is the new behavior?

  • Both unsubscribe closures (ConnectWhenLoaded, Connect) hold the host through a WeakReference.
  • SweepDeadInstances and CleanupIfHostDisposed share one check, IsHostGone. It falls back to the target's weak reference when the extension never connected.
  • An element that never loads is collected once nothing else references it.

Reviewer's guide

  • Start here: src/Uno.Toolkit.UI/Markup/ResponsiveExtension.cs, IsHostGone and the two Disposable.Create closures.
  • Test that proves it: ResponsiveExtensionsLeakTests.NeverLoaded_Host_IsCollectible_AfterSweep. Without the fix it fails on Assert.IsFalse(hostRef.IsAlive).
  • Test change: SweepDeadInstances_Prunes_CollectedTrackedInstances now takes its baseline count after a collect and sweep, so a dead entry left by another test can't skew it.
  • Not covered by a test:
    • The weak capture in the Connect closure. The case it guards is a connected host dropped without Unloaded, which the harness can't fake.
    • A target that isn't the host, for example a ColumnDefinition on a Grid that never loads.
  • Known limitation, unchanged: the sweep only runs when another extension connects. Until then a dead entry stays in the list, but it no longer holds its element.

PR Checklist

  • Tested the changes where applicable:
    • Desktop (Windows), Skia runtime tests on net10.0-desktop
  • Updated the documentation as needed: n/a, no behaviour change to document
  • Runtime Tests and/or UI Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Contains NO breaking changes
  • Associated with an issue (GitHub or internal)
  • Commits must be following the Conventional Commits specification.

Other information

Runtime tests, desktop, Debug, filter Responsive: 12 passed, 2 failed, 7 skipped. The 2 failures are ResponsiveHelperLeakTests.Reset_TearsDown_DefaultProvider and DefaultProvider_IsCollectible_WhileXamlRootAlive. Both time out waiting for "Border loaded", and fail the same way without this change.

🤖 Generated with Claude Code

nickrandolph and others added 2 commits October 27, 2026 10:56
- Install an extension on a host that is never loaded, so it never connects
- Release the host, collect, sweep, collect again
- Assert the host and the extension are both collected
- Fails today: the pending Loaded unsubscription holds the host strongly,
  and the sweep never removes an extension that has no host weak-reference
- Take the TrackedInstances baseline after a sweep, so dead entries left by
  earlier tests do not skew the count

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Capture the host weakly in the Loaded and Unloaded unsubscribe closures,
  so the static HardSelfReferences list cannot root the host through them
- Judge a never-connected extension's liveness by its target, in both
  SweepDeadInstances and CleanupIfHostDisposed
- Without this, an extension provided on an element that never loads stays
  in the static list forever and keeps its element tree alive; an app loaded
  in a collectible AssemblyLoadContext then cannot unload

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

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.

🔵 Needs a closer look

Human validation is needed for the untested connected-host collection path and reported runtime-test failures, alongside correction of the pruning-test baseline.

1 open issue
What changed in this PR

Fixes responsive-extension retention of never-loaded elements, addressing issue #1683 and helping collectible app contexts unload.

Changes:

  • Uses weak host references in both unsubscribe closures.
  • Shares a liveness check that falls back to the target before connection.
  • Adds a never-loaded-host regression test and adjusts pruning-test setup.
File Description
src/​Uno.Toolkit.UI/​Markup/​ResponsiveExtension.cs Weakens host captures and extends cleanup to never-connected instances.
src/​Uno.Toolkit.RuntimeTests/​Tests/​ResponsiveExtensionsLeakTests.cs Adds collection coverage and revises the pruning baseline.

Review effort: Balanced

@kazo0

kazo0 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

@Mergifyio backport servicing/10.0

@mergify

mergify Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

backport servicing/10.0

✅ Backports have been created

Details

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@kazo0
kazo0 enabled auto-merge (squash) October 27, 2026 02:45
@github-actions

Copy link
Copy Markdown

@kazo0
kazo0 merged commit 71e1615 into main Oct 27, 2026
26 checks passed
@kazo0
kazo0 deleted the dev/nr/responsive-extension-unloaded-host-leak branch October 27, 2026 03:05
kazo0 added a commit that referenced this pull request Oct 27, 2026
…s (backport #1684) (#1685)

Co-authored-by: Nick Randolph <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Steve Bilogan <[email protected]>
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.

Responsive markup extension keeps an element alive forever when the element never loads

3 participants