From ded7e1a7a5a745f931ee048d04d6dcfa483a49a2 Mon Sep 17 00:00:00 2001 From: Dawid Wojtas Date: Tue, 30 Aug 2022 08:41:13 +0200 Subject: [PATCH] [MOS-676] Fix destroying wrong sentinels If the service is closed the governor removes connected sentinel. The base iterator refers to the element that is next in the reversed list. Adding std::next the iterator points to the correct element. --- .../GovernorSentinelOperations.cpp | 4 +- .../tests/unittest_CpuSentinelsGovernor.cpp | 37 +++++++++++++++++++ pure_changelog.md | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/module-sys/SystemManager/GovernorSentinelOperations.cpp b/module-sys/SystemManager/GovernorSentinelOperations.cpp index 7ed1e2b64b1a039d7ab585f1da40f8ea4d464776..1ab68b3d9349af28e05ff2bd4cc6895c5ee23aa5 100644 --- a/module-sys/SystemManager/GovernorSentinelOperations.cpp +++ b/module-sys/SystemManager/GovernorSentinelOperations.cpp @@ -17,7 +17,9 @@ namespace sys auto sentinelWeakPointer = (*sentinel)->GetSentinel(); // remove expired ptrs if (sentinelWeakPointer.expired()) { - sentinels.erase(sentinel.base()); + // shift iterator to point on the correct successor, + // without std::next it removes (sentinel - 1) element + sentinels.erase(std::next(sentinel).base()); continue; } // call foo on sentinel diff --git a/module-sys/SystemManager/tests/unittest_CpuSentinelsGovernor.cpp b/module-sys/SystemManager/tests/unittest_CpuSentinelsGovernor.cpp index d9a4a393a12d48ae06118fe36af49a74a7ec58f3..8de531b39cf3786e227c437f4e46193f69bd1207 100644 --- a/module-sys/SystemManager/tests/unittest_CpuSentinelsGovernor.cpp +++ b/module-sys/SystemManager/tests/unittest_CpuSentinelsGovernor.cpp @@ -119,6 +119,43 @@ TEST_CASE("GovernorSentinelsVector - single sentinel add and remove") REQUIRE(container.empty()); } +TEST_CASE("GovernorSentinelsVector - a few removed in the middle") +{ + using namespace sys; + size_t count = 0; + constexpr size_t size = 10; + GovernorSentinelsVector container; + std::vector> sentinels; + std::vector sentinelName; + + governed_callback foo = [&](const GovernorSentinel &s) { return false; }; + + for (size_t i = 0; i < size; ++i) { + sentinelName.push_back("test_" + std::to_string(i)); + sentinels.push_back(std::make_shared(sentinelName[i], nullptr)); + container.push_back(std::make_unique(sentinels[i])); + } + + // Remove sentinel in the middle + sentinels.erase(sentinels.begin() + 2); + sentinelName.erase(sentinelName.begin() + 2); + + // Remove sentinel in the middle + sentinels.erase(sentinels.begin() + 6); + sentinelName.erase(sentinelName.begin() + 6); + + for_each_governed_sentinel(container, foo); + + auto countSentinels = sentinels.size(); + for (size_t i = 0; i < countSentinels; ++i) { + auto cont = container[i]->GetSentinel(); + if (not cont.expired()) { + count++; + } + } + REQUIRE(count == countSentinels); +} + TEST_CASE("GovernorSentinelsVector - multiple sentinels add and remove") { using namespace sys; diff --git a/pure_changelog.md b/pure_changelog.md index 0f87f86daea65718123ced2130611cafe6703e40..b485bf1230abd3ce546471e36d1c4af3e07d3728 100644 --- a/pure_changelog.md +++ b/pure_changelog.md @@ -7,6 +7,7 @@ * Separated system volume from Bluetooth device volume for A2DP ### Fixed +* Fixed removing wrong sentinels * Fixed music player behaviour when connecting/disconnecting audio devices * Fixed dropping the call during the DND mode * Fixed wrong tethering popup order