MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
452 stars 55 forks source link

The registered host objects can't be reached sometimes. #2823

Closed yowu closed 1 year ago

yowu commented 2 years ago

Description We are moving our CefSharp based application to WebView2. It uses a html page as start page and allow user open their recent files from the system. When user clicks a button "Open model" in the html page, it calls an async function openModel of the registered host object to handle the action in native side.

We found a strange bug recently. Sometimes, the promises in JavaScript which call host objects function never be resolved. Need to start the application to work again.

See the attached screenshot, the host objects work fine at the beginning. When start from callid 25, any calls to host objects is never resolved. In native side, there are no calls reached too. We also can't get any error from devtools console and native debug output.

Thanks!

Version SDK: 1.0.1343.22 Runtime: 105.0.1343.50 Framework: WPF OS: Win 10 21H2, 19044.2006

Repro Steps I can't reproduce it in a sample file, but it could be consistently reproduced in our software.

Screenshots Screenshot 2022-09-25 220447

Additional context

yowu commented 2 years ago

More information found.

When the bug happens, the call embedded_browser::mojom::HostStubDispatch::Accept is never been reached anymore.

    EmbeddedBrowserWebView.dll!embedded_browser::mojom::HostStubDispatch::Accept(class embedded_browser::mojom::Host *,class mojo::Message *)   Unknown
    EmbeddedBrowserWebView.dll!base::trace_event::TraceEventMemoryOverhead::GetCount(enum base::trace_event::TraceEventMemoryOverhead::ObjectType)  Unknown
    EmbeddedBrowserWebView.dll!mojo::MessageDispatcher::Accept(class mojo::Message *)   Unknown
    EmbeddedBrowserWebView.dll!mojo::InterfaceEndpointClient::HandleIncomingMessage(class mojo::Message *)  Unknown
    EmbeddedBrowserWebView.dll!mojo::internal::MultiplexRouter::ProcessIncomingMessage()    Unknown
    EmbeddedBrowserWebView.dll!mojo::internal::MultiplexRouter::Accept(class mojo::Message *)   Unknown
    EmbeddedBrowserWebView.dll!mojo::MessageDispatcher::Accept(class mojo::Message *)   Unknown
    EmbeddedBrowserWebView.dll!base::internal::Invoker<base::internal::BindState<void (*)(const base::RepeatingCallback<void (unsigned int)> &, unsigned int, const mojo::HandleSignalsState &),base::RepeatingCallback<void (unsigned int)>>,void (unsigned int, const mojo::HandleSignalsState &)>::Run() Unknown
    EmbeddedBrowserWebView.dll!mojo::SimpleWatcher::OnHandleReady() Unknown
    EmbeddedBrowserWebView.dll!base::TaskAnnotator::RunTaskImpl(struct base::PendingTask &) Unknown
    EmbeddedBrowserWebView.dll!base::TaskAnnotator::RunTask<>() Unknown
    EmbeddedBrowserWebView.dll!embedded_browser_webview::internal::AppTaskRunner::DoWork()  Unknown
    EmbeddedBrowserWebView.dll!embedded_browser_webview::internal::AppTaskRunner::MessageCallback() Unknown
    EmbeddedBrowserWebView.dll!base::win::MessageWindow::WindowProc()   Unknown
    EmbeddedBrowserWebView.dll!base::win::WrappedWindowProc<&base::win::MessageWindow::WindowProc>()    Unknown
    user32.dll!UserCallWinProcCheckWow()    Unknown
    user32.dll!CallWindowProcW()    Unknown
    Microsoft.VisualStudio.Debugger.Runtime.Impl.dll!DetouredCallWindowProcW(__int64 (*)(struct HWND__ *,unsigned int,unsigned __int64,__int64),struct HWND__ *,unsigned int,unsigned __int64,__int64)  Unknown
    mfc140u.dll!_AfxActivationWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 478   C++
    user32.dll!UserCallWinProcCheckWow()    Unknown
    user32.dll!DispatchMessageWorker()  Unknown
    mfc140u.dll!AfxInternalPumpMessage() Line 183   C++
    mfc140u.dll!CWinThread::Run() Line 629  C++
    mfc140u.dll!AfxWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 61 C++
victorthoang commented 2 years ago

Hello @yowu

We will have this issue triaged to the right developer to take a look at the issue soon.

Thanks!

crushonme commented 1 year ago

Hi @plantree Do we have update about this issue?

novac42 commented 1 year ago

@yowu Thanks for reporting and our team are looking into the info you shared. Will keep you posted. @crushonme

xu-ms commented 1 year ago

@crushonme @yowu Hi, I use your software reproduce the issue. But I cannot debug our WebView use the .exe project. I do not know what happened when the software start pages no responding, call host object has complex steps, I need a source code to debug and find the error. Could you mock a demo project which can reproduce the same issue, maybe your host object to invoke some special method? You can try to mock them.

As your screenshot, you can try use a = await chrome.webview.hostObjects.sample (add await), Does the issue still exist?

xu-ms commented 1 year ago

WebView2 relies on a message pump of the UI thread to run event handler callbacks and async method completion callback. The message pump will get a message to run a task when user request a host object in JavaScript. In this scenario, the task will invoke the host object. If the messages aren't received by message pump, the host object can't be invoked. The message pump implements by a HWND, so special attention is required when you use window getMessage(), using dispatchMessage() to ensure webview2 HWND can get messages successful. In this issue, host app has a pop-up message box unexpected gets the messages which belong WebView2 message pump after clicking open model, and so WebView2 message pump cannot receive the messages. Therefore, the host object can't be invoked successful.

yowu commented 1 year ago

Thanks, @xu-ms!