ConnectionManager: remove pending callback after success

Change-Id: I03cf7bead8bcf5760d035e32a82ec3412ef6fea9
diff --git a/src/connectionmanager.cpp b/src/connectionmanager.cpp
index b1a2f1a..5474fa0 100644
--- a/src/connectionmanager.cpp
+++ b/src/connectionmanager.cpp
@@ -472,6 +472,7 @@
      * @param deviceId  to identify the linked ConnectCallback
      */
     void sendChannelRequest(const std::weak_ptr<DeviceInfo>& dinfo,
+                            const std::weak_ptr<ConnectionInfo>& cinfo,
                             const std::shared_ptr<MultiplexedSocket>& sock,
                             const std::string& name,
                             const dht::Value::Id& vid);
@@ -898,7 +899,7 @@
                 lk.unlock();
                 if (sthis->config_->logger)
                     sthis->config_->logger->debug("[device {}] Peer already connected. Add a new channel", deviceId);
-                sthis->sendChannelRequest(di, sock, name, vid);
+                sthis->sendChannelRequest(di, info, sock, name, vid);
                 return;
             }
         }
@@ -1035,7 +1036,8 @@
 }
 
 void
-ConnectionManager::Impl::sendChannelRequest(const std::weak_ptr<DeviceInfo>& dinfo,
+ConnectionManager::Impl::sendChannelRequest(const std::weak_ptr<DeviceInfo>& dinfow,
+                                            const std::weak_ptr<ConnectionInfo>& cinfow,
                                             const std::shared_ptr<MultiplexedSocket>& sock,
                                             const std::string& name,
                                             const dht::Value::Id& vid)
@@ -1044,18 +1046,23 @@
     if (!channelSock) {
         if (config_->logger)
             config_->logger->error("sendChannelRequest failed - cannot create channel");
-        if (auto info = dinfo.lock())
+        if (auto info = dinfow.lock())
             info->executePendingOperations(vid, nullptr);
         return;
     }
-    channelSock->onShutdown([dinfo, name, vid] {
-        if (auto info = dinfo.lock())
+    channelSock->onShutdown([dinfow, name, vid] {
+        if (auto info = dinfow.lock())
             info->executePendingOperations(vid, nullptr);
     });
     channelSock->onReady(
-        [dinfo, wSock = std::weak_ptr(channelSock), name, vid](bool accepted) {
-            if (auto info = dinfo.lock())
-                info->executePendingOperations(vid, accepted ? wSock.lock() : nullptr, accepted);
+        [dinfow, cinfow, wSock = std::weak_ptr(channelSock), name, vid](bool accepted) {
+            if (auto dinfo = dinfow.lock()) {
+                dinfo->executePendingOperations(vid, accepted ? wSock.lock() : nullptr, accepted);
+                if (auto cinfo = cinfow.lock()) {
+                    std::lock_guard<std::mutex> lk(cinfo->mutex_);
+                    cinfo->cbIds_.erase(vid);
+                }
+            }
         });
 
     ChannelRequest val;
@@ -1219,7 +1226,7 @@
             if (config_->logger)
                 config_->logger->debug("[device {}] Send request on TLS socket for channel {}",
                     deviceId, name);
-            sendChannelRequest(dinfo, info->socket_, name, id);
+            sendChannelRequest(dinfo, info, info->socket_, name, id);
         }
     }
 }