ConversationModule: avoid deadlock if callback is called synchronously

Change-Id: I7aedd81b133985210cf817d2c524ef3415a219c0
This commit is contained in:
Adrien Béraud
2024-03-04 08:52:40 -05:00
parent a4418d3e35
commit ce6adec5f8

View File

@ -568,25 +568,27 @@ ConversationModule::Impl::fetchNewCommits(const std::string& peer,
onNeedSocket_(
conversationId,
deviceId,
[this,
[w = weak(),
conv,
conversationId = std::move(conversationId),
peer = std::move(peer),
deviceId = std::move(deviceId),
commitId = std::move(commitId)](const auto& channel) {
std::lock_guard lk(conv->mtx);
// auto conversation = conversations_.find(conversationId);
auto acc = account_.lock();
if (!channel || !acc || !conv->conversation) {
auto sthis = w.lock();
auto acc = sthis ? sthis->account_.lock() : nullptr;
std::unique_lock lk(conv->mtx);
auto conversation = conv->conversation;
if (!channel || !acc || !conversation) {
conv->stopFetch(deviceId);
syncCnt.fetch_sub(1);
if (sthis) sthis->syncCnt.fetch_sub(1);
return false;
}
conv->conversation->addGitSocket(channel->deviceId(), channel);
conv->conversation->sync(
conversation->addGitSocket(channel->deviceId(), channel);
lk.unlock();
conversation->sync(
peer,
deviceId,
[w = weak(),
[w,
conv,
conversationId = std::move(conversationId),
peer = std::move(peer),