From 7e8b056dcdb33bf4e69c250ed5c4e3dd3631a40d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 13 Jun 2023 18:10:53 -0400
Subject: [PATCH] connectionmanager: do not cancel all waiting channel if first
 is declined

backport of 22d5417da3023095695d3b9655403b5632f528db in jami-daemon

Change-Id: I1ccf2edfad8b0d69be08e64ab894bb52027088a7
---
 src/connectionmanager.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/connectionmanager.cpp b/src/connectionmanager.cpp
index fc104ed..03e4c65 100644
--- a/src/connectionmanager.cpp
+++ b/src/connectionmanager.cpp
@@ -329,7 +329,7 @@ public:
 
     std::map<DeviceId, PendingOperations> pendingOperations_ {};
 
-    void executePendingOperations(const DeviceId& deviceId, const dht::Value::Id& vid, const std::shared_ptr<ChannelSocket>& sock)
+    void executePendingOperations(const DeviceId& deviceId, const dht::Value::Id& vid, const std::shared_ptr<ChannelSocket>& sock, bool accepted = true)
     {
         std::vector<PendingCb> ret;
         std::unique_lock<std::mutex> lk(connectCbsMtx_);
@@ -351,7 +351,8 @@ public:
         } else if (auto n = pendingOperations.connecting.extract(vid)) {
             ret.emplace_back(std::move(n.mapped()));
             // If sock is nullptr, execute if it's the last connecting operation
-            if (!sock && pendingOperations.connecting.empty()) {
+            // If accepted is false, it means that underlying socket is ok, but channel is declined
+            if (!sock && pendingOperations.connecting.empty() && accepted) {
                 for (auto& [vid, cb] : pendingOperations.waiting)
                     ret.emplace_back(std::move(cb));
                 pendingOperations.waiting.clear();
@@ -799,11 +800,11 @@ ConnectionManager::Impl::sendChannelRequest(std::shared_ptr<MultiplexedSocket>&
             shared->executePendingOperations(deviceId, vid, nullptr);
     });
     channelSock->onReady(
-        [wSock = std::weak_ptr<ChannelSocket>(channelSock), name, deviceId, vid, w = weak()]() {
+        [wSock = std::weak_ptr<ChannelSocket>(channelSock), name, deviceId, vid, w = weak()](bool accepted) {
             auto shared = w.lock();
             auto channelSock = wSock.lock();
             if (shared)
-                shared->executePendingOperations(deviceId, vid, channelSock);
+                shared->executePendingOperations(deviceId, vid, accepted ? channelSock : nullptr, accepted);
         });
 
     ChannelRequest val;
-- 
GitLab