Avoids PostTask to repost a repeated task.

There seems to be a race caused by the libevent wrapping TaskQueue
implementation when reposting a repeated task at destruction time. This
race results in the posted task being leaked according to asan.

Bug: webrtc:10278
Change-Id: Ida40b884547f3f789a804ca0ab3ce36982a4d68e
Reviewed-on: https://webrtc-review.googlesource.com/c/121424
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26839}
This commit is contained in:
Sebastian Jansson
2019-02-04 16:39:28 +01:00
committed by Commit Bot
parent ce7a4fb67b
commit a497d12a02
2 changed files with 9 additions and 10 deletions

View File

@ -38,12 +38,10 @@ bool RepeatingTaskBase::Run() {
TimeDelta lost_time = Timestamp::us(rtc::TimeMicros()) - next_run_time_;
next_run_time_ += delay;
delay -= lost_time;
delay = std::max(delay, TimeDelta::Zero());
if (delay <= TimeDelta::Zero()) {
task_queue_->PostTask(absl::WrapUnique(this));
} else {
task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms());
}
// Return false to tell the TaskQueue to not destruct this object since we
// have taken ownership with absl::WrapUnique.
return false;

View File

@ -112,13 +112,14 @@ TEST(RepeatingTaskTest, CompensatesForShortRunTime) {
rtc::TaskQueue task_queue("TestQueue");
RepeatingTaskHandle::Start(&task_queue, [&] {
++counter;
// Sleeping for the 5 ms should be compensated.
Sleep(TimeDelta::ms(5));
return TimeDelta::ms(10);
// Sleeping for the 10 ms should be compensated.
Sleep(TimeDelta::ms(10));
return TimeDelta::ms(30);
});
Sleep(TimeDelta::ms(15));
Sleep(TimeDelta::ms(40));
// We expect that the task have been called twice, once directly at Start and
// once after 10 ms has passed.
// once after 30 ms has passed.
EXPECT_EQ(counter.load(), 2);
}