From c8069856c3bd0286986651fc5a8e58dbfcc3e7f5 Mon Sep 17 00:00:00 2001 From: Lais Andrade Date: Fri, 26 Apr 2024 14:57:13 +0100 Subject: Fix VibratorCallbackScheduler destructor lock The VibratorCallbackScheduler destructor joins on the scheduler thread to wait for the main loop to finish, but the conditional variable is waiting indefinitely without a predicate, which can cause it sometimes to miss the notify call from the destructor and get stuck. Adding a predicate condition fixes the VibratorCallbackSchedulerTest flakiness for the timeout "No test results." failures. Bug: 293603710 Bug: 293623689 Test: atest --rerun-until-failure 1000 VibratorCallbackSchedulerTest Change-Id: Id9501c10fe5209003d9b74b0f39f2bcf87de05c2 --- services/vibratorservice/VibratorCallbackScheduler.cpp | 8 ++++---- services/vibratorservice/test/test_utils.h | 8 +++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/services/vibratorservice/VibratorCallbackScheduler.cpp b/services/vibratorservice/VibratorCallbackScheduler.cpp index 7eda9ef0c7..b2b1988d97 100644 --- a/services/vibratorservice/VibratorCallbackScheduler.cpp +++ b/services/vibratorservice/VibratorCallbackScheduler.cpp @@ -87,13 +87,13 @@ void CallbackScheduler::loop() { lock.lock(); } if (mQueue.empty()) { - // Wait until a new callback is scheduled. - mCondition.wait(mMutex); + // Wait until a new callback is scheduled or destructor was called. + mCondition.wait(lock, [this] { return mFinished || !mQueue.empty(); }); } else { - // Wait until next callback expires, or a new one is scheduled. + // Wait until next callback expires or a new one is scheduled. // Use the monotonic steady clock to wait for the measured delay interval via wait_for // instead of using a wall clock via wait_until. - mCondition.wait_for(mMutex, mQueue.top().getWaitForExpirationDuration()); + mCondition.wait_for(lock, mQueue.top().getWaitForExpirationDuration()); } } } diff --git a/services/vibratorservice/test/test_utils.h b/services/vibratorservice/test/test_utils.h index bf13aa7487..715c2215c4 100644 --- a/services/vibratorservice/test/test_utils.h +++ b/services/vibratorservice/test/test_utils.h @@ -90,13 +90,15 @@ public: TestCounter(int32_t init = 0) : mMutex(), mCondVar(), mCount(init) {} int32_t get() { - std::unique_lock lock(mMutex); + std::lock_guard lock(mMutex); return mCount; } void increment() { - std::unique_lock lock(mMutex); - mCount += 1; + { + std::lock_guard lock(mMutex); + mCount += 1; + } mCondVar.notify_all(); } -- cgit v1.2.3-59-g8ed1b