App slow to close when using RegisterJsObject in version >=43
Hi, after upgrade cefsharp to 43 (winforms) and I have a problem with closing my program. It takes about 10 seconds. The problem is with RegisterJsObject. I have no idea, why it is happening, but when I delete the line with RegisterJsObject, everything is fine. It does not matter, if website contain function or not. After first load of website, the program has problem with closing.
But I can not reproduce this fault on your minimal example. Any idea where could be the problem?
chromeBrowser = new ChromiumWebBrowser(String.Empty);
chromeBrowser.RegisterJsObject("jscall", this, false);
Likely relates to the WCF channel timing out rather than closing properly.
Can you provide an example to reproduce the problem? Are you using popups?
No, I am not using popups. I will try to provide example asap, but probably tomorrow.
The line that blocks is likely https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp.Core/ManagedCefBrowserAdapter.h#L104
Basically the WCF service has a 10 second time out before it closes. Under normal circumstances it will close without issue, obviously in this case something is keeping it open. The last time this problem occurred was because of popups not being closed. If your not using popups then there will be something else causing the problem.
The below simple example shows the 10 second close issue. Winforms App with CefSharp 43.0.0.0 from Nuget.
public partial class Form1 : Form
{
private ChromiumWebBrowser browser;
public Form1()
{
InitializeComponent();
browser = new ChromiumWebBrowser("about:blank")
{
Dock = DockStyle.Fill,
};
browser.RegisterJsObject("jsBind", new JSBinding());
browserPanel.Controls.Add(browser);
}
private void btnExit_Click(object sender, EventArgs e)
{
Cef.Shutdown();
Application.Exit();
}
}
public class JSBinding
{
public string temp = "";
public string getTemp()
{
return temp;
}
public void setTemp(string value)
{
this.temp = value;
}
}
I also tried adding a stopwatch before executing Cef.Shutdown() to confirm that is where the latency is, and it took exactly 10 seconds to process.
Try disposing of the browser instance before you exit.
https://github.com/cefsharp/CefSharp.MinimalExample/blob/master/CefSharp.MinimalExample.WinForms/BrowserForm.cs#L115
Also if you can work the MinimalExample project to demo the problem that would make things easier to debug.
Disposing also has the same issue.
Amended code.
private void btnExit_Click(object sender, EventArgs e)
{
var sw = Stopwatch.StartNew();
if(browser != null)
browser.Dispose();
var time = sw.ElapsedMilliseconds;
Cef.Shutdown();
Console.WriteLine(String.Format("Dispose: {0}, Shutdown: {1}",time,sw.ElapsedMilliseconds - time));
Application.Exit();
}
Output
Dispose: 11052, Shutdown: 21
Update: If I remove the JS Object binding then I do not see the issue. Is there a way to manually terminate/unbind the JS object before disposing?
Is there a way to manually terminate/unbind the JS object before disposing?
No, it's not that simple, the object isn't the relevant part, it's the underlying WCF channel that's not closing.
If you can fork the MinimalExample I'll have at look further.
Sure - Have forked it here : https://github.com/expertsonline/CefSharp.MinimalExample/blob/master/CefSharp.MinimalExample.WinForms/BrowserForm.cs#L27
Simply adding a binding allows the issue to be reproduced.
Simply adding a binding allows the issue to be reproduced.
Steps to reproduce? I'm not seeing a problem, I pulled in your changes, recompiled, ran CefSharp.MinimalExample.WinForms, I'm using VS2013u4 Community.
Did you try to close the program with file->exit ?
Are you both calling Dispose and Cef.Shutdown() as per the example?
For now it's probably worth letting CefSharp dispose/shutdown for you if possible (there are a few cases where this isn't going to work)
https://github.com/cefsharp/CefSharp.MinimalExample/commit/aab09ca804e44729f403ac70317dd8229c223c8e
Yes, I do.
{
chromeBrowser.Dispose();
Cef.Shutdown();
Application.Exit();
}
Yes, I do.
Try calling Close() on your form, if your lucky the rest should just happen for you (and by the rest I mean disposing of the browser and shutting down CEF)
Sorry, but it didn't help.
So your code now looks like?
{
//chromeBrowser.Dispose();
//Cef.Shutdown();
//Application.Exit();
Close();
}
I see this issue in 43 and by simply running the SimpleBrowserForm in the WinForms.Example from https://github.com/cefsharp/CefSharp/commit/ac4df2e7c59a791c8628f88ccb2c35a9343144fa. The browser.Dispose() call is what is hanging for about 10 seconds.
I see this issue in 43 and by simply running the SimpleBrowserForm in the WinForms.Example from ac4df2e
For clarity that commit references the master branch, the 43 branch is https://github.com/cefsharp/CefSharp/tree/cefsharp/43
Any fix will need to be targeted against 43 (there is no release based on master yet).
The browser.Dispose() call is what is hanging for about 10 seconds.
It should be exactly 10 seconds as that's the default WCF timeout for closing the host.
Using the example below works fine for me, it might not work in all scenarios (it works in the standard CefSharp.WinForms.Example app as it doesn't dispose the browser explicitly.
https://github.com/cefsharp/CefSharp.MinimalExample/commit/aab09ca804e44729f403ac70317dd8229c223c8e
My intention was to indicate that the issue existed in 43 with my application and in the example in the master. I commented the dispose and shutdown calls and it appears to work.
Looks like the problem only appears in the WinForms version. Likely a timing issue.
Possible workarounds
- Change
Close()toAbort()for theWCFchannel, not sure about this option, it may have some side effects - Change
Closeto use a smallerTimeoutwhich will still have the problem - Make timeout configurable, using
CefSharpSettingsclass, could callAbortif timeout is0
Someone will need to investigate the problem further, doesn't look like the BrowserSubProcess is calling OnBrowserDestroyed
https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp.BrowserSubprocess/CefRenderProcess.cs#L101
Relevant code that blocks is https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp.Core/ManagedCefBrowserAdapter.h#L104