Skip to content

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

Merged
ajpinedam merged 5 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-24841
Oct 1, 2026
Merged

ajpinedam merged 5 commits into
servicing/6.8from
mergify/bp/servicing/6.8/pr-24841

Conversation

@mergify

@mergify mergify Bot commented Sep 30, 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).

(cherry picked from commit b17a85a)

# Conflicts:
#	src/Uno.UI/NativeElementHosting/BrowserHtmlElement.cs
@mergify mergify Bot added the conflicts Automated merge hit conflicts label Sep 30, 2026
@mergify

mergify Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Cherry-pick of b17a85a has failed:

On branch mergify/bp/servicing/6.8/pr-24841
Your branch is up to date with 'origin/servicing/6.8'.

You are currently cherry-picking commit b17a85a.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   src/Uno.UI.Runtime.Skia.WebAssembly.Browser/UI/Xaml/Controls/WebView/BrowserWebViewProvider.cs

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/Uno.UI/NativeElementHosting/BrowserHtmlElement.cs

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@github-actions github-actions Bot added the platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform label Sep 30, 2026
@github-actions github-actions Bot added the area/skia ✏️ Categorizes an issue or PR as relevant to Skia label Oct 1, 2026
@unodevops

Copy link
Copy Markdown
Contributor

@ajpinedam

Copy link
Copy Markdown
Contributor

Resolved the cherry-pick conflict in 7987a9d.

The first commit conflicted in src/Uno.UI/NativeElementHosting/BrowserHtmlElement.cs, and Mergify committed the file with conflict markers, which broke the builds. On servicing/6.8, BrowserHtmlElement is split across .cs, .skia.cs, .wasm.cs and .reference.cs, and DisposeNative and the JS interop live in BrowserHtmlElement.skia.cs. So:

  • BrowserHtmlElement.cs is restored to its 6.8 version.
  • The change from fix(wasm): Remove WebView2 iframe once it is collected #24841 is applied to BrowserHtmlElement.skia.cs, including the follow-up hardening commit: RemoveFromDomWhenCollected(), the finalizable DomRemovalOnCollect sentinel, and the Cancel() call in DisposeNative.
  • BrowserWebViewProvider.cs and the tests applied cleanly and are unchanged. The tests landed in Tests/Windows_UI_Xaml_Controls/Given_WebView2.cs.

Against servicing/6.8, the PR now changes three files, with 163 lines added and none removed.

Validated locally on this branch: the 6.8 SamplesApp.Skia.WebAssembly.Browser builds in Release, and Given_WebView2.When_Collected_Then_Native_Iframe_Is_Removed and When_Unloaded_While_Referenced_Then_Native_Iframe_Is_Kept pass in headless Chrome.

The same three files are identical on release/stable/6.7, so the 6.7 backport will need the same resolution.

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 236668 has failed on Uno.UI - CI.

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 236668 did not find any UI Test snapshots differences.

@mergify

mergify Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor Author

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@ajpinedam
ajpinedam merged commit 1f29a9e into servicing/6.8 Oct 1, 2026
43 of 44 checks passed
@ajpinedam
ajpinedam deleted the mergify/bp/servicing/6.8/pr-24841 branch October 1, 2026 22:52
@agneszitte

Copy link
Copy Markdown
Member

@Mergifyio backport release/stable/6.7

@mergify

mergify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

backport release/stable/6.7

✅ Backports have been created

Details

agneszitte added a commit that referenced this pull request Oct 28, 2026
…6.7/pr-24842

fix(wasm): Remove WebView2 iframe once it is collected (backport #24841) (backport #24842)
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 conflicts Automated merge hit conflicts 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.

4 participants