Skip to content

fix(wasm): Remove WebView2 iframe once it is collected (backport #24841) (backport #24842) - #25780

Merged
agneszitte merged 5 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-24842
Oct 28, 2026
Merged

agneszitte merged 5 commits into
release/stable/6.7from
mergify/bp/release/stable/6.7/pr-24842

Conversation

@mergify

@mergify mergify Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: related to unoplatform/kahua-private#547

PR Type:

🐞 Bugfix

What changed? 🚀

On Skia WebAssembly, every WebView2 leaves its <iframe> in the DOM for the rest of the session. BrowserWebViewProvider creates the iframe with BrowserHtmlElement.CreateHtmlElement("iframe"), nothing ever disposes it, and native-element hosting only hides elements when they are detached. The hidden iframes keep their loaded documents alive. After a 700-page SamplesApp walk, 31 hidden iframes held about 510 MiB. Chrome keeps cross-site iframes in separate renderer processes; Safari keeps them in the tab's own process.

This PR removes the iframe once its WebView2 is garbage collected, and keeps it across unload and reload while the control is still referenced. That matches WinUI (controls/dev/WebView2/WebView2.cpp): OnUnloaded only hides and disconnects the browser, and only Close() or the destructor close it. It also follows the Skia macOS convention, where ~MacOSNativeElement releases the native peer.

  • BrowserHtmlElement gets an internal RemoveFromDomWhenCollected(). A private sentinel, referenced only by the element, carries the finalizer; it removes the element on the UI thread at idle priority. An explicit Dispose() cancels it.
  • The public type gets no finalizer, so elements created by application code keep their explicit Dispose() contract.
  • BrowserWebViewProvider opts in the iframe it creates.

There are no public API changes.

Validation

  • Runtime tests (Skia WebAssembly, headless Chrome): Given_WebView2.When_Collected_Then_Native_Iframe_Is_Removed and When_Unloaded_While_Referenced_Then_Native_Iframe_Is_Kept pass. With only the provider's opt-in line removed, the collection test fails at the DOM check while the WebView2 itself is collected. The reload test passes either way; it guards against removing the iframe too early and checks that the navigated document survives.
  • SamplesApp page walk (700 pages, same change ported to 6.7): hidden iframes at page 700 went from 31 to 0, and the memory of the processes hosting them from 659 MiB to 138 to 154 MiB. With only natural collections, iframes stay until the next full collection, which took up to about 70 pages in that walk.
  • Build: Uno.UI, Uno.UI.Runtime.Skia.WebAssembly.Browser and Uno.UI.RuntimeTests.Skia compile for net10.0 with analyzers, code-style enforcement and warnings as errors. net11.0 was not built locally.

Not in this PR

  • WebView2.Close() is still a generated [NotImplemented] stub, so release depends on garbage collection. Implementing it would give apps a deterministic release.
  • MediaPlayerElement releases its <video> element from its own finalizer, directly instead of on the UI thread. It could move to the same mechanism.

PR Checklist ✅

🤖 Generated with Claude Code


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

(cherry picked from commit b17a85a)

# Conflicts:
#	src/Uno.UI/NativeElementHosting/BrowserHtmlElement.cs
(cherry picked from commit 19bd61e)
(cherry picked from commit ff3557e)
(cherry picked from commit 3ff1c62)
(cherry picked from commit 41b5171)
(cherry picked from commit f71783a)
(cherry picked from commit dadbc48)
(cherry picked from commit 4899ae5)
@github-actions github-actions Bot added the area/skia ✏️ Categorizes an issue or PR as relevant to Skia label Oct 28, 2026
@agneszitte
agneszitte merged commit af96506 into release/stable/6.7 Oct 28, 2026
12 of 17 checks passed
@agneszitte
agneszitte deleted the mergify/bp/release/stable/6.7/pr-24842 branch October 28, 2026 01:54
agneszitte added a commit that referenced this pull request Oct 28, 2026
They read the browser DOM through WasmSemanticDomHelper and
BrowserHtmlElement, which the native heads don't build, so native Android
failed with CS0234 since #25780. Same change as on servicing/6.8 (#25552).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants