Skip to content

WebView2.Close() does nothing on WebAssembly, so closed web views keep their iframes until garbage collection #25736

Open

Description

@mikernet

Current behavior 🐛

WebView2.Close() is the generated not-implemented stub on all Skia targets: it reports itself as not implemented and does nothing else.

On WebAssembly, a WebView2 shows its content in an <iframe>. Since #24841, that iframe is removed from the page once the WebView2 is garbage collected. Until then, it stays in the page (hidden once the control is unloaded) with its document still loaded, so the document's scripts, timers and memory stay alive too. Chrome keeps a cross-site document in a renderer process of its own. A WebView2 that is still referenced somewhere keeps its iframe for good. An app has no way to release a web view when it's done with it.

We hit this in a WebAssembly app that opens and closes web views in dialogs: it kept their frames in the page until garbage collection.

The native web view on WebAssembly (NativeWebView.wasm.cs) has a Dispose() with a // Todo call this and reattach if needed comment. Nothing calls it, and it only unregisters the instance from a static dictionary.

What a closed WebView2 does on master today, compared with WinUI:

After webView.Close() Uno on WebAssembly today WinUI
The browser the iframe stays in the page until the WebView2 is collected closed right away
CoreWebView2 still the same object null
EnsureCoreWebView2Async() completes fails with RO_E_CLOSED (ObjectDisposedException in .NET)
Setting Source navigates throws RO_E_CLOSED
NavigateToString, Reload, ExecuteScriptAsync work throw E_ILLEGAL_METHOD_CALL (InvalidOperationException)
GoBack, GoForward work do nothing
CanGoBack, CanGoForward unchanged false
NavigationStarting, NavigationCompleted still raised no longer raised

The WinUI column comes from WebView2::Close and CloseInternal in controls/dev/WebView2/WebView2.cpp (microsoft-ui-xaml) and from the documentation of CoreWebView2Controller.Close: "After running Close, all methods fail and event handlers stop running."

Expected behavior 🎯

As in WinUI, Close() releases the browser right away. On WebAssembly, that means removing the iframe from the page. Afterwards, the WebView2 behaves as the WinUI column above.

How to reproduce it (as minimally and precisely as possible) 🔬

In a WebAssembly app:

var webView = new WebView2 { Width = 400, Height = 300 };
var dialog = new ContentDialog { Content = webView, CloseButtonText = "Close", XamlRoot = XamlRoot };
dialog.Closed += (s, e) => webView.Close();

webView.Source = new Uri("https://platform.uno/");
await dialog.ShowAsync();

Close the dialog, then count the frames in the browser console with document.querySelectorAll('iframe').length. The closed web view's iframe is still there, and each dialog opened this way adds one until the garbage collector runs.

Workaround 🛠️

Drop every reference to the WebView2 once it's closed, so the garbage collector removes its iframe (#24841). For a deterministic release, show the content in an iframe of your own: create it with BrowserHtmlElement.CreateHtmlElement("iframe"), use it as the Content of a ContentControl, and Dispose() it when you're done.

Affected platforms 📱💻🖥️

WebAssembly

Uno.Sdk version (and other relevant versions) 📦

master at b40e235 (Uno 7.0 dev), and Uno.Sdk 6.8.0-dev.66 (Uno.WinUI 6.8.0-dev.222).

IDE version 🧑‍💻

Not IDE-related.

Anything else we need to know? 💬

#24841 listed this as a follow-up: "WebView2.Close() is still a generated [NotImplemented] stub, so release depends on garbage collection. Implementing it would give apps a deterministic release."

Close() isn't implemented on the other Skia targets either (Win32, macOS, X11, and Skia on Android and iOS). Their native web views have no close path yet, and each one needs its own native teardown, so this issue is about WebAssembly.

The open #23754 implements Close() as part of a larger change. On WebAssembly, it navigates the iframe to about:blank and leaves it in the page until garbage collection.

I have a fix with runtime tests ready and will open a PR.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions