Skip to content

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

Merged
kazo0 merged 3 commits into
servicing/10.0from
mergify/bp/servicing/10.0/pr-1647
Oct 8, 2026
Merged

kazo0 merged 3 commits into
servicing/10.0from
mergify/bp/servicing/10.0/pr-1647

Conversation

@mergify

@mergify mergify Bot commented Oct 8, 2026

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.


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

- 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

(cherry picked from commit ce1af4d)
- 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>

(cherry picked from commit 55d7a90)
…-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

(cherry picked from commit 0efe6ae)
@kazo0
kazo0 merged commit 970f035 into servicing/10.0 Oct 8, 2026
18 checks passed
@kazo0
kazo0 deleted the mergify/bp/servicing/10.0/pr-1647 branch October 8, 2026 05:02
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.

2 participants