Repository navigation
fix(imaging): Hold image source subscribers weakly (6.8) (backport #25597) - #25958
Merged
agneszitte merged 3 commits intoNov 3, 2026
Merged
Conversation
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)
2 of 5 tasks
Contributor
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
agneszitte
approved these changes
Nov 3, 2026
1 of 16 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GitHub Issue: closes #25596 (for 6.8)
PR Type:
🐞 Bugfix
What changed? 🚀
This PR targets
servicing/6.8directly. On master, #25124 (open) fixes theImagepart of this leak: anImagesubscribes 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 whenImageOpenedandImageFailedare raised.The leak:
ImageSourcekept its subscribers in a strong list.Image: the SkiaImagealso subscribed to anSvgImageSource'sSourceLoadedandOpenFailedevents.ImageBrush: it did the same with its subscription and with a handler onImageSource.Invalidated.ImageandImageBrushthat 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:
Image(Skia and native WebAssembly) andImageBrush(crossruntime and native WebAssembly, throughShape) subscribe through a newSubscribeWeakly, whichImageSourceholds only weakly. The disposable it returns keeps the callback alive for as long as the consumer holds it, and each of them stores it.Subscribestill keeps its subscribers alive until they dispose, so a caller that doesn't hold the disposable keeps working.BitmapSource.SetSourceAsyncandSvgImageSource.SetSourceAsynchold theirs only in an async local, so they use it.ImageOpenedan app awaits. No test covers this case.Image.skia.cs) andInvalidated(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:
_associatedParent) until a second user appears. So a source used by one element at a time still keeps that element alive. fix: Stop shared dependency objects retaining discarded elements and their DataContext #25102 (open) addresses this for master, and the tests here use two consumers to stay independent of it.ImageorImageBrushleaves its forwarder subscribed to a long-lived source until the source next raises that event. A staticBitmapImageorSvgImageSourceraisesInvalidatedonly when itsUriSourcechanges, andSourceLoadedorOpenFailedonly when it reloads. The forwarder no longer holds the consumer, but on a static icon they add up. The fixes proposed for master avoid them (fix(imaging): Release a decoded image when nothing displays it #25124 and fix(imaging): Let a long-lived source drop the brushes it painted #25633, both open): anImagelistens only while it's loaded, and anImageBrushregisters throughWeakEventHelper. This PR doesn't useWeakEventHelper, because it keeps the first subscriber of each event alive (WeakEventHelper keeps the first subscriber of each weak event alive for as long as the event source lives #25590), and the fix for that (fix(weakevents): Don't keep the first subscriber of a weak event alive #25591) isn't on 6.8.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-awaitedSetSourceAsynccompletes 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
Subscribeholds 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_RenderTargetBitmapandGiven_BitmapIcon.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.UIbuilds cleanly for Skia, native WebAssembly and Reference, with analyzers on.PR Checklist ✅
Screenshots Compare Test Runresults.This is an automatic backport of pull request #25597 done by [Mergify](https://mergify.com).