Skip to content

fix(responsive): reconnect a re-parented host instead of tearing down - #1647

Merged
nickrandolph merged 3 commits into
mainfrom
dev/ks/1646-responsive-reconnect
Sep 23, 2026
Merged

nickrandolph merged 3 commits into
mainfrom
dev/ks/1646-responsive-reconnect

Conversation

@Kunal22shah

Copy link
Copy Markdown
Contributor

GitHub Issue (If applicable): #1646

PR Type

  • Bugfix

What is the current behavior?

Since #1608, ResponsiveExtension.OnHostUnloaded tears the extension down for good: it drops the hard self-reference, removes the tracking entry and detaches from the static WindowSizeChanged, and nothing re-arms Loaded. An element that is unloaded and loaded again therefore never reconnects. It keeps the value it last resolved and ignores every later size change.

Any host that re-parents live content hits this: a ContentControl swapping its content back, a Frame re-showing a cached page, a designer or preview surface moving a previewed tree into and out of a canvas. The first Loaded resolves against whatever size is in effect then, the move unloads and reloads the element, and from that point the value is frozen.

It looks intermittent, because whether a given run is wrong depends only on whether the reload lands before or after the next size change.

A second, unrelated bug in the same area blocked the tests that would have caught this. WeakEventHelper.AssertStatic asserts Delegate.Target is null, but Roslyn emits a non-capturing lambda as an instance method on a cached singleton, so the target is never null and the assert fires for every caller. Every Debug runtime test that reaches CreateWeakHandler fails on main today, including ResponsiveExtensionsLeakTests.

What is the new behavior?

An element that is unloaded and loaded again resolves against the size in effect on its next Loaded, exactly like a freshly created one.

OnHostUnloaded still drops both process-lifetime roots — the HardSelfReferences entry and the static WindowSizeChanged subscription — so #1608's goal is unchanged: nothing static roots a dead extension, and an abandoned host graph (and its collectible AssemblyLoadContext) is still collectible. It then re-arms Loaded through the existing ConnectWhenLoaded path. The re-armed subscription is held by the host, not by a static, so a host that really is gone still takes the extension with it.

AssertStatic now compares captured state instead of the target reference: a closure's display class declares one instance field per captured variable, while the singleton Roslyn caches a non-capturing lambda on declares none.

PR Checklist

  • Tested code with current supported SDKs
  • Tested the changes where applicable:
    • UWP
    • WinUI
    • iOS
    • Android
    • WASM
    • MacOS
  • Updated the documentation as needed
  • 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

ResponsiveExtensionsReconnectTests.Reparented_Host_ResolvesForTheSizeInEffectOnReload re-parents a host while the override size changes underneath it, then asserts the value follows. Run through the Material sample's desktop runtime-test host:

Branch state ResponsiveExtensions filter
Without the OnHostUnloaded change 3 passed, 1 failed
With it 4 passed, 0 failed

The one failure is the new test, and it fails with Expected:<Narrow>. Actual:<Wide>. — the frozen value. ResponsiveExtensionsLeakTests.Connected_Extension_IsCollectible_WithoutResizeOrExplicitUninstall passes in both runs, so the collectibility #1608 established is intact.

Both runs need the AssertStatic commit; without it the same filter is 1 passed, 3 failed on main for a reason unrelated to either change. The two commits are separable, and the assert one stands on its own.

One consequence worth a reviewer's eye: an unloaded extension now stays in the public TrackedInstances list, where #1608 removed it, because it is dormant rather than dead. GetInstanceFor can therefore return an extension whose host is out of the tree. That is the intended meaning of "tracked from Install until Uninstall", and Uninstall still removes it, but it is a semantic widening for anything that read a non-null result as "live".

Reproduced and fixed against 10.0.0-dev.3; 9.2.0-dev.20 contains #1608 too and has the same defect.

- Debug.Assert required a null delegate Target, but Roslyn emits a
  non-capturing lambda as an instance method on a cached singleton, so
  the assert fired for every caller and failed every Debug runtime test
  reaching CreateWeakHandler
- compare captured state instead: a closure's display class declares an
  instance field per captured variable, the cached singleton declares none
- OnHostUnloaded dropped the process-lifetime pins and the tracking
  entry with nothing re-arming Loaded, so an element that was unloaded
  and loaded again kept the value it last resolved
- drop the pins as before, then re-arm Loaded: a host that is really
  gone still takes the extension with it, a re-parented one reconnects
- runtime test re-parents a host while the size changes and asserts it
  re-resolves; it fails without this change with Expected:<Narrow>
  Actual:<Wide>
Copilot AI lite review requested due to automatic review settings September 22, 2026 17:58
@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.

Copilot review overview

🟡 Changes recommended

The bound-instance delegate case in WeakEventHelper remains unresolved.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Fixes ResponsiveExtension reconnection after host re-parenting and updates weak-handler validation with regression coverage.

Changes:

  • Re-arms Loaded after host unload.
  • Updates delegate capture validation.
  • Adds a re-parenting runtime test.
File Review summary
src/​Uno.Toolkit.UI/​Markup/​ResponsiveExtension.cs Reconnects unloaded hosts and resolves the current size.
src/​Uno.Toolkit.UI/​Helpers/​WeakEventHelper.cs Moderate issue (3 votes): bound instance delegates with zero fields may still be strongly retained. Nit (2 votes): clarify wording.
src/​Uno.Toolkit.RuntimeTests/​Tests/​ResponsiveExtensionsReconnectTests.cs Adds regression coverage for re-parented host behavior.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@Kunal22shah Kunal22shah self-assigned this Sep 22, 2026
…-free target

- a zero-field check alone also accepted an instance method on a
  stateless object, whose target the wrapper still keeps alive
- require the target's type to be compiler-generated as well, which the
  singleton Roslyn caches non-capturing lambdas on is and an ordinary
  class is not
@github-actions

Copy link
Copy Markdown

@nickrandolph
nickrandolph merged commit 228ae1f into main Sep 23, 2026
22 checks passed
@nickrandolph
nickrandolph deleted the dev/ks/1646-responsive-reconnect branch September 23, 2026 14:39
@kazo0

kazo0 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

@Mergifyio backport servicing/10.0

@mergify

mergify Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

backport servicing/10.0

✅ Backports have been created

Details

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.

5 participants