Split UI thread shutdown into before and after stages (see #3609)

Existing UI thread shutdown tasks need to be executed before the UI
thread RunLoop is terminated. Those tasks have now been moved to
CefMainRunner::StartShutdownOnUIThread and FinishShutdownOnUIThread is
now called after the RunLoop is terminated.

This fixes a shutdown crash in ChromeProcessSingleton::DeleteInstance.
DeleteInstance needs to be called on the UI thread near the end of the
shutdown process (after ChromeProcessSingleton::Cleanup is called via
PostMainMessageLoopRun).
This commit is contained in:
Marshall Greenblatt
2023-12-11 15:36:56 -05:00
parent 80c65f25a3
commit 262a93b2f7
7 changed files with 77 additions and 60 deletions

View File

@@ -9,6 +9,7 @@
#include "libcef/common/alloy/alloy_main_delegate.h"
#include "libcef/renderer/alloy/alloy_content_renderer_client.h"
#include "chrome/browser/chrome_process_singleton.h"
#include "content/public/browser/render_process_host.h"
#include "ui/base/resource/resource_bundle.h"
@@ -42,13 +43,17 @@ void AlloyMainRunnerDelegate::AfterUIThreadInitialize() {
->OnContextInitialized();
}
void AlloyMainRunnerDelegate::AfterUIThreadShutdown() {
void AlloyMainRunnerDelegate::BeforeUIThreadShutdown() {
static_cast<ChromeBrowserProcessAlloy*>(g_browser_process)
->CleanupOnUIThread();
ui::ResourceBundle::GetSharedInstance().CleanupOnUIThread();
}
void AlloyMainRunnerDelegate::AfterUIThreadShutdown() {
ChromeProcessSingleton::DeleteInstance();
}
void AlloyMainRunnerDelegate::BeforeMainThreadShutdown() {
if (content::RenderProcessHost::run_renderer_in_process()) {
// Blocks until RenderProcess cleanup is complete.

View File

@@ -33,6 +33,7 @@ class AlloyMainRunnerDelegate : public CefMainRunnerDelegate {
void BeforeMainThreadInitialize(const CefMainArgs& args) override;
void BeforeMainThreadRun(bool multi_threaded_message_loop) override;
void AfterUIThreadInitialize() override;
void BeforeUIThreadShutdown() override;
void AfterUIThreadShutdown() override;
void BeforeMainThreadShutdown() override;
void AfterMainThreadShutdown() override;

View File

@@ -12,6 +12,7 @@
#include "base/run_loop.h"
#include "chrome/browser/browser_process_impl.h"
#include "chrome/browser/chrome_content_browser_client.h"
#include "chrome/browser/chrome_process_singleton.h"
#include "chrome/common/profiler/main_thread_stack_sampling_profiler.h"
#include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
@@ -46,6 +47,8 @@ void ChromeMainRunnerDelegate::BeforeMainThreadInitialize(
void ChromeMainRunnerDelegate::BeforeMainThreadRun(
bool multi_threaded_message_loop) {
if (multi_threaded_message_loop) {
multi_threaded_message_loop_ = true;
// Detach from the main thread so that these objects can be attached and
// modified from the UI thread going forward.
metrics::GlobalPersistentSystemProfile::GetInstance()
@@ -83,7 +86,7 @@ void ChromeMainRunnerDelegate::BeforeUIThreadInitialize() {
sampling_profiler_ = std::make_unique<MainThreadStackSamplingProfiler>();
}
void ChromeMainRunnerDelegate::AfterUIThreadShutdown() {
void ChromeMainRunnerDelegate::BeforeUIThreadShutdown() {
static_cast<ChromeContentBrowserClient*>(
CefAppManager::Get()->GetContentClient()->browser())
->CleanupOnUIThread();
@@ -92,6 +95,14 @@ void ChromeMainRunnerDelegate::AfterUIThreadShutdown() {
sampling_profiler_.reset();
}
void ChromeMainRunnerDelegate::AfterUIThreadShutdown() {
if (multi_threaded_message_loop_) {
// Don't wait for this to be called in ChromeMainDelegate::ProcessExiting.
// It is safe to call multiple times.
ChromeProcessSingleton::DeleteInstance();
}
}
void ChromeMainRunnerDelegate::BeforeExecuteProcess(const CefMainArgs& args) {
BeforeMainThreadInitialize(args);
}

View File

@@ -37,6 +37,7 @@ class ChromeMainRunnerDelegate : public CefMainRunnerDelegate {
void BeforeMainMessageLoopRun(base::RunLoop* run_loop) override;
bool HandleMainMessageLoopQuit() override;
void BeforeUIThreadInitialize() override;
void BeforeUIThreadShutdown() override;
void AfterUIThreadShutdown() override;
void BeforeExecuteProcess(const CefMainArgs& args) override;
void AfterExecuteProcess() override;
@@ -50,6 +51,8 @@ class ChromeMainRunnerDelegate : public CefMainRunnerDelegate {
CefMainRunnerHandler* const runner_;
CefSettings* const settings_;
CefRefPtr<CefApp> application_;
bool multi_threaded_message_loop_ = false;
};
#endif // CEF_LIBCEF_COMMON_CHROME_CHROME_MAIN_RUNNER_DELEGATE_CEF_

View File

@@ -26,6 +26,7 @@ class CefMainRunnerDelegate {
virtual bool HandleMainMessageLoopQuit() { return false; }
virtual void BeforeUIThreadInitialize() {}
virtual void AfterUIThreadInitialize() {}
virtual void BeforeUIThreadShutdown() {}
virtual void AfterUIThreadShutdown() {}
virtual void BeforeMainThreadShutdown() {}
virtual void AfterMainThreadShutdown() {}