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}
diff --git a/rtc_base/task_utils/repeating_task.cc b/rtc_base/task_utils/repeating_task.cc
index c8cdf6a..5f366cb 100644
--- a/rtc_base/task_utils/repeating_task.cc
+++ b/rtc_base/task_utils/repeating_task.cc
@@ -38,12 +38,10 @@
   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());
-  }
+  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;
diff --git a/rtc_base/task_utils/repeating_task_unittest.cc b/rtc_base/task_utils/repeating_task_unittest.cc
index 79bce06..52683e3 100644
--- a/rtc_base/task_utils/repeating_task_unittest.cc
+++ b/rtc_base/task_utils/repeating_task_unittest.cc
@@ -112,13 +112,14 @@
   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);
 }