CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

App slow to close when using RegisterJsObject in version >=43

Open MartySVK opened this issue 11 years ago • 35 comments

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);

MartySVK avatar Sep 29 '15 15:09 MartySVK

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?

amaitland avatar Sep 29 '15 22:09 amaitland

No, I am not using popups. I will try to provide example asap, but probably tomorrow.

MartySVK avatar Sep 30 '15 10:09 MartySVK

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.

amaitland avatar Sep 30 '15 11:09 amaitland

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.

expertsonline avatar Sep 30 '15 12:09 expertsonline

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.

amaitland avatar Oct 01 '15 00:10 amaitland

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?

expertsonline avatar Oct 01 '15 01:10 expertsonline

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.

amaitland avatar Oct 01 '15 01:10 amaitland

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.

expertsonline avatar Oct 01 '15 13:10 expertsonline

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.

amaitland avatar Oct 01 '15 20:10 amaitland

Did you try to close the program with file->exit ?

MartySVK avatar Oct 02 '15 07:10 MartySVK

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

amaitland avatar Oct 02 '15 12:10 amaitland

Yes, I do.

        {
            chromeBrowser.Dispose();
            Cef.Shutdown();
            Application.Exit();
        }

MartySVK avatar Oct 02 '15 12:10 MartySVK

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)

amaitland avatar Oct 03 '15 08:10 amaitland

Sorry, but it didn't help.

MartySVK avatar Oct 05 '15 07:10 MartySVK

So your code now looks like?

{
  //chromeBrowser.Dispose();
  //Cef.Shutdown();
  //Application.Exit();
  Close();
}

amaitland avatar Oct 05 '15 09:10 amaitland

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.

mattjhammond avatar Oct 05 '15 23:10 mattjhammond

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

amaitland avatar Oct 05 '15 23:10 amaitland

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.

mattjhammond avatar Oct 05 '15 23:10 mattjhammond

Looks like the problem only appears in the WinForms version. Likely a timing issue.

Possible workarounds

  • Change Close() to Abort() for the WCF channel, not sure about this option, it may have some side effects
  • Change Close to use a smaller Timeout which will still have the problem
  • Make timeout configurable, using CefSharpSettings class, could call Abort if timeout is 0

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

amaitland avatar Oct 06 '15 01:10 amaitland

Relevant code that blocks is https://github.com/cefsharp/CefSharp/blob/cefsharp/43/CefSharp.Core/ManagedCefBrowserAdapter.h#L104

amaitland avatar Oct 06 '15 01:10 amaitland