Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebView in invalid state #125

Closed
klaykree opened this issue Feb 27, 2020 · 1 comment
Closed

WebView in invalid state #125

klaykree opened this issue Feb 27, 2020 · 1 comment

Comments

@klaykree
Copy link

I have a program which uses WebView2 to watch for changes to the DOM of a page.

The webview is set up to use the local app data folder for its user data folder (just in case this is relevant):

PWSTR AppDataLocal;
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &AppDataLocal);
WCHAR AppDataLocalDir[128];
wcscpy_s(AppDataLocalDir, AppDataLocal);
wcscat_s(AppDataLocalDir, 128, L"\\ChannelPointsDisplay");
CoTaskMemFree(AppDataLocal);
CreateCoreWebView2EnvironmentWithDetails(nullptr, AppDataLocalDir, nullptr...

In the CreateCoreWebView2Environment callback it uses AddScriptToExecuteOnDocumentCreated to set up a mutation observer.
Then a timer queue is used to run the following function at regular intervals to poll for mutations.

void MutationsTick()
{
    //Force the window to update otherwise the following script execute will miss some mutations
    ShowWindow(hwnd, SW_SHOW);
    UpdateWindow(hwnd);

    HRESULT result = WebView->ExecuteScript(
        L"var mutationString = null;\
                if(window.__webview_observers__['chat-list__lines'].mutations.length > 0) {\
                    mutationString = window.__webview_observers__['chat-list__lines'].mutations[0];\
                    window.__webview_observers__['chat-list__lines'].mutations.splice(0, 1);\
                }\
                mutationString;",
        Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
            [](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT {
                std::wstring Result = std::wstring(resultObjectAsJson);
                if(Result != L"null")
                {
                    const std::scoped_lock<std::mutex> lock(MutationsMutex);
                    Mutations.push(Result);
                }
                return S_OK;
            }).Get());
}

My issue is that very rarely ExecuteScript will return a ERROR_INVALID_STATE for its result, I believe the message for this error was "the group or resource is not in the correct state".
Once ExecuteScript returns that error then other WebView functions such as Reload and Navigate will return the same.

My question is: what could cause the WebView to enter an invalid state?

@david-risney
Copy link
Contributor

Once the WebView2 is closed, most methods return that error (doc). The WebView2 can enter that state via calling the Close method, when the last reference to the WebView2 is gone (implicit cleanup - shouldn't be happening here), or if one of the WebView2 processes crash. In the crashing case you should see the ProcessFailed event firing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants