Skip to content

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

Merged
ajpinedam merged 4 commits into
unoplatform:masterfrom
ajpinedam:dev/anpi/fix.wasm.webview.iframe.dispose
Sep 30, 2026
Merged

ajpinedam merged 4 commits into
unoplatform:masterfrom
ajpinedam:dev/anpi/fix.wasm.webview.iframe.dispose

Conversation

@ajpinedam

@ajpinedam ajpinedam 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

Copilot AI balanced review requested due to automatic review settings September 30, 2026 19:21
@github-actions github-actions Bot added the platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform label Sep 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The cleanup lifecycle is correctly scoped and covered by targeted runtime tests.

Review effort: Balanced
Findings: None

What changed in this PR

Removes orphaned WebView2 iframes after garbage collection while preserving them across unload/reload cycles.

Changes:

  • Adds opt-in DOM cleanup through a finalizer sentinel.
  • Enables cleanup for WebView2-created iframes.
  • Adds WebAssembly runtime coverage for collection and reload behavior.

Review: No issues identified.

File Description
BrowserHtmlElement.cs Implements deferred DOM removal.
Given_WebView2.uno.cs Tests collection and reload behavior.
BrowserWebViewProvider.cs Enables collection cleanup for iframes.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@unodevops

Copy link
Copy Markdown
Contributor

@nventive-devops

Copy link
Copy Markdown
Contributor

The build 236600 found UI Test snapshots differences: skia-linux-screenshots: 57, skia-windows-screenshots: 2304, wasm: 11

Details
  • skia-linux-screenshots: 57 changed over 2348

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • CalendarView_Theming.png-dark
    • CalendarView_Theming.png
    • ButtonClippingTestsControl.png-dark
    • ButtonClippingTestsControl.png
    • Buttons.png-dark
    • Buttons.png
    • ClipboardTests.png-dark
    • ContextRequested.png-dark
    • ContextRequested.png
    • ElementLevelTheme.png-dark
    • ElementLevelTheme.png
    • Examples.png-dark
    • Examples.png
    • ExpanderColorValidationPage.png-dark
    • ExpanderColorValidationPage.png
    • DropDownButtonPage.png-dark
    • DropDownButtonPage.png
    • Focus_FocusVisual_Properties.png-dark
    • ImageIconPage.png-dark
    • DisplayInformation.png-dark
  • skia-windows-screenshots: 2304 changed over 2502

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • AndroidWindowInsets.png
    • AnimatedIconPage.png
    • AppBarButtonTest.png-dark
    • AppBarToggleButtonTest.png-dark
    • AppBar_KeyBoard.png
    • AppWindowClosing.png
    • AppWindowPresenters.png-dark
    • ArcSegment.png
    • Attributed_text_Supserscript.png-dark
    • Attributed_text_Supserscript.png
    • AutomationProperties_AutomationId.png-dark
    • AutomationProperties_AutomationId.png
    • AutoSuggestBox_Icons.png-dark
    • AutoSuggestBox_Icons.png
    • Basics_Automated.png-dark
    • Basics_Automated.png
    • BeginTime_MultipleAnimations.png-dark
    • BeginTime_MultipleAnimations.png
    • BitmapIcon_Sizing.png-dark
    • BitmapIcon_Sizing.png
  • wasm: 11 changed over 1076

    🚨🚨 Comparison Details (first 20) 🚨🚨
    • UITests.Microsoft_UI_Xaml_Controls.WebView2Tests.WebView2_NavigationProperties
    • SamplesApp.Wasm.Windows_UI_Xaml_Controls.ListView.ListView_IsSelected
    • SamplesApp.Windows_UI_Xaml_Controls.ListView.ListViewSelectedItems
    • UITests.Shared.Microsoft_UI_Xaml_Controls.ExpanderTests.WinUIExpanderPage
    • SamplesApp.Microsoft_UI_Xaml_Controls.WebView2Tests.WebView2_EnableDevTools
    • UITests.Uno_Web.Http.CookieManagerTests
    • UITests.Shared.Windows_UI_Xaml_Controls.MediaPlayerElement.MediaPlayerElement_Minimal
    • UITests.Shared.Windows_UI_Xaml_Controls.MediaPlayerElement.MediaPlayerElement_Ogg_Extension
    • UITests.Windows_UI_Xaml_Media_Animation.ColorAnimation_Background
    • UITests.Windows_UI_Xaml_Controls.CalendarView.CalendarView_Theming
    • Uno.UI.Samples.Content.UITests.WebView.WebView_AnchorNavigation

@mergify

mergify Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

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 436dfd0 into unoplatform:master Sep 30, 2026
52 checks passed
@ajpinedam

Copy link
Copy Markdown
Contributor Author

@Mergifyio backport servicing/6.8

@mergify

mergify Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

backport servicing/6.8

✅ Backports have been created

Details

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants