Accessing the V8 context from other threads

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Accessing the V8 context from other threads

Postby alextusinean » Tue Feb 23, 2021 11:38 am

I'm trying to modify a V8 value from my browser's main frame V8 context (by executing the UpdateLoadState function), but it fails: (from debug.log)
Code: Select all
[0223/182019.775:FATAL:cef_ref_counted.h(325)] Assert failed: ptr_ != 0.

Code: Select all
void UpdateLoadState(std::wstring text, int percent)
{
   auto obj = browser->GetMainFrame()->GetV8Context()->GetGlobal()->GetValue("app")->GetValue("loadState");
   if (text != L"") {
      obj->SetValue("text", CefV8Value::CreateString(text), V8_PROPERTY_ATTRIBUTE_NONE);
   }

   if (percent != -1) {
      obj->SetValue("percent", CefV8Value::CreateInt(percent), V8_PROPERTY_ATTRIBUTE_NONE);
   }

   CefV8ValueList arguments;
   obj->GetValue("update")->ExecuteFunction(NULL, arguments);
}

The assert fails only when UpdateLoadState is executed from a std::thread so I've looked over the Wiki and found out that all V8 execution must take place on the main thread.
My question is: How could I use V8 in other threads?
alextusinean
Techie
 
Posts: 17
Joined: Thu Jan 07, 2021 3:23 pm

Re: Accessing the V8 context from other threads

Postby magreenblatt » Tue Feb 23, 2021 12:22 pm

Calling into V8 from other threads is not supported.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Accessing the V8 context from other threads

Postby alextusinean » Tue Feb 23, 2021 12:29 pm

magreenblatt wrote:Calling into V8 from other threads is not supported.

I understand that, but what would be a way to do it? I've also tried using process messages but the same error occurs.
I'm not that experienced in C++ and CEF.
alextusinean
Techie
 
Posts: 17
Joined: Thu Jan 07, 2021 3:23 pm

Re: Accessing the V8 context from other threads

Postby magreenblatt » Tue Feb 23, 2021 12:50 pm

You can post a task to the TID_RENDERER thread, and call Enter/Exit on the CefV8Context object.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Accessing the V8 context from other threads

Postby alextusinean » Wed Feb 24, 2021 9:27 am

magreenblatt wrote:You can post a task to the TID_RENDERER thread, and call Enter/Exit on the CefV8Context object.

I've managed to do this:
Code: Select all
void Manager::_UpdateLoadState(std::wstring text, int percent)
{
   auto ctx = browser->GetMainFrame()->GetV8Context();
   ctx->Enter();
   
   auto loadState = browser->GetMainFrame()->GetV8Context()->GetGlobal()->GetValue("app")->GetValue("loadState");
   if (text != L"") {
      loadState->SetValue("text", CefV8Value::CreateString(text), V8_PROPERTY_ATTRIBUTE_NONE);
   }
   
   if (percent != -1) {
      loadState->SetValue("percent", CefV8Value::CreateInt(percent), V8_PROPERTY_ATTRIBUTE_NONE);
   }
   
   CefV8ValueList arguments;
   loadState->GetValue("update")->ExecuteFunction(NULL, arguments);
   
   ctx->Exit();
}

void Manager::UpdateLoadState(std::wstring text, int percent)
{
   if (!CefCurrentlyOn(TID_RENDERER))
      CefPostTask(TID_RENDERER, base::Bind(Manager::_UpdateLoadState, this, text, percent));
   else
      _UpdateLoadState(text, percent);
}

The things in V8 seems to get updated but after _UpdateLoadState runs, the browser freezes.
alextusinean
Techie
 
Posts: 17
Joined: Thu Jan 07, 2021 3:23 pm

Re: Accessing the V8 context from other threads

Postby magreenblatt » Wed Feb 24, 2021 9:42 am

What does your “update” JS function do?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Accessing the V8 context from other threads

Postby alextusinean » Wed Feb 24, 2021 10:10 am

magreenblatt wrote:What does your “update” JS function do?

The browser is "running" a ReactJS app and the update function changes some html elements to the loadState's text and percent values. It is defined like this in my component:
Code: Select all
app.loadState.update = () => {
  this.forceUpdate();
};

Thought, I don't think the browser freezes because of the update function's execution. Even if I leave _UpdateLoadState's body blank (the function that does the V8 context stuff), the browser still freezes.
alextusinean
Techie
 
Posts: 17
Joined: Thu Jan 07, 2021 3:23 pm

Re: Accessing the V8 context from other threads

Postby alextusinean » Thu Feb 25, 2021 10:50 am

bump?
alextusinean
Techie
 
Posts: 17
Joined: Thu Jan 07, 2021 3:23 pm

Re: Accessing the V8 context from other threads

Postby magreenblatt » Thu Feb 25, 2021 11:21 am

What, specifically, freezes? Do you mean the web content stops responding? What about the rest of the UI (windows, menus, etc)? Can you navigate to a new URL?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Accessing the V8 context from other threads

Postby alextusinean » Thu Feb 25, 2021 12:39 pm

magreenblatt wrote:What, specifically, freezes? Do you mean the web content stops responding? What about the rest of the UI (windows, menus, etc)? Can you navigate to a new URL?

I'm not sure what happened, but yes, the web content would stop responding and 1-2 seconds later, the browser would turn blank.
Now it seems to be fixed, not sure what I did :roll:. Thanks anyways.
alextusinean
Techie
 
Posts: 17
Joined: Thu Jan 07, 2021 3:23 pm

Next

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 107 guests

cron