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

@ -162,6 +162,10 @@ class CefUIThread : public base::PlatformThread::Delegate {
CHECK_EQ(exit_code, -1); CHECK_EQ(exit_code, -1);
} }
void set_shutdown_callback(base::OnceClosure shutdown_callback) {
shutdown_callback_ = std::move(shutdown_callback);
}
protected: protected:
void ThreadMain() override { void ThreadMain() override {
base::PlatformThread::SetName("CefUIThread"); base::PlatformThread::SetName("CefUIThread");
@ -182,6 +186,8 @@ class CefUIThread : public base::PlatformThread::Delegate {
content::BrowserTaskExecutor::Shutdown(); content::BrowserTaskExecutor::Shutdown();
std::move(shutdown_callback_).Run();
// Run exit callbacks on the UI thread to avoid sequence check failures. // Run exit callbacks on the UI thread to avoid sequence check failures.
base::AtExitManager::ProcessCallbacksNow(); base::AtExitManager::ProcessCallbacksNow();
@ -194,6 +200,7 @@ class CefUIThread : public base::PlatformThread::Delegate {
CefMainRunner* const runner_; CefMainRunner* const runner_;
base::OnceClosure setup_callback_; base::OnceClosure setup_callback_;
base::OnceClosure shutdown_callback_;
std::unique_ptr<content::BrowserMainRunner> browser_runner_; std::unique_ptr<content::BrowserMainRunner> browser_runner_;
@ -252,29 +259,45 @@ bool CefMainRunner::Initialize(CefSettings* settings,
void CefMainRunner::Shutdown(base::OnceClosure shutdown_on_ui_thread, void CefMainRunner::Shutdown(base::OnceClosure shutdown_on_ui_thread,
base::OnceClosure finalize_shutdown) { base::OnceClosure finalize_shutdown) {
if (multi_threaded_message_loop_) { if (multi_threaded_message_loop_) {
// Events that will be used to signal when shutdown is complete. Start in // Start shutdown on the UI thread. This is guaranteed to run before the
// non-signaled mode so that the event will block. // thread RunLoop has stopped.
base::WaitableEvent uithread_shutdown_event( CEF_POST_TASK(CEF_UIT,
base::WaitableEvent::ResetPolicy::AUTOMATIC, base::BindOnce(&CefMainRunner::StartShutdownOnUIThread,
base::WaitableEvent::InitialState::NOT_SIGNALED); base::Unretained(this),
std::move(shutdown_on_ui_thread)));
// Finish shutdown on the UI thread. // Finish shutdown on the UI thread after the thread RunLoop has stopped and
CEF_POST_TASK( // before running exit callbacks.
CEF_UIT, ui_thread_->set_shutdown_callback(base::BindOnce(
base::BindOnce(&CefMainRunner::FinishShutdownOnUIThread, &CefMainRunner::FinishShutdownOnUIThread, base::Unretained(this)));
base::Unretained(this), std::move(shutdown_on_ui_thread),
&uithread_shutdown_event));
/// Block until UI thread shutdown is complete. // Blocks until the thread has stopped.
uithread_shutdown_event.Wait(); ui_thread_->Stop();
ui_thread_.reset();
FinalizeShutdown(std::move(finalize_shutdown));
} else {
// Finish shutdown on the current thread, which should be the UI thread.
FinishShutdownOnUIThread(std::move(shutdown_on_ui_thread), nullptr);
FinalizeShutdown(std::move(finalize_shutdown));
} }
main_delegate_->BeforeMainThreadShutdown();
if (!multi_threaded_message_loop_) {
// Main thread and UI thread are the same.
StartShutdownOnUIThread(std::move(shutdown_on_ui_thread));
browser_runner_->Shutdown();
browser_runner_.reset();
FinishShutdownOnUIThread();
}
// Shut down the content runner.
content::ContentMainShutdown(main_runner_.get());
main_runner_.reset();
std::move(finalize_shutdown).Run();
main_delegate_->AfterMainThreadShutdown();
main_delegate_.reset();
g_runtime_type = RuntimeType::UNINITIALIZED;
} }
void CefMainRunner::RunMessageLoop() { void CefMainRunner::RunMessageLoop() {
@ -493,9 +516,8 @@ void CefMainRunner::OnContextInitialized(
std::move(context_initialized).Run(); std::move(context_initialized).Run();
} }
void CefMainRunner::FinishShutdownOnUIThread( void CefMainRunner::StartShutdownOnUIThread(
base::OnceClosure shutdown_on_ui_thread, base::OnceClosure shutdown_on_ui_thread) {
base::WaitableEvent* uithread_shutdown_event) {
CEF_REQUIRE_UIT(); CEF_REQUIRE_UIT();
// Execute all pending tasks now before proceeding with shutdown. Otherwise, // Execute all pending tasks now before proceeding with shutdown. Otherwise,
@ -512,37 +534,11 @@ void CefMainRunner::FinishShutdownOnUIThread(
->ShutdownOnUIThread(); ->ShutdownOnUIThread();
std::move(shutdown_on_ui_thread).Run(); std::move(shutdown_on_ui_thread).Run();
main_delegate_->AfterUIThreadShutdown(); main_delegate_->BeforeUIThreadShutdown();
if (uithread_shutdown_event) {
uithread_shutdown_event->Signal();
}
} }
void CefMainRunner::FinalizeShutdown(base::OnceClosure finalize_shutdown) { void CefMainRunner::FinishShutdownOnUIThread() {
main_delegate_->BeforeMainThreadShutdown(); main_delegate_->AfterUIThreadShutdown();
if (browser_runner_.get()) {
browser_runner_->Shutdown();
browser_runner_.reset();
}
if (ui_thread_.get()) {
// Blocks until the thread has stopped.
ui_thread_->Stop();
ui_thread_.reset();
}
// Shut down the content runner.
content::ContentMainShutdown(main_runner_.get());
main_runner_.reset();
std::move(finalize_shutdown).Run();
main_delegate_->AfterMainThreadShutdown();
main_delegate_.reset();
g_runtime_type = RuntimeType::UNINITIALIZED;
} }
// From libcef/features/runtime.h: // From libcef/features/runtime.h:

View File

@ -71,13 +71,13 @@ class CefMainRunner : public CefMainRunnerHandler {
// Called on the UI thread after the context is initialized. // Called on the UI thread after the context is initialized.
void OnContextInitialized(base::OnceClosure context_initialized); void OnContextInitialized(base::OnceClosure context_initialized);
// Performs shutdown actions that need to occur on the UI thread before any // Performs shutdown actions that need to occur on the UI thread before the
// threads are destroyed. // thread RunLoop has stopped.
void FinishShutdownOnUIThread(base::OnceClosure shutdown_on_ui_thread, void StartShutdownOnUIThread(base::OnceClosure shutdown_on_ui_thread);
base::WaitableEvent* uithread_shutdown_event);
// Destroys the runtime and related objects. // Performs shutdown actions that need to occur on the UI thread after the
void FinalizeShutdown(base::OnceClosure finalize_shutdown); // thread RunLoop has stopped and before running exit callbacks.
void FinishShutdownOnUIThread();
const bool multi_threaded_message_loop_; const bool multi_threaded_message_loop_;
const bool external_message_pump_; const bool external_message_pump_;

View File

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

View File

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

View File

@ -12,6 +12,7 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "chrome/browser/browser_process_impl.h" #include "chrome/browser/browser_process_impl.h"
#include "chrome/browser/chrome_content_browser_client.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 "chrome/common/profiler/main_thread_stack_sampling_profiler.h"
#include "components/keep_alive_registry/keep_alive_types.h" #include "components/keep_alive_registry/keep_alive_types.h"
#include "components/keep_alive_registry/scoped_keep_alive.h" #include "components/keep_alive_registry/scoped_keep_alive.h"
@ -46,6 +47,8 @@ void ChromeMainRunnerDelegate::BeforeMainThreadInitialize(
void ChromeMainRunnerDelegate::BeforeMainThreadRun( void ChromeMainRunnerDelegate::BeforeMainThreadRun(
bool multi_threaded_message_loop) { bool multi_threaded_message_loop) {
if (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 // Detach from the main thread so that these objects can be attached and
// modified from the UI thread going forward. // modified from the UI thread going forward.
metrics::GlobalPersistentSystemProfile::GetInstance() metrics::GlobalPersistentSystemProfile::GetInstance()
@ -83,7 +86,7 @@ void ChromeMainRunnerDelegate::BeforeUIThreadInitialize() {
sampling_profiler_ = std::make_unique<MainThreadStackSamplingProfiler>(); sampling_profiler_ = std::make_unique<MainThreadStackSamplingProfiler>();
} }
void ChromeMainRunnerDelegate::AfterUIThreadShutdown() { void ChromeMainRunnerDelegate::BeforeUIThreadShutdown() {
static_cast<ChromeContentBrowserClient*>( static_cast<ChromeContentBrowserClient*>(
CefAppManager::Get()->GetContentClient()->browser()) CefAppManager::Get()->GetContentClient()->browser())
->CleanupOnUIThread(); ->CleanupOnUIThread();
@ -92,6 +95,14 @@ void ChromeMainRunnerDelegate::AfterUIThreadShutdown() {
sampling_profiler_.reset(); 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) { void ChromeMainRunnerDelegate::BeforeExecuteProcess(const CefMainArgs& args) {
BeforeMainThreadInitialize(args); BeforeMainThreadInitialize(args);
} }

View File

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

View File

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