Skip to content

[Windows] Fix for Runtime error when closing external window with WPF Webview Control#34006

Draft
BagavathiPerumal wants to merge 1 commit intodotnet:mainfrom
BagavathiPerumal:fix-32944
Draft

[Windows] Fix for Runtime error when closing external window with WPF Webview Control#34006
BagavathiPerumal wants to merge 1 commit intodotnet:mainfrom
BagavathiPerumal:fix-32944

Conversation

@BagavathiPerumal
Copy link
Contributor

@BagavathiPerumal BagavathiPerumal commented Feb 12, 2026

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Root cause

The issue occurs due to a race condition in the WPF BlazorWebView during window closure. When a Blazor component sends messages to the WebView, those messages are queued on the WPF dispatcher thread. If the window is closed while messages are still pending, WPF begins disposing of the WebView2CompositionControl along with its underlying CoreWebView2 COM object.

Once disposal starts, the queued messages may still execute. When they attempt to call PostWebMessageAsString(), the method tries to access an already disposed COM object, which results in an InvalidOperationException.

Regression PR: #31777

Description of Issue Fix

The fix introduces a three-layer defense mechanism to safely handle the disposal race condition.
The first layer adds a disposal flag that is checked before sending any message. If disposal has already started, the method exits immediately to prevent further operations.

The second layer wraps the PostWebMessageAsString() call in a try-catch block to safely handle cases where the COM object is disposed externally before the disposal flag is updated.

The third layer sets the disposal flag when an exception is caught. This creates a self-healing behavior that prevents repeated attempts after disposal is detected.

The solution ensures safe execution during both normal disposal and race scenarios while maintaining minimal performance overhead.

Tested the behavior in the following platforms.

  • Windows
  • Mac
  • iOS
  • Android

Testcase: Unable to add a test case for this scenario due to the reported issue with the WPF Blazor WebView.

Issues Fixed

Fixes #32944

Output

Before Issue Fix After Issue Fix
32944-BeforeFix.mp4
32944-AfterFix.mp4

…on preventing InvalidOperationException when CoreWebView2 is disposed during SendMessage.
@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Runtime error when closing external window with WPF Webview Control

2 participants