Core - ManagedCefBrowserAdapter::InitializeBrowserProcessServiceHost improve Dispose handling

As the browser is Disposed on a different Thread to OnAfterBrowserCreated is called there's possibility of an ObjectDisposedException.
Instead of locking just catch and ignore the error as it's extremely rare

Resolves #3437
This commit is contained in:
Alex Maitland
2021-03-11 15:06:26 +10:00
parent d130c578f0
commit d953ac42ec
4 changed files with 34 additions and 10 deletions
@@ -105,19 +105,28 @@ namespace CefSharp
// NOTE: This was moved out of OnAfterBrowserCreated to prevent the System.ServiceModel assembly from being loaded when WCF is not enabled.
__declspec(noinline) void ManagedCefBrowserAdapter::InitializeBrowserProcessServiceHost(IBrowser^ browser)
{
_browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browser->Identifier, _javascriptCallbackFactory);
//NOTE: Attempt to solve timing issue where browser is opened and rapidly disposed. In some cases a call to Open throws
// an exception about the process already being closed. Two relevant issues are #862 and #804.
if (_browserProcessServiceHost->State == CommunicationState::Created)
if (browser->IsDisposed)
{
try
return;
}
//As the default the browser is Disposed on a different thread to this method it's possible that
//we arrive here after browser has started Disposing. In that case there's likely to be an exception
//accessing the Browser and possibly the serviceHost.
//https://github.com/cefsharp/CefSharp/issues/3437
try
{
_browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browser->Identifier, _javascriptCallbackFactory);
//NOTE: Attempt to solve timing issue where browser is opened and rapidly disposed. In some cases a call to Open throws
// an exception about the process already being closed. Two relevant issues are #862 and #804.
if (_browserProcessServiceHost->State == CommunicationState::Created)
{
_browserProcessServiceHost->Open();
}
catch (Exception^)
{
//Ignore exception as it's likely cause when the browser is closing
}
}
}
catch (Exception^)
{
//Ignore exception as it's likely cause when the browser is closing/disposed
}
}
+5
View File
@@ -663,6 +663,11 @@ namespace CefSharp.OffScreen
/// <param name="browser">The browser.</param>
void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
{
if (IsDisposed || browser.IsDisposed)
{
return;
}
this.browser = browser;
Interlocked.Exchange(ref browserInitialized, 1);
+5
View File
@@ -607,6 +607,11 @@ namespace CefSharp.WinForms
/// <param name="browser">The browser.</param>
void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
{
if(IsDisposed || browser.IsDisposed)
{
return;
}
this.browser = browser;
Interlocked.Exchange(ref browserInitialized, 1);
+5
View File
@@ -1103,6 +1103,11 @@ namespace CefSharp.Wpf
/// <param name="browser">The browser.</param>
void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
{
if (IsDisposed || browser.IsDisposed)
{
return;
}
Interlocked.Exchange(ref browserInitialized, 1);
this.browser = browser;