Skip to content

Unmount ListView rows WinUI parks in the layout pass after a move - #1441

Merged
Alexandre Zollinger Chohfi (azchohfi) merged 22 commits into
mainfrom
azchohfi-listview-parked-rows-keep-live-component
Oct 30, 2026
Merged

Alexandre Zollinger Chohfi (azchohfi) merged 22 commits into
mainfrom
azchohfi-listview-parked-rows-keep-live-component

Conversation

@azchohfi

@azchohfi Alexandre Zollinger Chohfi (azchohfi) commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

ListView<T> / GridView<T> rows whose container WinUI parks after a layout pass no longer keep a live component. #1424's orphan sweep (extended by #1429) runs during the update, so it can't catch this case: WinUI processes a move synchronously, then unlinks the container (IndexFromContainer == -1) and parks it at (-15000, -15000) in the next layout pass, with no recycle ContainerContentChanging. A row moved twice, or moved out of the realized range, is enough to trigger it.

The sweep after layout

  • ReactorListState.StructureChanged is set by Source.CollectionChanged. After a structural update, RefreshRealizedContainers schedules a one-shot LayoutUpdated sweep. The sweep unmounts the view of every container the layout pass left unlinked. The visuals stay, so delete transitions still play.

  • A pass can't park containers in a list it doesn't lay out, so the sweep waits in two cases:

    • Not loaded (out of the tree, e.g. an unselected TabView tab, or inserted this tick): it waits for the list's own Loaded.
    • The list or an ancestor is collapsed: it waits for the list's own EffectiveViewportChanged.

    Either way it then sweeps after the next layout pass. Unmounting a list drops any pending handler.

  • The sweep runs from a WinUI event, so it uses the isolated-teardown contract from [Feature] Let apps replace the built-in error fallback that shows the full exception text #1291. A throwing cleanup doesn't stop it: every parked row is torn down, and failures go to the RenderErrorHandler as Cleanup. With no handler, the first failure is rethrown once the sweep finishes. A failure the app already declined in nested Reactor work keeps propagating.

Isolated teardowns, made complete (the sweep, released trees, boundary rollback)

  • Every unmount callback kind is now isolated: V1 control handlers, descriptor OnUnmount hooks, each ImperativeBridged property unmount, decorators, and RegisterType callbacks.
  • When one throws, the failure is reported once and in order. The children it didn't reach are torn down, through the children strategy for named slots and through the engine's child walk otherwise. Children already torn down are skipped, including across nested recoveries. The half-torn-down control is never pooled.

Linked issue / spec

Fixes #1440. Follow-up to #1423 / #1424.

Test plan

Selftests in TemplatedListOrphanContainerFixtures.cs. Each move fixture has a positive control (*_ParkedContainer) that skips visibly if WinUI parks nothing.

  • Moves. Fixtures TemplatedList_RowMoved{Twice,OutOfRange,AsListCollapses,AsListIsReinserted,InUnselectedTab}LeavesNoParkedComponent. Each checks:
    • no unlinked container keeps a live component;
    • parked views are released;
    • ReactorDiagnostics.TrySetState on a parked row is refused;
    • each row has at most one live component;
    • linked rows stay live;
    • the sweep unsubscribes;
    • the moved row responds, after being scrolled back into view if needed.
  • Parked-row teardown failures. Fixtures TemplatedList_ParkedRow{CleanupFailureIsReported,CleanupKeepsDeclinedPropagation,DescriptorTeardownFailureIsReported,RegisteredTeardownFailureIsReported,DecoratorTeardownFailureIsReported}.
  • Isolated teardown recovery, on both the plain and the pooling unmount walk. Fixtures TemplatedList_{,Nested}IsolatedRegisteredTeardownRecovers*, TemplatedList_DecoratorIsolatedTeardownRecovers*, TemplatedList_IsolatedDescriptor{TeardownTearsDownSlots,PartialTeardownSkipsSlots}*, TemplatedList_IsolatedPropertyUnmountsRunIndependently* and TemplatedList_IsolatedFailuresKeepEncounterOrder*. They check:
    • each child is torn down exactly once;
    • a failed control is not pooled;
    • failures are reported in order.
  • Lifetime. TemplatedList_UnmountWhileCollapsedDropsPendingSweep.
  • Unit tests. ReactorListStateTests: StructureChanged is set by Reset, move, insert and remove, and stays clear when the keys don't change.
  • Mutation checks. Every guard above was disabled in turn, and each mutant fails at least one check: the sweep itself, the post-layout timing, the unsubscribe, the Move flag, the collapsed and Loaded waits, the cancel on unmount, every isolation catch, the strategy run after a failed hook, the skip sets and their union, pool exclusion, the per-callback isolation and the report order.
  • dotnet restore Reactor.slnx, then dotnet build Reactor.slnx --no-restore -c Release: passed, 0 errors. The only warnings are the existing WIN2D0001. Behind this machine's TLS proxy, restore used a mirror config passed through RestoreConfigFile.
  • dotnet test tests/Reactor.Tests --no-build -p:Platform=x64: passed, 15,379 total, 0 failed.
  • Selftests. The TemplatedList_, RenderErrorHandler_, DescriptorUnmount, WrapElementSlot, ConditionalChildSlot_ and ConnectedAnimation filters all pass. Full --self-test runs show only environment-dependent failures, which vary from run to run (window topmost, an AutoSuggestBox timeout, scroll anchoring).
  • End to end, in the winapp DevTools test app (churn panel, 16 ms random insert / remove / swap for 90 s, then stop and count):
    • main: 147 live ChurnRow components where 47 were expected; 100 unlinked containers still held a live view.
    • This branch: the live count equals the expected count; 0 unlinked containers hold a live view.

Risk / breaking changes

  • Public API: none. Everything new is internal or private.
  • Behavior: a row's view in a container WinUI parks is unmounted after the layout pass instead of staying live. Isolated teardowns (ReleaseReplacedTree, boundary rollback, this sweep) now drain past throwing handler, descriptor, property, decorator and RegisterType callbacks instead of stopping at the first one.
  • Cost: no new work on the hot unmount path; the non-isolated path keeps the direct handler calls. After a structural update the sweep makes one pass over the items panel's children. The tracking sets are allocated only inside isolated teardowns. The stress perf harness doesn't exercise ListView / GridView.

#1424's orphan sweep found a container's row through ListViewItem.Content,
which WinUI leaves null for these lists, so it never unmounted anything; and
it ran during the update, before the layout pass in which WinUI parks the
container of a row moved twice or moved out of the realized range without
raising the recycle ContainerContentChanging. Those rows kept a live
component: effects never cleaned up, and diagnostics writes were accepted
but never rendered.

The list state now flags structural changes, and after one the reconciler
unmounts, once the next layout pass completes, the view of every container
that pass left unlinked from any item. Visuals stay for delete transitions.

Fixes #1440

Co-authored-by: Copilot App <[email protected]>
…scribe

LayoutUpdated fires for every layout pass in the window, so the one-shot sweep
could spend itself while the list was collapsed and miss the container WinUI
parks once the list is shown. The sweep now waits while the list has a
collapsed ancestor, and an unloaded list re-arms StructureChanged instead.

Tests: a fixture that collapses the list with the last move; every linked
container keeps a live view; the sweep unsubscribes (test-only run counter);
headless StructureChanged gate tests.

Co-authored-by: Copilot App <[email protected]>
A list inserted into the tree is laid out (and its container parked) before
its Loaded is raised, so the sweep no longer gives up on an unloaded list; it
only waits while the list or an ancestor is collapsed.

Tests: a fixture that reinserts the list (keyed reorder) with the last move,
which fails with the unloaded give-up; the out-of-range row is scrolled back
into view and must respond.

Co-authored-by: Copilot App <[email protected]>
…he sweep on unmount

A sweep that found the list collapsed stayed on the window-wide LayoutUpdated,
walking the ancestors on every layout pass for as long as the list stayed
collapsed, and outlived a list unmounted meanwhile. It now waits on the list's
own EffectiveViewportChanged, which the pass that lays the list out raises,
and unmounting a list drops either pending handler.

Tests: a fixture unmounts the list while the sweep waits; mutants that skip
the cancel, the collapsed wait, or the reschedule are caught.

Co-authored-by: Copilot App <[email protected]>
A list in a TabView tab that is not selected stays mounted but leaves the
tree; WinUI parks the container only when the tab is selected again, after a
sweep scheduled meanwhile had already run. The sweep now waits on the list's
Loaded (then sweeps after the next layout pass) when the list is not loaded.

Test: the list's tab is switched away in the render of the last move; fails
without the wait.

Co-authored-by: Copilot App <[email protected]>
@azchohfi Alexandre Zollinger Chohfi (azchohfi) added ready-for-review Agent work and technical checks complete; awaiting review, not approval or merge agent-preparing Agent is addressing feedback or completing required validation and CI and removed ready-for-review Agent work and technical checks complete; awaiting review, not approval or merge labels Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 Build metrics

Artifact sizes for 27004aa vs the base branch (8f17bac).

Packages (compressed .nupkg)

Artifact base PR Δ
Microsoft.UI.Reactor.nupkg 1.98 MB 1.99 MB +3.86 KB (+0.19%) ⚠️
Microsoft.UI.Reactor.Advanced.nupkg 495.7 KB 495.7 KB -7 B (0.00%) ≈
Microsoft.UI.Reactor.Devtools.nupkg 287.8 KB 287.8 KB -1 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor

Artifact base PR Δ
Reactor.Analyzers.dll 411.5 KB 411.5 KB +0 B (0.00%) ≈
Reactor.dll 2.72 MB 2.73 MB +7.00 KB (+0.25%) ⚠️
Reactor.Localization.Generator.dll 16.0 KB 16.0 KB +0 B (0.00%) ≈
Reactor.Wrappers.Abstractions.dll 10.5 KB 10.5 KB +0 B (0.00%) ≈
Reactor.Wrappers.Generator.dll 145.5 KB 145.5 KB +0 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor.Advanced

Artifact base PR Δ
Reactor.Advanced.dll 1.00 MB 1.00 MB +0 B (0.00%) ≈

Assemblies in Microsoft.UI.Reactor.Devtools

Artifact base PR Δ
Microsoft.UI.Reactor.Devtools.dll 787.5 KB 787.5 KB +0 B (0.00%) ≈

✅ smaller / ⚠️ larger / ≈ within noise. Sizes come from a Release dotnet pack on the CI runner: packages are the compressed .nupkg download size, assemblies the uncompressed DLL inside it.
workflow run.

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.

🟡 Changes recommended

A throwing parked-row cleanup can abort the unsubscribed sweep, bypass the configured error handler, and leave rows mounted until another structural update; cleanup isolation and regression coverage are needed.

1 open issue
What changed in this PR

Addresses #1440 by moving cleanup of parked ListView<T> / GridView<T> rows to a post-layout sweep, correcting the timing and container-identification gaps in #1424.

Changes:

  • Tracks structural changes and schedules a one-shot sweep after layout, waiting for unloaded or collapsed lists.
  • Cancels pending sweep handlers when a list unmounts.
  • Adds structural-change unit tests and move-related selftests.
File Description
tests/​Reactor.Tests/​Internal/​ReactorListStateTests.cs Tests structural-change flag behavior.
tests/​Reactor.AppTests.Host/​SelfTest/​SelfTestFixtureRegistry.cs Registers six new selftest fixtures.
tests/​Reactor.AppTests.Host/​SelfTest/​Fixtures/​TemplatedListOrphanContainerFixtures.cs Tests parked-row cleanup, deferred sweeps, and cancellation.
src/​Reactor/​Core/​Reconciler.Update.cs Implements post-layout sweeping and pending-handler management.
src/​Reactor/​Core/​Reconciler.Mount.cs Updates cleanup documentation to reference the new sweep.
src/​Reactor/​Core/​Reconciler.cs Cancels pending sweeps during both unmount paths.
src/​Reactor/​Core/​Internal/​ReactorListState.cs Tracks structural changes and pending sweep handlers.
CHANGELOG.md Documents cleanup of containers parked after moves.

Review effort: Balanced

Comment thread src/Reactor/Core/Reconciler.Update.cs Resolved
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🧪 Merged coverage

Coverage for 27004aa vs the base branch (8f17bac) — unit + selftest merged.

Metric base PR Δ
Line 88.18% 88.21% +0.03 pp ≈
Branch 65.99% (1011/1532) 65.48% (1011/1544) -0.51 pp ⚠️

✅ higher / ⚠️ lower / ≈ within noise. Δ is in percentage points; coverage is unit + selftest merged (Debug x64) on the CI runner. Cobertura reports attached to the workflow run as artifacts.

The sweep runs from LayoutUpdated after unsubscribing, so a throwing effect
cleanup stopped it mid-way, left rows mounted and bypassed the
RenderErrorHandler. It now drains every cleanup under
IsolateUnmountCleanupFailures and routes failures through
ReportCleanupFailures (Cleanup source; with no handler the first is rethrown
after the sweep), as host teardown does.

Test: a parked row's cleanup throws once; the failure is reported and the
parked view still unmounts (the host crashes without the isolation).

Co-authored-by: Copilot App <[email protected]>

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.

🟡 Changes recommended

The propagation scope clears a nested exception’s declined marker during parked-row cleanup, allowing the list handler to swallow it; the scope must cover unmounting through rethrow, with regression coverage.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread src/Reactor/Core/Reconciler.Update.cs Resolved
- Open the render-error propagation scope before the parked-row unmounts, not
  just around reporting: a cleanup's nested Reactor work that the app already
  declined now keeps going out instead of being handed to the list's handler
  (which could swallow it). Regression: a parked row's cleanup mounts a host
  whose failing render is declined.
- Subscribe the Loaded / EffectiveViewportChanged waits through non-null locals.
- Count sweeps from a static helper; LINQ in the fixture's linked-row check.

Co-authored-by: Copilot App <[email protected]>

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

Descriptor unmount exceptions can abort cleanup and bypass error reporting, while cleanup-error fixtures incorrectly fail on the supported no-parking path.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Isolate descriptor teardown failures during unmount cleanup

src/​Reactor/​Core/​Reconciler.Update.cs:1393

A descriptor's OnUnmount callback still bypasses cleanup isolation: DescriptorHandler.Unmount invokes it directly, and V1HandlerAdapter.Unmount lets the exception escape. If a parked row contains a control whose descriptor teardown throws, this one-shot sweep stops before clearing the view's tag, processing later containers, or reporting failures to the configured handler. Route descriptor teardown through cleanup isolation, ensure remaining descendants and containers are drained, and add a regression with a throwing descriptor callback.

Medium severity Handle cleanup assertions when WinUI parks no container

tests/​Reactor.AppTests.Host/​SelfTest/​Fixtures/​TemplatedListOrphanContainerFixtures.cs:351

When WinUI parks no container, the positive control above skips, but both cleanup-error variants still fail here: the cleanup's offscreen-container guard never consumes s_parkedCleanup, so ParkedCleanupRan and the subsequent error/propagation assertions fail. This makes those fixtures fail on the explicitly supported no-parking path even when the list is healthy. Skip the cleanup-specific assertions when neither a parked container nor an executed cleanup was observed, while retaining failures when a parked container exists but its cleanup was missed.

1 resolved since last review

Review effort: Balanced

…st lookup

- A control descriptor's OnUnmount hook now runs under the active
  IsolateUnmountCleanupFailures scope like component cleanups: a throw is
  collected (and reported) and the control's children are still torn down.
  Regression: a parked row holds a control whose descriptor teardown throws.
- Fixtures: skip the parked-teardown checks when WinUI parked nothing (the
  teardown never ran); check every parked container's view is released; find
  the fixture's ListView by its ReactorRow source (under NativeAOT the TabView's
  tab strip matched an exact-type check).
- Wait locals are non-nullable.

Co-authored-by: Copilot App <[email protected]>

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 assessment is needed because a throwing registered-control unmount callback can abort parked-row cleanup and bypass the configured error handler.

No open issues 🎉

Review effort: Balanced

…kind

V1 handlers (standard, descriptor, decorator) and RegisterType callbacks now
share one engine helper for isolated teardowns: a throw is collected, the
children the handler did not reach are torn down once each (inherited skips
included), and the half-torn-down control is never pooled. The adapter-level
catches are gone (a decorator's continued traversal could repeat a child's
teardown; a failed descriptor control could be pooled).

Regressions: a decorator tears down one child and throws (both walks); the
pooling walk never pools a control whose teardown failed.

Co-authored-by: Copilot App <[email protected]>
…ked-rows-keep-live-component

# Conflicts:
#	CHANGELOG.md
#	src/Reactor/Core/Reconciler.Update.cs
#	tests/Reactor.AppTests.Host/SelfTest/SelfTestFixtureRegistry.cs

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.

🟡 Changes recommended

A throwing OnUnmount can bypass teardown of non-default child slots, leaving components mounted and preventing later cleanup retries once the view tag is cleared.

1 open issue

Review effort: Balanced

Comment thread src/Reactor/Core/Reconciler.cs Resolved
…hook throws

Inside an isolated teardown, a throwing descriptor OnUnmount no longer skips
the strategy-driven child teardown (named slots such as TeachingTip.HeroContent
are outside the engine's generic child walk). The adapter tears the children
down, then rethrows so the engine reports the failure once, recovers with its
skip set and keeps the control out of the pool.

Regression (both walks): a descriptor whose hook throws, with a leaf in a
named slot; the leaf is torn down exactly once.

Co-authored-by: Copilot App <[email protected]>
…ked-rows-keep-live-component

# Conflicts:
#	src/Reactor/Core/Reconciler.cs

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.

🟡 Changes recommended

Strategy recovery can tear down a slot child twice when an unmount hook partially succeeds before throwing; already-visited children must be skipped and regression coverage added for both unmount paths.

1 open issue
1 resolved since last review

Review effort: Balanced

When a handler's unmount hook throws inside an isolated teardown, the
adapter's strategy teardown now runs with the reconciler's skip set covering
what the hook already tore down, so a slot child it reached is not torn down
twice. Regression (both walks): the hook unmounts the named slot's child,
then throws; the child is torn down exactly once.

Co-authored-by: Copilot App <[email protected]>

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.

🟡 Changes recommended

A throwing unmount callback can skip remaining property callbacks and the descriptor hook, leaving components mounted because cleanup isolation does not protect each callback independently.

1 open issue
1 resolved since last review

Review effort: Balanced

…dently

Inside an isolated teardown, every ImperativeBridged property unmount callback
and the descriptor's OnUnmount hook now run even when an earlier one throws
(each may own a secondary slot no other walk reaches). The first failure is
rethrown for the adapter and engine (strategy teardown, recovery, no pooling);
later ones are reported directly, so each is reported once.

Regression (both walks): the first of two property callbacks throws; the
second still tears its child down and the hook still runs.

Co-authored-by: Copilot App <[email protected]>

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.

🟡 Changes recommended

Cleanup failures can be reported out of encounter order, causing a later exception to be rethrown instead of the first when no handler is configured.

1 open issue
1 resolved since last review

Review effort: Balanced

…ter order

Each property unmount callback and hook failure is now reported to the
isolation sink as it happens; the handler then throws an already-reported
marker so the adapter and engine still run the strategy teardown and recovery
and keep the control out of the pool, without reporting again. With no
handler, the first failure is the one that escapes.

Regression (both walks): a property callback fails, then the hook; the sink
sees first,second and the first escapes.

Co-authored-by: Copilot App <[email protected]>

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 assessment is needed because cleanup recovery can report failures out of order and invoke teardown twice for the same native control through distinct wrappers.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@azchohfi Alexandre Zollinger Chohfi (azchohfi) added ready-for-review Agent work and technical checks complete; awaiting review, not approval or merge and removed agent-preparing Agent is addressing feedback or completing required validation and CI labels Oct 30, 2026
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) merged commit 59541d3 into main Oct 30, 2026
68 of 71 checks passed
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) deleted the azchohfi-listview-parked-rows-keep-live-component branch October 30, 2026 16:29
@azchohfi Alexandre Zollinger Chohfi (azchohfi) removed the ready-for-review Agent work and technical checks complete; awaiting review, not approval or merge label Oct 30, 2026
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.

[Bug] ListView<T> rows WinUI parks after a move keep a live component (follow-up to #1423 / #1424)

2 participants