From 974beeae4c41277fe8c3e05880f40d9730c7cc19 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 31 Mar 2017 13:39:23 -0700 Subject: [PATCH] Fix a race condition in MessageLoopImpl due to difference in calculation of the next wake and the timer rearm. (flutter/engine#3548) --- engine/src/flutter/fml/message_loop_impl.cc | 35 ++++++++++----------- engine/src/flutter/fml/message_loop_impl.h | 7 ++--- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/engine/src/flutter/fml/message_loop_impl.cc b/engine/src/flutter/fml/message_loop_impl.cc index 448ed5f219..9a3b4c003b 100644 --- a/engine/src/flutter/fml/message_loop_impl.cc +++ b/engine/src/flutter/fml/message_loop_impl.cc @@ -43,11 +43,11 @@ MessageLoopImpl::~MessageLoopImpl() = default; void MessageLoopImpl::PostTask(ftl::Closure task, ftl::TimePoint target_time) { FTL_DCHECK(task != nullptr); - WakeUp(RegisterTaskAndGetNextWake(task, target_time)); + RegisterTask(task, target_time); } void MessageLoopImpl::RunExpiredTasksNow() { - WakeUp(RunExpiredTasksAndGetNextWake()); + RunExpiredTasks(); } void MessageLoopImpl::AddTaskObserver(TaskObserver* observer) { @@ -75,15 +75,16 @@ void MessageLoopImpl::DoRun() { // Allow the implementation to do its thing. Run(); - // The message loop is shutting down. Check if there are expired tasks. This - // is the last chance for expired tasks to be serviced. - RunExpiredTasksNow(); - // The loop may have been implicitly terminated. This can happen if the // implementation supports termination via platform specific APIs or just // error conditions. Set the terminated flag manually. terminated_ = true; + // The message loop is shutting down. Check if there are expired tasks. This + // is the last chance for expired tasks to be serviced. Make sure the + // terminated flag is already set so we don't accrue additional tasks now. + RunExpiredTasksNow(); + // When the message loop is in the process of shutting down, pending tasks // should be destructed on the message loop's thread. We have just returned // from the implementations |Run| method which we know is on the correct @@ -97,21 +98,20 @@ void MessageLoopImpl::DoTerminate() { Terminate(); } -ftl::TimePoint MessageLoopImpl::RegisterTaskAndGetNextWake( - ftl::Closure task, - ftl::TimePoint target_time) { +void MessageLoopImpl::RegisterTask(ftl::Closure task, + ftl::TimePoint target_time) { + FTL_DCHECK(task != nullptr); if (terminated_) { // If the message loop has already been terminated, PostTask should destruct // |task| synchronously within this function. - return ftl::TimePoint::Max(); + return; } - FTL_DCHECK(task != nullptr); ftl::MutexLocker lock(&delayed_tasks_mutex_); delayed_tasks_.push({++order_, std::move(task), target_time}); - return delayed_tasks_.top().target_time; + WakeUp(delayed_tasks_.top().target_time); } -ftl::TimePoint MessageLoopImpl::RunExpiredTasksAndGetNextWake() { +void MessageLoopImpl::RunExpiredTasks() { TRACE_EVENT0("fml", "MessageLoop::RunExpiredTasks"); std::vector invocations; @@ -119,7 +119,7 @@ ftl::TimePoint MessageLoopImpl::RunExpiredTasksAndGetNextWake() { ftl::MutexLocker lock(&delayed_tasks_mutex_); if (delayed_tasks_.empty()) { - return ftl::TimePoint::Max(); + return; } auto now = ftl::TimePoint::Now(); @@ -131,6 +131,9 @@ ftl::TimePoint MessageLoopImpl::RunExpiredTasksAndGetNextWake() { invocations.emplace_back(std::move(top.task)); delayed_tasks_.pop(); } + + WakeUp(delayed_tasks_.empty() ? ftl::TimePoint::Max() + : delayed_tasks_.top().target_time); } for (const auto& invocation : invocations) { @@ -139,10 +142,6 @@ ftl::TimePoint MessageLoopImpl::RunExpiredTasksAndGetNextWake() { observer->DidProcessTask(); } } - - ftl::MutexLocker lock(&delayed_tasks_mutex_); - return delayed_tasks_.empty() ? ftl::TimePoint::Max() - : delayed_tasks_.top().target_time; } } // namespace fml diff --git a/engine/src/flutter/fml/message_loop_impl.h b/engine/src/flutter/fml/message_loop_impl.h index 572778e0cc..f353a056f3 100644 --- a/engine/src/flutter/fml/message_loop_impl.h +++ b/engine/src/flutter/fml/message_loop_impl.h @@ -76,12 +76,9 @@ class MessageLoopImpl : public ftl::RefCountedThreadSafe { size_t order_ FTL_GUARDED_BY(delayed_tasks_mutex_); std::atomic_bool terminated_; - FTL_WARN_UNUSED_RESULT - ftl::TimePoint RegisterTaskAndGetNextWake(ftl::Closure task, - ftl::TimePoint target_time); + void RegisterTask(ftl::Closure task, ftl::TimePoint target_time); - FTL_WARN_UNUSED_RESULT - ftl::TimePoint RunExpiredTasksAndGetNextWake(); + void RunExpiredTasks(); FTL_DISALLOW_COPY_AND_ASSIGN(MessageLoopImpl); };