diff --git a/libcef/browser/main_runner.cc b/libcef/browser/main_runner.cc index 3dbf759ab..32a442961 100644 --- a/libcef/browser/main_runner.cc +++ b/libcef/browser/main_runner.cc @@ -181,12 +181,20 @@ class CefUIThread : public base::PlatformThread::Delegate { runner_->RunMessageLoop(); - browser_runner_->Shutdown(); - browser_runner_.reset(); + // Stop may be called before InitializeBrowserRunner if + // content::ContentMainRun was not successful (for example, due to process + // singleton relaunch). + if (browser_runner_) { + browser_runner_->Shutdown(); + browser_runner_.reset(); + } + // This will be a no-op if there is no BrowserTaskExecutor. content::BrowserTaskExecutor::Shutdown(); - std::move(shutdown_callback_).Run(); + if (!shutdown_callback_.is_null()) { + std::move(shutdown_callback_).Run(); + } // Run exit callbacks on the UI thread to avoid sequence check failures. base::AtExitManager::ProcessCallbacksNow(); @@ -433,6 +441,11 @@ int CefMainRunner::ContentMainRun(bool* initialized, int* exit_code) { runner->main_delegate_->BeforeUIThreadInitialize(); *exit_code = content::ContentMainRun(runner->main_runner_.get()); + + if (*exit_code != content::RESULT_CODE_NORMAL_EXIT) { + runner->FinishShutdownOnUIThread(); + } + event->Signal(); }, base::Unretained(this), base::Unretained(&uithread_startup_event), @@ -444,6 +457,13 @@ int CefMainRunner::ContentMainRun(bool* initialized, // We need to wait until content::ContentMainRun has finished. uithread_startup_event.Wait(); + + if (exit_code != content::RESULT_CODE_NORMAL_EXIT) { + // content::ContentMainRun was not successful (for example, due to process + // singleton relaunch). Stop the UI thread and block until done. + ui_thread_->Stop(); + ui_thread_.reset(); + } } else { *initialized = true; main_delegate_->BeforeUIThreadInitialize(); diff --git a/libcef/common/chrome/chrome_main_runner_delegate.cc b/libcef/common/chrome/chrome_main_runner_delegate.cc index ce44e62e6..c639e1f60 100644 --- a/libcef/common/chrome/chrome_main_runner_delegate.cc +++ b/libcef/common/chrome/chrome_main_runner_delegate.cc @@ -58,6 +58,11 @@ void ChromeMainRunnerDelegate::BeforeMainThreadRun( void ChromeMainRunnerDelegate::BeforeMainMessageLoopRun( base::RunLoop* run_loop) { + // May be nullptr if content::ContentMainRun exits early. + if (!g_browser_process) { + return; + } + // The ScopedKeepAlive instance triggers shutdown logic when released on the // UI thread before terminating the message loop (e.g. from CefQuitMessageLoop // or FinishShutdownOnUIThread when running with multi-threaded message loop). @@ -67,12 +72,17 @@ void ChromeMainRunnerDelegate::BeforeMainMessageLoopRun( // The QuitClosure will be executed from BrowserProcessImpl::Unpin() via // KeepAliveRegistry when the last ScopedKeepAlive is released. // ScopedKeepAlives are also held by Browser objects. - DCHECK(g_browser_process); static_cast(g_browser_process) ->SetQuitClosure(run_loop->QuitClosure()); } bool ChromeMainRunnerDelegate::HandleMainMessageLoopQuit() { + // May be nullptr if content::ContentMainRun exits early. + if (!g_browser_process) { + // Proceed with direct execution of the QuitClosure(). + return false; + } + // May be called multiple times. See comments in RunMainMessageLoopBefore. keep_alive_.reset(); diff --git a/tests/cefclient/browser/root_window_views.cc b/tests/cefclient/browser/root_window_views.cc index d2553665d..db85d63a5 100644 --- a/tests/cefclient/browser/root_window_views.cc +++ b/tests/cefclient/browser/root_window_views.cc @@ -58,8 +58,20 @@ void RootWindowViews::Init(RootWindow::Delegate* delegate, CreateClientHandler(config_->url); initialized_ = true; - // Continue initialization on the UI thread. - InitOnUIThread(settings, delegate_->GetRequestContext(this)); + if (!CURRENTLY_ON_MAIN_THREAD()) { + // Execute GetRequestContext() on the main thread. + MAIN_POST_CLOSURE(base::BindOnce( + [](scoped_refptr self, + const CefBrowserSettings& settings) { + // Continue initialization on the UI thread. + self->InitOnUIThread(settings, + self->delegate_->GetRequestContext(self.get())); + }, + scoped_refptr(this), settings)); + } else { + // Continue initialization on the UI thread. + InitOnUIThread(settings, delegate_->GetRequestContext(this)); + } } void RootWindowViews::InitAsPopup(RootWindow::Delegate* delegate,