Skip to content

Commit

Permalink
Prevent DCHECK on shutdown of EmbeddedApplicationRunner
Browse files Browse the repository at this point in the history
When EmbeddedApplicationRunner is destroyed, if the application
instance still has any bound connections, those connections must
be destroyed on the application task runner. Otherwise there is
a DCHECK failure when the binding is destroyed.

BUG=

Review-Url: https://codereview.chromium.org/2188503003
Cr-Commit-Position: refs/heads/master@{#408246}
  • Loading branch information
kmackay authored and Commit bot committed Jul 27, 2016
1 parent 8c13e2d commit 901b099
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions content/common/mojo/embedded_application_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,26 @@ class EmbeddedApplicationRunner::Instance

void ShutDown() {
DCHECK(runner_thread_checker_.CalledOnValidThread());
if (thread_) {
thread_.reset();
application_task_runner_ = nullptr;
if (!application_task_runner_)
return;
// Any extant ServiceContexts must be destroyed on the application thread.
if (application_task_runner_->BelongsToCurrentThread()) {
Quit();
} else {
application_task_runner_->PostTask(FROM_HERE,
base::Bind(&Instance::Quit, this));
}
}

private:
friend class base::RefCountedThreadSafe<Instance>;

~Instance() {
// If this instance had its own thread, it MUST be explicitly destroyed by
// QuitOnRunnerThread() by the time this destructor is run.
DCHECK(!thread_);
}

void BindServiceRequestOnApplicationThread(
shell::mojom::ServiceRequest request) {
DCHECK(application_task_runner_->BelongsToCurrentThread());
Expand All @@ -76,15 +89,6 @@ class EmbeddedApplicationRunner::Instance
new_connection));
}

private:
friend class base::RefCountedThreadSafe<Instance>;

~Instance() {
// If this instance had its own thread, it MUST be explicitly destroyed by
// ShutDown() on the runner's thread by the time this destructor is run.
DCHECK(!thread_);
}

void OnStop(shell::ServiceContext* connection) {
DCHECK(application_task_runner_->BelongsToCurrentThread());

Expand All @@ -102,13 +106,20 @@ class EmbeddedApplicationRunner::Instance

shell_connections_.clear();
service_.reset();
quit_task_runner_->PostTask(
FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this));
if (quit_task_runner_->BelongsToCurrentThread()) {
QuitOnRunnerThread();
} else {
quit_task_runner_->PostTask(
FROM_HERE, base::Bind(&Instance::QuitOnRunnerThread, this));
}
}

void QuitOnRunnerThread() {
DCHECK(runner_thread_checker_.CalledOnValidThread());
ShutDown();
if (thread_) {
thread_.reset();
application_task_runner_ = nullptr;
}
quit_closure_.Run();
}

Expand Down

0 comments on commit 901b099

Please sign in to comment.