[Windows] Fix for Runtime error when closing external window with WPF Webview Control#34006
Draft
BagavathiPerumal wants to merge 1 commit intodotnet:mainfrom
Draft
[Windows] Fix for Runtime error when closing external window with WPF Webview Control#34006BagavathiPerumal wants to merge 1 commit intodotnet:mainfrom
BagavathiPerumal wants to merge 1 commit intodotnet:mainfrom
Conversation
…on preventing InvalidOperationException when CoreWebView2 is disposed during SendMessage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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
32944-BeforeFix.mp4
32944-AfterFix.mp4