Skip to content

[Skia] BitmapImage: a cached in-flight download is cancelled by its first requester, failing every other BitmapImage sharing it #25096

Closed

Description

@mikernet

Current behavior

In a virtualized ListView whose item template contains a BitmapImage with a remote UriSource, calling ScrollIntoView on an item far down the list right after ItemsSource is set leaves one or two of the images at the bottom of the newly visible region blank. The affected items are the last ones materialized to fill the viewport after the scroll. For a given viewport height it reproduces most of the time but not always. The images never appear afterwards, until the container is recycled for a different item or the list is reloaded.

Reproduces identically on the desktop (Skia) and WebAssembly (Skia) heads, and identically whether the bitmap is shown by an Image element or by an ImageBrush on a Border, with Uno.WinUI 6.8.0-dev.212 / Uno.Sdk 6.8.0-dev.60 on .NET 10.

Template shape (fixed-size tile):

<DataTemplate x:DataType="local:SlideItem">
  <StackPanel Padding="0,10">
    <Grid Width="90" Height="60">
      <Image Stretch="UniformToFill">
        <Image.Source><BitmapImage UriSource="{x:Bind ThumbnailUri}" /></Image.Source>
      </Image>
    </Grid>
    <TextBlock Text="{x:Bind Title}" />
  </StackPanel>
</DataTemplate>

Code-behind (the deferral is the workaround for #24332; the images stay blank with or without it):

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var listView = (ListView)sender;
    DispatcherQueue.TryEnqueue(() => {
        if (listView.SelectedItem is { } selected)
            listView.ScrollIntoView(selected);
    });
}

Steps:

  1. Set ItemsSource to ~100 items with distinct remote thumbnail URLs.
  2. Set SelectedItem to an item near the end (e.g. index 58) and call ScrollIntoView for it.
  3. Observe the list after the scroll: the last one or two tiles inside the viewport have no image. Resizing the window changes which tiles are affected.

Expected behavior

Every BitmapImage whose download completes successfully renders, regardless of what other BitmapImage instances for the same URL do.

Cause

BitmapImage.TryOpenSourceAsync (src/Uno.UI/UI/Xaml/Media/Imaging/BitmapImage.cs) keys an in-flight Task<ImageData> in _bitmapImageCache by (URI, decode size) and hands that same task to every BitmapImage that opens the same URL while it is running. The task is created with the cancellation token of the first requester (Task.Run(..., ct), and GetImageDataFromUriAsCompositionSurface(uri, ct) threads it through the stream fetch and the read), and the token's registration removes the cache entry when it fires.

ImageSource.Open() (ImageSource.crossruntime.cs) cancels the previous open whenever it is called again on the same instance (_opening.Disposable = Disposable.Create(cts.Cancel)), which happens on every UriSource change through InvalidateSource().

With the managed virtualizing panel this combination fails as follows:

  1. A container is realized for item N and its BitmapImage starts the download; the task is cached.
  2. The panel recycles that container (RecycleViewForItem). Pooled containers keep their DataContext, so the bitmap keeps downloading.
  3. Item N is realized again, through a different container popped from the pool (TryDequeueCachedContainer is LIFO, so it is often not the same one). Its BitmapImage hits the cache and awaits the first container's task.
  4. The first container is reused for item M: UriSource changes, InvalidateSource() → Open() cancels the first token. Inside the shared task, GetImageDataFromUriAsCompositionSurface catches the OperationCanceledException and returns ImageData.FromError(...), so the task completes with an error rather than cancelling.
  5. The second container receives that error: RaiseImageFailed, OnOpened(error), blank tile. The cache entry has been removed by the registration, so a later subscriber would download fresh, but nothing retries.

Steps 1 to 3 are precisely what ScrollIntoView does to the items at the viewport edge (materialize to fill, correct the estimate, unfill, refill), which is why the last visible tiles are the ones affected, and whether the download finishes before step 4 is what makes it intermittent.

Workaround

CreateOptions="IgnoreImageCache" on the BitmapImage in the template, so each container downloads independently.

Suggested fix

Either run the cached download on a token that is independent of any single requester (for example cancel it only when the last subscriber goes away, or never, since the entry is evicted by the LRU anyway), or when a requester's own token fires, detach it from the shared task instead of cancelling the task for everyone.

Environment

  • Uno.WinUI 6.8.0-dev.212, Uno.Sdk 6.8.0-dev.60, Uno.Wasm.Bootstrap 10.1.0-dev.219
  • .NET 10.0.401 SDK, runtime pack 10.0.12
  • Heads: net10.0-desktop (Windows 11, Skia) and net10.0-browserwasm (Skia renderer, Chromium-based browsers)

Activity

  1. added 2 commits that reference this issue on Oct 8, 2026
    a06c0a8
    b77bdb4
  2. added 2 commits that reference this issue on Nov 2, 2026
    aa284d7
    f667db9
  3. added 2 commits that reference this issue on Nov 3, 2026
    9804660
    998caa6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions