fix: race condition in async Proxy::callMethod

Signed-off-by: Anthony Brandon <anthony@amarulasolutions.com>
This commit is contained in:
Anthony Brandon
2023-02-07 22:23:35 +01:00
committed by Stanislav Angelovič
parent 737f04abc7
commit c39bc637b8
2 changed files with 13 additions and 13 deletions
+10 -7
View File
@@ -33,7 +33,7 @@
#include <string>
#include <memory>
#include <map>
#include <unordered_map>
#include <deque>
#include <mutex>
#include <atomic>
#include <condition_variable>
@@ -138,6 +138,7 @@ namespace sdbus::internal {
Proxy& proxy;
async_reply_handler callback;
Slot slot;
bool finished { false };
};
~AsyncCalls()
@@ -145,18 +146,20 @@ namespace sdbus::internal {
clear();
}
bool addCall(void* slot, std::shared_ptr<CallData> asyncCallData)
void addCall(std::shared_ptr<CallData> asyncCallData)
{
std::lock_guard lock(mutex_);
return calls_.emplace(slot, std::move(asyncCallData)).second;
if (!asyncCallData->finished) // The call may have finished in the mean time
calls_.emplace_back(std::move(asyncCallData));
}
void removeCall(void* slot)
void removeCall(CallData* data)
{
std::unique_lock lock(mutex_);
if (auto it = calls_.find(slot); it != calls_.end())
data->finished = true;
if (auto it = std::find_if(calls_.begin(), calls_.end(), [data](auto const& entry){ return entry.get() == data; }); it != calls_.end())
{
auto callData = std::move(it->second);
auto callData = std::move(*it);
calls_.erase(it);
lock.unlock();
@@ -182,7 +185,7 @@ namespace sdbus::internal {
private:
std::mutex mutex_;
std::unordered_map<void*, std::shared_ptr<CallData>> calls_;
std::deque<std::shared_ptr<CallData>> calls_;
} pendingAsyncCalls_;
std::atomic<const Message*> m_CurrentlyProcessedMessage{nullptr};