Skip to content

fix(imaging): Hold image source subscribers weakly (6.8) (backport #25597) - #25958

Merged
agneszitte merged 3 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-25597
Nov 3, 2026
Merged

agneszitte merged 3 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-25597

Conversation

@mergify

@mergify mergify Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25596 (for 6.8)

PR Type:

🐞 Bugfix

What changed? 🚀

This PR targets servicing/6.8 directly. On master, #25124 (open) fixes the Image part of this leak: an Image subscribes to its source only while it's loaded. That PR is too large to backport, so this is a narrow fix for 6.8 that changes nothing about when sources open or decode, or when ImageOpened and ImageFailed are raised.

The leak:

  • The source: ImageSource kept its subscribers in a strong list.
  • Image: the Skia Image also subscribed to an SvgImageSource's SourceLoaded and OpenFailed events.
  • ImageBrush: it did the same with its subscription and with a handler on ImageSource.Invalidated.
  • The effect: all of these were only released when the consumer's source changed. So a source that outlives its consumers, such as an icon in a static field or in app resources, kept every Image and ImageBrush that ever used it alive, along with everything they reference. In a WebAssembly app, static SVG icon sources held 176 handlers this way, and about 30 MB of departed pages behind them.

The fix:

  • Weak subscribers: Image (Skia and native WebAssembly) and ImageBrush (crossruntime and native WebAssembly, through Shape) subscribe through a new SubscribeWeakly, which ImageSource holds only weakly. The disposable it returns keeps the callback alive for as long as the consumer holds it, and each of them stores it.
  • Strong by default: Subscribe still keeps its subscribers alive until they dispose, so a caller that doesn't hold the disposable keeps working. BitmapSource.SetSourceAsync and SvgImageSource.SetSourceAsync hold theirs only in an async local, so they use it.
  • Opens in progress: an open keeps the subscribers it starts with alive until it completes, as one may be waiting for it with nothing else referencing it, such as a brush whose ImageOpened an app awaits. No test covers this case.
  • Forwarders: the SVG events (Image.skia.cs) and Invalidated (ImageBrush.cs) go through small forwarders that hold the consumer weakly. Each forwarder unsubscribes itself the next time the source raises that event after its consumer is gone.

Not covered here:

Tests

These are Skia runtime tests.

  • Given_Image.When_Source_Outlives_Images (bitmap and SVG): two Images sharing a source the test keeps alive are collected once removed from the tree.
  • Given_ImageBrush.When_Source_Outlives_Brushes: two Borders painted by ImageBrushes over a shared source are collected once removed.
  • Given_Image.When_Source_Updated_After_GC (bitmap and SVG): an un-awaited SetSourceAsync completes despite a collection, and a live Image still receives its source's next update after several collections. This guards the weak references against dropping live subscribers.

On vanilla 6.8, the three collection tests fail and the two update tests pass. With this PR, all five pass, five times in a row. The update tests time out if Subscribe holds its subscribers only weakly and the open doesn't keep them alive.

Related classes with this PR: Given_Image, Given_ImageBrush, Given_BitmapSource, Given_ImageSource, Given_WriteableBitmap, Given_RenderTargetBitmap and Given_BitmapIcon.

  • Passed: 77 of 79.
  • When_WriteableBitmap_Assigned_With_Data_Present: fails with the same message on vanilla 6.8 on this machine.
  • When_Render_Border_GetPixelsAsync: inconclusive, as it is on vanilla.

Uno.UI builds cleanly for Skia, native WebAssembly and Reference, with analyzers on.

PR Checklist ✅


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

Show a source kept alive by the test, like an icon in a static field or
in the application resources, in two Images and in two Borders painted
by ImageBrushes, then remove them and check that they are collected.
This is done for a BitmapImage and an SvgImageSource.

Also check that an Image keeps receiving the updates of its source
after collections, including from a SetSourceAsync that nothing but the
source references while it runs.

The collection tests fail until the next commit.

(cherry picked from commit 66842eb)
An ImageSource kept its subscribers in a strong list, and the Skia
Image subscribed to the SourceLoaded and OpenFailed events of an
SvgImageSource. ImageBrush did the same with its subscription and with
a handler on ImageSource.Invalidated. All of these were only released
when the consumer's source changed, so a source that outlives its
consumers, such as an icon in a static field or in the application
resources, kept every Image and ImageBrush that ever used it alive,
along with everything they reference.

Hold the subscribers weakly instead: the disposable returned by
Subscribe keeps the callback alive for as long as the consumer holds
it, and an open in progress keeps the current subscribers alive until
it completes, as a SetSourceAsync awaiting it may be referenced by
nothing else. The SVG events and Invalidated are forwarded through
handlers that hold the consumer weakly and unsubscribe once it is gone.
When sources open and when ImageOpened and ImageFailed are raised is
unchanged.

This is a narrow servicing fix: on master, Image subscribing only while
loaded (#25124) fixes this as part of a much larger change. A source
used by a single element still references it as its inheritance
context parent until another element uses it, which is addressed
separately (#25102) and not covered here.

Fixes #25596

(cherry picked from commit 4c103e4)
Subscribe holds its subscribers again, so a caller that doesn't keep
the returned disposable keeps being notified. Image and ImageBrush,
which a long-lived source must not keep alive, subscribe through the
new SubscribeWeakly instead. BitmapSource.SetSourceAsync and
SvgImageSource.SetSourceAsync hold their subscription only in an async
local, so they now rely on Subscribe holding it rather than on the open
keeping its subscribers alive.

Subscribe and InvalidateSource only need the number of subscribers, so
they now count them without building a list.

Refs #25596

(cherry picked from commit 3cc4c1e)
@github-actions github-actions Bot added platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform area/skia ✏️ Categorizes an issue or PR as relevant to Skia labels Nov 3, 2026
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@agneszitte
agneszitte merged commit dabc980 into release/stable/6.7 Nov 3, 2026
39 of 46 checks passed
@agneszitte
agneszitte deleted the mergify/bp/release/stable/6.7/pr-25597 branch November 3, 2026 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/skia ✏️ Categorizes an issue or PR as relevant to Skia platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants