Skip to content

ItemsRepeater: animated content (ProgressRing/AnimatedVisualPlayer) in recycled elements keeps rendering — render loop never idles (high CPU on Skia/WASM) #23446

Open

Description

@DerekRaven

Summary

On the Skia render path (Desktop and WebAssembly), an ItemsRepeater keeps a recycled element parented in the visual tree until it is reused. If that element hosts continuously-animating content — e.g. an indeterminate ProgressRing (which is an AnimatedVisualPlayer + Lottie) — the animation keeps running on the recycled-but-still-parented element. The Skia render loop therefore never goes idle: the whole canvas repaints every frame at ~100% of a CPU core, even though nothing animated is visible on screen.

This happens whenever a list item with active animated content is removed/replaced and no later item reuses its element (for example, the replacement item resolves to a different DataTemplate).

Environment

  • Uno Platform 6.7 (also present on current master).
  • .NET 10.
  • Reproduces on the Skia render path: Desktop (X11 / macOS / Windows Skia) and WebAssembly (Skia).

Root cause

  1. Recycled elements stay parented. RecyclePool.PutElementCore adds the element to the pool without detaching it from its parent. It is only removed from the parent later, in RecyclePool.TryGetElementCore, when the element is reused for another item. → If it is never reused, it stays a child of the ItemsRepeater indefinitely.
    • src/Uno.UI/UI/Xaml/Controls/Repeater/RecyclePool.cs — PutElementCore (no detach) vs TryGetElementCore (detaches on reuse).
  2. DataContext is not cleared on recycle (Uno-specific divergence). On Uno, it is set on prepare (ViewManager → elementAsFE.DataContext = elementDataContext) and only overwritten on reuse. A recycled element keeps its stale DataContext, so a data-bound ProgressRing.IsActive stays true and no DataContextChanged fires. Current WinUI behaves differently: ViewManager::ClearElementToElementFactory nulls the DataContext on recycle when the repeater set it (MustClearDataContext); Uno's Repeater port predates that change. This divergence is not what makes the bug Skia-specific, though: measured on real WinUI, the nulled DataContext leaves the {Binding}-driven IsActive at its last value (true) — the pooled ring keeps animating there too.
  3. The animation self-schedules while parented + active. For the indeterminate ProgressRing the Skia source (src/AddIns/Uno.UI.Lottie/LottieVisualSource.Skottie.cs) re-invalidates from its own Render() every frame while playing (if (_stopwatch.IsRunning) _skCanvasElement?.Invalidate();), and the looped animation restarts the stopwatch forever. Because the recycled element is still in the visual tree and the ring is still active, it keeps invalidating the canvas → the render loop runs at full frame rate continuously.
  4. ElementClearing does fire on the granular clear path, but the framework does nothing to stop/pause animations in the cleared element, and (per 1–2) the element is not unloaded — and on Uno its DataContext is not cleared either.

Net effect: a removed item's animated element is left parented and animating forever — high idle CPU, no visible animation.

Minimal reproduction

A minimal UI with two DataTemplates (so the spinner element is recycled but not reused). On Skia desktop or WASM:

MainPage.xaml

<Page x:Class="RepeaterSpinnerRepro.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
      xmlns:local="using:RepeaterSpinnerRepro">

    <Page.Resources>
        <DataTemplate x:Key="SpinnerTemplate">
            <!-- Active indeterminate spinner. -->
            <muxc:ProgressRing IsActive="True" Width="24" Height="24" Margin="8" />
        </DataTemplate>

        <DataTemplate x:Key="TextTemplate">
            <TextBlock Text="{Binding Title}" Margin="8" />
        </DataTemplate>

        <local:ReproTemplateSelector x:Key="Selector"
                                     SpinnerTemplate="{StaticResource SpinnerTemplate}"
                                     TextTemplate="{StaticResource TextTemplate}" />
    </Page.Resources>

    <StackPanel Padding="24" Spacing="12">
        <Button Content="Remove the spinner item" Click="OnRemoveSpinner" />
        <ScrollViewer Height="200">
            <muxc:ItemsRepeater x:Name="Repeater"
                                ItemTemplate="{StaticResource Selector}" />
        </ScrollViewer>
    </StackPanel>
</Page>

MainPage.xaml.cs

using System.Collections.ObjectModel;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace RepeaterSpinnerRepro;

public sealed record Item(string Title, bool IsSpinner);

public sealed partial class MainPage : Page
{
    private readonly ObservableCollection<Item> _items = new()
    {
        new Item("a plain row", IsSpinner: false), // TextTemplate (a different template)
        new Item("the spinner",  IsSpinner: true),  // SpinnerTemplate (active ProgressRing)
    };

    public MainPage()
    {
        InitializeComponent();
        Repeater.ItemsSource = _items;
    }

    private void OnRemoveSpinner(object sender, RoutedEventArgs e)
    {
        // Remove the spinner item. Its element is recycled, but nothing reuses it
        // (the only remaining item uses TextTemplate), so it stays parented + animating.
        var spinner = _items.FirstOrDefault(i => i.IsSpinner);
        if (spinner is not null) _items.Remove(spinner);
    }
}

public sealed class ReproTemplateSelector : DataTemplateSelector
{
    public DataTemplate? SpinnerTemplate { get; set; }
    public DataTemplate? TextTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item)
        => (item is Item { IsSpinner: true } ? SpinnerTemplate : TextTemplate)!;
}

Steps

  1. Run on Skia desktop or WebAssembly. Both rows render; the spinner animates.
  2. Click “Remove the spinner item.” The spinner disappears from view (only the plain row remains).
  3. Observe CPU: it stays pegged at ~100% of one core indefinitely, with no visible spinner.
    • WASM: DevTools → Rendering → Paint flashing shows the whole canvas repainting every frame; the Performance monitor shows continuous CPU.
    • Desktop: a system CPU monitor (or dotnet-counters monitor) shows sustained high CPU; the render loop never idles.

To confirm the mechanism programmatically: capture the realized spinner element via Repeater.TryGetElement(index) before removal, then after removal observe that it is still parented (VisualTreeHelper.GetParent(...) != null), its DataContext is unchanged on Uno (no DataContextChanged; on real WinUI the DataContext is cleared on recycle — see root cause 2), and the nested ProgressRing.IsActive is still true (measured on both Uno/Skia and WinUI).

Contrast: if the replacement item uses the same template (so the element is reused), the DataContext updates on reuse and the spinner deactivates — no leak. The bug only manifests when a recycled element is not reused.

What is (and is not) Uno's to fix

Of the behaviors described above, the first is WinUI-parity by design and should NOT change; the second is a real divergence but orthogonal to this bug:

  • Recycled elements staying parented (RecyclePool.PutElementCore not detaching) matches WinUI's MUX implementation; same-owner elements are deliberately kept attached "so that we don't incur the enter/leave cost during recycling". Detaching on recycle would regress recycle performance and diverge from WinUI.
  • Stale DataContext on pooled elements turns out to be Uno-specific, not parity: current WinUI clears the DataContext on recycle when it set it (MustClearDataContext); Uno's port predates that change. Porting the clear is a separate parity work item (tracked in ItemsRepeater: DataContext not cleared on recycle (missing WinUI MustClearDataContext parity) #23624) — but measurably not a fix for this bug: on real WinUI the ring's bound IsActive keeps its last value (true) after the clear, so the pooled spinner keeps animating there as well. Either way, data bindings are not a viable shutdown mechanism for a recycled element; the documented WinUI contract remains that the app stops animations in ItemsRepeater.ElementClearing, and that contract holds for Uno too.

The Uno-owned defect is the render-loop cost model for animated visuals that are not effectively rendered:

  • On WinUI, forgetting the ElementClearing hygiene is near-free — composition animations run on the compositor with dirty-region rendering, so an invisible animated visual does not pin the UI thread.
  • On the Skia path, the same omission permanently pins a CPU core with full-canvas repaints from content that contributes zero visible pixels (and on WebAssembly that is effectively fatal). A render loop that can be pinned forever by a non-rendered animation is a platform defect regardless of app hygiene.

Expected

  1. Render layer (the fix): a recycle-pooled element that stays parented should not keep the render loop awake. Its visual should be excluded from composition while it sits in the pool, and restored when it is reused — so continuously-animating content it hosts cannot keep invalidating the canvas once its item has been removed.
  2. Ideally, partial invalidation: one small animating visual should dirty its own region, not force a whole-canvas repaint every frame.
  3. Docs: the ElementClearing stop-animations pattern should be documented as a workaround on Skia targets for ItemsRepeater content hosting continuous animations on versions without the fix, since the failure mode there is catastrophic rather than benign.

Actual

The recycled element remains parented with its ProgressRing active; the Skia render loop runs at full frame rate continuously (~100% of a core), invisibly.

Notes / downstream workaround

Consumers can subscribe to ItemsRepeater.ElementClearing and stop animations in the cleared element (deactivate any ProgressRing, and/or collapse the element so it leaves the render pass), restoring on ElementPrepared. Two practical traps for anyone applying this workaround:

  • A plain IsActive = false write on the cleared element can be overridden while the stale DataContext keeps a visual state / attached-property driver asserting "active" — collapsing the element is the robust part of the workaround.
  • The restore on ElementPrepared must re-drive the animation state from the new DataContext, not just restore visibility: value-change-driven mechanisms (bindings, attached-property walks, StateTriggers) do not re-fire when the new item produces the same values as the stale one (true→true), which otherwise leaves a visible row with a permanently stopped spinner.

Activity

  1. self-assigned this
    on Jun 12, 2026
  2. DerekRaven commented on Jul 2, 2026

    @DerekRaven
    ContributorAuthor

    WinUI vs Skia: same framework behavior, different consequences

    To clarify why this issue is Skia-specific even though the ItemsRepeater/RecyclePool behavior is identical to WinUI, here is the exact distinction, with references to the WinUI sources (pinned at microsoft/microsoft-ui-xaml@d4aeb08).

    What is identical in WinUI (framework parity)

    The recycled element stays parented and its animation stays active in WinUI too — that part is not Uno divergence:

    1. RecyclePool::PutElementCore does not detach, hide, or stop anything. It only records the element in the pool: RecyclePool.cpp#L43-L66. The element is removed from its parent only later, in TryGetElementCore, and only when it is reused under a different owner: RecyclePool.cpp#L99-L117. If it is never reused, it stays a child of the ItemsRepeater indefinitely — in WinUI as well.

    2. WinUI's only mitigation is a layout-level one: ItemsRepeater::ArrangeOverride arranges pool-owned children at (-10000, -10000) with size 0 — "Toss it away. And arrange it with size 0 so that XYFocus won't use it.": ItemsRepeater.cpp#L195-L204 (constant at ItemsRepeater.cpp#L23). Note the stated intent: it exists for XYFocus, not to stop rendering or animations. This code is already faithfully ported to Uno: ItemsRepeater.cs#L322-L331.

    3. The ProgressRing animation keeps playing while pooled in WinUI. When IsActive is true, ProgressRing starts a looped AnimatedVisualPlayer play — player.PlayAsync(0, 1, /*looped*/ true): ProgressRing.cpp#L329-L341. Nothing in RecyclePool, ViewManager, or ProgressRing pauses it when the host element is recycled — the ring in the pool is still "spinning" in WinUI, exactly as on Skia.

    One measured divergence: current WinUI clears the DataContext on recycle

    Running the #23447 repro sample on the WinAppSDK head (real WinUI) reports stillParented=True / dataContextUnchanged=False / ringIsActive=True after the spinner item is removed. The False comes from ViewManager::ClearElementToElementFactory, which nulls the DataContext when the repeater was the one that set it (the MustClearDataContext flag): ViewManager.cpp#L144-L167, set on prepare at ViewManager.cpp#L805-L844. Uno's Repeater port predates this MUX change and never clears the DataContext — a real parity gap (tracked in #23624), but orthogonal to this bug: with the DataContext nulled, the {Binding}-driven ProgressRing.IsActive simply keeps its last value (true), so the pooled ring keeps animating on WinUI as well — measured, not just inferred from the sources above.

    Why the consequences differ

    The divergence is entirely in how the animation is driven and what an invisible animating visual costs:

    • WinUI: the AnimatedVisualPlayer Lottie is a composition (independent) animation — its progress is advanced by an AnimationController on the composition engine's clock, off the XAML UI thread. A zero-size visual parked at (-10000, -10000) produces no pixels, wakes no per-frame UI-thread work, and the XAML render loop stays idle. The animation "runs" as retained-mode compositor state at effectively zero cost.

    • Uno Skia: the Lottie player is paint-driven. Each Render() pass seeks the animation and schedules the next frame by invalidating its canvas element again: LottieVisualSource.Skottie.cs#L302 → #L345-L351. Render() has no size or on-screen early-out, and the Skia compositor culls a subtree only on Opacity == 0 / IsVisible == false (Visual.skia.cs#L312) — not on being off-screen or zero-sized. So the pooled ring keeps getting painted, each paint re-invalidates, and the render loop never idles: full-canvas repaints at frame rate, ~100% of one core, indefinitely.

    WinUI Uno Skia (before fix)
    Pooled element stays parented ✔️ (RecyclePool.cpp#L43) ✔️ (ported 1:1)
    Ring animation still active while pooled ✔️ measured (ProgressRing.cpp#L340) ✔️
    DataContext on recycle Cleared when the repeater set it (ViewManager.cpp#L144-L167) Kept — port predates the MUX change (separate parity gap, orthogonal to this bug)
    Animation clock Composition engine (independent animation, off UI thread) Paint-driven (each paint schedules the next invalidation)
    Cost of the invisible pooled spinner ~0 (retained compositor state, no pixels, UI thread idle) Full-canvas repaint every frame, ~1 core pinned, forever

    Implication

    There is no WinUI source to align with for a fix: the one relevant mechanism WinUI has (the size-0 off-screen arrange) is already ported and running, and it cannot help because on Skia the cost comes from the paint-driven invalidation loop, not from visible pixels. Preventing the pooled element from participating in composition (or, longer-term, teaching the Skia compositor/Lottie player to stop self-invalidating when not effectively painted) is inherently renderer-level, Uno-specific work.

    This has now been measured with the same scenario on both frameworks (the repro sample in #23447 builds for the WinAppSDK head and uses only public API for its readouts): after removal, WinUI reports stillParented=True / dataContextUnchanged=False / ringIsActive=True with the app near idle, versus True / True / True and ~100% of a core, indefinitely, on Skia. The key readout is ringIsActive=True on both: even after WinUI clears the DataContext, the pooled ring keeps animating — WinUI just doesn't pay for it. So even the strongest available "align with WinUI sources" move (porting the MustClearDataContext clear) demonstrably would not fix this bug.


    Edited: this comment originally predicted dataContextUnchanged=True on WinUI; an actual WinAppSDK run measured True / False / True. The DataContext section above documents the divergence (MustClearDataContext), which Uno's port predates.

  3. github-actions commented on Nov 10, 2026

    @github-actions
    Contributor

    This issue has had no activity for 120 days. It will be closed in 10 days if there is no further activity. Remove the Stale label or leave a comment to keep it open.

  4. added
    stale
    on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stale

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions