Fix flaky GNSS VTS

In the original code, vector::erase() would invalidate iterators so we
should move the thread object into the std::async() for the blocking
operation.

Bug: 344250967
Test: atest VtsHalGnssTargetTest
Change-Id: I4cc82131bb070a37cb6ed9dbe9d7cccc7ab5ee89
This commit is contained in:
Yu-Han Yang 2024-06-11 21:59:02 +00:00
parent 29e5340a69
commit 23e78269a3
2 changed files with 20 additions and 14 deletions

View file

@ -146,15 +146,18 @@ void GnssMeasurementInterface::stop() {
mIsActive = false; mIsActive = false;
mGnss->setGnssMeasurementEnabled(false); mGnss->setGnssMeasurementEnabled(false);
mThreadBlocker.notify(); mThreadBlocker.notify();
for (auto iter = mThreads.begin(); iter != mThreads.end(); ++iter) { for (auto iter = mThreads.begin(); iter != mThreads.end();) {
if (iter->joinable()) { if (iter->joinable()) {
mFutures.push_back(std::async(std::launch::async, [this, iter] { // Store the thread object by value
iter->join(); std::thread threadToMove = std::move(*iter);
mThreads.erase(iter);
})); mFutures.push_back(std::async(std::launch::async,
} else { [threadToMove = std::move(threadToMove)]() mutable {
mThreads.erase(iter); ALOGD("joining thread");
threadToMove.join();
}));
} }
iter = mThreads.erase(iter);
} }
} }

View file

@ -90,15 +90,18 @@ void GnssNavigationMessageInterface::stop() {
ALOGD("stop"); ALOGD("stop");
mIsActive = false; mIsActive = false;
mThreadBlocker.notify(); mThreadBlocker.notify();
for (auto iter = mThreads.begin(); iter != mThreads.end(); ++iter) { for (auto iter = mThreads.begin(); iter != mThreads.end();) {
if (iter->joinable()) { if (iter->joinable()) {
mFutures.push_back(std::async(std::launch::async, [this, iter] { // Store the thread object by value
iter->join(); std::thread threadToMove = std::move(*iter);
mThreads.erase(iter);
})); mFutures.push_back(std::async(std::launch::async,
} else { [threadToMove = std::move(threadToMove)]() mutable {
mThreads.erase(iter); ALOGD("joining thread");
threadToMove.join();
}));
} }
iter = mThreads.erase(iter);
} }
} }