Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1348)

Unified Diff: third_party/WebKit/Source/platform/scheduler/child/idle_helper.cc

Issue 2487343004: Make sure scheduler shutdown prevents idle tasks from running (Closed)
Patch Set: Added a test which fails without the patch. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/scheduler/child/idle_helper.cc
diff --git a/third_party/WebKit/Source/platform/scheduler/child/idle_helper.cc b/third_party/WebKit/Source/platform/scheduler/child/idle_helper.cc
index 3daea10ee1023dbc7a3bace837268058ab185e54..d9a946e874448d73630a128a18852d6d463418c6 100644
--- a/third_party/WebKit/Source/platform/scheduler/child/idle_helper.cc
+++ b/third_party/WebKit/Source/platform/scheduler/child/idle_helper.cc
@@ -36,6 +36,7 @@ IdleHelper::IdleHelper(
required_quiescence_duration_before_long_idle_period),
disabled_by_default_tracing_category_(
disabled_by_default_tracing_category),
+ is_shutdown_(false),
weak_factory_(this) {
weak_idle_helper_ptr_ = weak_factory_.GetWeakPtr();
enable_next_long_idle_period_closure_.Reset(
@@ -53,6 +54,18 @@ IdleHelper::IdleHelper(
}
IdleHelper::~IdleHelper() {
+ Shutdown();
+}
+
+void IdleHelper::Shutdown() {
+ if (is_shutdown_)
+ return;
+
+ EndIdlePeriod();
+ is_shutdown_ = true;
+ weak_factory_.InvalidateWeakPtrs();
+ // Belt & braces, might not be needed.
+ idle_queue_->SetQueueEnabled(false);
helper_->RemoveTaskObserver(this);
}
@@ -109,9 +122,6 @@ IdleHelper::IdlePeriodState IdleHelper::ComputeNewLongIdlePeriodState(
bool IdleHelper::ShouldWaitForQuiescence() {
helper_->CheckOnValidThread();
- if (helper_->IsShutdown())
- return false;
-
if (required_quiescence_duration_before_long_idle_period_ ==
base::TimeDelta()) {
return false;
@@ -126,7 +136,7 @@ bool IdleHelper::ShouldWaitForQuiescence() {
void IdleHelper::EnableLongIdlePeriod() {
TRACE_EVENT0(disabled_by_default_tracing_category_, "EnableLongIdlePeriod");
helper_->CheckOnValidThread();
- if (helper_->IsShutdown())
+ if (is_shutdown_)
return;
// End any previous idle period.
@@ -158,6 +168,7 @@ void IdleHelper::EnableLongIdlePeriod() {
void IdleHelper::StartIdlePeriod(IdlePeriodState new_state,
base::TimeTicks now,
base::TimeTicks idle_period_deadline) {
+ DCHECK(!is_shutdown_);
DCHECK_GT(idle_period_deadline, now);
helper_->CheckOnValidThread();
DCHECK(IsInIdlePeriod(new_state));
@@ -182,6 +193,9 @@ void IdleHelper::StartIdlePeriod(IdlePeriodState new_state,
}
void IdleHelper::EndIdlePeriod() {
+ if (is_shutdown_)
+ return;
+
helper_->CheckOnValidThread();
TRACE_EVENT0(disabled_by_default_tracing_category_, "EndIdlePeriod");
@@ -197,10 +211,13 @@ void IdleHelper::EndIdlePeriod() {
base::TimeTicks());
}
-void IdleHelper::WillProcessTask(const base::PendingTask& pending_task) {}
+void IdleHelper::WillProcessTask(const base::PendingTask& pending_task) {
+ DCHECK(!is_shutdown_);
+}
void IdleHelper::DidProcessTask(const base::PendingTask& pending_task) {
helper_->CheckOnValidThread();
+ DCHECK(!is_shutdown_);
TRACE_EVENT0(disabled_by_default_tracing_category_, "DidProcessTask");
if (IsInIdlePeriod(state_.idle_period_state()) &&
state_.idle_period_state() !=
@@ -221,6 +238,7 @@ void IdleHelper::DidProcessTask(const base::PendingTask& pending_task) {
void IdleHelper::UpdateLongIdlePeriodStateAfterIdleTask() {
helper_->CheckOnValidThread();
+ DCHECK(!is_shutdown_);
DCHECK(IsInLongIdlePeriod(state_.idle_period_state()));
TRACE_EVENT0(disabled_by_default_tracing_category_,
"UpdateLongIdlePeriodStateAfterIdleTask");
@@ -262,6 +280,8 @@ base::TimeTicks IdleHelper::CurrentIdleTaskDeadline() const {
void IdleHelper::OnIdleTaskPosted() {
TRACE_EVENT0(disabled_by_default_tracing_category_, "OnIdleTaskPosted");
+ if (is_shutdown_)
+ return;
if (idle_task_runner_->RunsTasksOnCurrentThread()) {
OnIdleTaskPostedOnMainThread();
} else {
@@ -273,6 +293,8 @@ void IdleHelper::OnIdleTaskPosted() {
void IdleHelper::OnIdleTaskPostedOnMainThread() {
TRACE_EVENT0(disabled_by_default_tracing_category_,
"OnIdleTaskPostedOnMainThread");
+ if (is_shutdown_)
+ return;
if (state_.idle_period_state() ==
IdlePeriodState::IN_LONG_IDLE_PERIOD_PAUSED) {
// Restart long idle period ticks.
@@ -283,12 +305,15 @@ void IdleHelper::OnIdleTaskPostedOnMainThread() {
base::TimeTicks IdleHelper::WillProcessIdleTask() {
helper_->CheckOnValidThread();
+ DCHECK(!is_shutdown_);
state_.TraceIdleIdleTaskStart();
return CurrentIdleTaskDeadline();
}
void IdleHelper::DidProcessIdleTask() {
helper_->CheckOnValidThread();
+ if (is_shutdown_)
+ return;
state_.TraceIdleIdleTaskEnd();
if (IsInLongIdlePeriod(state_.idle_period_state())) {
UpdateLongIdlePeriodStateAfterIdleTask();

Powered by Google App Engine
This is Rietveld 408576698