CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

Error while fulfilling of JS: Request BrowserId : 1 not found it's likely the browser is already closed

Open maksimrudn opened this issue 1 year ago • 35 comments

Is there an existing issue for this?

  • [X] I have searched both open/closed issues, no issue already exists.

CefSharp Version

117.2.40

Operating System

Windows 10

Architecture

x64

.Net Version

.net 6.0

Implementation

OffScreen

Reproduction Steps

Version: Above 108

Steps: Create instance of CefSharp.OffScreen.ChromiumWebBrowser br = CefSharp.OffScreen.ChromiumWebBrowser();

Go by url 2 times: br.Load(url) await br.EvaluateScriptAsPromiseAsync(script); br.Load(url) await br.GetSourceAsync(); br.Load(url)

Fulfill JS _browser.EvaluateScriptAsPromiseAsync(script)

Expected behavior

Fulfilling of JS

Actual behavior

JS Error Request BrowserId : 1 not found it's likely the browser is already closed

Regression?

After regression to version 108 problem dissapeared.

Known Workarounds

Likely problem is here https://github.com/cefsharp/CefSharp/blob/master/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp Look at history..

Does this problem also occur in the CEF Sample Application

Yes using WPF/OffScreen command line args

Other information

No response

maksimrudn avatar Nov 01 '23 13:11 maksimrudn

Reproduction Steps

Version: Above 108

Please create a Unit test that reliably reproduces the problem and submit a PR.

https://github.com/cefsharp/CefSharp/tree/master/CefSharp.Test

Your code is incomplete and not enough for me to investigate this.

Likely problem is here https://github.com/cefsharp/CefSharp/blob/master/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp Look at history..

What is the problem exactly? Please provide details of your analysis.

Just saying look at history is meaningless. If you can see a specific error that needs fixing then submit a PR.

Likely this is the result of upstream changes to CEF/Chromium.

amaitland avatar Nov 01 '23 20:11 amaitland

Reproduction Steps

Version: Above 108

Please create a Unit test that reliably reproduces the problem and submit a PR.

https://github.com/cefsharp/CefSharp/tree/master/CefSharp.Test

Your code is incomplete and not enough for me to investigate this.

Likely problem is here https://github.com/cefsharp/CefSharp/blob/master/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp Look at history..

What is the problem exactly? Please provide details of your analysis.

Just saying look at history is meaningless. If you can see a specific error that needs fixing then submit a PR.

Likely this is the result of upstream changes to CEF/Chromium.

bro, are you interesting in solution of error of your project? If you have concrete questions - ask me. I don't see any concrete questions. Looks like you just don't want to solve the problem.

Exactly, problem is in Error while fulfilling JS. You see last call of _browser.EvaluateScriptAsPromiseAsync(script) entails error.

maksimrudn avatar Nov 02 '23 09:11 maksimrudn

Likely problem is here https://github.com/cefsharp/CefSharp/blob/master/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp Look at history..

What is the problem exactly? Please provide details of your analysis.

I don't see a problem, so I need more information.

You see last call of _browser.EvaluateScriptAsPromiseAsync(script) entails error.

I need a reliable means to reproduce the problem. The best way to achieve that is to provide a failing test case.

bro, are you interesting in solution of error of your project?

There's a high change this is a change in CEF/Chromium and will need to be addressed upstream.

I don't see any concrete questions. Looks like you just don't want to solve the problem.

I'm very busy. If you want my assistance then you need to provide sufficient information. Just saying look at history is entirely pointless. Link to the exact commit that you think introduced the problem and provide an analysis of where you think the problem is.

amaitland avatar Nov 02 '23 20:11 amaitland

https://github.com/cefsharp/CefSharp/blob/bb30e237951c50707f9c75f89ccfb1c4dbadeff7/CefSharp.Test/Javascript/EvaluateScriptAsyncGenericTests.cs#L137

There are a number of unit tests that could be used as a base for reproducing this issue. Above is one such example.

If someone can create a test that reliability reproduces the issue, please create a pull request. Then I can have a look to see what's going on.

amaitland avatar Nov 09 '23 00:11 amaitland

This is still an issue as of 119.4.30 Core.

Once a web page has been loaded, and any automated action instigated on that page, for example clicking on a link, clicking a submit button, in fact ANYTHING that causes a new page to load in it's place, then any subsequent EvaluateScriptAsPromiseAsync or EvaluateScriptAsync executed on the new page will fail with the error message "Request BrowserId : 1 not found it's likely the browser is already closed".

As others have pointed out, the issue seems to be here:

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.BrowserSubprocess.Core/CefAppUnmanagedWrapper.cpp

function OnProcessMessageReceived

auto browserWrapper = FindBrowserWrapper(browser->GetIdentifier());

is returning a nullptr, which triggers the error message.

But if you prefer to just respond with snarky comments or complain that no one will make a unit test for you, when the issue has been clearly described by multiple users, then honestly that's a you problem.

I'd imagine most people just pull packages via NuGet, and when they identify a problem and report it, it's on you as the maintainer to address it. No one else is obliged to make unit tests for you, or delve into the inner workings of C# wrappers to a C++ interface to a 3rd party chromium library.

Until then, your project will remain fundamentally broken.

daveime68 avatar Dec 14 '23 23:12 daveime68

It seems that happening with only some Websites. I enabled the CEF logging, maybe can you find here something.

cef.log

ficho-hun avatar Jan 09 '24 14:01 ficho-hun

good day @amaitland I did https://github.com/netmaxik/CefSharp.MinimalExample.netcore special. Use WPF project and press Test button. On title you will see Elements under mouse. Press Test again to stop. After you go to : https://accounts.google.com/?hl=en, wait to load and press test again you see that error appears . For me last version where it was work correctly was : 112.3.0 After that i got that error everywhere.

netmaxik avatar Feb 01 '24 01:02 netmaxik

@netmaxik have you tried the new v121.3.x ? Has the issue been resolved @amaitland ? I'm experiencing the same problem @netmaxik is, basically anything related to Google (google drive, docs, gmail, accounts, etc...) is broken on all versions past 112.3.0. Haven't tested v121.3.x though.

nebojsasandin avatar Feb 23 '24 14:02 nebojsasandin

There's a high change this is a change in CEF/Chromium and will need to be addressed upstream.

As above.

If someone can create a test that reliability reproduces the issue, please create a pull request. Then I can have a look to see what's going on.

When someone creates a unit test then I'll have another look.

Your asking for my time. I'm asking for for the community to contribute their time. It's a simple ask.


MAKE SURE you are waiting for the page to load correctly before executing javascript.

br.Load(url) await br.EvaluateScriptAsPromiseAsync(script);

Calling Load then immediately evaluating javascript isn't reliable. You never know which V8 context the javascript will be executed in. If you perform a cross origin navigation then it's likely even more problematic.

await br.LoadAsync(url) await br.EvaluateScriptAsPromiseAsync(script);

Waits for the page to load.


You can try disabling site isolation trivls to see if the behaviour changes.

settings.CefCommandLineArgs.Add("disable-site-isolation-trials");

amaitland avatar Feb 24 '24 01:02 amaitland

@amaitland Thank you for your reply.

"disable-site-isolation-trials" did not help.

Here's the simplest test I could compile: https://github.com/nebojsasandin/ceftest

And it isn't much more complicated than few lines, so it can even fit into this comment here:

    using (var browser = new ChromiumWebBrowser("https://www.google.com/"))
    {
        browser.WaitForInitialLoadAsync().Wait();
        browser.Load("https://accounts.google.com/");
        browser.WaitForNavigationAsync().Wait();
        var rez = browser.GetMainFrame().EvaluateScriptAsync<string>("(function() {  return document.querySelector(\"button[aria-haspopup='menu']\").innerText; })();").Result;
    }

It is expected from this script to return "Create Account" text from the querySelector button, but what happens is the crash with the Request BrowserId : 1 not found it's likely the browser is already closed inner exception.

If the original url is: about:blank, https://accounts.google.com, https://www.yahoo.com, https://www.bing.com, etc.... it all works fine. The point where it breaks is if the original url is: https://www.google.com, and then it navigates to https://accounts.google.com/ This is why it was so hard to catch it. I haven't found another instance of this problem. Only when Google is being used. Also, Script Execution, without any return values, works just fine, and produces no exceptions.

nebojsasandin avatar Feb 29 '24 16:02 nebojsasandin

@nebojsasandin Thanks. You can create a test which mimics your example in https://github.com/cefsharp/CefSharp/blob/bb30e237951c50707f9c75f89ccfb1c4dbadeff7/CefSharp.Test/OffScreen/OffScreenBrowserTests.cs#L31

Please create a unit test inside the CefSharp.Test project and submit a PR.


If the original url is: about:blank, https://accounts.google.com, https://www.yahoo.com, https://www.bing.com, etc.... it all works fine. The point where it breaks is if the original url is: https://www.google.com, and then it navigates to https://accounts.google.com/ This is why it was so hard to catch it. I haven't found another instance of this problem. Only when Google is being used. Also, Script Execution, without any return values, works just fine, and produces no exceptions.

What you are describing sounds like an upstream issue.


You are using the async API in a sync fashion which is not supported.

There's an async example available at https://github.com/cefsharp/CefSharp.MinimalExample/blob/master/CefSharp.MinimalExample.OffScreen/Program.cs#L48 which you can use as a reference.

amaitland avatar Mar 02 '24 01:03 amaitland

Not sure if this was what I was supposed to do (I've never done this before), but I forked the entire master branch, changed the offscreen test cs file, included the part which causes an exception, and initialized pull request on this one single file from my fork: https://github.com/cefsharp/CefSharp/pull/4741

I could not get it to compile or run though, there was a whole bunch of errors in the C++ sources which I'm unfamiliar with, so I could not see if the test truly replicates the issue described here, but I did test it in async fashion as well in the original test I sent you, and it did produce the same exception, so hopefully you'll be able to replicate the issue on your end and see what's causing it in the first place, so we can get this resolved.

Thank you.

nebojsasandin avatar Mar 03 '24 13:03 nebojsasandin

initialized pull request on this one single file from my fork: #4741

For the example provided by @nebojsasandin you can forcibly isolate the origins and the script executes correctly.

settings.CefCommandLineArgs.Add("isolate-origins", "https://accounts.google.com,https://chrome.google.com,https://chromewebstore.google.com");

amaitland avatar Mar 24 '24 05:03 amaitland

Loading chrome://process-internals/#site-isolation in Chrome I see

image

In the CEF Sample Application (cefclient) it looks like

image

So CEF doesn't have the same google urls isolated like Chrome does.

You can add the following command line arg to mimic the Chrome behaviour.

settings.CefCommandLineArgs.Add("isolate-origins", "https://accounts.google.com,https://chrome.google.com,https://chromewebstore.google.com");

amaitland avatar Mar 24 '24 06:03 amaitland

I've created https://github.com/chromiumembedded/cef/issues/3672 to see if CEF can set the built-in isolations by default.

Until then you can use the workaround I've listed above.

It seems that happening with only some Websites.

@ficho-hun Did you observe the issue on websites other than the google ones?

After you go to : https://accounts.google.com/?hl=en,

@netmaxik If you are still experiencing this issue then please test with the workaround provided in https://github.com/cefsharp/CefSharp/issues/4621#issuecomment-2016708655

amaitland avatar Mar 24 '24 06:03 amaitland

@amaitland I did test with : settings.CefCommandLineArgs.Add("isolate-origins", "https://accounts.google.com,https://chrome.google.com,https://chromewebstore.google.com");.

What I found out is :

  1. If I start application - press Test button - then press Sign In or went to https://accounts.google.com/?hl=en, - I got problem. same error. BUT
  2. If I start application - DO not press Test and press Sign In to google or https://accounts.google.com/?hl=en - then press test - its working normally.

netmaxik avatar Mar 25 '24 01:03 netmaxik

@netmaxik If you create a failing Unit test and create a PR then I will have a look. You can use https://github.com/cefsharp/CefSharp/pull/4741 as a reference for creating a test.

amaitland avatar Mar 25 '24 20:03 amaitland

@netmaxik Have you tried adding more domains to the list? In my use-case I had to add few extra domains: https://accounts.google.com,https://chrome.google.com,https://chromewebstore.google.com,https://reportcontent.google.com,https://support.google.com,https://www.google.com It would be good if we could do: *.google.com to cover all of google's sub-domains, but it appears that "isolate-origins" setting is not that versatile. @amaitland , it seems that the CEF staff don't recognize this as a bug, or anything that needs to be changed, however, this does break the functionality of the CefSharp. Is there a more permanent or future-proof solution other than knowing which domains are breaking this entire facility? What if there is another domain (other than Google) which also breaks JavaScript in CefSharp, but in order to capture it - one would have to know the domain name.

nebojsasandin avatar Mar 26 '24 15:03 nebojsasandin

@nebojsasandin I did most off them.. nothing help. I do not know how to create test units... so do not know my next steps. But issue still continue :(.

@amaitland Im sorry I cannot create Test Units. I did not know how to do that. :( And now really do not have time to learn and understand that :( really sorry..

netmaxik avatar Mar 27 '24 02:03 netmaxik

@netmaxik , when I test your code based on the instructions you provided (click on Test button, then navigate to Sign In, it still works when I include "https://www.google.com" to the isolate-origins).

[EDIT] - turns out you can do this: https://[*.].google.com but still, the error persists when one tries to switch from Google to Gmail.

...However, I realized that there are still issues with this even with the workaround. So maybe that's what you're talking about. @amaitland , I've created a new pull request: https://github.com/cefsharp/CefSharp/pull/4771 with the new method created, the one which demonstrates that "isolate-origins" which includes all domains being visited is still producing the same error.

[EDIT] - as a side-note, I did try: disable-web-security; disable-features=IsolateOrigins,site-per-process; disable-site-isolation-trials; and neither of these helped.

nebojsasandin avatar Mar 27 '24 13:03 nebojsasandin

it seems that the CEF staff don't recognize this as a bug, or anything that needs to be changed, however, this does break the functionality of the CefSharp. Is there a more permanent or future-proof solution other than knowing which domains are breaking this entire facility?

The problem is in the Alloy Bootstrap, not something that can be fixed directly in CefSharp. The interesting news is that CEF is just started to remove the Alloy Boostrap (https://github.com/chromiumembedded/cef/issues/3685) and has added support for using the Chrome Bootstrap with Alloy Style OSR windows.

Starting in M125 you'll be able to use CefSettings.ChromeRuntime = true and the same isolation settings a Chrome will be used.

I've added both tests (they'll only run locally, not on Appveyor) and migrated the CefSharp.Test project to using the ChromeRuntime. Both now pass without any changes or custom origin isolations.

In ~M127 the Alloy Bootstrap will be removed.

I've created a new pull request: #4771 with the new method created, the one which demonstrates that "isolate-origins" which includes all domains being visited is still producing the same error.

@nebojsasandin Thanks for providing the tests 👍 The problem is likely in the alloy bootstrap and should be resolved by switching to the Chrome bootstrap.

amaitland avatar May 06 '24 01:05 amaitland

The above-mentioned issue still persists. Does anyone have a solution?

  • Project type: WinForm, VB .NET Framework 4.8.1

  • Operating System: Win 10, x64

  • CEFSharp version: Stable releases 125 and 126 from NuGet.

  • Other settings and prerequisites:

    • ChromeRuntime=true.
    • The error occurs regardless of whether "custom origin isolations" are enabled or not.
    • The timing for allowing Script execution has been confirmed through methods such as CanExecuteJavascriptInMainFrame.
  • Issue occurrence process: The issue occurs only after a page navigation is triggered by either manual or script operations on any webpage. ExecuteScriptAsync() always runs normally, but EvaluateScriptAsync() throws the above error. On the JavaScript side, CefSharp.BindObjectAsync() fails.

I am unsure whether this is a default behavior change in CEFSharp or a bug. Looking forward to a solution, thank you.

heart-of-hero avatar Jul 14 '24 10:07 heart-of-hero

True, I'm randomly getting this error after upgrading to latest version

EDIT: or maybe not so randomly! It happens every 10 minutes, accurately.

I'm checking a URL every 3 seconds - in short, I do:

while (true) { Browser.Load(url); // Run several calls to JS Sleep(3000); }

Every 10 minutes I get that error when running the JS call. A dirty workaround for me is to redirect to another page by clicking on a button in the same page, so let's say there's a "go to my profile" button, I press that, and after going to my profile, I can Browser.Load(url) and everything keeps working fine for another solid 10 minutes.

stormcleric avatar Jul 23 '24 18:07 stormcleric

The above-mentioned issue still persists. Does anyone have a solution?

  • Project type: WinForm, VB .NET Framework 4.8.1

  • Operating System: Win 10, x64

  • CEFSharp version: Stable releases 125 and 126 from NuGet.

  • Other settings and prerequisites:

    • ChromeRuntime=true.
    • The error occurs regardless of whether "custom origin isolations" are enabled or not.
    • The timing for allowing Script execution has been confirmed through methods such as CanExecuteJavascriptInMainFrame.
  • Issue occurrence process: The issue occurs only after a page navigation is triggered by either manual or script operations on any webpage. ExecuteScriptAsync() always runs normally, but EvaluateScriptAsync() throws the above error. On the JavaScript side, CefSharp.BindObjectAsync() fails.

I am unsure whether this is a default behavior change in CEFSharp or a bug. Looking forward to a solution, thank you.

Well it seems to be fixed if you specifically set ChromeRuntime=false in your settings. At least this worked for me after upgrading, and everything seems to be working alright.

stormcleric avatar Jul 26 '24 07:07 stormcleric

I have also been experiencing the same issue with ChromeRuntime=true on random websites. It's not just related to Google websites.

I found it on: https://www.visualcom.pl/kontakt/wymagania-plikow-produkcyjnych'

GrabzIt avatar Jul 26 '24 13:07 GrabzIt

Well it seems to be fixed if you specifically set ChromeRuntime=false in your settings. At least this worked for me after upgrading, and everything seems to be working alright.

If the problem only occurs when using the Chrome Runtime then an upstream issue will need to be raised.

https://github.com/chromiumembedded/cef/issues

The alloy bootstrap will be removed in M128, you'll only be able to revert back for a short time.

I have also been experiencing the same issue with ChromeRuntime=true on random websites. It's not just related to Google websites.

I found it on: https://www.visualcom.pl/kontakt/wymagania-plikow-produkcyjnych'

@GrabzIt Can you please create a unit test that reproduces the problem you are seeing (and submit a PR).

There are already tests at https://github.com/cefsharp/CefSharp/blob/63cce27846e2c112bd6bfa510db25a619d40e739/CefSharp.Test/Issues/Issue4621.cs#L14 you can use those as a basis, just add another to the same class


To set expectations, I've not seen anything here that suggests this is an issue that can be fixed directly in CefSharp. If upstream changes are required then this will likely take some time to get fixed as reproducing the issue directly in C++ will be difficult.

I potential workaround would be to switch to using Chrome DevTools Protocol for executing javascript.

https://github.com/cefsharp/CefSharp.Dom?tab=readme-ov-file#dom-access


Hopefully at some point CEF will add equivalent functionality directly (https://github.com/chromiumembedded/cef/issues/3631)

amaitland avatar Jul 26 '24 22:07 amaitland

I have found that the errors happen after a long period of time, so hours or days, then carry on happening. So this isn't something that is suitable for a unit test.

I am going to try and use the Chrome DevTools Protocol to see if that fixes the issue.

GrabzIt avatar Jul 28 '24 17:07 GrabzIt

I have found that the errors happen after a long period of time, so hours or days, then carry on happening. So this isn't something that is suitable for a unit test.

I am going to try and use the Chrome DevTools Protocol to see if that fixes the issue.

Not on my specific case - they were after 10 solid minutes - but it's funny it's time dependant.

stormcleric avatar Jul 29 '24 05:07 stormcleric

I have found that the errors happen after a long period of time, so hours or days, then carry on happening. So this isn't something that is suitable for a unit test. I am going to try and use the Chrome DevTools Protocol to see if that fixes the issue.

Not on my specific case - they were after 10 solid minutes - but it's funny it's time dependant.

yes also to me afte 10 minutes, dont know why this timing. (rendering working, on paint work, just JS code dont work)

drRobertDev avatar Aug 04 '24 17:08 drRobertDev

I have found that the errors happen after a long period of time, so hours or days, then carry on happening. So this isn't something that is suitable for a unit test. I am going to try and use the Chrome DevTools Protocol to see if that fixes the issue.

Not on my specific case - they were after 10 solid minutes - but it's funny it's time dependant.

I feel this is Google's way of breaking Chrome Extensions that need (JS level magic to do stuff like hide ads.) I have the exact issue after upgrading from 112 to the latest stable nuget.

nabkawe avatar Aug 12 '24 16:08 nabkawe