Fix occasional WebView2 Crash when restoring minimized app (0x80070057) #6667
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.
If an app is minimized while WebView2 has focus, and is then restored, it occasionally crashes with 0x80070057/ERROR_INVALID_PARAMETER. In my local repro, i observe this on ~1 out of 20 tries. In such cases the Windows restore operation is initiated but for some reason not completed; the app remains minimized. Nonetheless Xaml internal focus handling proceeds to restore focus to WebVIew2, which in turn calls CoreWebView2Controller::MoveFocus to restore web element focus. The latter call fails in Win32 ::SetFocus() API w/ ERROR_INVALID_PARAMETER; this is expected since the target HWND remained minimized.
The fix is to ignore HR 0x80070057 in this situation. Since the app remained minimized in the repro case, there is no observable effect and no additional state cleanup is required. The next attempt to restore the app should restore focus correctly.